/* recipMPI.c - MPI version of master0/slave0.c in PVM * calculate the reciprocal * variation of greetings.c -- 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 #include #include "mpi.h" using namespace std; 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 */ double reciprocal; int num; char name[100]; // Processor name int len; // Length of the name double start_time, end_time; 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(); MPI_Get_processor_name(name, &len); if (my_rank != 0) { source = 0; //receive from the "master process" MPI::COMM_WORLD.Recv(&num, 1, MPI_INT, source, tag, status); if (num==0) {reciprocal = 0.0;} else {reciprocal = 1.0 / num;} /* Create message */ sprintf(message, "Greetings from process %d!", my_rank); dest = 0; MPI::COMM_WORLD.Send(&reciprocal, 1, MPI_DOUBLE,dest, tag); /* Use strlen+1 so that '\0' gets transmitted */ MPI::COMM_WORLD.Send(message, strlen(message)+1, MPI_CHAR, dest, tag); MPI::COMM_WORLD.Send(name, len+1, MPI_CHAR, dest, tag); } else { /* my_rank == 0 */ start_time = MPI_Wtime(); cout << "Hello from process 0 on " << name << endl; for (source = 1; source < p; source++) { num = source; dest = source; MPI::COMM_WORLD.Send(&num, 1, MPI_INT,dest, tag); MPI::COMM_WORLD.Recv(&reciprocal, 1, MPI_DOUBLE, source, tag, status); MPI::COMM_WORLD.Recv(message, 100, MPI_CHAR, source, tag, status); MPI::COMM_WORLD.Recv(name, 100, MPI_CHAR, source, tag, status); cout << message << " on machine " << name << " reciprocal = " << reciprocal << endl; } end_time = MPI_Wtime(); cout << "Total time for sending/recieving data: " << (end_time - start_time) << endl; } /* Shut down MPI */ MPI::Finalize(); } /* main */ /* lamboot -v lamhosts mpiCC -o recipMPICC recipMPI.cpp mpirun -np 10 recipMPICC Hello from process 0 on slingshot Greetings from process 1! on machine slingshot reciprocal = 1 Greetings from process 2! on machine slingshot reciprocal = 0.5 Greetings from process 3! on machine slingshot reciprocal = 0.333333 Greetings from process 4! on machine slingshot reciprocal = 0.25 Greetings from process 5! on machine slingshot reciprocal = 0.2 Greetings from process 6! on machine slingshot reciprocal = 0.166667 Greetings from process 7! on machine slingshot reciprocal = 0.142857 Greetings from process 8! on machine slingshot reciprocal = 0.125 Greetings from process 9! on machine slingshot reciprocal = 0.111111 Total time for sending/recieving data: 0.0826579 */