program example16 include 'mpif.h' integer ierr, rank, i, size, size1 integer a(4), b(4) integer status(MPI_STATUS_SIZE) integer group, group1, group2 integer ranks(128), rank1, rank2, rank3 call MPI_INIT(ierr) call MPI_COMM_SIZE(MPI_COMM_WORLD, size, ierr) call MPI_COMM_RANK(MPI_COMM_WORLD, rank, ierr) call MPI_COMM_GROUP(MPI_COMM_WORLD, group, ierr) size1 = size/2 do i = 1, size1 ranks(i) = i-1 enddo call MPI_GROUP_INCL(group, size1, ranks, group1, ierr) call MPI_GROUP_EXCL(group, size1, ranks, group2, ierr) call MPI_GROUP_RANK(group1, rank1, ierr) call MPI_GROUP_RANK(group2, rank2, ierr) if (rank1 .eq. MPI_UNDEFINED) then if(rank2 .lt. size1) then call MPI_GROUP_TRANSLATE_RANKS(group1, 1, rank2, & group, rank3, ierr) else rank3 = MPI_UNDEFINED end if else call MPI_GROUP_TRANSLATE_RANKS(group2, 1, rank1, & group, rank3, ierr) end if a(1) = rank a(2) = rank1 a(3) = rank2 a(4) = rank3 if (rank3 .ne. MPI_UNDEFINED) then call MPI_SENDRECV(a, 4, MPI_INTEGER, rank3, 1, & b, 4, MPI_INTEGER, rank3, 1, & MPI_COMM_WORLD, status, ierr) end if call MPI_GROUP_FREE(group, ierr) call MPI_GROUP_FREE(group1, ierr) call MPI_GROUP_FREE(group2, ierr) print *, 'process ', rank, ' a=', a, ' b=', b call MPI_FINALIZE(ierr) end