! reduceF.f90 ! This program uses MPI_Reduce to collect data from all the processors - ! including the root process - and apply some operation on all of this data. ! In this program the operation is to sum all of the data collected from all ! processes. The first parameter in MPI_Reduce is the value being sent from ! each process. The second parameter is the variable storing the sum of ! the data collected from all processes (in this case, the sum of all the ! PIs). The fifth parameter, MPI_SUM, is the operation to be done on all ! of the data collected from all processors. ! The total sum, piSum, has a valid value only in the root process. ! The root process is defined in the sixth parameter of MPI_Reduce ('source'). 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 == 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_REDUCE(pi, piSum, 1, MPI_DOUBLE, MPI_SUM, source, & MPI_COMM_WORLD, ierr) if (rank == source) then print *, 'Inside process ', source, & " sum of pi's = ", piSum end if call MPI_FINALIZE(ierr) end program ! CRAY SV1: ! cc reduce.c ! mpirun -np 8 a.out ! Rank 0 is broadcasting 3.14159265358979323846264300001E+0 ! Rank 2 receives 3.1415926535897824578569270833E+0 from process 0 ! Rank 3 receives 3.1415926535897824578569270833E+0 from process 0 ! Rank 4 receives 3.1415926535897824578569270833E+0 from process 0 ! Rank 5 receives 3.1415926535897824578569270833E+0 from process 0 ! Inside process 0 sum of pi's = 25.13274122871826 ! Rank 1 receives 3.1415926535897824578569270833E+0 from process 0 ! Rank 6 receives 3.1415926535897824578569270833E+0 from process 0 ! Rank 7 receives 3.1415926535897824578569270833E+0 from process 0 ! mpirun -np 4 a.out ! Rank 0 is broadcasting 3.14159265358979323846264300001E+0 ! Rank 1 receives 3.1415926535897824578569270833E+0 from process 0 ! Rank 2 receives 3.1415926535897824578569270833E+0 from process 0 ! Rank 3 receives 3.1415926535897824578569270833E+0 from process 0 ! Inside process 0 sum of pi's = 12.56637061435913