/* 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 */ // MPImessage3.c: guests send rank to the host, who prints them #include "mpi.h" #include int main(int argc,char *argv[]) { int rank, size; int msg_size = 6; MPI_Status status; int tag = 10; int host = 0; MPI_Init(&argc,&argv); // Initialize MPI MPI_Comm_rank(MPI_COMM_WORLD,&rank); // Get CPU's rank MPI_Comm_size(MPI_COMM_WORLD,&size); // Get number of CPUs if (rank != host) { int n[1]; // Array of 1 integer n[0] = rank; printf("node %d about to send message\n", rank); MPI_Send(&n, 1, MPI_INTEGER, host, tag, MPI_COMM_WORLD); } else { int r[1]; int i; for(i=1; i < size; i++) { MPI_Recv(&r, 1, MPI_INTEGER, MPI_ANY_SOURCE, tag, MPI_COMM_WORLD, &status); printf("Message recieved: %d\n", r[0]); } } MPI_Finalize(); // Finalize MPI return 0; }