/* nonblockingtest.c SJ An example of interleaved communication and communication */ #include #include #include #include #define MAXP 32 /* a variable needeed for call, result not used */ static MPI_Status tmpstat; int main(int argc, char **argv) { int PID, /* process-ID */ P; /* number of processes */ /* own variables */ int v1, i; /* keep these 3 calls */ MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &PID); MPI_Comm_size(MPI_COMM_WORLD, &P); /* actual code */ if (PID == 0) { /* process 0 is the master */ /* an array to control progress of send/recv requests */ MPI_Request req[MAXP]; /* an array of statuses */ MPI_Status stats[MAXP]; /* array of messages to send */ int rnd[MAXP]; int p; /* initiate sends */ for (p = 1; p < P; p++) { rnd[p] = 3 + rand()%(2*P); MPI_Isend(&rnd[p], 1, MPI_INT, p, 0, MPI_COMM_WORLD, &req[p]); } printf("0: all sends initiated\n"); /* "do" something */ sleep(1); /* ensure that all messages were sent */ MPI_Waitall(P-1, req+1, stats+1); /* req+1 == &(req[1]), i.e., addr of element at index 1 */ printf("0: all sent\n"); /* receive from all */ /* initiate receive */ for (p = 1; p < P; p++) { MPI_Irecv(&rnd[p], 1, MPI_INT, p, 0, MPI_COMM_WORLD, &req[p]); } printf("0: start receiving\n"); i = P-1; while(i) { /* exit after all are ready, see below */ int which; MPI_Status stat; MPI_Waitany(P-1, req+1, &which, &stat); /* if no request was anymore active, exit loop */ if (which == MPI_UNDEFINED) break; printf("0: process %d returned %d\n", stat.MPI_SOURCE, rnd[stat.MPI_SOURCE]); i--; } printf("0: all received\n"); } else { /* all other processes */ /* receive from 0 */ MPI_Recv(&v1, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, &tmpstat); /* pretend to work */ sleep(v1); /* send to 0 */ MPI_Send(&v1, 1, MPI_INT, 0, 0, MPI_COMM_WORLD); } /* keep this call */ MPI_Finalize(); exit(0); }