/* greetings.cpp -- greetings program * * Send a message from all processes with rank != 0 to process 0. * Process 0 prints the messages received. * * Input: none. * Output: contents of messages received by process 0. * * See Chapter 3, pp. 41 & ff in PPMPI. */ #include #include //for the sprintf() function #include #include int main(int argc, char* argv[]) { int my_rank; /* rank of process */ int p; /* number of processes */ int source; /* rank of sender */ int dest; /* rank of receiver */ int tag = 0; /* tag for messages */ char message[100]; /* storage for message */ MPI::Status status; /* return status for */ /* receive */ /* Start up MPI */ MPI::Init(argc, argv); /* Find out process rank */ my_rank = MPI::COMM_WORLD.Get_rank(); /* Find out number of processes */ p = MPI::COMM_WORLD.Get_size(); if (my_rank != 0) { /* Create message */ sprintf(message, "Greetings from process %d!", my_rank); dest = 0; /* Use strlen+1 so that '\0' gets transmitted */ MPI::COMM_WORLD.Send(message, strlen(message)+1, MPI::CHAR, dest, tag); } else { /* my_rank == 0 */ for (source = 1; source < p; source++) { MPI::COMM_WORLD.Recv(message, 100, MPI::CHAR, source, tag, status); cout << message << endl; } } /* Shut down MPI */ MPI::Finalize(); } /* main */