Computing Library › HPC & Compute
HPC & Compute

MPI Scatter and Gather

Scatter distributes distinct chunks of a root's array to each process; gather is its inverse, collecting each process's piece back to the root.

Two inverse operations

MPI_Scatter takes an array on the root, splits it into P equal pieces, and sends the i-th piece to rank i. MPI_Gather reverses this: each rank contributes a piece and the root assembles them into one array in rank order. Broadcast differs from scatter in that broadcast sends the same data to everyone, while scatter sends different slices.

Variable-size variants

Kronos motion — process heat

When pieces are unequal, the v variants MPI_Scatterv and MPI_Gatherv take arrays of counts and displacements so each rank can send or receive a different amount. These handle domain decompositions where subdomains are not identical in size, which is the common case for irregular meshes.

Allgather

MPI_Allgather is gather followed by broadcast: every rank ends up with every other rank's piece. It is the natural operation for exchanging boundary summaries or building a global list that all ranks need. Its bandwidth-optimal form is a ring, the same pattern used inside ring allreduce.

python
from mpi4py import MPI
import numpy as np
comm = MPI.COMM_WORLD
P = comm.Get_size()
if comm.Get_rank() == 0:
    data = np.arange(P*4, dtype='d').reshape(P, 4)
else:
    data = None
local = np.empty(4, dtype='d')
comm.Scatter(data, local, root=0)
# each rank now owns one row of the original array

In simulation

Distributing subdomains of a Hyperion breeder mesh across ranks at startup is a scatterv; collecting per-rank diagnostics into a single field for output is a gatherv. Assembling a global tritium-breeding profile that every rank must see for a normalization step is an allgather. Choosing the right variant avoids both redundant data motion and needless memory duplication.