OAOzden Akinci
MPI lecture notes / Chapter 5 of 7

Communicators and virtual topologies

Create sub-communicators with MPI_Comm_split, and describe a process grid with Cartesian topologies so MPI finds your neighbours.

Communicators, groups and contexts

A communicator combines a group of processes with a private context. Messages sent on one communicator can never be received on another, even between the same two processes. That is what makes communicators so useful for libraries: a solver library that works on its own duplicate of your communicator (MPI_Comm_dup) cannot accidentally receive your messages, and you cannot receive its messages.

You need new communicators whenever only part of the processes should take part in a collective: a reduction along one row of a process grid, a group of I/O processes, or a coupled code where two programs share one MPI job.

MPI_Comm_split

The simplest way to make sub-communicators. Every process passes a colour and a key. Processes with the same colour end up in the same new communicator, ordered by key:

int row = rank / NCOLS, col = rank % NCOLS;

MPI_Comm row_comm, col_comm;
MPI_Comm_split(MPI_COMM_WORLD, row, col, &row_comm);   /* colour = row, key = col */
MPI_Comm_split(MPI_COMM_WORLD, col, row, &col_comm);   /* colour = col, key = row */

MPI_Allreduce(&rank, &row_sum, 1, MPI_INT, MPI_SUM, row_comm);   /* only within my row */
world rank 0 -> row 0 (rank 0 of 3, row sum 3), column 0 (max 3)
world rank 1 -> row 0 (rank 1 of 3, row sum 3), column 1 (max 4)
world rank 4 -> row 1 (rank 1 of 3, row sum 12), column 1 (max 4)
...

A process can pass MPI_UNDEFINED as its colour to stay out of every new communicator. It then receives MPI_COMM_NULL. Free communicators you created with MPI_Comm_free.

Other constructors are worth knowing:

  • MPI_Comm_split_type with MPI_COMM_TYPE_SHARED gives one communicator per node, containing the processes that share memory. This is the starting point for hybrid MPI + OpenMP codes and for shared-memory windows (chapter 6).
  • MPI_Comm_group, MPI_Group_incl and MPI_Comm_create build a communicator from an explicit list of ranks.

Cartesian topologies

For a structured grid, a Cartesian topology is the better tool. You describe the grid once, and MPI computes coordinates and neighbours for you:

int dims[2] = {0, 0};
MPI_Dims_create(size, 2, dims);                 /* e.g. 6 processes -> 3 x 2 */

int periods[2] = {1, 0};                        /* periodic in x, closed in y */
MPI_Comm grid;
MPI_Cart_create(MPI_COMM_WORLD, 2, dims, periods, 1, &grid);
MPI_Comm_rank(grid, &rank);                     /* ranks may be reordered */

int west, east, north, south;
MPI_Cart_shift(grid, 0, 1, &west, &east);
MPI_Cart_shift(grid, 1, 1, &north, &south);

At a non-periodic edge, MPI_Cart_shift returns MPI_PROC_NULL. Communication with MPI_PROC_NULL completes immediately and does nothing, so the same exchange code runs on interior and boundary processes:

MPI_Sendrecv(&mine, 1, MPI_DOUBLE, east, 0, &from_west, 1, MPI_DOUBLE, west, 0,
             grid, MPI_STATUS_IGNORE);

MPI_Cart_sub splits the grid into rows or columns, like MPI_Comm_split above but without any arithmetic. This article walks through 09_cart2d.c step by step, with its output.

Graph topologies and neighbourhood collectives

For irregular decompositions, MPI_Dist_graph_create_adjacent lets every process list its own neighbours. On any topology, MPI 3.0’s neighbourhood collectives, such as MPI_Neighbor_alltoall and MPI_Neighbor_alltoallv, exchange data with all neighbours in a single call. That replaces a loop of MPI_Isend/MPI_Irecv pairs and gives the library the whole pattern to optimise.

Exercises

  1. Extend 08_comm_split.c so that it works for any number of processes, not only multiples of NCOLS.
  2. Use MPI_Comm_split_type to print how many processes run on each node of your job.
  3. Make both dimensions of 09_cart2d.c periodic. Which outputs change?