Skip to main content

Does your MPI span the nodes?

The symptom of an MPI that is not wired up to the cluster, a job that runs but does not scale, and a short mpi4py program that checks whether all the ranks form one world across the nodes.

The introduction said that an MPI from conda, launched with srun, gives every process a world of its own. This page is about what that looks like, and how to check for it, because it does not look like an error.

The symptom

Say you need mpi4py, and do what you would on your own computer:

mamba env create -f environment.yml

with this environment.yml, which asks for mpi4py, and MPICH as the MPI under it, both from conda-forge:

name: mpi4py-plain
channels:
  - conda-forge
dependencies:
  - mpi4py
  - mpich

Then you ask for 2 nodes, 4 tasks on each, and launch your program with srun. Every one of the 8 processes starts, imports mpi4py, and initializes MPI. None of them can find the others, since conda’s MPI does not understand what Slurm tells it, so each one decides it is alone, and becomes rank 0 of a world of size 1.

Nothing about that is an error from MPI’s point of view. A program written to split its work between the ranks sees one rank, does all of the work, and writes its output, and so do the 7 others, at the same time, to the same files if it writes any. The job finishes in about the time it would take on one core, rather than 8 times faster, and if you didn’t look closely, you’d conclude that your program “doesn’t scale”. The allocation is spent, and the result may even be correct, 8 times over.

The check

So before you trust an MPI environment with a real run, check that the ranks form one world, with a program that does nothing else. Here is one in Python, with mpi4py, which also works as a check of the MPI under a compiled program, as long as mpi4py is built against the same MPI:

#!/usr/bin/env python3
"""Check that mpi4py forms a single MPI world spanning every rank and node."""

from collections import Counter
import socket
import sys

from mpi4py import MPI

comm = MPI.COMM_WORLD
rank = comm.Get_rank()
size = comm.Get_size()
host = socket.gethostname()

# Collective over every rank; the sum matches the world size only when
# all ranks (including those on other nodes) take part.
total = comm.allreduce(1, op=MPI.SUM)

# Gather each rank's host on rank 0 to show the rank-to-node mapping.
hosts = comm.gather(host, root=0)

if rank != 0:
    sys.exit(0)

ranks_per_host = Counter(hosts)
print(f"MPI library: {MPI.Get_library_version().splitlines()[0]}")
print(f"world size: {size}")
print(f"allreduce sum: {total} (expected {size})")
print(f"nodes in world: {len(ranks_per_host)}")
for name, count in sorted(ranks_per_host.items()):
    print(f"  {name}: {count} ranks")

if size > 1 and total == size:
    print("RESULT: PASS - one MPI world spans every rank")
    sys.exit(0)

print("RESULT: FAIL - ranks did not join a single MPI world")
sys.exit(1)

Every rank takes part in an allreduce, which adds up a 1 from each of them, so the sum is the number of ranks that actually communicated. Then rank 0 collects the name of the node each rank is on, prints a report, and says whether one world spans every rank.

Run it with a batch script set up exactly the way your real MPI job is, the same modules and the same environment, on at least 2 nodes, since a world on one node tells you nothing about the network between them:

#!/bin/bash
#SBATCH --job-name=mpi-hello
#SBATCH --nodes=2
#SBATCH --ntasks-per-node=4
#SBATCH --cpus-per-task=1
#SBATCH --time=00:05:00
set -eo pipefail

# Set up the environment the same way as your real MPI job does.
# Here, that is a conda environment with mpi4py in it.
source "$(conda info --base)/etc/profile.d/conda.sh"
conda activate "${CONDA_ENV:-mpi4py-plain}"

srun python mpi_hello.py

Reading the output

With the environment above, each of the 8 processes is rank 0 of its own world, so each of them prints the report, 8 times in all, every time with

world size: 1
allreduce sum: 1 (expected 1)
nodes in world: 1

and ends with RESULT: FAIL - ranks did not join a single MPI world, so the job fails too. That is the singleton world: the program ran, and no MPI communication happened at all.

With an MPI that is wired up properly, as on the next page, you get one report, from the one rank 0, and on a Cray EX system it looks like this:

MPI library: MPI VERSION    : CRAY MPICH version 8.1.30.9 (ANL base 3.4a2)
world size: 8
allreduce sum: 8 (expected 8)
nodes in world: 2
  x3003c0s13b4n0: 4 ranks
  x3003c0s15b1n0: 4 ranks
RESULT: PASS - one MPI world spans every rank

The first line tells you which MPI mpi4py is running on, here the vendor’s, Cray MPICH. The world has all 8 ranks, all 8 took part in the sum, and they are on 2 nodes, 4 on each, as the job asked for.

A pass means the ranks form one world across the nodes. It does not by itself prove that they talk over the fast network rather than a slower one: on the next page, one route passes this check without using it. For that, the first line, which MPI is running, is the thing to look at.

The next page is about how to get the second output rather than the first, with conda.