Every rank holds a local array. Rank zero needs either all arrays in order, or their elementwise sum. Those are different mathematical objects. Gathering concatenates data; reducing combines corresponding values. Choosing the operation begins with specifying the desired result, before selecting an MPI routine.
Point-to-point messages describe individual transfers. A collective describes a pattern involving all members of a communicator. The library can choose an implementation suited to message size and hardware, while the application states the data movement or algebra it needs.
1. Read the result's shape first
Take three ranks with two values each:
Gather produces , a vector of length six. Sum-reduce produces , a vector of length two. The reduction is across ranks at each element, not a sum over each local array followed by an implicit second reduction.
All examples below are intracommunicator collectives with compatible types, valid significant buffers, count n>=0 and root in . Here every local block has the same length. Variable-count routines such as Gatherv extend this case but are outside the original material.
Now trace where each input value goes. Watch whether the root retains separate positions or combines corresponding entries.
Send the same three local pairs toward the root. What is the shape of the result after they arrive?
Play, pause, or scrub to inspect a moment.
Paused. Rank-labelled blocks occupy consecutive root positions. The completed result is (1,2,3,4,5,6).
2. Replicate a decision: broadcast
Often only a designated rank reads standard input reliably under the launcher. Have it read the configuration, then broadcast:
MPI_Bcast(a, n, MPI_FLOAT, root, MPI_COMM_WORLD);
The root supplies the values in its a; other ranks' a buffers receive them. Each rank calls the operation, including the root. In these simple examples counts and datatypes agree; the more general MPI requirement is compatible type signatures. The root's rank and communicator must agree everywhere.
Returning from this blocking call makes the local buffer safe for subsequent use. It does not make the call a general barrier proving that every other rank has returned.
A broadcast need not make the root perform every transfer. Once another rank has the value, it can help forward it. Count the propagation rounds here; the topology chapter will examine what those rounds cost.
Only informed ranks can forward the value. Compare a tree with a root sending to one rank at a time.
Play, pause, or scrub to inspect a moment.
Paused. Binomial tree: 1 ranks currently hold the value. The schedule finishes in 3 abstract rounds.
3. Combine contributions: reduce and allreduce
MPI_Reduce(a, sum, n, MPI_FLOAT, MPI_SUM, root, MPI_COMM_WORLD);
MPI_Allreduce(a, sum, n, MPI_FLOAT, MPI_SUM, MPI_COMM_WORLD);
Use these as alternative operations for the same inputs, not as a requirement to call both. Reduce makes the result available on root; its receive buffer is insignificant elsewhere. Allreduce gives every rank the combined result. It is equivalent in effect to reduce-then-broadcast, but need not use that algorithm.
Operations include MPI_SUM, MPI_PROD, MPI_MAX, MPI_MIN, logical operations, and location-returning MPI_MAXLOC/MPI_MINLOC with suitable datatypes. Reduction operators must obey the MPI algebraic contract. Mathematical addition is associative; floating-point addition is only approximately so, and different reduction trees can change rounding.
Ordinary input/output buffers must not overlap. MPI has an explicit in-place convention: for an intracommunicator MPI_Reduce, root can use MPI_IN_PLACE as its send buffer, with its contribution already in the receive buffer; in-place allreduce is also supported. Do not alias ordinary pointers and assume that is equivalent. See the MPI reduction reference.
A distributed scalar sum usually has two stages:
An OpenMP reduction can compute inside each process; MPI then combines processes. This algebra will return in hybrid programming.
4. Retain contributions: gather and allgather
MPI_Gather(a, n, MPI_FLOAT, b, n, MPI_FLOAT, root, MPI_COMM_WORLD);
MPI_Allgather(a, n, MPI_FLOAT, b, n, MPI_FLOAT, MPI_COMM_WORLD);
For gather, root allocates n*P elements in b; its block from rank occupies offsets through . The receive count is per sender, not total. The root contributes a block too. For allgather, every rank allocates the full output. Replication can cost substantial memory; use it only when every rank needs every block.
5. Assign blocks: scatter
MPI_Scatter(a, n, MPI_FLOAT, b, n, MPI_FLOAT, root, MPI_COMM_WORLD);
Root's a contains n*P elements. Rank receives the rank- block into local b. Root's send buffer is insignificant on non-root ranks and can be NULL there; every rank still needs a valid receive buffer for a positive count. An argument being insignificant is an MPI semantic fact, not permission to use an invalid significant buffer.
Scatter → local computation → gather is a useful initial decomposition. It can also bottleneck on the root's memory and traffic; a scalable implementation may construct or read each partition locally instead.
6. Transpose ownership: all-to-all
MPI_Alltoall(a, n, MPI_FLOAT, b, n, MPI_FLOAT, MPI_COMM_WORLD);
Every rank allocates n*P send and receive elements. Denote by the block on rank intended for rank . After the operation, rank holds blocks in source-rank order. The operation transposes the two rank indices, not the elements inside each block. This view explains its role in distributed transposes and FFT decompositions.
7. Collective order identifies the conversation
Collectives have no user tag. Participating ranks must issue matching collectives in the same order on a communicator, with compatible arguments. They must not assume a blocking collective synchronises unrelated subsequent communication as a barrier would.
Consider two broadcasts with identical types, counts and root. Root broadcasts a=5 then b=10; one receiver passes its local b first, then its local a. The calls can legally match by sequence, leaving that receiver with b=5,a=10: MPI cannot know which variable name you intended. If ranks instead disagree on roots or collective kinds, the program can be erroneous or deadlock. This distinction is more useful than labelling every swapped variable a library error.
Exercises and continuation
- Reproduce the gather and reduce outputs in the opening example. Then replace reduce with allreduce and state who owns the result.
- Broadcast
N, scatter an array, square each local block, and gather it. State the divisibility and allocation assumptions. - Let
a[s] = 100*rank + swith one element per destination. Predict every receive buffer after all-to-all. - Compare MPI sums against a serial reference with cancellation-prone inputs and a stated tolerance.
The communicator has so far been MPI_COMM_WORLD. Communicators and topologies lets us restrict a collective to a row, distinguish independent conversations and express geometric neighbours.