Table of Contents
Reduction (MPI)
A reduction combines all elements of a collection into a single value using an associative operator. In sequential C, that is a loop:
double sum = 0.0; for (int i = 0; i < N; i++) sum += arr[i];
In a parallel program, each process holds a different slice of the data and computes a partial result. To get the global value, those partial results need to be combined. MPI_Reduce collects the per-process values and applies the operator, delivering the result only to a root rank. MPI_Allreduce does the same but gives every process the result, which is the right choice when the global value drives the next iteration (a convergence check, for example).
double local = compute(); double global; MPI_Allreduce(&local, &global, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
Built-in operators include MPI_SUM, MPI_PROD, MPI_MAX, MPI_MIN, MPI_LAND (logical and), MPI_LOR, MPI_BAND (bitwise and), MPI_BOR, MPI_MAXLOC, and MPI_MINLOC. MPI_MAXLOC and MPI_MINLOC return both the extreme value and the rank of the process that holds it, using paired datatypes like MPI_DOUBLE_INT. Custom operators can be defined with MPI_Op_create.
