A communicator has a membership of processes, called a group, and only group members can use their communicator for communications. A process that executes a Send/Receive call with a communicator which doesn't have the process as a member, will fail. New groups are formed from existing groups by calling one of several different subroutines of the general form mpi_group_xxx, as listed in the following table:
| Routine | Function |
|---|---|
mpi_comm_group
|
returns group reference of communicator |
mpi_group_incl
|
forms new group by inclusion list |
mpi_group_excl
|
forms new group by exclusion list |
mpi_comm_create
|
creates communicator for new group |
Once a new group is formed, you must create a new communicator for it before attempting to send or receive messages using that context. All initial processes are members of the MPI_COMM_WORLD communicator.
Like groups, new communicators are created from previously defined communicators and all processes must call mpi_comm_create to generate a new communicator. That is, the creation process is collective, meaning that communications between all processors is required to establish the new communicator.
To recap, groups are formed to limit and identify communications, among a subset of processors. A new group can only be created from a previously defined group. A group must also have a context for communication and, therefore, must have a communicator created for it. The basic steps to form a group are:
- Obtain a complete set of task IDs from a communicator using mpi_comm_group.
- Create a group as a subset of the complete set by calling mpi_group_excl, mpi_group_incl, ....
- Create the new communicator for the group (subset) using mpi_comm_create.
call mpi_comm_group (icomm, igroup, ierr)
where:
| Parameter | Description | Status |
|---|---|---|
icomm
| communicator | [IN] |
igroup
|
group of icomm communicator | [OUT] |
ierr
|
MPI error number (0 = no error) | [OUT] |
call mpi_group_incl (ioldgrp, icount, ivincl, inewgrp, ierr) call mpi_group_excl (ioldgrp, icount, ivexcl, inewgrp, ierr)
where:
| Parameter | Description | Status |
|---|---|---|
ioldgrp
| reference group, used as the basis from which the new group is drawn | [IN] |
icount
|
number of members in new group (number of element in ivincl or ivexcl) | [IN] |
ivincl
|
an array of ranks numbers of size icount to include in new group | [IN] |
ivexcl
|
an array of ranks numbers of size icount to exclude from new group | [IN] |
inewgrp
|
new group | [OUT] |
ierr
|
MPI error number (0 = no error) | [OUT] |
call mpi_comm_create (icomm, inewgrp, inewcomm, ierr)
where:
| Parameter | Description | Status |
|---|---|---|
icomm
| communicator of reference group | [IN] |
inewgrp
|
create new communicator for this (new) group | [IN] |
inewcomm
|
new communicator created for this (new) group | [OUT] |
ierr
|
MPI error number (0 = no error) | [OUT] |
The following example illustrates the creation of two groups and their communicators. The even-numbered and odd-numbered ranks of the MPI_COMM_WORLD communicator form the new groups.
Explanation: The mpi_comm_group subroutine returns an
integer reference (stored in the variable iwgroup) for the group
of the MPI_COMM_WORLD communicator. The mpi_group_incl
and mpi_group_excl subroutines create new groups from the
iwgroup reference. The new group consists of neven
members with process ranks listed in the integer array iranks;
mpi_group_incl includes all ranks listed in the iranks
array while mpi_group_excl creates a group which excludes the
ranks listed in that array (other methods of group generation are available --
see the MPI reference
standard for more information).
The integer variables iegroup and iogroup are
returned values that delineate the new groups.
The new ranks of the even-numbered and odd-numbered groups range from 0
to one less than the number of "elements" in the group.
Note: the mpi_group_xxx subroutines are
collective and do not return rank values of the new group.
The first two arguments of the mpi_comm_create subroutines are
the communicator from which the new group is derived and the reference integer
(iegroup or iogroup) of the new group. The
third argument returns a reference integer for the new communicator. The
mpi_group_rank subroutine returns the rank of a process within
a group. Its
first argument is the integer group reference, the second is the returned rank
value, and the third is an error value. The mapping of the
MPI_COMM_WORLD
group to the even/odd groups is shown in the output at the end. The ranks of
the even/odd (subset) groups are numbered from 0 to the number of members in
the group less one.
Program groups
parameter(maxpes=128)
dimension iranks(maxpes)
include 'mpif.h'
c
call mpi_init(ierr)
call mpi_comm_size(MPI_COMM_WORLD,npes, ierr)
call mpi_comm_rank(MPI_COMM_WORLD,itask,ierr)
c
c Extract group from World Comm.
c
call mpi_comm_group(MPI_COMM_WORLD,iwgroup,ierr)
c
c Make list of even ids(ranks).
c
neven = (npes+1)/2
if(neven.gt.maxpes) stop
do i = 0,npes-1,2
iranks((i+2)/2) = i
enddo
c
c Form even and odd groups.
c
call mpi_group_incl(iwgroup,neven,iranks,iegroup,ierr)
call mpi_group_excl(iwgroup,neven,iranks,iogroup,ierr)
c
call mpi_comm_create(MPI_COMM_WORLD,iegroup,iecomm,ierr)
call mpi_comm_create(MPI_COMM_WORLD,iogroup,iocomm,ierr)
c
call mpi_group_rank(iegroup,iegid,ierr)
if(iegid.ne.MPI_UNDEFINED) then
print*,itask,iegid,' of even group'
else
call mpi_group_rank(iogroup,iogid,ierr)
print*,itask,iogid,' of odd group'
endif
c
end
For a four processor run, the output is:
0 0 of even group
2 1 of even group
1 0 of odd group
3 1 of odd group



