MPI supports process grouping capability and allows the programmer to:
The programmer can create a group and associate a communicator with that group. The new communicator can be used in point-to-point or collective communication routines. Both groups and communicators are MPI objects (stored in system space) accessed by handles (returned from or passed to MPI routines).
| MPI_Comm_size | returns number of processes in communicator's group |
| MPI_Comm_rank | returns rank of calling process in communicator's group |
| MPI_Comm_compare | compares two communicators |
| MPI_Comm_dup | duplicates a communicator |
| MPI_Comm_create | creates a new communicator for a group |
| MPI_Comm_split | splits a communicator into multiple, non-overlapping communicators |
| MPI_Comm_free | marks a communicator for deallocation |
More Group and Communicator routines can be found on the MPI standard home page:
www.mpi-forum.org
Example 1:
To create two different process groups for separate collective communications:
/* Begin of the C code/ #include "mpi.h" #include <stdio.h> #define NPROCS 8 int main(argc,argv) int argc; char *argv[]; { int rank, new_rank, sendbuf, recvbuf, ranks1[4]={0,1,2,3}, ranks2[4]={4,5,6,7}; MPI_Group orig_group, new_group; MPI_Comm new_comm; MPI_Init(&argc,&argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); sendbuf = rank; /* Extract the original group handle */ MPI_Comm_group(MPI_COMM_WORLD, &orig_group); /* Divide tasks into two distinct groups based upon rank */ if (rank < NPROCS/2) { MPI_Group_incl(orig_group, NPROCS/2, ranks1, &new_group);} else { MPI_Group_incl(orig_group, NPROCS/2, ranks2, &new_group); } /* Create new communicator and then perform collective communications */ MPI_Comm_create(MPI_COMM_WORLD, new_group, &new_comm); MPI_Allreduce(&sendbuf, &recvbuf, 1, MPI_INT, MPI_SUM, new_comm); MPI_Group_rank (new_group, &new_rank); printf("rank= %d newrank= %d recvbuf= %d\n",rank,new_rank,recvbuf); MPI_Finalize(); } /* END of the C code/ Sample program output: rank= 7 newrank= 3 recvbuf= 22 rank= 0 newrank= 0 recvbuf= 6 rank= 1 newrank= 1 recvbuf= 6 rank= 2 newrank= 2 recvbuf= 6 rank= 6 newrank= 2 recvbuf= 22 rank= 3 newrank= 3 recvbuf= 6 rank= 4 newrank= 0 recvbuf= 22 rank= 5 newrank= 1 recvbuf= 22
Example 2:
To create virtual topology of matrix structure:
#include <stdio.h> #include <mpi.h> main(int argc, char **argv) { MPI_Comm row_comm, col_comm; int myrank, size, P=4, Q=3, p, q; MPI_Init (&argc, &argv); MPI_Comm_rank (MPI_COMM_WORLD, &myrank); MPI_Comm_size (MPI_COMM_WORLD, &size); /* Determine row and column position */ p = myrank / Q; q = myrank % Q; /* pick a row-major mapping */ /* Split comm into row and column comms */ MPI_Comm_split(MPI_COMM_WORLD, p, q, &row_comm); /* color by row, rank by column */ MPI_Comm_split(MPI_COMM_WORLD, q, p, &col_comm); /* color by column, rank by row */ printf("[%d]:My coordinates are (%d,%d)\n",myrank,p,q); MPI_Finalize(); }
Example 3:
To divide a communicator into two non-overlapping groups:
![]()
#include <stdio.h> #include <mpi.h> main(int argc, char **argv) { MPI_Comm row_comm; int myrank, size, p,q, Q; MPI_Init (&argc, &argv); MPI_Comm_rank (MPI_COMM_WORLD, &myrank); MPI_Comm_size (MPI_COMM_WORLD, &size); /* Determine row and column position */ Q = size /2; p = 1; q = myrank - Q; if(myrank < Q) { p = 0; q = myrank; } /* Split comm into two group */ MPI_Comm_split(MPI_COMM_WORLD, p, q, &row_comm); /* color by row, rank by column */ printf("[%d]:My coordinates are (%d,%d)\n",myrank,p,q); MPI_Finalize(); }
Example 4:
To divide a communicator such that:
Hints: color = (rank % 2 == 0) ? 0 : 1 ; key = size - rank ; MPI_Comm_split(comm, color, key, &newcomm) ;
- all processes with even ranks are in one group
- all processes with odd ranks are in the other group
- maintain the reverse order by rank
![]()
Comments: Roles of parameters "color" and "key" in MPI_Comm_split "color" controls subset assignment for each group under the communicator "comm". "key" controls rank assignment for each process inside a group.
Exercise:
- Divide the default communicator MPI_COMM_WORLD into 3 non-overlapping communicators such that
- processes with rank 0, 3, 6,... belong to the first group
- processes with rank 1, 4, 7,... belong to the second group
- processes with rank 2, 5, 8,... belong to the third group
![]()
- Print the rank in MPI_COMM_WORLD and rank in new communicator.