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 #include #include // Grouped Calculs program int main(int argc, char** argv) { MPI_Init(NULL, NULL); int nb_mpi_processes; MPI_Comm_size(MPI_COMM_WORLD, &nb_mpi_processes); int rank; MPI_Comm_rank(MPI_COMM_WORLD, &rank); double val,sum_val,mul_val,max_val,min_val; val = (rank + 1)*1.0; double aval[8]; double bval[2]; double cval[4]; MPI_Allreduce ( &val , &sum_val , 1 , MPI_DOUBLE , MPI_SUM , MPI_COMM_WORLD ); MPI_Allreduce ( &val , &mul_val , 1 , MPI_DOUBLE , MPI_PROD , MPI_COMM_WORLD ); MPI_Allreduce ( &val , &max_val , 1 , MPI_DOUBLE , MPI_MAX , MPI_COMM_WORLD ); MPI_Allreduce ( &val , &min_val , 1 , MPI_DOUBLE , MPI_MIN , MPI_COMM_WORLD ); // check printf("Process %d I have the values %lf %lf %lf %lf %lf\n",rank,val,sum_val,mul_val,max_val,min_val); // OTHERS val = 0.0; if(rank==0) {val = 7777.0;} MPI_Bcast( &val , 1 , MPI_DOUBLE , 0 , MPI_COMM_WORLD); // check printf("Process %d after BCAST %lf\n",rank,val); int n; if(rank==3) { for(n=0;n<8;++n) aval[n] = n*10.0; } MPI_Scatter( &aval , 2 , MPI_DOUBLE , &bval , 2 , MPI_DOUBLE , 3 , MPI_COMM_WORLD); // check printf("Process %d after SCATTER %lf %lf\n",rank,bval[0],bval[1]); MPI_Finalize(); // Close MPI return 0; }