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 Send the data to worker dest, use a non-blocking send and use C request(i) 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 C Do a non-blocking receive from source, placing the incoming number C in the sum array, again use request[i] 30 continue C Wait for the receives to complete 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 do 50, i = 1, len total = total + buffer(i) 50 continue C Send my sum back to the manager endif call MPI_FINALIZE(ierr) stop end