Why collectives
Many communication patterns involve every process: distribute the input, add up partial results, collect the output. You could write them with sends and receives, but a collective call is shorter, harder to get wrong, and usually much faster. MPI libraries implement collectives with tree and pipeline algorithms tuned for the network, so a broadcast to 1,000 processes takes about ten steps instead of 999 sends from one rank.
Three rules apply to all collectives:
- Every process in the communicator must call it, with matching arguments (same root, same count and type). If one rank skips a collective, the others wait forever.
- Collectives are matched in order of the calls. There are no tags.
- They are not barriers (except
MPI_Barrieritself). Some ranks may leave a collective before others have entered it. Don’t use them to synchronise timing.
The basic set
| Call | Before | After |
|---|---|---|
MPI_Bcast |
root has a value | everyone has it |
MPI_Reduce |
everyone has a value | root has the sum, max, … |
MPI_Allreduce |
everyone has a value | everyone has the sum, max, … |
MPI_Scatter |
root has an array | each rank has one piece |
MPI_Gather |
each rank has one piece | root has the array |
MPI_Allgather |
each rank has one piece | everyone has the array |
MPI_Alltoall |
each rank has a piece for every rank | each rank has the pieces addressed to it |
MPI_Barrier |
everyone has arrived |
Reductions take an operation: MPI_SUM, MPI_PROD, MPI_MAX, MPI_MIN, logical and bitwise operations, and MPI_MAXLOC/MPI_MINLOC, which also return the rank or index where the extreme value is. You can define your own operations with MPI_Op_create.
Example: computing pi
The integral of 4/(1+x²) from 0 to 1 is π. The midpoint rule with n intervals turns it into a sum that is easy to divide among processes:
if (rank == 0) n = (argc > 1) ? atol(argv[1]) : 10000000;
MPI_Bcast(&n, 1, MPI_LONG, 0, MPI_COMM_WORLD); /* everyone now knows n */
double h = 1.0 / (double)n, sum = 0.0;
for (long i = rank + 1; i <= n; i += size) { /* cyclic distribution */
double x = h * ((double)i - 0.5);
sum += 4.0 / (1.0 + x * x);
}
double mypi = h * sum, pi = 0.0;
MPI_Reduce(&mypi, &pi, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
n = 100000000, processes = 1: pi = 3.1415926535904264, error = 6.33e-13, time = 0.127 s
n = 100000000, processes = 2: pi = 3.1415926535900223, error = 2.29e-13, time = 0.065 s
Two details are worth noticing. The result changes in the last digits with the number of processes, because floating-point addition is not associative and the partial sums are added in a different order. That is normal, but it surprises people who compare outputs bit for bit. And the loop distributes indices cyclically (i += size), which balances the work automatically, even when n is not a multiple of the number of processes.
Example: scatter, allreduce, gather
06_scatter_gather.c normalises a vector that starts on rank 0:
MPI_Scatter(full, PER_RANK, MPI_DOUBLE, part, PER_RANK, MPI_DOUBLE, 0, MPI_COMM_WORLD);
double local = 0.0, global;
for (int i = 0; i < PER_RANK; i++) local += part[i] * part[i];
MPI_Allreduce(&local, &global, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
double norm = sqrt(global);
for (int i = 0; i < PER_RANK; i++) part[i] /= norm;
MPI_Gather(part, PER_RANK, MPI_DOUBLE, full, PER_RANK, MPI_DOUBLE, 0, MPI_COMM_WORLD);
norm before = 38.678159, first values after: 0.0259 0.0517 0.0776, norm after = 1.000000
MPI_Allreduce is the workhorse of iterative solvers: dot products, norms and convergence checks all need a global value on every rank. It is equivalent to a reduce followed by a broadcast, but faster.
When the pieces have different sizes, use the v variants: MPI_Scatterv, MPI_Gatherv, MPI_Allgatherv and MPI_Alltoallv take an array of counts and an array of displacements instead of a single count.
In place and non-blocking
- Pass
MPI_IN_PLACEas the send buffer to reduce into the receive buffer directly, for exampleMPI_Allreduce(MPI_IN_PLACE, x, n, MPI_DOUBLE, MPI_SUM, comm). This saves a copy and some memory. - Since MPI 3.0 every collective has a non-blocking form (
MPI_Ibcast,MPI_Iallreduce, …) that returns a request. The pattern is the same as in chapter 2: start it, do independent work, thenMPI_Wait.
Performance notes
- A global reduction costs time that grows with the logarithm of the number of processes, plus the message size. In a solver that does several
MPI_Allreducecalls per iteration on many thousands of cores, they often dominate. Combine reductions where you can: oneMPI_Allreduceon an array of three values is cheaper than three calls on one value each. MPI_Barrieris rarely needed for correctness. Remove it from production code unless you can explain why it is there. It is useful when timing a section of code, though.
Exercises
- Replace
MPI_Reducein the pi program withMPI_Allreduce, and let every rank print the result. What changes? - Change the loop to a block distribution, where each rank takes one contiguous range of indices. Handle the case where n is not divisible by the number of processes.
- Use
MPI_MAXLOCto find the largest element of a distributed array and the rank that owns it.