C C Open Systems Laboratory C http://www.lam-mpi.org/tutorials/ C Indiana University C C MPI Tutorial C C Mail questions regarding tutorial material to lam at lam dash mpi dot org C program main include 'mpif.h' integer rank, size integer done integer leftDest, rightDest integer tag double precision X, leftX, rightX integer ierr integer req(4) integer status(4, MPI_STATUS_SIZE) tag = 4 call MPI_INIT(ierr) call MPI_COMM_RANK(MPI_COMM_WORLD, rank, ierr) call MPI_COMM_SIZE(MPI_COMM_WORLD, size, ierr) if (rank .eq. 0) then write(*, *) 'Enter the number of iterations: ' read(*,*) done endif call MPI_BCAST(done, 1, MPI_INTEGER, 0, MPI_COMM_WORLD, ierr) if (rank .eq. 0 .or. rank .eq. (size-1)) then X = 1 else X = 2 endif if (rank .eq. 0) then leftX = 1 leftDest = MPI_PROC_NULL else leftX = 2 leftDest = rank - 1 endif if (rank .eq. size-1) then rightX = 1 rightDest = MPI_PROC_NULL else rightX = 2 rightDest = rank + 1 endif 10 done = done - 1 if (done .le. 0) goto 20 C Post receives from neighbors call MPI_IRECV(leftX, 1, MPI_DOUBLE_PRECISION, leftDest, tag, $ MPI_COMM_WORLD, req(1), ierr) call MPI_IRECV(rightX, 1, MPI_DOUBLE_PRECISION, rightDest, tag, $ MPI_COMM_WORLD, req(2), ierr) C Post sends to neighbors call MPI_ISEND(X, 1, MPI_DOUBLE_PRECISION, leftDest, tag, $ MPI_COMM_WORLD, req(3), ierr) call MPI_ISEND(X, 1, MPI_DOUBLE_PRECISION, rightDest, tag, $ MPI_COMM_WORLD, req(4), ierr) C Do local computation C Unfortunately, there is none. C But this is where you would do it. C Wait for the requests to complete call MPI_WAITALL(4, req, status, ierr) C Calculate the average (using leftX and rightX) X = (rightX + leftX) / 2.0 goto 10 20 continue if (rank .eq. 0) then write(*, *) 'Final average is: ', X endif c terminate MPI call MPI_FINALIZE(ierr) stop end