/* * Illustrate the usage of MPI_SEND_RECV * All the proc send message to the next proc simultaneously, * and then receive message from the previous proc */ #include //for input/output #include //for mpi routines main( int argc, char** argv) { int rank; //the rank of this process int tag=0; //not important here int nproc; int sendbuf[1], recvbuf[1]; int dest; //store the destination MPI_Status status; //not important here MPI_Init(&argc, &argv); //Initializing mpi MPI_Comm_rank(MPI_COMM_WORLD, &rank); //Getting my rank MPI_Comm_size(MPI_COMM_WORLD, &nproc); //Getting total no of proc dest = (rank + 1) % nproc; printf("Proc %d: Sending message to %d...\n", rank, dest); if( rank == 0 ) { MPI_Sendrecv(sendbuf, 1, MPI_INT, dest, tag, recvbuf, 1, MPI_INT, nproc - 1, tag, MPI_COMM_WORLD, &status); } else { MPI_Sendrecv(sendbuf, 1, MPI_INT, dest, tag, recvbuf, 1, MPI_INT, rank - 1, tag, MPI_COMM_WORLD, &status); } printf("Proc %d: Receive message from %d...\n", rank, status.MPI_SOURCE); MPI_Finalize(); //Finalize MPI return 0; }