program example12 include 'mpif.h' integer ierr, rank, size, prev, next, buf(2) integer status1(MPI_STATUS_SIZE), status2(MPI_STATUS_SIZE) call MPI_INIT(ierr) call MPI_COMM_SIZE(MPI_COMM_WORLD, size, ierr) call MPI_COMM_RANK(MPI_COMM_WORLD, rank, ierr) prev = rank - 1 next = rank + 1 if(rank .eq. 0) prev = size - 1 if(rank .eq. size - 1) next = 0 call MPI_SENDRECV(rank, 1, MPI_INTEGER, prev, 6, & buf(2), 1, MPI_INTEGER, next, 6, & MPI_COMM_WORLD, status2, ierr) call MPI_SENDRECV(rank, 1, MPI_INTEGER, next, 5, & buf(1), 1, MPI_INTEGER, prev, 5, & MPI_COMM_WORLD, status1, ierr) print *, 'process ', rank, & ' prev=', buf(1), ' next=', buf(2) call MPI_FINALIZE(ierr) end