      program example20b
      include 'mpif.h'
      integer ierr, rank, size, i, rbuf
      integer group, new_group, ranks(128), new_comm, new_rank
      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)
      do i = 1, size/2
         ranks(i) = i-1
      end do
      if(rank .lt. size/2) then
         call MPI_GROUP_INCL(group, size/2, ranks,
     &                       new_group, ierr)
      else 
         call MPI_GROUP_EXCL(group, size/2, ranks,
     &                       new_group, ierr)
      end if
      call MPI_COMM_CREATE(MPI_COMM_WORLD, new_group,
     &                     new_comm, ierr)
      call MPI_ALLREDUCE(rank, rbuf, 1, MPI_INTEGER,
     &                   MPI_SUM, new_comm, ierr)
      call MPI_GROUP_RANK(new_group, new_rank, ierr)
      print *, 'rank= ', rank, ' newrank= ',
     &         new_rank, ' rbuf= ', rbuf
      call MPI_FINALIZE(ierr)
      end
