/* nonblocking1.c SJ An example of better communication practice */ #include #include /* a variable needeed for call, result not used */ static MPI_Status tmpstat; /* variable to control progress of requests */ static MPI_Request myrequest; int main(int argc, char **argv) { int PID, /* process-ID */ P; /* number of processes */ /* own variables */ int v1 = 1, v2, other, 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 */ /* a simple two processor example */ if (P == 2) { other = 1-PID; /* send on backround */ MPI_Isend(&v1, 1, MPI_INT, other, 0, MPI_COMM_WORLD, &myrequest); /* receive from anohter */ MPI_Recv(&v2, 1, MPI_INT, other, 0, MPI_COMM_WORLD, &tmpstat); /* wait until send has completed */ MPI_Wait(&myrequest, &tmpstat); printf("%d: sent:%d received:%d\n", PID, v1, v2); /* receive on backround */ MPI_Irecv(&v2, 1, MPI_INT, other, 0, MPI_COMM_WORLD, &myrequest); /* send to another */ MPI_Send(&v1, 1, MPI_INT, other, 0, MPI_COMM_WORLD); /* wait until receive has completed */ MPI_Wait(&myrequest, &tmpstat); /* now you may use the received value */ printf("%d: sent:%d received:%d\n", PID, v1, v2); } else { printf("This example works only for 2 processes\n"); } /* keep this call */ MPI_Finalize(); exit(0); }