maybempi
Install from PyPI with
pip install maybempi # serial runs onlypip install "maybempi[mpi]" # with mpi4py, for MPI runsThe same code runs under mpirun/srun and as a plain python script.py, and the serial run never starts MPI:
from maybempi import MPI # instead of: from mpi4py import MPI
# mpi4py.MPI under mpirun/mpiexec/srun, a serial stand-in otherwisecomm = MPI.COMM_WORLDtotal = comm.allreduce(local_total, op=MPI.SUM) # local_total in a serial runImporting mpi4py.MPI calls MPI_Init, which costs close to a second and makes every collective cost something, even on one process. maybempi decides from the environment variables that MPI launchers export, without importing mpi4py, and in a serial run returns a stand-in whose collectives give the results MPI gives on one process. It is pure Python with no dependencies.
You can find the documentation here.
Links: