Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix dist
[simgrid.git] / teshsuite / smpi / mpich3-test / coll / nonblocking3.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *  (C) 2011 by Argonne National Laboratory.
4  *      See COPYRIGHT in top-level directory.
5  */
6
7 /* This test attempts to execute multiple simultaneous nonblocking collective
8  * (NBC) MPI routines at the same time, and manages their completion with a
9  * variety of routines (MPI_{Wait,Test}{,_all,_any,_some}).  It also throws a
10  * few point-to-point operations into the mix.
11  *
12  * Possible improvements:
13  * - post operations on multiple comms from multiple threads
14  */
15
16 #include "mpi.h"
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <string.h>
20 #include <assert.h>
21
22 #ifdef HAVE_UNISTD_H
23 #include <unistd.h>
24 #endif
25
26 static int errs = 0;
27
28 /* Constants that control the high level test harness behavior. */
29 /* MAIN_ITERATIONS is how many NBC ops the test will attempt to issue. */
30 #define MAIN_ITERATIONS (100000)
31 /* WINDOW is the maximum number of outstanding NBC requests at any given time */
32 #define WINDOW (20)
33 /* we sleep with probability 1/CHANCE_OF_SLEEP */
34 #define CHANCE_OF_SLEEP (1000)
35 /* JITTER_DELAY is denominated in microseconds (us) */
36 #define JITTER_DELAY (50000)    /* 0.05 seconds */
37 /* NUM_COMMS is the number of communicators on which ops will be posted */
38 #define NUM_COMMS (4)
39
40 /* Constants that control behavior of the individual testing operations.
41  * Altering these can help to explore the testing space, but increasing them too
42  * much can consume too much memory (often O(n^2) usage). */
43 /* FIXME is COUNT==10 too limiting? should we try a larger count too (~500)? */
44 #define COUNT (10)
45 #define PRIME (17)
46
47 #define my_assert(cond_)                                                                 \
48     do {                                                                                 \
49         if (!(cond_)) {                                                                  \
50             ++errs;                                                                      \
51             if (errs < 10) {                                                             \
52                 fprintf(stderr, "assertion (%s) failed on line %d\n", #cond_, __LINE__); \
53             }                                                                            \
54         }                                                                                \
55     } while (0)
56
57 /* Intended to act like "rand_r", but we can be sure that it will exist and be
58  * consistent across all of comm world.  Returns a number in the range
59  * [0,GEN_PRN_MAX] */
60 #define GEN_PRN_MAX (4294967291-1)
61 static unsigned int gen_prn(unsigned int x)
62 {
63     /* a simple "multiplicative congruential method" PRNG, with parameters:
64      *   m=4294967291, largest 32-bit prime
65      *   a=279470273, good primitive root of m from "TABLES OF LINEAR
66      *                CONGRUENTIAL GENERATORS OF DIFFERENT SIZES AND GOOD
67      *                LATTICE STRUCTURE", by Pierre L’Ecuyer */
68     return (279470273UL * (unsigned long) x) % 4294967291UL;
69 }
70
71 /* given a random unsigned int value "rndval_" from gen_prn, this evaluates to a
72  * value in the range [min_,max_) */
73 #define rand_range(rndval_,min_,max_) \
74     ((unsigned int)((min_) + ((rndval_) * (1.0 / (GEN_PRN_MAX+1.0)) * ((max_) - (min_)))))
75
76
77 static void sum_fn(void *invec, void *inoutvec, int *len, MPI_Datatype * datatype)
78 {
79     int i;
80     int *in = invec;
81     int *inout = inoutvec;
82     for (i = 0; i < *len; ++i) {
83         inout[i] = in[i] + inout[i];
84     }
85 }
86
87 /* used to keep track of buffers that should be freed after the corresponding
88  * operation has completed */
89 struct laundry {
90     int case_num;               /* which test case initiated this req/laundry */
91     MPI_Comm comm;
92     int *buf;
93     int *recvbuf;
94     int *sendcounts;
95     int *recvcounts;
96     int *sdispls;
97     int *rdispls;
98     int *sendtypes;
99     int *recvtypes;
100 };
101
102 static void cleanup_laundry(struct laundry *l)
103 {
104     l->case_num = -1;
105     l->comm = MPI_COMM_NULL;
106     if (l->buf)
107         free(l->buf);
108     if (l->recvbuf)
109         free(l->recvbuf);
110     if (l->sendcounts)
111         free(l->sendcounts);
112     if (l->recvcounts)
113         free(l->recvcounts);
114     if (l->sdispls)
115         free(l->sdispls);
116     if (l->rdispls)
117         free(l->rdispls);
118     if (l->sendtypes)
119         free(l->sendtypes);
120     if (l->recvtypes)
121         free(l->recvtypes);
122 }
123
124 /* Starts a "random" operation on "comm" corresponding to "rndnum" and returns
125  * in (*req) a request handle corresonding to that operation.  This call should
126  * be considered collective over comm (with a consistent value for "rndnum"),
127  * even though the operation may only be a point-to-point request. */
128 static void start_random_nonblocking(MPI_Comm comm, unsigned int rndnum, MPI_Request * req,
129                                      struct laundry *l)
130 {
131     int i, j;
132     int rank, size;
133     int *buf = NULL;
134     int *recvbuf = NULL;
135     int *sendcounts = NULL;
136     int *recvcounts = NULL;
137     int *sdispls = NULL;
138     int *rdispls = NULL;
139     int *sendtypes = NULL;
140     int *recvtypes = NULL;
141     signed char *buf_alias = NULL;
142
143     MPI_Comm_rank(comm, &rank);
144     MPI_Comm_size(comm, &size);
145
146     *req = MPI_REQUEST_NULL;
147
148     l->case_num = -1;
149     l->comm = comm;
150
151     l->buf = buf = malloc(COUNT * size * sizeof(int));
152     l->recvbuf = recvbuf = malloc(COUNT * size * sizeof(int));
153     l->sendcounts = sendcounts = malloc(size * sizeof(int));
154     l->recvcounts = recvcounts = malloc(size * sizeof(int));
155     l->sdispls = sdispls = malloc(size * sizeof(int));
156     l->rdispls = rdispls = malloc(size * sizeof(int));
157     l->sendtypes = sendtypes = malloc(size * sizeof(MPI_Datatype));
158     l->recvtypes = recvtypes = malloc(size * sizeof(MPI_Datatype));
159
160 #define NUM_CASES (21)
161     l->case_num = rand_range(rndnum, 0, NUM_CASES);
162     switch (l->case_num) {
163     case 0:    /* MPI_Ibcast */
164         for (i = 0; i < COUNT; ++i) {
165             if (rank == 0) {
166                 buf[i] = i;
167             }
168             else {
169                 buf[i] = 0xdeadbeef;
170             }
171         }
172         MPI_Ibcast(buf, COUNT, MPI_INT, 0, comm, req);
173         break;
174
175     case 1:    /* MPI_Ibcast (again, but designed to stress scatter/allgather impls) */
176         /* FIXME fiddle with PRIME and buffer allocation s.t. PRIME is much larger (1021?) */
177         buf_alias = (signed char *) buf;
178         my_assert(COUNT * size * sizeof(int) > PRIME);  /* sanity */
179         for (i = 0; i < PRIME; ++i) {
180             if (rank == 0)
181                 buf_alias[i] = i;
182             else
183                 buf_alias[i] = 0xdb;
184         }
185         for (i = PRIME; i < COUNT * size * sizeof(int); ++i) {
186             buf_alias[i] = 0xbf;
187         }
188         MPI_Ibcast(buf_alias, PRIME, MPI_SIGNED_CHAR, 0, comm, req);
189         break;
190
191     case 2:    /* MPI_Ibarrier */
192         MPI_Ibarrier(comm, req);
193         break;
194
195     case 3:    /* MPI_Ireduce */
196         for (i = 0; i < COUNT; ++i) {
197             buf[i] = rank + i;
198             recvbuf[i] = 0xdeadbeef;
199         }
200         MPI_Ireduce(buf, recvbuf, COUNT, MPI_INT, MPI_SUM, 0, comm, req);
201         break;
202
203     case 4:    /* same again, use a user op and free it before the wait */
204         {
205             MPI_Op op = MPI_OP_NULL;
206             MPI_Op_create(sum_fn, /*commute= */ 1, &op);
207             for (i = 0; i < COUNT; ++i) {
208                 buf[i] = rank + i;
209                 recvbuf[i] = 0xdeadbeef;
210             }
211             MPI_Ireduce(buf, recvbuf, COUNT, MPI_INT, op, 0, comm, req);
212             MPI_Op_free(&op);
213         }
214         break;
215
216     case 5:    /* MPI_Iallreduce */
217         for (i = 0; i < COUNT; ++i) {
218             buf[i] = rank + i;
219             recvbuf[i] = 0xdeadbeef;
220         }
221         MPI_Iallreduce(buf, recvbuf, COUNT, MPI_INT, MPI_SUM, comm, req);
222         break;
223
224     case 6:    /* MPI_Ialltoallv (a weak test, neither irregular nor sparse) */
225         for (i = 0; i < size; ++i) {
226             sendcounts[i] = COUNT;
227             recvcounts[i] = COUNT;
228             sdispls[i] = COUNT * i;
229             rdispls[i] = COUNT * i;
230             for (j = 0; j < COUNT; ++j) {
231                 buf[i * COUNT + j] = rank + (i * j);
232                 recvbuf[i * COUNT + j] = 0xdeadbeef;
233             }
234         }
235         MPI_Ialltoallv(buf, sendcounts, sdispls, MPI_INT, recvbuf, recvcounts, rdispls, MPI_INT,
236                        comm, req);
237         break;
238
239     case 7:    /* MPI_Igather */
240         for (i = 0; i < size * COUNT; ++i) {
241             buf[i] = rank + i;
242             recvbuf[i] = 0xdeadbeef;
243         }
244         MPI_Igather(buf, COUNT, MPI_INT, recvbuf, COUNT, MPI_INT, 0, comm, req);
245         break;
246
247     case 8:    /* same test again, just use a dup'ed datatype and free it before the wait */
248         {
249             MPI_Datatype type = MPI_DATATYPE_NULL;
250             MPI_Type_dup(MPI_INT, &type);
251             for (i = 0; i < size * COUNT; ++i) {
252                 buf[i] = rank + i;
253                 recvbuf[i] = 0xdeadbeef;
254             }
255             MPI_Igather(buf, COUNT, MPI_INT, recvbuf, COUNT, type, 0, comm, req);
256             MPI_Type_free(&type);       /* should cause implementations that don't refcount
257                                          * correctly to blow up or hang in the wait */
258         }
259         break;
260
261     case 9:    /* MPI_Iscatter */
262         for (i = 0; i < size; ++i) {
263             for (j = 0; j < COUNT; ++j) {
264                 if (rank == 0)
265                     buf[i * COUNT + j] = i + j;
266                 else
267                     buf[i * COUNT + j] = 0xdeadbeef;
268                 recvbuf[i * COUNT + j] = 0xdeadbeef;
269             }
270         }
271         MPI_Iscatter(buf, COUNT, MPI_INT, recvbuf, COUNT, MPI_INT, 0, comm, req);
272         break;
273
274     case 10:   /* MPI_Iscatterv */
275         for (i = 0; i < size; ++i) {
276             /* weak test, just test the regular case where all counts are equal */
277             sendcounts[i] = COUNT;
278             sdispls[i] = i * COUNT;
279             for (j = 0; j < COUNT; ++j) {
280                 if (rank == 0)
281                     buf[i * COUNT + j] = i + j;
282                 else
283                     buf[i * COUNT + j] = 0xdeadbeef;
284                 recvbuf[i * COUNT + j] = 0xdeadbeef;
285             }
286         }
287         MPI_Iscatterv(buf, sendcounts, sdispls, MPI_INT, recvbuf, COUNT, MPI_INT, 0, comm, req);
288         break;
289
290     case 11:   /* MPI_Ireduce_scatter */
291         for (i = 0; i < size; ++i) {
292             recvcounts[i] = COUNT;
293             for (j = 0; j < COUNT; ++j) {
294                 buf[i * COUNT + j] = rank + i;
295                 recvbuf[i * COUNT + j] = 0xdeadbeef;
296             }
297         }
298         MPI_Ireduce_scatter(buf, recvbuf, recvcounts, MPI_INT, MPI_SUM, comm, req);
299         break;
300
301     case 12:   /* MPI_Ireduce_scatter_block */
302         for (i = 0; i < size; ++i) {
303             for (j = 0; j < COUNT; ++j) {
304                 buf[i * COUNT + j] = rank + i;
305                 recvbuf[i * COUNT + j] = 0xdeadbeef;
306             }
307         }
308         MPI_Ireduce_scatter_block(buf, recvbuf, COUNT, MPI_INT, MPI_SUM, comm, req);
309         break;
310
311     case 13:   /* MPI_Igatherv */
312         for (i = 0; i < size * COUNT; ++i) {
313             buf[i] = 0xdeadbeef;
314             recvbuf[i] = 0xdeadbeef;
315         }
316         for (i = 0; i < COUNT; ++i) {
317             buf[i] = rank + i;
318         }
319         for (i = 0; i < size; ++i) {
320             recvcounts[i] = COUNT;
321             rdispls[i] = i * COUNT;
322         }
323         MPI_Igatherv(buf, COUNT, MPI_INT, recvbuf, recvcounts, rdispls, MPI_INT, 0, comm, req);
324         break;
325
326     case 14:   /* MPI_Ialltoall */
327         for (i = 0; i < size; ++i) {
328             for (j = 0; j < COUNT; ++j) {
329                 buf[i * COUNT + j] = rank + (i * j);
330                 recvbuf[i * COUNT + j] = 0xdeadbeef;
331             }
332         }
333         MPI_Ialltoall(buf, COUNT, MPI_INT, recvbuf, COUNT, MPI_INT, comm, req);
334         break;
335
336     case 15:   /* MPI_Iallgather */
337         for (i = 0; i < size * COUNT; ++i) {
338             buf[i] = rank + i;
339             recvbuf[i] = 0xdeadbeef;
340         }
341         MPI_Iallgather(buf, COUNT, MPI_INT, recvbuf, COUNT, MPI_INT, comm, req);
342         break;
343
344     case 16:   /* MPI_Iallgatherv */
345         for (i = 0; i < size; ++i) {
346             for (j = 0; j < COUNT; ++j) {
347                 recvbuf[i * COUNT + j] = 0xdeadbeef;
348             }
349             recvcounts[i] = COUNT;
350             rdispls[i] = i * COUNT;
351         }
352         for (i = 0; i < COUNT; ++i)
353             buf[i] = rank + i;
354         MPI_Iallgatherv(buf, COUNT, MPI_INT, recvbuf, recvcounts, rdispls, MPI_INT, comm, req);
355         break;
356
357     case 17:   /* MPI_Iscan */
358         for (i = 0; i < COUNT; ++i) {
359             buf[i] = rank + i;
360             recvbuf[i] = 0xdeadbeef;
361         }
362         MPI_Iscan(buf, recvbuf, COUNT, MPI_INT, MPI_SUM, comm, req);
363         break;
364
365     case 18:   /* MPI_Iexscan */
366         for (i = 0; i < COUNT; ++i) {
367             buf[i] = rank + i;
368             recvbuf[i] = 0xdeadbeef;
369         }
370         MPI_Iexscan(buf, recvbuf, COUNT, MPI_INT, MPI_SUM, comm, req);
371         break;
372
373     case 19:   /* MPI_Ialltoallw (a weak test, neither irregular nor sparse) */
374         for (i = 0; i < size; ++i) {
375             sendcounts[i] = COUNT;
376             recvcounts[i] = COUNT;
377             sdispls[i] = COUNT * i * sizeof(int);
378             rdispls[i] = COUNT * i * sizeof(int);
379             sendtypes[i] = MPI_INT;
380             recvtypes[i] = MPI_INT;
381             for (j = 0; j < COUNT; ++j) {
382                 buf[i * COUNT + j] = rank + (i * j);
383                 recvbuf[i * COUNT + j] = 0xdeadbeef;
384             }
385         }
386         MPI_Ialltoallw(buf, sendcounts, sdispls, sendtypes, recvbuf, recvcounts, rdispls, recvtypes,
387                        comm, req);
388         break;
389
390     case 20:   /* basic pt2pt MPI_Isend/MPI_Irecv pairing */
391         /* even ranks send to odd ranks, but only if we have a full pair */
392         if ((rank % 2 != 0) || (rank != size - 1)) {
393             for (j = 0; j < COUNT; ++j) {
394                 buf[j] = j;
395                 recvbuf[j] = 0xdeadbeef;
396             }
397             if (rank % 2 == 0)
398                 MPI_Isend(buf, COUNT, MPI_INT, rank + 1, 5, comm, req);
399             else
400                 MPI_Irecv(recvbuf, COUNT, MPI_INT, rank - 1, 5, comm, req);
401         }
402         break;
403
404     default:
405         fprintf(stderr, "unexpected value for l->case_num=%d)\n", (l->case_num));
406         MPI_Abort(comm, 1);
407         break;
408     }
409 }
410
411 static void check_after_completion(struct laundry *l)
412 {
413     int i, j;
414     int rank, size;
415     MPI_Comm comm = l->comm;
416     int *buf = l->buf;
417     int *recvbuf = l->recvbuf;
418     int *sendcounts = l->sendcounts;
419     int *recvcounts = l->recvcounts;
420     int *sdispls = l->sdispls;
421     int *rdispls = l->rdispls;
422     int *sendtypes = l->sendtypes;
423     int *recvtypes = l->recvtypes;
424     char *buf_alias = (char *) buf;
425
426     MPI_Comm_rank(comm, &rank);
427     MPI_Comm_size(comm, &size);
428
429     /* these cases all correspond to cases in start_random_nonblocking */
430     switch (l->case_num) {
431     case 0:    /* MPI_Ibcast */
432         for (i = 0; i < COUNT; ++i) {
433             if (buf[i] != i)
434                 printf("buf[%d]=%d i=%d\n", i, buf[i], i);
435             my_assert(buf[i] == i);
436         }
437         break;
438
439     case 1:    /* MPI_Ibcast (again, but designed to stress scatter/allgather impls) */
440         for (i = 0; i < PRIME; ++i) {
441             if (buf_alias[i] != i)
442                 printf("buf_alias[%d]=%d i=%d\n", i, buf_alias[i], i);
443             my_assert(buf_alias[i] == i);
444         }
445         break;
446
447     case 2:    /* MPI_Ibarrier */
448         /* nothing to check */
449         break;
450
451     case 3:    /* MPI_Ireduce */
452         if (rank == 0) {
453             for (i = 0; i < COUNT; ++i) {
454                 if (recvbuf[i] != ((size * (size - 1) / 2) + (i * size)))
455                     printf("got recvbuf[%d]=%d, expected %d\n", i, recvbuf[i],
456                            ((size * (size - 1) / 2) + (i * size)));
457                 my_assert(recvbuf[i] == ((size * (size - 1) / 2) + (i * size)));
458             }
459         }
460         break;
461
462     case 4:    /* same again, use a user op and free it before the wait */
463         if (rank == 0) {
464             for (i = 0; i < COUNT; ++i) {
465                 if (recvbuf[i] != ((size * (size - 1) / 2) + (i * size)))
466                     printf("got recvbuf[%d]=%d, expected %d\n", i, recvbuf[i],
467                            ((size * (size - 1) / 2) + (i * size)));
468                 my_assert(recvbuf[i] == ((size * (size - 1) / 2) + (i * size)));
469             }
470         }
471         break;
472
473     case 5:    /* MPI_Iallreduce */
474         for (i = 0; i < COUNT; ++i) {
475             if (recvbuf[i] != ((size * (size - 1) / 2) + (i * size)))
476                 printf("got recvbuf[%d]=%d, expected %d\n", i, recvbuf[i],
477                        ((size * (size - 1) / 2) + (i * size)));
478             my_assert(recvbuf[i] == ((size * (size - 1) / 2) + (i * size)));
479         }
480         break;
481
482     case 6:    /* MPI_Ialltoallv (a weak test, neither irregular nor sparse) */
483         for (i = 0; i < size; ++i) {
484             for (j = 0; j < COUNT; ++j) {
485                 /*printf("recvbuf[%d*COUNT+%d]=%d, expecting %d\n", i, j, recvbuf[i*COUNT+j], (i + (rank * j))); */
486                 my_assert(recvbuf[i * COUNT + j] == (i + (rank * j)));
487             }
488         }
489         break;
490
491     case 7:    /* MPI_Igather */
492         if (rank == 0) {
493             for (i = 0; i < size; ++i) {
494                 for (j = 0; j < COUNT; ++j) {
495                     my_assert(recvbuf[i * COUNT + j] == i + j);
496                 }
497             }
498         }
499         else {
500             for (i = 0; i < size * COUNT; ++i) {
501                 my_assert(recvbuf[i] == 0xdeadbeef);
502             }
503         }
504         break;
505
506     case 8:    /* same test again, just use a dup'ed datatype and free it before the wait */
507         if (rank == 0) {
508             for (i = 0; i < size; ++i) {
509                 for (j = 0; j < COUNT; ++j) {
510                     my_assert(recvbuf[i * COUNT + j] == i + j);
511                 }
512             }
513         }
514         else {
515             for (i = 0; i < size * COUNT; ++i) {
516                 my_assert(recvbuf[i] == 0xdeadbeef);
517             }
518         }
519         break;
520
521     case 9:    /* MPI_Iscatter */
522         for (j = 0; j < COUNT; ++j) {
523             my_assert(recvbuf[j] == rank + j);
524         }
525         if (rank != 0) {
526             for (i = 0; i < size * COUNT; ++i) {
527                 /* check we didn't corrupt the sendbuf somehow */
528                 my_assert(buf[i] == 0xdeadbeef);
529             }
530         }
531         break;
532
533     case 10:   /* MPI_Iscatterv */
534         for (j = 0; j < COUNT; ++j) {
535             my_assert(recvbuf[j] == rank + j);
536         }
537         if (rank != 0) {
538             for (i = 0; i < size * COUNT; ++i) {
539                 /* check we didn't corrupt the sendbuf somehow */
540                 my_assert(buf[i] == 0xdeadbeef);
541             }
542         }
543         for (i = 1; i < size; ++i) {
544             for (j = 0; j < COUNT; ++j) {
545                 /* check we didn't corrupt the rest of the recvbuf */
546                 my_assert(recvbuf[i * COUNT + j] == 0xdeadbeef);
547             }
548         }
549         break;
550
551     case 11:   /* MPI_Ireduce_scatter */
552         for (j = 0; j < COUNT; ++j) {
553             my_assert(recvbuf[j] == (size * rank + ((size - 1) * size) / 2));
554         }
555         for (i = 1; i < size; ++i) {
556             for (j = 0; j < COUNT; ++j) {
557                 /* check we didn't corrupt the rest of the recvbuf */
558                 my_assert(recvbuf[i * COUNT + j] == 0xdeadbeef);
559             }
560         }
561         break;
562
563     case 12:   /* MPI_Ireduce_scatter_block */
564         for (j = 0; j < COUNT; ++j) {
565             my_assert(recvbuf[j] == (size * rank + ((size - 1) * size) / 2));
566         }
567         for (i = 1; i < size; ++i) {
568             for (j = 0; j < COUNT; ++j) {
569                 /* check we didn't corrupt the rest of the recvbuf */
570                 my_assert(recvbuf[i * COUNT + j] == 0xdeadbeef);
571             }
572         }
573         break;
574
575     case 13:   /* MPI_Igatherv */
576         if (rank == 0) {
577             for (i = 0; i < size; ++i) {
578                 for (j = 0; j < COUNT; ++j) {
579                     my_assert(recvbuf[i * COUNT + j] == i + j);
580                 }
581             }
582         }
583         else {
584             for (i = 0; i < size * COUNT; ++i) {
585                 my_assert(recvbuf[i] == 0xdeadbeef);
586             }
587         }
588         break;
589
590     case 14:   /* MPI_Ialltoall */
591         for (i = 0; i < size; ++i) {
592             for (j = 0; j < COUNT; ++j) {
593                 /*printf("recvbuf[%d*COUNT+%d]=%d, expecting %d\n", i, j, recvbuf[i*COUNT+j], (i + (i * j))); */
594                 my_assert(recvbuf[i * COUNT + j] == (i + (rank * j)));
595             }
596         }
597         break;
598
599     case 15:   /* MPI_Iallgather */
600         for (i = 0; i < size; ++i) {
601             for (j = 0; j < COUNT; ++j) {
602                 my_assert(recvbuf[i * COUNT + j] == i + j);
603             }
604         }
605         break;
606
607     case 16:   /* MPI_Iallgatherv */
608         for (i = 0; i < size; ++i) {
609             for (j = 0; j < COUNT; ++j) {
610                 my_assert(recvbuf[i * COUNT + j] == i + j);
611             }
612         }
613         break;
614
615     case 17:   /* MPI_Iscan */
616         for (i = 0; i < COUNT; ++i) {
617             my_assert(recvbuf[i] == ((rank * (rank + 1) / 2) + (i * (rank + 1))));
618         }
619         break;
620
621     case 18:   /* MPI_Iexscan */
622         for (i = 0; i < COUNT; ++i) {
623             if (rank == 0)
624                 my_assert(recvbuf[i] == 0xdeadbeef);
625             else
626                 my_assert(recvbuf[i] == ((rank * (rank + 1) / 2) + (i * (rank + 1)) - (rank + i)));
627         }
628         break;
629
630     case 19:   /* MPI_Ialltoallw (a weak test, neither irregular nor sparse) */
631         for (i = 0; i < size; ++i) {
632             for (j = 0; j < COUNT; ++j) {
633                 /*printf("recvbuf[%d*COUNT+%d]=%d, expecting %d\n", i, j, recvbuf[i*COUNT+j], (i + (rank * j))); */
634                 my_assert(recvbuf[i * COUNT + j] == (i + (rank * j)));
635             }
636         }
637         break;
638
639     case 20:   /* basic pt2pt MPI_Isend/MPI_Irecv pairing */
640         /* even ranks send to odd ranks, but only if we have a full pair */
641         if ((rank % 2 != 0) || (rank != size - 1)) {
642             for (j = 0; j < COUNT; ++j) {
643                 /* only odd procs did a recv */
644                 if (rank % 2 == 0) {
645                     my_assert(recvbuf[j] == 0xdeadbeef);
646                 }
647                 else {
648                     if (recvbuf[j] != j)
649                         printf("recvbuf[%d]=%d j=%d\n", j, recvbuf[j], j);
650                     my_assert(recvbuf[j] == j);
651                 }
652             }
653         }
654         break;
655
656     default:
657         printf("invalid case_num (%d) detected\n", l->case_num);
658         assert(0);
659         break;
660     }
661 }
662
663 #undef NUM_CASES
664
665 static void complete_something_somehow(unsigned int rndnum, int numreqs, MPI_Request reqs[],
666                                        int *outcount, int indices[])
667 {
668     int i, idx, flag;
669
670 #define COMPLETION_CASES (8)
671     switch (rand_range(rndnum, 0, COMPLETION_CASES)) {
672     case 0:
673         MPI_Waitall(numreqs, reqs, MPI_STATUSES_IGNORE);
674         *outcount = numreqs;
675         for (i = 0; i < numreqs; ++i) {
676             indices[i] = i;
677         }
678         break;
679
680     case 1:
681         MPI_Testsome(numreqs, reqs, outcount, indices, MPI_STATUS_IGNORE);
682         if (*outcount == MPI_UNDEFINED) {
683             *outcount = 0;
684         }
685         break;
686
687     case 2:
688         MPI_Waitsome(numreqs, reqs, outcount, indices, MPI_STATUS_IGNORE);
689         if (*outcount == MPI_UNDEFINED) {
690             *outcount = 0;
691         }
692         break;
693
694     case 3:
695         MPI_Waitany(numreqs, reqs, &idx, MPI_STATUS_IGNORE);
696         if (idx == MPI_UNDEFINED) {
697             *outcount = 0;
698         }
699         else {
700             *outcount = 1;
701             indices[0] = idx;
702         }
703         break;
704
705     case 4:
706         MPI_Testany(numreqs, reqs, &idx, &flag, MPI_STATUS_IGNORE);
707         if (idx == MPI_UNDEFINED) {
708             *outcount = 0;
709         }
710         else {
711             *outcount = 1;
712             indices[0] = idx;
713         }
714         break;
715
716     case 5:
717         MPI_Testall(numreqs, reqs, &flag, MPI_STATUSES_IGNORE);
718         if (flag) {
719             *outcount = numreqs;
720             for (i = 0; i < numreqs; ++i) {
721                 indices[i] = i;
722             }
723         }
724         else {
725             *outcount = 0;
726         }
727         break;
728
729     case 6:
730         /* select a new random index and wait on it */
731         rndnum = gen_prn(rndnum);
732         idx = rand_range(rndnum, 0, numreqs);
733         MPI_Wait(&reqs[idx], MPI_STATUS_IGNORE);
734         *outcount = 1;
735         indices[0] = idx;
736         break;
737
738     case 7:
739         /* select a new random index and wait on it */
740         rndnum = gen_prn(rndnum);
741         idx = rand_range(rndnum, 0, numreqs);
742         MPI_Test(&reqs[idx], &flag, MPI_STATUS_IGNORE);
743         *outcount = (flag ? 1 : 0);
744         indices[0] = idx;
745         break;
746
747     default:
748         assert(0);
749         break;
750     }
751 #undef COMPLETION_CASES
752 }
753
754 int main(int argc, char **argv)
755 {
756     int i, num_posted, num_completed;
757     int wrank, wsize;
758     unsigned int seed = 0x10bc;
759     unsigned int post_seq, complete_seq;
760     struct laundry larr[WINDOW];
761     MPI_Request reqs[WINDOW];
762     int outcount;
763     int indices[WINDOW];
764     MPI_Comm comms[NUM_COMMS];
765     MPI_Comm comm;
766
767     MPI_Init(&argc, &argv);
768     MPI_Comm_rank(MPI_COMM_WORLD, &wrank);
769     MPI_Comm_size(MPI_COMM_WORLD, &wsize);
770
771     /* it is critical that all processes in the communicator start with a
772      * consistent value for "post_seq" */
773     post_seq = complete_seq = gen_prn(seed);
774
775     num_completed = 0;
776     num_posted = 0;
777
778     /* construct all of the communicators, just dups of comm world for now */
779     for (i = 0; i < NUM_COMMS; ++i) {
780         MPI_Comm_dup(MPI_COMM_WORLD, &comms[i]);
781     }
782
783     /* fill the entire window of ops */
784     for (i = 0; i < WINDOW; ++i) {
785         reqs[i] = MPI_REQUEST_NULL;
786         memset(&larr[i], 0, sizeof(struct laundry));
787         larr[i].case_num = -1;
788
789         /* randomly select a comm, using a new seed to avoid correlating
790          * particular kinds of NBC ops with particular communicators */
791         comm = comms[rand_range(gen_prn(post_seq), 0, NUM_COMMS)];
792
793         start_random_nonblocking(comm, post_seq, &reqs[i], &larr[i]);
794         ++num_posted;
795         post_seq = gen_prn(post_seq);
796     }
797
798     /* now loop repeatedly, completing ops with "random" completion functions,
799      * until we've posted and completed MAIN_ITERATIONS ops */
800     while (num_completed < MAIN_ITERATIONS) {
801         complete_something_somehow(complete_seq, WINDOW, reqs, &outcount, indices);
802         complete_seq = gen_prn(complete_seq);
803         for (i = 0; i < outcount; ++i) {
804             int idx = indices[i];
805             assert(reqs[idx] == MPI_REQUEST_NULL);
806             if (larr[idx].case_num != -1) {
807                 check_after_completion(&larr[idx]);
808                 cleanup_laundry(&larr[idx]);
809                 ++num_completed;
810                 if (num_posted < MAIN_ITERATIONS) {
811                     comm = comms[rand_range(gen_prn(post_seq), 0, NUM_COMMS)];
812                     start_random_nonblocking(comm, post_seq, &reqs[idx], &larr[idx]);
813                     ++num_posted;
814                     post_seq = gen_prn(post_seq);
815                 }
816             }
817         }
818
819         /* "randomly" and infrequently introduce some jitter into the system */
820         if (0 == rand_range(gen_prn(complete_seq + wrank), 0, CHANCE_OF_SLEEP)) {
821             usleep(JITTER_DELAY);       /* take a short nap */
822         }
823     }
824
825     for (i = 0; i < NUM_COMMS; ++i) {
826         MPI_Comm_free(&comms[i]);
827     }
828
829     if (wrank == 0) {
830         if (errs)
831             printf("found %d errors\n", errs);
832         else
833             printf(" No errors\n");
834     }
835
836     MPI_Finalize();
837
838     return 0;
839 }