MPI for Python

4.1.1 · active · verified Sat Apr 11

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

Install

Imports

Quickstart

This 'Hello, World!' example demonstrates how to initialize MPI, get the rank of the current process, and the total number of processes in the communicator. Save it as `hello.py` and run with `mpirun -np 4 python hello.py` for 4 processes.

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()

view raw JSON →