/* * Open Systems Laboratory * http://www.lam-mpi.org/tutorials/ * Indiana University * * MPI Tutorial Lab * Rewrite the ring program using persistent communication * * Mail questions regarding tutorial material to lam at lam dash mpi dot org */ #include #include int main(int argc, char *argv[]) { int num; int rank, size, tag, next, from; MPI_Request send_request, recv_request; MPI_Init(&argc, &argv); MPI_Comm_rank( MPI_COMM_WORLD, &rank); MPI_Comm_size( MPI_COMM_WORLD, &size); tag = 201; next = (rank + 1) % size; from = MPI_ANY_SOURCE; if (rank == 0) { printf("Enter the number of times around the ring: "); scanf("%d", &num); printf("Process %d sending %d to %d\n", rank, num, next); MPI_Send(&num, 1, MPI_INT, next, tag, MPI_COMM_WORLD); } MPI_Send_init(&num, 1, MPI_INT, next, tag, MPI_COMM_WORLD, &send_request); MPI_Recv_init(&num, 1, MPI_INT, from, tag, MPI_COMM_WORLD, &recv_request); do { MPI_Start(&recv_request); MPI_Wait(&recv_request, MPI_STATUS_IGNORE); printf("Process %d received %d\n", rank, num); if (rank == 0) { --num; printf("Process 0 decremented num\n"); } printf("Process %d sending %d to %d\n", rank, num, next); MPI_Start(&send_request); MPI_Wait(&send_request, MPI_STATUS_IGNORE); } while (num > 0); printf("Process %d exiting\n", rank); /* The last process does one extra send to process 0, which needs */ /* to be received before the program can exit */ if (rank == 0) MPI_Recv(&num, 1, MPI_INT, from, tag, MPI_COMM_WORLD, MPI_STATUS_IGNORE); /* Now free the persistent requests */ MPI_Request_free(&send_request); MPI_Request_free(&recv_request); /* Quit */ MPI_Finalize(); return 0; }