Foundations to Frontier
Tests
C. Programming, Tools and HPC › C5 High-Performance Computing

MPI and NCCL Collectives

When a computation spans many processes or GPUs, they must exchange data: MPI is the standard for message passing in scientific computing, and NCCL provides the same collective operations on NVIDIA GPUs over NVLink and InfiniBand for deep learning. This topic covers the SPMD model (ranks, communicators), point-to-point messages, the collectives (broadcast, reduce, all-reduce, all-gather, reduce-scatter, all-to-all), the algorithms behind them (binomial trees, rings, recursive doubling and halving), the alpha-beta cost model for choosing among them, and how data-parallel training overlaps gradient all-reduce with the backward pass using buckets. In the exercises you simulate message passing between ranks to implement and verify tree broadcast and reduce, ring and recursive-doubling all-reduce and ring all-gather, counting rounds and bytes; then you use the alpha-beta model to find which algorithm wins at which message size and how DDP's bucket size trades latency against overlap.

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.

Ring all-reduce, step by step
start: each rank holds only its own gradient
rank 0rank 1rank 2rank 3
Each small bar is one chunk; darker = more ranks' contributions summed in; blue = complete. Orange arrows: the chunk each rank sends this step.
data sent per rank so far: 0.00 × vector size
at the end: 2(P − 1)/P = 1.50× vector, in 6 steps
The bytes each rank sends approach twice the vector size no matter how many ranks join, so the ring is bandwidth-optimal for large messages; its 2(P − 1) sequential steps make it latency-heavy for small ones, where trees and recursive doubling win.
Why engineers care

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.

📝 Quick check (Level 0)
0/3 answered
  1. 1. All-reduce gives every participant…
  2. 2. Why not send everything to one process?
  3. 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
📝 Quick check (Level 1)
0/3 answered
  1. 1. Which collective leaves every rank with the sum?
  2. 2. SPMD means…
  3. 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.

🐍 Exercise 1: collectives by message passing
Simulate P ranks exchanging messages in synchronous rounds (counting rounds and bytes sent per rank). Implement binomial-tree broadcast from any root and binomial reduce, ring all-reduce (reduce-scatter then all-gather, any P, uneven chunks), recursive-doubling all-reduce, ring all-gather, and MPI_Comm_split by colour; verify results against NumPy sums.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Rounds of a binomial broadcast over 1000 ranks?
  2. 2. Bytes each rank sends in a ring all-reduce of n bytes over P ranks?
  3. 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.

📝 Quick check (Level 3)
0/3 answered
  1. 1. For a 1 KB all-reduce on 64 ranks, which algorithm is fastest in the alpha-beta model?
  2. 2. DDP overlaps communication with…
  3. 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.)

🐍 Exercise 2 (research): algorithms and buckets under the alpha-beta model
Implement alpha-beta cost formulas for ring, binomial tree, recursive doubling and Rabenseifner all-reduce; find the best algorithm per message size for P = 8, 64, 512 and the ring/recursive-doubling crossover; compute bus bandwidth; simulate DDP's bucketed all-reduce overlapping a 250 ms backward pass for a 2 GB gradient and find the bucket size with the least exposed communication.

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

  1. Message Passing Interface Forum (2021). MPI: A Message-Passing Interface Standard, Version 4.0.
  2. R. Thakur, R. Rabenseifner, W. Gropp (2005). “Optimization of collective communication operations in MPICH.” International Journal of High Performance Computing Applications, 19(1).
  3. P. Patarasuk, X. Yuan (2009). “Bandwidth optimal all-reduce algorithms for clusters of workstations.” Journal of Parallel and Distributed Computing, 69(2).
  4. S. Li et al. (2020). “PyTorch Distributed: experiences on accelerating data parallel training.” Proc. VLDB Endowment, 13(12).
  5. NVIDIA. NCCL documentation and nccl-tests: performance (bus bandwidth definitions), github.com/NVIDIA/nccl-tests.