!******************************************************************** ! Sample program 2 : Summing up integers from 1 to 100 ! using collective operations of MPI ! Programmer : Makoto Nakajima ! Date : April 2002 !******************************************************************** PROGRAM sum_1_100 include 'mpif.h' !******* variable declaration ******* integer:: partial_sum, total_sum integer:: int_top, int_end, int_counter !******* variables related MPI ******* integer:: ierr !return error message from MPI subroutines integer:: id !identification number of each processor integer:: nproc !number of processors integer, dimension(MPI_STATUS_SIZE):: STATUS !******* initialize MPI system ******* call MPI_INIT(ierr) call MPI_COMM_RANK(MPI_COMM_WORLD, id, ierr) call MPI_COMM_SIZE(MPI_COMM_WORLD, nproc, ierr) !******* self introduction ******* print *, 'Hello! I am processor number ',id !******* summing up separately ******* total_sum=0 partial_sum=0 int_top=20*id+1 int_end=20*(id+1) if (id<=4) then do int_counter=int_top, int_end partial_sum=partial_sum+int_counter end do end if !******* calculating total sum directly by MPI_REDUCE ******* call MPI_REDUCE(partial_sum,total_sum,1,MPI_INTEGER,MPI_SUM,0,& MPI_COMM_WORLD,ierr) !******* broadcast the total sum to all the processors ******* call MPI_BCAST(total_sum,1,MPI_INTEGER,0,MPI_COMM_WORLD,ierr) !******* write out the result ******* print *,'This is a report from processor ',id print *,' my partial sum is',partial_sum print *,' my total sum is',total_sum !******* finalization of MPI environment call MPI_FINALIZE(ierr) END PROGRAM sum_1_100