Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Adding up everyone’s numbers
Imagine 8 people each holding a list of a million numbers, and everyone needs the total of all 8 lists. They could all send their lists to one person to add (that person is swamped), or pass pieces around a circle, each adding what they receive and passing it on. The circle method keeps everyone busy and nobody overloaded. Supercomputers and multi-GPU training do this constantly: it’s called all-reduce.
Large simulations (weather, CFD, orbital debris propagation) split their domain across thousands of processes that exchange boundary data and global sums every step, and training a model on many GPUs averages gradients with an all-reduce every iteration. When these programs scale badly, communication is usually why.
- 1. All-reduce gives every participant…
- 2. Why not send everything to one process?
- 3. In the demo, how much data does each rank send in total for large P?
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.MPI basics
from mpi4py import MPI # run with: mpirun -n 8 python script.py (or srun under Slurm)
import numpy as np
comm = MPI.COMM_WORLD
rank, size = comm.Get_rank(), comm.Get_size()
local = np.full(1_000_000, rank, dtype=np.float64) # this rank's share of the data
total = np.empty_like(local)
comm.Allreduce(local, total, op=MPI.SUM) # every rank gets the element-wise sum
if rank == 0:
comm.Send(total[:10], dest=1, tag=7) # point-to-point
elif rank == 1:
buf = np.empty(10); comm.Recv(buf, source=0, tag=7)Every process runs the same program (SPMD) and branches on its rank. Collectives must be called by all ranks of the communicator in the same order. In PyTorch, torch.distributed offers the same operations with backends nccl (GPUs) and gloo/mpi (CPUs).
| Collective | before | after |
|---|---|---|
| broadcast | root has x | all have x |
| reduce | each has xᵢ | root has Σxᵢ |
| all-reduce | each has xᵢ | all have Σxᵢ |
| all-gather | each has xᵢ | all have [x₀, …, x_(P−1)] |
| reduce-scatter | each has a vector | rank i has chunk i of the sum |
| all-to-all | each has P chunks | chunk j of rank i goes to rank j |
- 1. Which collective leaves every rank with the sum?
- 2. SPMD means…
- 3. If one rank skips a collective call, the others…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 40 minUndergraduate students and practising engineers.Algorithms
- Binomial tree broadcast/reduce: ⌈log₂P⌉ rounds; each round doubles the set of ranks that have the data. Good for small messages.
- Ring all-reduce: reduce-scatter in P − 1 steps (pass a chunk to the right neighbour, add) then all-gather in P − 1 steps; each rank sends 2(P − 1)/P of the vector, the minimum possible. Bandwidth-optimal, latency 2(P − 1) messages.
- Recursive doubling: log₂P rounds, exchanging the full vector with rank r XOR 2ᵏ. Few messages, but sends log₂P full vectors.
- Rabenseifner: recursive halving reduce-scatter plus recursive doubling all-gather: 2log₂P messages and the optimal byte count.
NCCL picks rings, trees or combinations (and pipelines chunks) based on topology and message size: NVLink within a node, InfiniBand or RoCE between nodes.
- 1. Rounds of a binomial broadcast over 1000 ranks?
- 2. Bytes each rank sends in a ring all-reduce of n bytes over P ranks?
- 3. Recursive doubling pairs rank r with…
Level 3: Advanced
★★★★☆ Advanced⏱ 30 minGraduate level, with full derivations and proofs.The alpha-beta model
A message of n bytes costs α + nβ (α: latency, β: inverse bandwidth). For all-reduce:
- ring: 2(P − 1)α + 2(P − 1)/P · nβ
- binomial reduce + broadcast: 2log₂P (α + nβ)
- recursive doubling: log₂P (α + nβ)
- Rabenseifner: 2log₂P α + 2(P − 1)/P · nβ
Small messages are latency-bound (fewest rounds win), large ones bandwidth-bound (fewest bytes win). NCCL reports bus bandwidth = (n/t)·2(P − 1)/P, which can be compared directly with the link speed.
Data-parallel training
Each GPU computes gradients on its own mini-batch; an all-reduce averages them before the optimizer step. PyTorch’s DistributedDataParallel groups gradients into buckets (25 MB by default) and starts each bucket’s all-reduce as soon as its gradients are ready during the backward pass, overlapping communication with computation. Too-small buckets pay latency on thousands of calls; one huge bucket can’t start until backward finishes.
- 1. For a 1 KB all-reduce on 64 ranks, which algorithm is fastest in the alpha-beta model?
- 2. DDP overlaps communication with…
- 3. Ring all-reduce time for large n tends to…
Level 4: Expert
★★★★★ Expert⏱ 35 minResearchers: original papers and open problems.Collectives, measured
Exercise 1. With 8 ranks and 1000-element float64 vectors: binomial broadcast from rank 3 and binomial reduce take 3 rounds; ring all-reduce takes 14 rounds and every rank sends 14 000 bytes (2·7/8 of 8000); recursive doubling takes 3 rounds but sends 24 000 bytes per rank; ring all-gather takes 7 rounds. The ring also works for 5 ranks with uneven chunks, and colour-based splitting builds node-local communicators.
Exercise 2: choosing algorithms and buckets. Alpha-beta model with α = 5 µs and 25 GB/s links (InfiniBand-class):
| All-reduce at P = 64 | 1 KB | 1 MB | 16 MB | 1 GB |
|---|---|---|---|---|
| ring | 630 µs | 0.71 ms | 1.95 ms | 85.2 ms |
| binomial tree | 61 µs | 0.56 ms | 8.1 ms | 515 ms |
| recursive doubling | 30 µs | 0.28 ms | 4.1 ms | 258 ms |
| Rabenseifner | 60 µs | 0.14 ms | 1.38 ms | 84.6 ms |
Recursive doubling wins below the crossover (1.1 MB at P = 8, 3.7 MB at P = 64); for large messages the ring and Rabenseifner both move the optimal 2(P − 1)/P·n bytes, reaching a bus bandwidth equal to the link speed (24.8 GB/s at 1 GB). The same pattern holds for P = 8 and 512.
DDP bucketing for a 2 GB gradient (1B parameters in FP16) on 64 GPUs with a 250 ms backward pass:
| bucket size | 100 KB | 1 MB | 5 MB | 25 MB | 100 MB | 500 MB | 2 GB |
|---|---|---|---|---|---|---|---|
| exposed communication | 12.5 s | 1.17 s | 160 ms | 2.6 ms | 8.5 ms | 40 ms | 158 ms |
Tiny buckets drown in per-message latency (each 100 KB all-reduce still costs 630 µs of ring latency); a single bucket can’t start until backward ends. The minimum sits at PyTorch’s default of 25 MB. (In the pure alpha-beta model Rabenseifner looks as good as the ring; NCCL prefers rings and trees because they pipeline well over real NVLink and network topologies.)
Collectives in AI, robotics and aerospace
- Distributed training: DDP and FSDP (all-reduce, reduce-scatter, all-gather of parameters and gradients), tensor parallelism (C6).
- Simulation: halo exchanges between subdomains in CFD and weather codes; global reductions for time-step control and convergence checks.
- Ensembles: Monte Carlo trajectory campaigns gather statistics with reduces at the end.
- Multi-robot systems: consensus and distributed estimation are all-reduce-like operations over unreliable networks.
References
- Message Passing Interface Forum (2021). MPI: A Message-Passing Interface Standard, Version 4.0.
- R. Thakur, R. Rabenseifner, W. Gropp (2005). “Optimization of collective communication operations in MPICH.” International Journal of High Performance Computing Applications, 19(1).
- P. Patarasuk, X. Yuan (2009). “Bandwidth optimal all-reduce algorithms for clusters of workstations.” Journal of Parallel and Distributed Computing, 69(2).
- S. Li et al. (2020). “PyTorch Distributed: experiences on accelerating data parallel training.” Proc. VLDB Endowment, 13(12).
- NVIDIA. NCCL documentation and nccl-tests: performance (bus bandwidth definitions), github.com/NVIDIA/nccl-tests.