C C Open Systems Laboratory C http://www.lam-mpi.org/tutorials/ C Indiana University C C MPI Tutorial C Homework: Manager/worker (calculate an average in parallel) C C Mail questions regarding tutorial material to lam at lam dash mpi dot org C program main include 'mpif.h' integer rank, size, ierr, i, total integer workers integer max_workers, len double precision avg parameter (MAX_WORKERS = 10) parameter (LEN = 100) integer buffer(MAX_WORKERS * LEN) integer sum(MAX_WORKERS) integer request(MAX_WORKERS) integer status(MAX_WORKERS, MPI_STATUS_SIZE) call MPI_INIT(ierr) call MPI_COMM_RANK(MPI_COMM_WORLD, rank, ierr) call MPI_COMM_SIZE(MPI_COMM_WORLD, size, ierr) C Manager if (rank .eq. 0) then workers = size - 1 do 10, i = 1, workers * LEN buffer(i) = i - 1 10 continue C Send the data to each of the other processes do 20, i = 1, workers C Do any immediate send to all the workers and give them their C numbers call MPI_ISEND(buffer((i - 1) * len + 1), len, $ MPI_INTEGER, i, 201, MPI_COMM_WORLD, request(i), $ ierr) 20 continue C Wait for the sends to complete call MPI_WAITALL(workers, request, status, ierr) C Receive the results do 30, i = 1, workers call MPI_IRECV(sum(i), 1, MPI_INTEGER, i, $ 201, MPI_COMM_WORLD, request(i), ierr) 30 continue C Wait for the receives to complete call MPI_WAITALL(workers, request, status, ierr) total = 0 do 40, i = 1, workers total = total + sum(i) 40 continue avg = total / (len * workers) print *, 'The average is ', avg C Workers else total = 0 C Receive the data from the manager call MPI_RECV(buffer, len, MPI_INTEGER, 0, 201, $ MPI_COMM_WORLD, status, ierr) do 50, i = 1, len total = total + buffer(i) 50 continue C Send my sum back to the manager call MPI_SEND(total, 1, MPI_INTEGER, 0, 201, $ MPI_COMM_WORLD, ierr) endif call MPI_FINALIZE(ierr) stop end