Debian Codegraph @

packages/zoltan/test/Utilities_Tests/DDirectory/DD_Main.c

raw
1 // @HEADER
2 // *****************************************************************************
3 // Zoltan Toolkit for Load-balancing, Partitioning, Ordering and Coloring
4 //
5 // Copyright 2012 NTESS and the Zoltan contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10
11 /* Program tests the Zoltan Distributed Directory software (stand-alone mode).
12 */
13
14 #include <mpi.h>
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include "zoltan_dd.h"
18 #include "zoltan_mem.h"
19 #include "zoltan_util.h"
20 #include "zoltan_id.h"
21 #include "zoltan_align.h"
22 #include "zoltan_timer.h"
23
24 #ifdef __cplusplus
25 /* if C++, define the rest of this header file as extern C */
26 extern "C" {
27 #endif
28
29
30 #define DD_TEST_NAME_NUMERIC 100 /* arbitrary starting value for defines */
31 #define NO_PROC -1
32
33
34 typedef struct {
35 int old_owner ;
36 int new_owner ; /* old != new ==> simultated data move */
37 int partition ;
38 ZOLTAN_ID_TYPE id[1] ; /* malloc'd beyond end of struct */
39 /* ZOLTAN_ID_TYPE lid[] at gid + glen */
40 /* ZOLTAN_ID_TYPE user[] at gid + glen + llen */
41 } Data ;
42
43
44 typedef struct {
45 int count ; /* number of global IDs to simulate */
46 int pdelete ; /* fraction of GIDs removed per cycle x100 */
47 int pmove ; /* fraction of GIDs moved per cycle x100 */
48 int pscatter ; /* fraction of GIDs scattered per cycle x 100 */
49 int nloops ; /* number of times to loop though simulation */
50 int rseed ; /* seed for random number generator */
51
52 int glen ; /* global ID length */
53 int llen ; /* local ID length */
54 int ulen ; /* user data ID length */
55 int tlen ; /* hash table length */
56 int slen ; /* length of Data * id stuff */
57
58 int name_scheme ;
59 int debug_level ;
60 } Param ;
61
62
63 static int get_params (Param *p) ;
64
65 static int initialize_data (Param *p, char *store, int nproc) ;
66
67 /****************************************************************************/
68 #define MAXNAMELEN 31
69 #define INITLENGTH 30
70
71 /* Structure that implements an individual timer. */
72 typedef struct TimeStruct {
73 double Start_Time; /* Most recent start time;
74 set by Zoltan_Timer_Start */
75 double Stop_Time; /* Most recent end time;
76 set by Zoltan_Timer_Stop */
77 char Start_File[MAXNAMELEN+1]; /* Filename for most recent Start */
78 char Stop_File[MAXNAMELEN+1]; /* Filename for most recent Stop */
79 int Start_Line; /* Line # in Start_File for most recent Start */
80 int Stop_Line; /* Line # in Stop_File for most recent Stop */
81 double My_Tot_Time; /* Sum of stop_time-start_time over all invocations
82 of this timer */
83 int Use_Barrier; /* Flag indicating whether to perform a barrier
84 operation before starting the timer. */
85 int Status; /* Flag indicating status of TimeStruct:
86 > 0 --> In Use
87 > 2 --> Running */
88 char Name[MAXNAMELEN+1];/* String associated (and printed) with timer info */
89
90 #ifdef VAMPIR
91 int vt_handle; /* state handle for vampir traces */
92 #endif
93 } ZTIMER_TS;
94
95 /* Timer object consisting of many related timers.
96 * Applications access this structure. */
97 typedef struct Zoltan_Timer {
98 int Timer_Flag; /* Zoltan Timer_Flag flag passed to Zoltan_Time */
99 int Length; /* # of entries allocated in Times */
100 int NextTimeStruct; /* Index of next unused TimeStruct */
101 ZTIMER_TS *Times; /* Array of actual timing data -- individual timers */
102 } ZTIMER;
103
104 /***********************************************************************************/
105
106 #define MACRO_TIMER_START(arg, message, sync)\
107 {if (timer[arg] == -1)\
108 timer[arg] = Zoltan_Timer_Init(zt, sync, message);\
109 ZOLTAN_TIMER_START(zt, timer[arg], MPI_COMM_WORLD);}
110
111 #define MACRO_TIMER_STOP(arg)\
112 {ZOLTAN_TIMER_STOP(zt, timer[arg], MPI_COMM_WORLD);}
113
114 int main (int argc, char *argv[])
115 {
116 Zoltan_DD_Directory *dd;
117 ZTIMER *zt;
118
119 int myproc; /* MPI rank, my processor's MPI ID */
120 int nproc; /* MPI size, number of processors */
121 ZOLTAN_ID_PTR glist = NULL; /* list of GIDs */
122 ZOLTAN_ID_PTR llist = NULL; /* list of LIDs */
123 ZOLTAN_ID_PTR ulist = NULL; /* list of user data of type
124 ZOLTAN_ID_TYPE */
125 int *plist = NULL; /* list of partitions */
126 int *olist = NULL; /* list of owners */
127 Param param ; /* program's adjustable parameters */
128 char *store = NULL; /* non directory storage of test data */
129 Data *data = NULL; /* pointer into store */
130
131 /* these are all temporary variables */
132 int new_owner;
133 int count;
134 int i;
135 char *p;
136 int err;
137 int errcount;
138 int loop;
139 char str[100]; /* for building output messages */
140 static int timer[7] = {-1,-1,-1,-1,-1,-1,-1};
141 char *yo = "DD_Main";
142
143
144 /* initialize MPI communications, ignore errors */
145 MPI_Init (&argc, &argv);
146 MPI_Comm_rank (MPI_COMM_WORLD, &myproc);
147 MPI_Comm_size (MPI_COMM_WORLD, &nproc);
148
149 get_params (&param); /* read input parameters */
150
151 zt = Zoltan_Timer_Create(1);
152 MACRO_TIMER_START(5, "Total time", 0);
153
154 MACRO_TIMER_START(0, "DD_Create time", 0);
155 err = Zoltan_DD_Create (&dd, MPI_COMM_WORLD, param.glen, param.llen,
156 param.ulen*sizeof(ZOLTAN_ID_TYPE), param.tlen, param.debug_level);
157 if (err != ZOLTAN_OK)
158 ZOLTAN_PRINT_ERROR (myproc, yo, "Failed return from DD Create");
159 MACRO_TIMER_STOP(0);
160
161 param.slen = sizeof (Data) + sizeof(ZOLTAN_ID_TYPE)
162 * (param.glen + param.llen + param.ulen);
163 param.slen = Zoltan_Align(param.slen);
164 store = (char*) ZOLTAN_MALLOC (param.count * param.slen);
165
166 /* allocate storage for various lists */
167 glist = (ZOLTAN_ID_PTR) ZOLTAN_MALLOC(sizeof(ZOLTAN_ID_TYPE) * param.count
168 * param.glen);
169 plist = (int*) ZOLTAN_MALLOC (sizeof(int) * param.count);
170 olist = (int*) ZOLTAN_MALLOC (sizeof(int) * param.count);
171 if (param.llen)
172 llist = (ZOLTAN_ID_PTR) ZOLTAN_MALLOC (sizeof (ZOLTAN_ID_TYPE)
173 * param.count * param.llen);
174 if (param.ulen)
175 ulist = (ZOLTAN_ID_PTR) ZOLTAN_MALLOC (sizeof (ZOLTAN_ID_TYPE)
176 * param.count * param.ulen) ;
177
178
179 if (store == NULL || glist == NULL || (param.llen != 0 && llist == NULL)
180 || (param.ulen != 0 && ulist == NULL) || plist == NULL || olist == NULL) {
181 ZOLTAN_PRINT_ERROR (myproc, yo, "Unable to malloc storage lists");
182 return ZOLTAN_MEMERR;
183 }
184
185 initialize_data (&param, store, nproc);
186
187 /* create & update directory with myproc's initial simulated GIDs */
188 count = 0;
189 for (p = store; p < store + param.count * param.slen; p += param.slen)
190 if (((Data *)p)->new_owner == myproc) {
191 ZOLTAN_SET_ID (param.glen, glist + count * param.glen, ((Data*)p)->id);
192 if (param.llen)
193 ZOLTAN_SET_ID (param.llen, llist + count * param.llen,
194 ((Data *)p)->id + param.glen);
195 if (param.ulen)
196 ZOLTAN_SET_ID (param.ulen, ulist + count * param.ulen,
197 ((Data *)p)->id + (param.glen + param.llen));
198 plist [count] = ((Data *)p)->partition;
199 ++count;
200 }
201 MACRO_TIMER_START (1, "DD_Update timer", 0);
202 err = Zoltan_DD_Update (dd, glist, llist, (char*)ulist, plist, count);
203 if (err != ZOLTAN_OK)
204 ZOLTAN_PRINT_ERROR (myproc, yo, "Failed return from DD Update");
205 MACRO_TIMER_STOP(1);
206
207 i = 0;
208 for (p = store; p < store + param.count * param.slen; p += param.slen) {
209 ZOLTAN_SET_ID (param.glen, glist + i * param.glen, ((Data *)p)->id);
210 ++i;
211 }
212 MACRO_TIMER_START(2, "DD_Find timer", 0);
213 err = Zoltan_DD_Find (dd, glist, llist, (char*)ulist, plist,
214 param.count, olist);
215 if (err != ZOLTAN_OK)
216 ZOLTAN_PRINT_ERROR (myproc, yo, "Failed return from DD Find");
217 MACRO_TIMER_STOP(2);
218
219 errcount = 0;
220 for (i = 0; i < param.count; i++)
221 if (olist[i] != ((Data *) (store + i * param.slen))->new_owner)
222 ++errcount;
223 if (errcount)
224 sprintf (str, "FIRST TEST FAILED, errcount is %d", errcount);
225 else
226 sprintf (str, "FIRST TEST SUCCESSFUL");
227 ZOLTAN_PRINT_INFO (myproc, yo, str);
228
229 /* repeatedly simulate moving "dots" around the system */
230 for (loop = 0; loop < param.nloops; loop++) {
231 for (p = store; p < store + param.count * param.slen; p += param.slen)
232 ((Data*) p)->old_owner = ((Data*) p)->new_owner;
233
234 /* randomly exchange some dots and randomly reassign others */
235 for (i = 0; i < (param.pmove * param.count)/100; i++) {
236 Data *d1, *d2;
237 d1 = (Data*) (store + param.slen * (rand() % param.count));
238 d2 = (Data*) (store + param.slen * (rand() % param.count));
239 new_owner = d1->new_owner;
240 d1->new_owner = d2->new_owner;
241 d2->new_owner = new_owner;
242 }
243
244 for (i = 0; i < (param.count * param.pscatter)/100; i++)
245 ((Data*) (store + param.slen *(rand() % param.count)))->new_owner
246 = rand() % nproc;
247
248 /* determine which new GIDs myproc gained, and update directory */
249 count = 0;
250 for (p = store; p < store + param.count * param.slen; p += param.slen)
251 if (((Data*)p)->new_owner == myproc) {
252 ((Data*)p)->id[param.glen] = count; /* set LID */
253 ZOLTAN_SET_ID (param.glen, glist + count * param.glen,
254 ((Data *)p)->id);
255 if (param.llen)
256 ZOLTAN_SET_ID (param.llen, llist + count * param.llen,
257 ((Data*)p)->id + param.glen);
258 if (param.ulen)
259 ZOLTAN_SET_ID (param.ulen, ulist + count * param.ulen,
260 ((Data*)p)->id + (param.glen + param.llen));
261 plist [count] = ((Data *)p)->partition;
262 ++count;
263 }
264 MACRO_TIMER_START(1, "DD_Update", 0);
265 err = Zoltan_DD_Update (dd, glist, llist, (char*)ulist, plist, count);
266 if (err != ZOLTAN_OK)
267 ZOLTAN_PRINT_ERROR (myproc, yo, "Failed return from DD Update");
268 MACRO_TIMER_STOP(1);
269
270 /* use directory to "find" GIDs */
271 i = 0;
272 for (p = store; p < store + param.count * param.slen; p += param.slen) {
273 ZOLTAN_SET_ID (param.glen, glist + i * param.glen, ((Data *)p)->id);
274 ++i;
275 }
276
277 MACRO_TIMER_START(2, "DD_Find timer", 0);
278 if (loop % 5 == 0)
279 err = Zoltan_DD_Find(dd,glist,NULL,(char*)ulist,
280 plist,param.count,olist);
281 else if (loop % 7 == 0)
282 err = Zoltan_DD_Find(dd,glist,llist,NULL,plist,param.count,olist);
283 else if (loop % 9 == 0)
284 err = Zoltan_DD_Find(dd,glist,llist,(char*)ulist,
285 NULL,param.count,olist);
286 else if (loop % 2 == 0)
287 err = Zoltan_DD_Find(dd,glist,llist,(char*)ulist,
288 plist,param.count,NULL);
289 else
290 err = Zoltan_DD_Find(dd,glist,llist,(char*)ulist,
291 plist,param.count,olist);
292 if (err != ZOLTAN_OK)
293 ZOLTAN_PRINT_ERROR (myproc, yo, "Failed return from DD Find");
294 MACRO_TIMER_STOP(2);
295
296 if (loop % 2) {
297 errcount = 0 ;
298 for (i = 0; i < param.count; i++)
299 if (olist[i] != ((Data *)(store + i * param.slen))->new_owner)
300 ++errcount;
301 if (errcount)
302 sprintf (str,"LOOP %d TEST FAILED, errcount is %d",loop,errcount);
303 else
304 sprintf (str, "LOOP %d TEST SUCCESSFUL", loop);
305 ZOLTAN_PRINT_INFO (myproc, yo, str) ;
306 }
307 else {
308 sprintf (str, "LOOP %d Completed", loop);
309 ZOLTAN_PRINT_INFO (myproc, yo, str);
310 }
311
312 /* randomly remove a percentage of GIDs from the directory */
313 count = 0;
314 for (i = 0; i < (param.count * param.pdelete)/100; i++) {
315 data = (Data*) (store + param.slen * (rand() % param.count));
316 if (data->new_owner == myproc) {
317 ZOLTAN_SET_ID (param.glen, glist + count * param.glen, data->id);
318 ++count;
319 }
320 data->new_owner = NO_PROC;
321 }
322 MACRO_TIMER_START(3, "DD_Remove timer", 0);
323 err = Zoltan_DD_Remove (dd, glist, count);
324 MACRO_TIMER_STOP(3);
325 if (err != ZOLTAN_OK)
326 ZOLTAN_PRINT_ERROR (myproc, yo, "Failed return from DD Remove");
327
328 /* update directory (put directory entries back in) */
329 for (p = store; p < store + param.count * param.slen; p += param.slen)
330 if (((Data*)p)->new_owner == NO_PROC)
331 ((Data*)p)->new_owner = loop % nproc; /* place in new location */
332
333 count = 0;
334 for (p = store; p < store + param.count * param.slen; p += param.slen)
335 if (((Data*)p)->new_owner == myproc) {
336 ZOLTAN_SET_ID(param.glen,glist+count*param.glen,((Data*)p)->id);
337 if (param.llen)
338 ZOLTAN_SET_ID (param.llen, llist + count * param.llen,
339 ((Data*)p)->id + param.glen);
340 if (param.ulen)
341 ZOLTAN_SET_ID(param.ulen, ulist + count * param.ulen,
342 ((Data *)p)->id + (param.glen + param.llen));
343 plist [count] = ((Data*)p)->partition;
344 ++count;
345 }
346 MACRO_TIMER_START(1, "DD_Update timer", 0);
347 err = Zoltan_DD_Update (dd, glist, NULL, NULL, NULL, count);
348 if (err != ZOLTAN_OK)
349 ZOLTAN_PRINT_ERROR (myproc, yo, "Failed return from DD Update");
350 MACRO_TIMER_STOP(1);
351 }
352
353 /* now Find directory info for GIDs myproc now owns and validate */
354 count = 0;
355 for (i = 0; i < param.count; i++) {
356 data = (Data *) (store + i * param.slen);
357 if (data->new_owner == myproc) {
358 ZOLTAN_SET_ID (param.glen, glist + count * param.glen, data->id);
359 ++count;
360 }
361 }
362 MACRO_TIMER_START(2, "DD_Find", 0);
363 err = Zoltan_DD_Find (dd, glist, NULL, NULL, NULL, count, olist);
364 if (err != ZOLTAN_OK)
365 ZOLTAN_PRINT_ERROR (myproc, yo, "Failed return from DD Find");
366 MACRO_TIMER_STOP(2);
367
368 errcount = 0;
369 for (i = 0; i < count; i++)
370 if (olist[i] != myproc)
371 ++errcount;
372 if (errcount) {
373 sprintf (str, "errcount is %d", errcount);
374 ZOLTAN_PRINT_ERROR (myproc, yo, str);
375 }
376 else
377 ZOLTAN_PRINT_INFO (myproc, yo, "TEST SUCCESSFUL");
378
379 Zoltan_DD_Stats (dd);
380
381 /* done, now free memory, stop MPI & directory, return */
382 ZOLTAN_FREE (&store);
383 ZOLTAN_FREE (&glist);
384 ZOLTAN_FREE (&plist);
385 ZOLTAN_FREE (&olist);
386
387 if (param.llen) ZOLTAN_FREE (&llist);
388 if (param.ulen) ZOLTAN_FREE (&ulist);
389
390 ZOLTAN_PRINT_INFO (myproc, yo, "Completing program");
391 MACRO_TIMER_START(4, "DD_Destroy", 0);
392 Zoltan_DD_Destroy (&dd);
393 MACRO_TIMER_STOP(4);
394 MACRO_TIMER_STOP(5);
395
396 Zoltan_Timer_PrintAll(zt, 0, MPI_COMM_WORLD, stderr);
397
398
399 MPI_Finalize ();
400 return ZOLTAN_OK;
401 }
402
403
404
405 static int initialize_data (Param *param, char *store, int nproc)
406 {
407 int i;
408 int j;
409 Data *data;
410
411 if (param->name_scheme == DD_TEST_NAME_NUMERIC) {
412 for (i = 0; i < param->count; i++) {
413 data = (Data*) (store + i * param->slen);
414 for (j = 0; j < param->glen; j++)
415 data->id[j] = (ZOLTAN_ID_TYPE) i;
416
417 for (j = 0; j < param->llen; j++)
418 data->id[j + param->glen] = (ZOLTAN_ID_TYPE) (i % nproc);
419
420 for (j = 0; j < param->ulen; j++)
421 data->id[j+param->glen+param->llen] = (ZOLTAN_ID_TYPE)(i%nproc);
422
423 data->partition = i % 7;
424 data->new_owner = i % nproc;
425 data->old_owner = i % nproc;
426 }
427 }
428
429 srand ((unsigned int) param->rseed);
430
431 return ZOLTAN_OK;
432 }
433
434
435
436
437 static int get_params (Param *param)
438 {
439 /* establish default values */
440 param->count = 250000;
441 param->pmove = 10;
442 param->pdelete = 3;
443 param->pscatter = 4;
444 param->nloops = 20;
445
446 param->glen = 5;
447 param->llen = 5;
448 param->ulen = 10;
449 param->tlen = 0;
450
451 param->debug_level = 0;
452 param->name_scheme = DD_TEST_NAME_NUMERIC;
453
454 param->rseed = 31415926;
455
456 /* read external file for optional non default parameter values */
457
458 return ZOLTAN_OK;
459 }
460
461
462 #ifdef __cplusplus
463 } /* closing bracket for extern "C" */
464 #endif