/* 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 "mpi.h" 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 */ MPI_Comm_rank(MPI_COMM_WORLD, &my_rank); /* Find out number of processes */ MPI_Comm_size(MPI_COMM_WORLD, &p); MPI_Get_processor_name(name, &len); if (my_rank != 0) { source = 0; //receive from the "master process" MPI_Recv(&num, 1, MPI_INT, source, tag, MPI_COMM_WORLD, &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_Send(&reciprocal, 1, MPI_DOUBLE,dest, tag, MPI_COMM_WORLD); /* Use strlen+1 so that '\0' gets transmitted */ MPI_Send(message, strlen(message)+1, MPI_CHAR, dest, tag, MPI_COMM_WORLD); MPI_Send(name, len+1, MPI_CHAR, dest, tag, MPI_COMM_WORLD); } else { /* my_rank == 0 */ start_time = MPI_Wtime(); printf("Hello from process 0 on %s\n", name); for (source = 1; source < p; source++) { num = source; dest = source; MPI_Send(&num, 1, MPI_INT,dest, tag, MPI_COMM_WORLD); MPI_Recv(&reciprocal, 1, MPI_DOUBLE, source, tag, MPI_COMM_WORLD, &status); MPI_Recv(message, 100, MPI_CHAR, source, tag, MPI_COMM_WORLD, &status); MPI_Recv(name, 100, MPI_CHAR, source, tag, MPI_COMM_WORLD, &status); printf("%s on machine %s, reciprocal = %lf\n", message, name, reciprocal); } end_time = MPI_Wtime(); printf("Total time for sending/recieving data: %f\n", end_time - start_time); } /* Shut down MPI */ MPI_Finalize(); } /* main */ /* lamboot -v hostfile mpicc -o recipMPI recipMPI.c mpirun N recipMPI Hello from process 0 on helium Greetings from process 1! on machine silicon, reciprocal = 1.000000 Greetings from process 2! on machine phosphorus, reciprocal = 0.500000 Greetings from process 3! on machine magnesium, reciprocal = 0.333333 Greetings from process 4! on machine deadend, reciprocal = 0.250000 mpirun -np 6 recipMPI Hello from process 0 on helium Greetings from process 1! on machine silicon, reciprocal = 1.000000 Greetings from process 2! on machine phosphorus, reciprocal = 0.500000 Greetings from process 3! on machine magnesium, reciprocal = 0.333333 Greetings from process 4! on machine deadend, reciprocal = 0.250000 Greetings from process 5! on machine helium, reciprocal = 0.200000 mpirun -np 10 recipMPI Hello from process 0 on helium Greetings from process 1! on machine silicon, reciprocal = 1.000000 Greetings from process 2! on machine phosphorus, reciprocal = 0.500000 Greetings from process 3! on machine magnesium, reciprocal = 0.333333 Greetings from process 4! on machine deadend, reciprocal = 0.250000 Greetings from process 5! on machine helium, reciprocal = 0.200000 Greetings from process 6! on machine silicon, reciprocal = 0.166667 Greetings from process 7! on machine phosphorus, reciprocal = 0.142857 Greetings from process 8! on machine magnesium, reciprocal = 0.125000 Greetings from process 9! on machine deadend, reciprocal = 0.111111 */