// bcast.c // This program using MPI_BCAST (MPI Broadcast) to "broadcast" data - in this // case PI - from the root process (processor 0) to all the other processors. // Note that ALL of the processors have MPI_Bcast, not just the root process. // One of the parameters in MPI_Bcast provides the numerical value of the root // process. In this program the root process is 'source', and it equals 0. // This means that 0 is the root process sending the broadcast. If the // process is not 0, then it is receiving the broadcast from 0 (the 'source' // parameter in MPI_Bcast) // Make sure you see which parameter in MPI_Bcast represents the root process. #include #include #include "mpi.h" int main(int argc, char **argv) { int rank, size; MPI_Status status; double pi; int i, count, source; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &size); count = 1; source = 0; if (rank == source) { pi = M_PI; printf("Rank %d is broadcasting %.10f\n", rank, pi); } MPI_Bcast(&pi, 1, MPI_DOUBLE, source, MPI_COMM_WORLD); if (rank != source) { printf("Rank %d receives %.10f from process %d\n", rank, pi, source); } MPI_Finalize(); return 0; } /* CRAY SV1: cc bcast.c mpirun -np 8 a.out Rank 0 is broadcasting 3.1415926536 Rank 1 receives 3.1415926536 from process 0 Rank 2 receives 3.1415926536 from process 0 Rank 3 receives 3.1415926536 from process 0 Rank 4 receives 3.1415926536 from process 0 Rank 5 receives 3.1415926536 from process 0 Rank 6 receives 3.1415926536 from process 0 Rank 7 receives 3.1415926536 from process 0 mpirun -np 4 a.out Rank 0 is broadcasting 3.1415926536 Rank 2 receives 3.1415926536 from process 0 Rank 3 receives 3.1415926536 from process 0 Rank 1 receives 3.1415926536 from process 0 */