PX457 · Course contents

MPI: Communicators and Cartesian Topologies

A two-dimensional stencil is local in geometry: each subdomain needs four neighbouring faces. World-rank numbers contain no geometry by themselves. We need both a way to name neighbours and a way to keep a row's collective separate from another row's collective.

A communicator answers who participates and which communication context is used. A topology adds logical coordinates and adjacency. These extend the rank-local ownership model; they do not change its separate memory spaces.

1. A communicator is more than a list of ranks

An intracommunicator combines an ordered group with a context. Two communicators can contain the same processes yet have independent message matching. A library can therefore communicate internally without accidentally receiving the application's message with the same tag. MPI_Comm is an opaque handle: pass it to MPI, do not inspect its representation.

Split by colour, order by key

MPI_Comm_split(parent, colour, key, &child);

All members of parent participate. Equal colours form a group; increasing key determines rank order within that group, with parent rank breaking ties. MPI_UNDEFINED excludes a process and gives it MPI_COMM_NULL.

For P=N2P=N^2 ranks in an explicitly chosen row-major logical arrangement:

/* All ranks have checked that P is a positive perfect square. */
int N = (int)sqrt((double)P);
int row = world_rank / N;
MPI_Comm row_comm;
MPI_Comm_split(MPI_COMM_WORLD, row, world_rank, &row_comm);
/* Row-local rank is world_rank % N for this ordering. */
MPI_Comm_free(&row_comm);

Include math.h; test N*N==P safely rather than silently truncating a nonsquare process count. Rank zero in a row communicator need not be world rank zero. MPI_Comm_split_type with MPI_COMM_TYPE_SHARED instead forms groups able to create a shared-memory region, commonly node-local groups:

MPI_Comm_split_type(MPI_COMM_WORLD, MPI_COMM_TYPE_SHARED,
                    world_rank, MPI_INFO_NULL, &node_comm);

All world ranks call it. This supplies a useful hardware-local grouping without assuming that consecutive world ranks share a node.

2. Select an arbitrary ordered subset

Groups describe membership without creating a communication context:

MPI_Group world_group, column_group;
MPI_Comm column_comm;
int column[] = {1, 5, 9, 13};  /* requires at least 14 world ranks */
MPI_Comm_group(MPI_COMM_WORLD, &world_group);
MPI_Group_incl(world_group, 4, column, &column_group);
MPI_Comm_create(MPI_COMM_WORLD, column_group, &column_comm);

if (column_comm != MPI_COMM_NULL) {
    /* Collective operations here involve just the four selected ranks. */
    MPI_Comm_free(&column_comm);
}
MPI_Group_free(&column_group);
MPI_Group_free(&world_group);

MPI_Comm_create is collective over the parent communicator; all its members must call it with consistent group information for this example. Excluded ranks receive MPI_COMM_NULL and must not use that as a communicator. In the new ordering, world rank 1 is local rank 0; a broadcast rooted there uses root=0, not root=1. Free groups and communicators once no longer needed.

3. Attach the geometry to the rank space

For sixteen ranks, create a periodic four-by-four topology:

int dims[2] = {4, 4};
int periods[2] = {1, 1};
MPI_Comm cart;
MPI_Cart_create(MPI_COMM_WORLD, 2, dims, periods, 1, &cart);
int rank, coords[2];
MPI_Comm_rank(cart, &rank);
MPI_Cart_coords(cart, rank, 2, coords);

The final 1 permits reordering for placement; it does not guarantee that a particular physical mapping is used. If fewer processes are included than exist in the parent, excluded ranks get MPI_COMM_NULL; guard usage. The fixed example assumes exactly sixteen ranks.

MPI_Cart_rank(cart, coords, &rank) is the inverse query. Cartesian ranks use row-major coordinate ordering, but world ranks may differ after reordering. Always query ranks in the communicator actually used by the communication.

Neighbourhood is geometric, rank is contextual
0123456789101112131415
A 4×4 Cartesian communicator in its own rank order. Rank 9 has coordinates (2,1): neighbours 5 and 13 in dimension 0, 8 and 10 in dimension 1. These are Cartesian ranks; do not substitute world ranks when reordering was allowed. Periodicity connects opposite edges, omitted here for clarity. ↗

The Cartesian layout names positions, while a world rank names a process in a different communicator. Follow the same highlighted process through a possible reordering and a row-local naming scheme.

The process stays; its rank changes

Regroup sixteen physical process identities into rows. Follow the highlighted process through the change in naming.

Play, pause, or scrub to inspect a moment.

C0W0C1W1C2W2C3W3C4W4C5W5C6W6C7W7C8W8C9W9C10W10C11W11C12W12C13W13C14W14C15W15W9world 9 → Cartesian 14 → row rank 2

Paused. The highlighted process stays world identity 9. In the target communicator its Cartesian rank is 14 and its row-local rank is 2.

A fixed illustrative permutation assigns world identities to Cartesian ranks. Cartesian row-major rank and row-local rank are then queried conceptually. Permitting MPI reordering does not require this particular permutation. ↗

Query the neighbours, then exchange

int src, dst;
MPI_Cart_shift(cart, 0, 1, &src, &dst);
MPI_Sendrecv(send_face, n, MPI_DOUBLE, dst, 10,
             recv_face, n, MPI_DOUBLE, src, 10,
             cart, MPI_STATUS_IGNORE);

Direction zero and displacement one give the negative-direction source and positive-direction destination. Dimension one gives left/right neighbours in the pictured convention. A negative displacement reverses the roles. Nonperiodic boundaries return MPI_PROC_NULL: sends and receives with it complete as no-ops, and a receive leaves the buffer unchanged. You must therefore initialise physical-boundary ghost values; MPI does not supply the boundary condition.

In a periodic two-by-two grid, positive and negative neighbours along a dimension are the same rank. Keep face buffers and tags/directional exchange conventions distinct so two different halos are not confused.

Slice a grid into rows or columns

int keep_row[2] = {0, 1}; /* let column coordinate vary */
int keep_col[2] = {1, 0}; /* let row coordinate vary */
MPI_Cart_sub(cart, keep_row, &row_comm);
MPI_Cart_sub(cart, keep_col, &col_comm);

A row reduction then calls MPI_Allreduce on row_comm, not cart. All Cartesian processes participate in forming their subcommunicators, then each slice communicates independently. To reproduce the same groups with MPI_Comm_split, use Cartesian coordinates for colour/key; world-rank arithmetic is insufficient if ranks were reordered.

4. Surface-to-volume ratio predicts a scaling limit

A square local subdomain of side ℓ\ell contains ℓ2\ell^2 points. A width-one four-face halo transfers approximately 4ℓ4\ell values, ignoring corners. Hence

halo sizelocal work size≈4ℓ.\frac{\text{halo size}}{\text{local work size}}\approx\frac4\ell.

For a fixed global square with NN points along each side and a square process grid, ℓ=N/P\ell=N/\sqrt P. The ratio grows as 4P/N4\sqrt P/N. Adding processes shrinks useful local work faster than boundary traffic: one reason strong scaling eventually degrades. This is a geometric count, before latency and network contention are included.

5. A topology suggests algorithms, not one guaranteed implementation

Use a simple message model

Tmessage(m)=α+βm,T_{\mathrm{message}}(m)=\alpha+\beta m,

where mm is bytes, α\alpha startup latency and β\beta seconds per byte. It excludes congestion and overlap. A root-to-all loop or serial ring propagation needs P−1P-1 stages; a binomial tree can broadcast in ⌈log⁡2P⌉\lceil\log_2P\rceil stages, with different link loads.

For an allreduce of mm bytes per rank, a chunked ring has a reduce-scatter phase of P−1P-1 rounds and an allgather phase of P−1P-1 rounds. Ignoring local reduction cost,

Tring≈2(P−1)α+2P−1Pβm.T_{\mathrm{ring}}\approx2(P-1)\alpha+ 2\frac{P-1}{P}\beta m.

For P=2kP=2^k, recursive doubling exchanges partial results with partner rank ^ (1<<j) at stage jj. A full-buffer allreduce can take kk exchanges for scalars or arrays, approximately k(α+βm)k(\alpha+\beta m) plus local reduction work. A reduce-then-broadcast tree takes about 2k2k stages; the source's “arrays require 2k2k” refers to such a choice, not an intrinsic array requirement.

Fewer startup rounds favour small messages. Less total per-rank transferred data can favour bandwidth-oriented algorithms for large messages. Logical ring adjacency does not guarantee physical proximity. MPI libraries choose among implementations; measure the library's MPI_Allreduce before writing a custom replacement.

The formulas count different kinds of rounds. Follow the recursive-doubling partners, then compare the two phases of a chunked ring. The ring view tracks chunk traffic and phase, rather than pretending each chunk is a full rank-local sum.

Two routes to the same global sum

Keep eight ranks and their local contributions. Compare small full-buffer exchanges with a chunked ring schedule.

Play, pause, or scrub to inspect a moment.

1R02R13R24R35R46R57R68R7round 0 / 3partner = rank XOR bit

Paused. Recursive-doubling round 0 of 3. Rank-local partials cover 1 original contributions.

For P=8, full-buffer recursive doubling has three partner exchanges. Chunked ring allreduce has seven reduce-scatter plus seven allgather rounds. The ring drawing tracks phase/round, not complete per-chunk contents. ↗

Exercises and continuation

  1. For sixteen ranks, tabulate world rank, Cartesian rank, coordinates and row-local rank. Repeat with reordering permitted; do not assume the implementation must reorder.
  2. Verify rank 9's neighbours and periodic boundary neighbours via queries. Explain what MPI_PROC_NULL does not initialise.
  3. Compare row communicators formed with Cartesian splitting and coordinate-based Comm_split.
  4. Compare ring and recursive-doubling cost models as mm grows. Which omitted effects might reverse their prediction?

We can now identify the exact remote values needed by a stencil. Advanced MPI asks how to compute its interior while those values are being transferred.

References

MPI standard documents for communicator, group and topology semantics; point-to-point ordering for matching and buffering constraints.