!******************************************************************** ! Sample program 1 : Summing up integers from 1 to 100 ! using MPI (5 processors: id =0,4) ! Programmer : Makoto Nakajima ! Date : April 2002 !******************************************************************** PROGRAM sum_1_100 include 'mpif.h' !******* variable declaration ******* integer:: proc_no !specify the number of processor integer:: partial_sum !store partial sums integer:: total_sum !store total sum integer:: int_top !top integer of sumation integer:: int_end !last integer of sumation !(different for each processor) integer:: int_counter !used to count the integer to be summed up integer, dimension(4):: recv_sum !used for id=0 to receive data from proc 1-4 !******* variables related MPI ******* integer:: ierr !return error message from MPI subroutines integer:: id !identification number of each processor integer:: nproc !number of processors integer:: tag !id number for particular jobs integer, dimension(MPI_STATUS_SIZE):: STATUS !used to record the status of each processors. !MPI_STATUS_SIZE is defined in the MPI library !******* initialize MPI system ******* !initialization of MPI environment call MPI_INIT(ierr) !returns id number for each processor !(different for each processor) call MPI_COMM_RANK(MPI_COMM_WORLD, id, ierr) !returns the number of processors minus 1 !(id starts from 0) call MPI_COMM_SIZE(MPI_COMM_WORLD, nproc, ierr) !******* self introduction ******* print *, 'Hello! I am processor ',id !Notice that each processor prints different id number !******* summing up separately ******* total_sum=0 partial_sum=0 !Be careful! The value of int_top and int_end are different !for each processor because different processors have different !id number. int_top=20*id+1 int_end=20*(id+1) !Of coutrse, the sums are also different among processors if (id<=4) then do int_counter=int_top, int_end partial_sum=partial_sum+int_counter end do end if !******* calculate total sum by sending the partial sums ******* !Processors 1 to 4 send the sums to processor 0 if (id>=1 .and. id<=4) then tag=100+id call MPI_SEND(partial_sum,1,MPI_INTEGER,0,tag,& MPI_COMM_WORLD,ierr) end if !Processor 0 receives the partial sums from processors !1 to 4, sums them up, and send back to processors 1 to 4 if (id==0) then do proc_no=1, 4 tag=100+proc_no call MPI_RECV(recv_sum(proc_no),1,MPI_INTEGER,proc_no,& tag,MPI_COMM_WORLD,STATUS,ierr) end do total_sum=sum(recv_sum(1:4))+partial_sum do proc_no=1, 4 tag=proc_no+200 call MPI_SEND(total_sum,1,MPI_INTEGER,proc_no,tag,& MPI_COMM_WORLD,ierr) end do end if !Processors 1 to 4 receives the result if (id>=1 .and. id<=4) then tag=id+200 call MPI_RECV(total_sum,1,MPI_INTEGER,0,tag,& MPI_COMM_WORLD,STATUS,ierr) end if !******* write out the result ******* !Notice the processors after 5 is also running this program !but does not do anything significant in the program. 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