HP-MPI User's Guide (11th Edition)
Example applications
master_worker.f90
Appendix A 243
master_worker.f90
In this Fortran 90 example, a master task initiates (numtasks - 1)
number of worker tasks. The master distributes an equal portion of an
array to each worker task. Each worker task receives its portion of the
array and sets the value of each element to (the element’s index + 1).
Each worker task then sends its portion of the modified array back to the
master.
program array_manipulation
include 'mpif.h'
integer (kind=4) :: status(MPI_STATUS_SIZE)
integer (kind=4), parameter :: ARRAYSIZE = 10000, MASTER = 0
integer (kind=4) :: numtasks, numworkers, taskid, dest, index,
i
integer (kind=4) :: arraymsg, indexmsg, source, chunksize,
int4, real4
real (kind=4) :: data(ARRAYSIZE), result(ARRAYSIZE)
integer (kind=4) :: numfail, ierr
call MPI_Init(ierr)
call MPI_Comm_rank(MPI_COMM_WORLD, taskid, ierr)
call MPI_Comm_size(MPI_COMM_WORLD, numtasks, ierr)
numworkers = numtasks - 1
chunksize = (ARRAYSIZE / numworkers)
arraymsg = 1
indexmsg = 2
int4 = 4
real4 = 4
numfail = 0
! ******************************** Master task
******************************
if (taskid .eq. MASTER) then
data = 0.0
index = 1
do dest = 1, numworkers
call MPI_Send(index, 1, MPI_INTEGER, dest, 0,
MPI_COMM_WORLD, ierr)
call MPI_Send(data(index), chunksize, MPI_REAL, dest, 0,
&
MPI_COMM_WORLD, ierr)
index = index + chunksize
end do
do i = 1, numworkers
source = i
call MPI_Recv(index, 1, MPI_INTEGER, source, 1,
MPI_COMM_WORLD, &
status, ierr)