MPI_Group_rank

MPI_Group_rank


MPI_Group_rank
queries the group rank of the calling process. It returns a negative number if current process does not belong to group.

Fortran Syntax

Subroutine MPI_Group_rank(group, rank, ierr)

C Syntax

int MPI_Group_rank(MPI_Group group, int *rank)


Example in Fortran


include "mpif.h"
implicit none
integer group_world, worker_group
integer i, p, ierr, group_rank

call MPI_Comm_size(MPI_COMM_WORLD, p, ierr)
call MPI_Comm_group(MPI_COMM_WORLD, group_world, ierr)
call MPI_Group_excl(group_world, 1, 0, worker_group, ierr)

call MPI_Group_rank(worker_group, group_rank, ierr)

Example in C


#include "mpi.h";
MPI_Group group_world, worker_group;
int i, p, ierr, group_rank;

MPI_Comm_size(MPI_COMM_WORLD, &p);
MPI_Comm_group(MPI_COMM_WORLD, &group_world);
MPI_Group_excl(group_world, 1, 0, &worker_group);

MPI_Group_rank(worker_group, &group_rank);

In the above example, first we create a new worker group whose members are all but process 0 of the group MPI_COMM_WORLD. Then we query for the group rank. The ranks of MPI_COMM_WORLD has the range (0, 1, 2, …, p-1). For this simple example, the rank range of the new group, worker_group, is (0, 1, 2, …, p-2) as it has one less member (i.e., process 0) than MPI_COMM_WORLD. Consequently, the calling process’ corresponding rank number in the new group would be 1 smaller. For instance, if the calling process is “i” (which is the rank number in the MPI_COMM_WORLD group), the corresponding rank number in the new group would be “i-1”. Note however that if the calling process is process 0, since it does not belong to worker_group, MPI_Group_rank would return the value of MPI_UNDEFINED for group_rank indicating that it is not a member of the worker_group. For other arrangements, the rank number of the calling process in the new group may be less straightforward. The use of MPI_Group_rank eliminates the need for you to have to keep track of that. Note also that MPI_UNDEFINED is implementation-dependent. For instance

  • MPI_UNDEFINED = -3 for SGI’s MPI
  • MPI_UNDEFINED = -32766 for MPICH

To check if calling process belongs to group, something along this line would be good to ensure portability:


      if (group_rank .eq. MPI_UNDEFINED) then
c  group_rank does not belong to group
        ...
      else
c  group_rank belongs to group
        ...
      endif