/* From: "COMPUTATIONAL PHYSICS, 2nd Ed" by RH Landau, MJ Paez, and CC Bordeianu Copyright Wiley-VCH 2007. Electronic Materials copyright: R Landau, Oregon State Univ, 2007; MJ Paez, Univ Antioquia, 2007; and CC Bordeianu, Univ Bucharest, 2007. Support by National Science Foundation */ // Code listing for MPIdeadlock.c #include #include "mpi.h" #define MAXLEN 100 main(int argc, char *argv[]) { int myrank, numprocs, fromrank, torank; char tosend[MAXLEN], received[MAXLEN]; MPI_Status status; MPI_Init(&argc,&argv); MPI_Comm_rank(MPI_COMM_WORLD,&myrank); MPI_Comm_size(MPI_COMM_WORLD,&numprocs); if (myrank == 0) fromrank = numprocs - 1; else fromrank = myrank - 1; if (myrank == numprocs - 1) torank = 0; else torank = myrank + 1; // Save string to send in tosend: sprintf(tosend,"Message from node %d to %d\n", myrank,torank); MPI_Recv(received,MAXLEN,MPI_CHAR,fromrank,0,MPI_COMM_WORLD, &status); MPI_Send(tosend,MAXLEN,MPI_CHAR,torank,0,MPI_COMM_WORLD); printf("%s",received); // Print string after successful receive MPI_Finalize(); exit(0); }