#include #include const int MAXITEMS = 10; int main(int argc, char **argv) { int size; int rank; int value; int root=0; int array[MAXITEMS]; int i; int anytag =0; double totalsum=0.0; double reciprocal; MPI_Status status; MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD, &size); MPI_Comm_rank(MPI_COMM_WORLD, &rank); if (rank == 0) { srand(time(0)); for (i = 1; i < size; i++) { value = rand() % 10; printf("Root process 0 sending %d to process %d\n", value, i); MPI_Send(&value, 1, MPI_INT, i, anytag, MPI_COMM_WORLD); } for (i=1; i < size; i++) { MPI_Recv(&reciprocal, 1, MPI_DOUBLE, i, anytag, MPI_COMM_WORLD, &status); printf("Root process 0 receiving %lf from process %i\n", reciprocal, i); } printf("\n"); } else { MPI_Recv(&value, 1, MPI_INT, 0, anytag, MPI_COMM_WORLD, &status); printf("Hello from process %d out of %d Received: %d\n", rank, size, value); if (value == 0) reciprocal = 0.0; else reciprocal = 1.0/ value; printf("Process %d sending reciprocal (of %d) %lf to root 0", rank, value, reciprocal); MPI_Send(&reciprocal, 1, MPI_DOUBLE, 0, anytag, MPI_COMM_WORLD); printf("\n"); } MPI_Finalize(); return 0; } /* mpicc -o reciprocalMPI reciprocalMPI.c mpirun N reciprocalMPI Root process 0 sending 7 to process 1 Root process 0 sending 0 to process 2 Root process 0 sending 9 to process 3 Root process 0 sending 6 to process 4 Root process 0 sending 8 to process 5 Root process 0 receiving 0.142857 from process 1 Hello from process 1 out of 6 Received: 7 Process 1 sending reciprocal (of 7) 0.142857 to root 0 Hello from process 3 out of 6 Received: 9 Process 3 sending reciprocal (of 9) 0.111111 to root 0 Hello from process 2 out of 6 Received: 0 Process 2 sending reciprocal (of 0) 0.000000 to root 0 Hello from process 4 out of 6 Received: 6 Process 4 sending reciprocal (of 6) 0.166667 to root 0 Hello from process 5 out of 6 Received: 8 Process 5 sending reciprocal (of 8) 0.125000 to root 0 Root process 0 receiving 0.000000 from process 2 Root process 0 receiving 0.111111 from process 3 Root process 0 receiving 0.166667 from process 4 Root process 0 receiving 0.125000 from process 5 */