/* lab01StarterB.c * * Send an integer from process 0 to all other processes */ #include #include #include int main(int argc, char* argv[]) { int my_rank; /* rank of process */ int size; /* number of processes */ int source; /* rank of sender */ int dest; /* rank of receiver */ int tag = 0; /* tag for messages */ double pi = 3.14159; int len; char name[100]; // Processor name char message[100]; int i; 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, &size); MPI_Get_processor_name(name, &len); if (my_rank == 0) { start_time = MPI_Wtime(); } // All processes MPI_Bcast(&pi, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD); if (my_rank > 0) { sprintf(message, "Process %d: received %f on machine %s", my_rank, pi, name); MPI_Send(message, strlen(message)+1, MPI_CHAR, 0, tag, MPI_COMM_WORLD); } else { printf("Inside process 0, there are currently %d processes\n", size); for(i = 1; i < size; i++) { MPI_Recv(message, 100, MPI_CHAR, i, tag, MPI_COMM_WORLD, &status); printf("%s\n", message); } end_time = MPI_Wtime(); printf("Total time for sending/recieving data: %f\n", end_time - start_time); } /* Shut down MPI */ MPI_Finalize(); return 0; } /* main */ /* cc lab01StarterB.c mpirun -np 4 a.out Inside process 0, there are currently 4 processes Process 1: received 3.141590 on machine sn3313 Process 2: received 3.141590 on machine sn3313 Process 3: received 3.141590 on machine sn3313 Total time for sending/recieving data: 0.446812 */