/* * 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 using namespace std; int main(int argc, char *argv[]) { int num; int rank, size, tag, next, from; MPI::Prequest send_request, recv_request; MPI::Init(argc, argv); rank = MPI::COMM_WORLD.Get_rank(); size = MPI::COMM_WORLD.Get_size(); tag = 201; next = (rank + 1) % size; from = MPI::ANY_SOURCE; if (rank == 0) { cout << "Enter the number of times around the ring: "; cin >> num; cout << "Process " << rank << " sending " << num << " to " << next << endl; MPI::COMM_WORLD.Send(&num, 1, MPI::INT, next, tag); } send_request = MPI::COMM_WORLD.Send_init(&num, 1, MPI::INT, next, tag); recv_request = MPI::COMM_WORLD.Recv_init(&num, 1, MPI::INT, from, tag); do { recv_request.Start(); recv_request.Wait(); cout << "Process " << rank << " received " << num << endl; if (rank == 0) { --num; cout << "Process 0 decremented num" << endl;; } cout << "Process " << rank << " sending " << num << " to " << next << endl; send_request.Start(); send_request.Wait(); } while (num > 0); cout << "Process " << rank << " exiting" << endl; // The last process does one extra send to process 0, which needs // to be received before the program can exit if (rank == 0) MPI::COMM_WORLD.Recv(&num, 1, MPI::INT, from, tag); /* Now free the persistent requests */ send_request.Free(); recv_request.Free(); // Quit MPI::Finalize(); return 0; }