Most domain-decomposed codes contain a few lines like north = rank - nx; if (north < 0) north = .... They work until someone changes the decomposition, adds periodic boundaries or runs on a process count nobody tested. MPI has had a better way since its first version: virtual topologies. You describe the shape of your process grid once, and MPI answers the questions you would otherwise code by hand: what are my coordinates, who are my neighbours, who is in my row.
This post is based on a lecture I gave on communicators and virtual topologies. The example below was tested with Open MPI 4.1.
Communicators in one paragraph
Every MPI call works inside a communicator, a group of processes with its own rank numbering. MPI_COMM_WORLD contains everyone. You can make smaller ones, for example with MPI_Comm_split by giving each process a “colour”, and collective operations on them involve only their members. A Cartesian topology is a communicator that also knows the shape of a grid.
Step 1: let MPI choose the grid shape
int dims[2] = {0, 0}; /* 0 = "you decide" */
MPI_Dims_create(size, 2, dims);
For 6 processes this gives a 3 × 2 grid, for 12 a 4 × 3 grid. Set an entry to a non-zero value to fix that dimension yourself.
Step 2: create the Cartesian communicator
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); /* ask again: the rank may have changed */
int coords[2];
MPI_Cart_coords(grid, rank, 2, coords);
The reorder = 1 argument allows the library to renumber the processes so that grid neighbours land close to each other on the machine. Whether it actually does so depends on the implementation, but asking for it costs nothing. The one rule: after MPI_Cart_create, always take your rank from the new communicator, not from MPI_COMM_WORLD.
Step 3: find neighbours with MPI_Cart_shift
int west, east, north, south;
MPI_Cart_shift(grid, 0, 1, &west, &east); /* dimension 0, distance 1 */
MPI_Cart_shift(grid, 1, 1, &north, &south); /* dimension 1, distance 1 */
In a periodic dimension, the neighbours wrap around. In a non-periodic one, a process on the edge gets MPI_PROC_NULL for the missing side. That special rank is what makes the next step clean.
Step 4: exchange halos without special cases
MPI_Sendrecv(&mine, 1, MPI_DOUBLE, east, 0, &from_west, 1, MPI_DOUBLE, west, 0,
grid, MPI_STATUS_IGNORE);
MPI_Sendrecv(&mine, 1, MPI_DOUBLE, south, 1, &from_north, 1, MPI_DOUBLE, north, 1,
grid, MPI_STATUS_IGNORE);
Every process makes the same two calls. A send to or a receive from MPI_PROC_NULL completes immediately and does nothing, so the boundary needs no if. And because MPI_Sendrecv pairs the send with the receive, the exchange cannot deadlock however large the halo is (see the previous post for why that matters). In a real code mine would be a whole boundary row or column, often described with an MPI_Type_vector.
Step 5: row and column communicators with MPI_Cart_sub
int keep[2] = {1, 0}; /* keep dimension 0, drop dimension 1 */
MPI_Comm row;
MPI_Cart_sub(grid, keep, &row);
MPI_Allreduce(&mine, &row_sum, 1, MPI_DOUBLE, MPI_SUM, row);
MPI_Cart_sub splits the grid into lower-dimensional slices. Here every process ends up in a communicator with the processes that share its y coordinate. Reductions along a row or column, which you need for things like line solvers or 2D FFTs, become a single call.
What it prints
On 6 processes, with “-” for MPI_PROC_NULL:
rank 0 at (0,0): W 4 E 2 N - S 1 | from W 4, from N -1 | row sum 6
rank 1 at (0,1): W 5 E 3 N 0 S - | from W 5, from N 0 | row sum 9
rank 2 at (1,0): W 0 E 4 N - S 3 | from W 0, from N -1 | row sum 6
rank 3 at (1,1): W 1 E 5 N 2 S - | from W 1, from N 2 | row sum 9
rank 4 at (2,0): W 2 E 0 N - S 5 | from W 2, from N -1 | row sum 6
rank 5 at (2,1): W 3 E 1 N 4 S - | from W 3, from N 4 | row sum 9
Ranks 0 and 4 are neighbours across the periodic x boundary. The processes at the top of the grid keep their initial value -1, because they received nothing from MPI_PROC_NULL. Each row sums to 0 + 2 + 4 or 1 + 3 + 5.
Beyond regular grids
- For unstructured meshes,
MPI_Dist_graph_create_adjacent(MPI 2.2) describes an arbitrary neighbour list per process. It replaces the olderMPI_Graph_create, which required every process to know the whole graph. - Since MPI 3.0, neighbourhood collectives such as
MPI_Neighbor_alltoallexchange data with all topology neighbours in one call, and the library can optimise the exchange as a whole.
Summary
- Describe your decomposition once with
MPI_Dims_createandMPI_Cart_create, and take ranks from the new communicator. - Get neighbours from
MPI_Cart_shift, and letMPI_PROC_NULLhandle the edges. - Use
MPI_Sendrecvor non-blocking calls for the exchange, andMPI_Cart_subfor row and column operations.
Code: cart2d.c. Build it with mpicc -O2 -o cart2d cart2d.c, run it with mpirun -np 6 ./cart2d.