! bcastF.f90 ! 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. program main include "mpif.h" double precision PI25DT parameter (PI25DT = 3.141592653589793238462643d0) double precision pi integer rank, size, count, source integer ierr, status(MPI_STATUS_SIZE) call MPI_INIT(ierr) call MPI_COMM_RANK(MPI_COMM_WORLD, rank, ierr) call MPI_COMM_SIZE(MPI_COMM_WORLD, size, ierr) count = 1; source = 0; if (rank .eq. source) then pi = PI25DT; print *, 'Rank ', rank, ' is broadcasting ', pi end if call MPI_BCAST(pi, 1, MPI_DOUBLE, source, MPI_COMM_WORLD, ierr) if (rank /= source) then print *, 'Rank ', rank, ' receives ', pi, & ' from process ', source end if call MPI_FINALIZE(ierr) end program ! CRAY SV1: ! f90 bcast.c ! mpirun -np 8 a.out ! Rank 0 is broadcasting 3.14159265358979323846264300001E+0 ! Rank 2 receives 3.14159265358978245785699378145E+0 from process 0 ! Rank 4 receives 3.14159265358978245785699378145E+0 from process 0 ! Rank 1 receives 3.14159265358978245785699378145E+0 from process 0 ! Rank 3 receives 3.14159265358978245785699378145E+0 from process 0 ! Rank 5 receives 3.14159265358978245785699378145E+0 from process 0 ! Rank 6 receives 3.14159265358978245785699378145E+0 from process 0 ! Rank 7 receives 3.14159265358978245785699378145E+0 from process 0 ! mpirun -np 4 a.out ! Rank 0 is broadcasting 3.14159265358979323846264300001E+0 ! Rank 2 receives 3.14159265358978245785699378145E+0 from process 0 ! Rank 3 receives 3.14159265358978245785699378145E+0 from process 0 ! Rank 1 receives 3.14159265358978245785699378145E+0 from process 0