/* 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; & CC Bordeianu, Univ Bucharest, 2007 Support by National Science Foundation */ // MPImessage2.c: source node sends message to dest #include "mpi.h" #include int main(int argc,char *argv[]) { int rank; int msg_size = 6; MPI_Status status; int tag = 10; int source = 0, dest = 1; MPI_Init(&argc,&argv); // Initialize MPI MPI_Comm_rank(MPI_COMM_WORLD,&rank); // Get CPU's rank if (rank == source) { char *msg = "Hello"; printf("Host about to send message: %s\n",msg); // Send message, may block till dest recieves message MPI_Send(msg, msg_size, MPI_CHAR, dest, tag, MPI_COMM_WORLD); } else if (rank == dest) { char buffer[msg_size+1]; // Receive message, may block till dest receieves message MPI_Recv( &buffer, msg_size, MPI_CHAR, source, tag, MPI_COMM_WORLD, &status ); printf("Message recieved by %d: %s\n", rank, buffer); } printf("NODE %d done.\n", rank); // All nodes print MPI_Finalize(); // Finalize MPI return 0; }