program Groupedcalculations
use mpi
implicit none
integer :: rank, nb_mpi_processes, ierror, tag, statu(MPI_STATUS_SIZE), n
real(8) :: val, sum_val,mul_val,max_val,min_val, val0
real(8), dimension(1:8) :: aval
real(8), dimension(1:2) :: bval
real(8), dimension(1:4) :: cval
! Grouped calculations program
call MPI_INIT( ierror )
call MPI_COMM_SIZE( MPI_COMM_WORLD , nb_mpi_processes , ierror )
call MPI_COMM_RANK( MPI_COMM_WORLD , rank , ierror )
! REDUCE
val = (rank + 1)*1.0d0
call MPI_ALLREDUCE ( val , sum_val , 1 , MPI_DOUBLE_PRECISION , MPI_SUM , MPI_COMM_WORLD , ierror)
call MPI_ALLREDUCE ( val , mul_val , 1 , MPI_DOUBLE_PRECISION , MPI_PROD , MPI_COMM_WORLD , ierror)
call MPI_ALLREDUCE ( val , max_val , 1 , MPI_DOUBLE_PRECISION , MPI_MAX , MPI_COMM_WORLD , ierror)
call MPI_ALLREDUCE ( val , min_val , 1 , MPI_DOUBLE_PRECISION , MPI_MIN , MPI_COMM_WORLD , ierror)
! check
print*, 'Process',rank,'I have the values',val,sum_val,mul_val,max_val,min_val
! OTHERS
val = 0.0d0
if(rank==0) val = 7777.0d0
call MPI_BCAST( val , 1 , MPI_DOUBLE_PRECISION , 0 , MPI_COMM_WORLD , ierror)
! check
print*, 'Process',rank,'after BCAST',val
if(rank==3) then
do n=1,8; aval(n) = n*10.0d0; end do
end if
call MPI_SCATTER( aval(1:8) , 2 , MPI_DOUBLE_PRECISION , bval(1:2) , 2 , MPI_DOUBLE_PRECISION , 3 , MPI_COMM_WORLD , ierror)
! check
print*, 'Process',rank,'after SCATTER',bval(1:2)
call MPI_FINALIZE ( ierror ) ! Close MPI
end program Groupedcalculations