MPI for Python
mpi4py provides Python bindings for the Message Passing Interface (MPI) standard, enabling parallel programming with Python. It allows Python applications to leverage high-performance distributed memory architectures. The current version is 4.1.1, and it typically sees a few minor and patch releases per year, with major feature updates (like MPI standard support) in new minor versions.
Warnings
- breaking Python 2.x support was dropped after mpi4py version 3.1.6. Versions 4.x and newer require Python 3.8+.
- breaking Version 4.0.0 introduced support for the MPI-4.0 standard, including new features like sessions, persistent collectives, and partitioned communication. While mostly backward compatible, existing code might need updates to leverage new functionalities or if it relied on specific MPI-3 behaviors that changed in MPI-4.
- gotcha mpi4py requires an underlying MPI implementation (e.g., Open MPI, MPICH, Intel MPI) to be installed on the system. It is a Python binding, not a standalone MPI library.
- gotcha MPI programs, including those written with mpi4py, must be executed using an MPI launcher like `mpirun` or `mpiexec`, not simply `python your_script.py`.
- gotcha When installing mpi4py from source (e.g., if pre-built wheels are not available for your system/MPI configuration), you typically need MPI development headers installed (e.g., `libopenmpi-dev` or `mpich-dev` on Debian/Ubuntu, `openmpi-devel` or `mpich-devel` on CentOS/Fedora).
- gotcha Pre-built `mpi4py` wheels available on PyPI are built against specific MPI Application Binary Interfaces (ABIs), typically MPICH or Open MPI. If your system's MPI implementation is ABI-incompatible with the wheel, you may encounter runtime errors and will need to build `mpi4py` from source against your specific MPI library.
Install
-
pip install mpi4py
Imports
- MPI
from mpi4py import MPI
Quickstart
from mpi4py import MPI
import os
# Get the default communicator (all processes)
comm = MPI.COMM_WORLD
# Get the rank of the current process
rank = comm.Get_rank()
# Get the total number of processes
size = comm.Get_size()
# Each process prints its rank and size
print(f"Hello from process {rank} of {size} on host {os.uname().nodename}")
# Use a barrier to ensure all processes reach this point before exiting
comm.Barrier()