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.
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):
- 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 passesMPI_PROC_NULLas the partner, and that half of the call does nothing. So the same code runs on every rank, with no special case. - 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.
- 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:
| per iteration, 8 × 8 on 4 ranks | 1D strips | 2D blocks |
|---|---|---|
| messages | 6 | 8 |
| halo values sent | 48 | 32 |
| most values sent by one rank | 16 | 8 |
| 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 ranks | updates per rank | strips: values sent (ratio) | blocks: values sent (ratio) |
|---|---|---|---|
| 16 | 65 536 | 2048 (3.1 %) | 1024 (1.6 %) |
| 64 | 16 384 | 2048 (12.5 %) | 512 (3.1 %) |
| 256 | 4096 | 2048 (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.