The idea: split the grid, and keep a copy of the border

Many simulations work on a grid. Each step sets every cell from its neighbours, like the temperature in a metal plate. A big grid does not fit, or runs too slowly, on one machine. So it is cut into pieces, and each MPI process (a rank) owns one piece. This is domain decomposition. A rank updates only its own cells. But a cell on the edge of a piece needs a neighbour that lives on another rank. So each rank keeps a one-cell border around its piece, the halo, made of ghost cells: copies of the neighbours' edge cells. Before each step the ranks refresh these copies by sending messages. That is the halo exchange.

Left: an 8 by 8 plate with a hot top edge, cut into 4 horizontal strips of 2 rows, owned by ranks 0 to 3. Right: rank 1's own array: a ghost row on top that copies row 2 from rank 0, its own rows 3 and 4, and a ghost row below that copies row 5 from rank 2. Arrows show the copies coming from the neighbours.
Rank 1 owns rows 3 and 4 but stores 4 rows: the two extra ones are ghost copies of rank 0's row 2 and rank 2's row 5.

The page uses a small plate of 8 × 8 cells. The top edge is held at 100 °C, the other three edges at 0 °C, and the inside starts cold. We want the steady temperature, where every cell equals the average of its 4 neighbours. The heat equation page shows how such a stencil comes from the physics; here the point is how 4 ranks share the work. On the canvas, own cells are coloured by temperature, ghost cells are blue-grey and the fixed edge cells are grey.

One iteration: exchange, sweep, agree

The Jacobi method repeats one simple update: new value = (north + south + west + east) / 4, using only the old values. On 4 ranks one iteration has three parts (Demo 1):

  1. Halo exchange. Each rank calls MPI_Sendrecv(top row → rank above, bottom ghost row ← rank below), then the same the other way. Rank 0 has no rank above. It passes MPI_PROC_NULL as the partner, and that half of the call does nothing. So the same code runs on every rank, with no special case.
  2. Sweep. Each rank updates its 16 own cells. Cells next to the piece's border read the ghost cells. In iteration 1 the row-1 cells become (100 + 0 + 0 + 0) / 4 = 25. Every other cell stays 0.
  3. Agree on the residual. Each rank knows only its own largest change. MPI_Allreduce(&diff, &res, 1, MPI_DOUBLE, MPI_MAX, comm) gives every rank the largest change over the whole plate, here 25. Every rank then makes the same choice to go on or stop. No rank can return from this call before all of them have called it, so it also acts as a barrier. A slow rank holds everyone up.

Heat moves only one row per iteration, and it crosses into another rank only through a ghost row (Demo 2). After 2 iterations rank 1 is still all 0. In iteration 3 its top ghost row brings in rank 0's row 2 (6.25), and rank 1's row 3 becomes 6.25 / 4 ≈ 1.6. Run to the end (Demo 3): the residual drops below 0.01 after 89 iterations, and the centre cell (row 4, column 4) is 29.407 °C. The counter under the code compares the result with one process that solves the whole grid. The difference is exactly 0. The ranks do the same additions in the same order, so splitting the grid changes who computes a cell, not what is computed.

Forget the exchange: it converges, to the wrong answer

Drop the two MPI_Sendrecv lines and nothing crashes (Demo 4). The ghost rows keep their first values, all 0. Rank 0 sees a plate only 2 rows deep with a cold floor, and it settles quickly. Ranks 1–3 never see any heat. The residual falls below 0.01 after only 24 iterations, so the program reports success. Yet rows 3–8 are 0 when they should be warm. After the same 24 iterations, one process has 33.5 °C at row 3, column 4, and 41.6 °C once it has converged. A residual tells you the iteration has stopped moving. It does not tell you the answer is right. Compare against a serial run on a small case, as the page does.

Every rank sends first: deadlock

An easy way to write the exchange is MPI_Send(top row → up); MPI_Send(bottom row → down); MPI_Recv(…); MPI_Recv(…). If sends are synchronous, as with MPI_Ssend or with real rows far above the eager limit, a send returns only when the partner has posted a receive. Every rank is still in its first send, so no receive is ever posted, and all 4 ranks wait for ever (Demo 5). MPI_Sendrecv posts the receive and the send together, so it cannot get stuck like this. MPI_Isend + MPI_Irecv + MPI_Waitall works too. Why small messages often hide this bug (eager sends) is shown on MPI: Send, Receive and Collectives.

A milder version of the bug is easy to miss. If every rank sends down first and then receives from above, nothing hangs. Rank 3 has nobody below, so it receives first. That lets rank 2's send finish, then rank 1's. The exchange works, but it runs one rank after another, P − 1 steps instead of one.

Strips or blocks: area against perimeter

The work of a rank grows with the area of its piece. The halo grows with its perimeter. Cutting the plate into 2 × 2 blocks instead of 4 strips gives the same numbers, digit for digit, and the same 89 iterations (Demo 6). It sends fewer values, but more messages:

Two 8 by 8 plates. Left, cut into 4 strips of 2 rows: 3 inner boundaries, each 8 cells long and crossed both ways, 6 messages and 48 values per iteration. Right, cut into 2 by 2 blocks of 4 by 4: 2 boundary lines inside, each pair of blocks swaps 4 values, 8 messages and 32 values per iteration.
Same 16 cells per rank: strips send 48 values in 6 messages per iteration, blocks 32 values in 8 messages.
per iteration, 8 × 8 on 4 ranks1D strips2D blocks
messages68
halo values sent4832
most values sent by one rank168
cell updates (per rank)64 (16)64 (16)

On a tiny plate this hardly matters. On a real one it decides how far the program scales. Take an N × N grid with N = 1024. A strip is N cells wide, so an inner rank sends 2N = 2048 values per iteration however many ranks there are. A block is N/√P on a side, so it sends 4N/√P. Meanwhile each rank's work, N²/P cell updates, shrinks as P grows:

P ranksupdates per rankstrips: values sent (ratio)blocks: values sent (ratio)
1665 5362048 (3.1 %)1024 (1.6 %)
6416 3842048 (12.5 %)512 (3.1 %)
25640962048 (50 %)256 (6.3 %)

With strips, the communication soon costs as much as the work. With blocks it shrinks too, only more slowly. Blocks do pay with 4 messages per rank instead of 2, and each message costs a fixed latency. So for small pieces, where latency matters most, strips can still win. How a fixed communication cost limits the speedup is the subject of Amdahl's and Gustafson's laws. On a 2D mesh or torus network each block's neighbours are one hop away; see Interconnection Networks.

Red-black Gauss–Seidel: fewer iterations, two exchanges

Gauss–Seidel uses a new value as soon as it exists, in place, instead of waiting for the next iteration. Done row by row, each cell would wait for the one before it, and the ranks would run one after another. The parallel form colours the cells like a chessboard. A cell is red when i + j is even and black when it is odd. The 4 neighbours of a red cell are all black. So all red cells can be updated at once, and then all black cells from the new red values. Each colour needs fresh ghost cells, so an iteration has two halo exchanges: 12 messages and 96 values on strips. In exchange it converges in 51 iterations instead of 89 (Demo 7), with the centre cell at 29.487 °C. Jacobi stopped at 29.407, a little short of the steady value; stopping at a residual of 0.01 leaves a small error either way.

See also Pregel, the same compute, exchange, barrier rhythm on a graph instead of a grid.

What the page leaves out

Real codes hide the exchange behind work: post MPI_Irecv and MPI_Isend, update the inner cells that need no ghost values, then wait and do the border cells. MPI can also describe the rank grid for you, with MPI_Cart_create and MPI_Cart_shift (which hands back MPI_PROC_NULL at the edges), and send a column with a strided MPI_Type_vector. Not shown: 3D grids, wider stencils that need halos 2 cells deep, corner exchanges for 9-point stencils, periodic edges, checking convergence only every few iterations to save Allreduce calls, and faster solvers (SOR, conjugate gradient, multigrid). Simplifications: every message takes the same time whatever its size, the 8 × 8 plate is far too small to be worth splitting, and the deadlock is drawn as if every send were synchronous.

On the mind map

Open the Computer Architecture mind mapComputerArchitectureAbstraction and numbers: layers and bit patternsAbstraction and numbersCombinational logic: output depends only on inputsCombinational logicSequential logic: state held by a clockSequential logicMIPS software: how C becomes machine wordsMIPS softwareThe processor: datapath plus controlThe processorPipelines and parallelism: more instructions at oncePipelines and parallelismMemory and I/O: small fast near, big slow farMemory and I/OMIPS pipelineMIPS pipelineforward data, stall on load-useBranch predictionBranch prediction2-bit counters guess, flush if wrongOut-of-order executionOut-of-order executionrenaming removes WAR and WAW hazardsSIMD, threads, multicoreSIMD, threads, multicoreAmdahl's law caps the speedupAmdahl vs GustafsonAmdahl vs Gustafsonfixed job caps at 1/s; scaled growsMPI Send/Recv & CollectivesMPI Send/Recv & CollectivesTrees: log₂ p rounds; Send may blockData-Parallel TrainingData-Parallel TrainingRing all-reduce keeps every copy equalJacobi halo exchangeGhost rows refreshed before every sweepInterconnect topologiesInterconnect topologiesHypercube: hops = bits that differParallel Prefix SumParallel Prefix SumBlelloch: 2(n−1) work, 2 log n spanOpenMP loop schedulesOpenMP loop schedulesUneven costs: dynamic beats static blocksGPU and SIMT warpsGPU and SIMT warps32 lanes, one instruction; branches splitOpen the Computer Architecture mind mapComputer Architecturemore instructions at oncePipelines and parallelismMIPS pipelineMIPS pipelineforward data, stall on load-useBranch predictionBranch prediction2-bit counters guess, flush if wrongOut-of-order executionOut-of-order executionrenaming removes WAR and WAW hazardsSIMD, threads, multicoreSIMD, threads, multicoreAmdahl's law caps the speedupAmdahl vs GustafsonAmdahl vs Gustafsonfixed job caps at 1/s; scaled growsMPI Send/Recv & CollectivesMPI Send/Recv & CollectivesTrees: log₂ p rounds; Send may blockData-Parallel TrainingData-Parallel TrainingRing all-reduce keeps every copy equalJacobi halo exchangeGhost rows refreshed before every sweepInterconnect topologiesInterconnect topologiesHypercube: hops = bits that differParallel Prefix SumParallel Prefix SumBlelloch: 2(n−1) work, 2 log n spanOpenMP loop schedulesOpenMP loop schedulesUneven costs: dynamic beats static blocksGPU and SIMT warpsGPU and SIMT warps32 lanes, one instruction; branches split
This page on the Computer Architecture mind map · 76 pages in 7 branches · open it to see the whole map and test yourself in recall mode.