MPI installation and first examples: Open MPI, MPICH and mpi4py
Updated · Scientifically reviewed by Dinkar Mishra, PhD on
Short answer
On Ubuntu, install an MPI implementation with sudo apt install openmpi-bin libopenmpi-dev (or mpich libmpich-dev). Compile C programs with mpicc and run them with mpirun -np 4 ./program. For Python, install mpi4py built against the same MPI and run mpirun -np 4 python script.py. On clusters, use the site's MPI modules and launch with srun instead of installing your own.
Who this is for
Students and researchers starting with parallel programming, and anyone who needs MPI working on a workstation or cluster.
Software
Open MPI 4.1 / 5.0MPICH 4.xmpi4py ≥ 3.1
Prerequisites
- Linux (or WSL2) with a C compiler (
sudo apt install build-essential). - For Python examples: Python 3 and pip, ideally in a virtual environment.
- On a cluster: find the provided MPI with
module avail mpiinstead of installing your own.
Step 1: install MPI
sudo apt install openmpi-bin libopenmpi-dev # Open MPI
# or
sudo apt install mpich libmpich-dev # MPICH (install only one of them)
mpicc --version && mpirun --version
Step 2: hello world in C
#include <mpi.h>
#include <stdio.h>
int main(int argc, char **argv) {
MPI_Init(&argc, &argv);
int rank, size, len;
char host[MPI_MAX_PROCESSOR_NAME];
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Get_processor_name(host, &len);
printf("Hello from rank %d of %d on %s\n", rank, size, host);
MPI_Finalize();
return 0;
}
mpicc -O2 hello.c -o hello
mpirun -np 4 ./hello
Each of the four processes prints its rank. The order is not deterministic.
Step 3: a parallel reduction
Each rank sums part of a series; MPI_Reduce combines the partial sums on rank 0. This example estimates π with the midpoint rule for :
#include <mpi.h>
#include <stdio.h>
int main(int argc, char **argv) {
MPI_Init(&argc, &argv);
int rank, size;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
const long n = 100000000;
const double h = 1.0 / n;
double local = 0.0;
for (long i = rank; i < n; i += size) {
double x = h * (i + 0.5);
local += 4.0 / (1.0 + x * x);
}
double pi = 0.0;
MPI_Reduce(&local, &pi, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
if (rank == 0) printf("pi ~ %.12f\n", pi * h);
MPI_Finalize();
return 0;
}
Step 4: the same in Python with mpi4py
python -m pip install mpi4py # builds against the MPI found via mpicc
from mpi4py import MPI
comm = MPI.COMM_WORLD
rank, size = comm.Get_rank(), comm.Get_size()
n = 10_000_000
h = 1.0 / n
local = sum(4.0 / (1.0 + (h * (i + 0.5)) ** 2) for i in range(rank, n, size))
pi = comm.reduce(local, op=MPI.SUM, root=0)
if rank == 0:
print(f"pi ~ {pi * h:.10f} using {size} processes")
mpirun -np 4 python pi_mpi.py
Key MPI concepts in one table
| Concept | Meaning | Example call |
|---|---|---|
| Communicator | A group of processes that can talk to each other | MPI_COMM_WORLD (all processes) |
| Rank | A process’s ID within a communicator, from 0 to size−1 | MPI_Comm_rank |
| Point-to-point | One process sends, one receives | MPI_Send / MPI_Recv, MPI_Isend / MPI_Irecv |
| Collective | All processes in a communicator take part | MPI_Bcast, MPI_Reduce, MPI_Allreduce, MPI_Barrier |
| Domain decomposition | Each rank owns part of the grid and exchanges boundary (“halo”) data with neighbours | Typical of PIC, CFD and other grid-based codes |
Most simulation codes need only a handful of these calls. The hard part is the decomposition and keeping communication small compared with computation.
Step 5: MPI under Slurm
#!/bin/bash
#SBATCH --nodes=2
#SBATCH --ntasks-per-node=32
#SBATCH --time=00:10:00
set -euo pipefail
module load openmpi
srun ./pi
srun starts one process per task across both nodes. See how to submit a Slurm job.
Verify
mpirun -np 4 ./helloprints four distinct ranks (0–3) with size 4.- The π example prints 3.14159265358… and the result does not depend on the number of processes, apart from the last digits (floating-point summation order).
- Timing the π program with 1, 2 and 4 processes (
time mpirun -np N ./pi) shows the runtime falling. That is a first look at strong scaling.
Troubleshooting
mpicc: command not found: install the development package (libopenmpi-dev), or load the MPI module on a cluster.- mpi4py import errors or crashes: mpi4py was built against a different MPI from the one launching the job. Reinstall it with
python -m pip install --no-cache-dir --force-reinstall mpi4pyafter loading the right MPI. - Programs hang at start-up across nodes: firewall or network interface problems. See MPI job hangs troubleshooting.
Assumptions and limitations
- Package names are for Ubuntu and Debian. Other distributions and clusters use modules or Spack.
- The examples demonstrate the API, not performance. Real speedup depends on the communication-to-computation ratio and the network.
- Building mpi4py from source requires the MPI development headers. Conda users can install
mpi4pyand an MPI together from conda-forge instead.
Frequently asked questions
Should I use Open MPI or MPICH?+
Both implement the MPI standard and are fine for learning and most applications. On a cluster, use the MPI the centre provides (often Open MPI, Intel MPI or a vendor MPI built on MPICH) because it is configured for the interconnect and the scheduler. Never mix implementations between compiling and running.
Why does mpirun complain when I run as root?+
Open MPI refuses to run as root by default, to protect the system. Run as a normal user. In containers where you must run as root, Open MPI provides an explicit --allow-run-as-root flag.
Why do I get "not enough slots available"?+
You asked for more processes than Open MPI detects cores. Use fewer processes, list hosts and slots in a hostfile, or add --oversubscribe for testing only (oversubscription slows real runs).
What is the difference between MPI and OpenMP?+
MPI runs separate processes with separate memory that communicate by messages; it scales across nodes. OpenMP runs threads sharing one process's memory on a single node. Large simulation codes often combine both (hybrid MPI+OpenMP).
References
- MPI Standard (MPI Forum) · MPI Forum
- Open MPI documentation · Open MPI project
- MPICH documentation · MPICH project
- mpi4py documentation · mpi4py project
Want this set up and verified for you?
We install, configure and validate scientific software on your workstation, cluster or cloud, and train your team to run it.
Services listed on this page are available today. Products marked “early access” or “in development” on our platform page are not yet generally available.
Spotted an error or an outdated command? Tell us. We correct verified issues promptly.