Metadata-Version: 2.4
Name: maybempi
Version: 0.1.2
Summary: Use MPI only when launched under MPI, and a serial stand-in for mpi4py otherwise.
Author: Max
License: MIT License
        
        Copyright (c) 2026 Max Lindqvist
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Changelog, https://github.com/max-models/maybempi/blob/main/CHANGELOG.md
Project-URL: Documentation, https://max-models.github.io/maybempi/
Project-URL: Source, https://github.com/max-models/maybempi
Keywords: hpc,mpi,mpi4py,parallel,serial,slurm
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Scientific/Engineering
Classifier: Topic :: System :: Distributed Computing
Classifier: Typing :: Typed
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE.txt
Provides-Extra: dev
Requires-Dist: jupyterlab; extra == "dev"
Requires-Dist: maybempi[docs,test]; extra == "dev"
Requires-Dist: pre-commit; extra == "dev"
Requires-Dist: pyproject-fmt; extra == "dev"
Requires-Dist: pyright; extra == "dev"
Requires-Dist: ruff; extra == "dev"
Provides-Extra: docs
Requires-Dist: griffe; extra == "docs"
Requires-Dist: ipykernel; extra == "docs"
Requires-Dist: nbclient>=0.10; extra == "docs"
Requires-Dist: nbformat>=5.10; extra == "docs"
Requires-Dist: numpy; extra == "docs"
Provides-Extra: mpi
Requires-Dist: mpi4py; extra == "mpi"
Provides-Extra: test
Requires-Dist: numpy; extra == "test"
Requires-Dist: pytest; extra == "test"
Requires-Dist: pytest-cov; extra == "test"
Dynamic: license-file

# maybempi


<!-- README.md is generated from README.qmd: edit the .qmd and run `make readme`. -->

[![Tests](https://github.com/max-models/maybempi/actions/workflows/test_pytest.yml/badge.svg)](https://github.com/max-models/maybempi/actions/workflows/test_pytest.yml)
[![Static
analysis](https://github.com/max-models/maybempi/actions/workflows/static_analysis.yml/badge.svg)](https://github.com/max-models/maybempi/actions/workflows/static_analysis.yml)
[![Docs](https://github.com/max-models/maybempi/actions/workflows/docs.yml/badge.svg)](https://max-models.github.io/maybempi/)
[![codecov](https://codecov.io/gh/max-models/maybempi/branch/main/graph/badge.svg)](https://codecov.io/gh/max-models/maybempi)
[![PyPI](https://img.shields.io/pypi/v/maybempi.png)](https://pypi.org/project/maybempi/)
[![Python](https://img.shields.io/pypi/pyversions/maybempi.png)](https://pypi.org/project/maybempi/)

Use MPI only when the process was launched under MPI, and a serial
stand-in for `mpi4py` otherwise. The same code runs under
`mpirun`/`srun` and as a plain `python script.py`, and the serial run
never starts MPI.

Documentation: <https://max-models.github.io/maybempi/>

``` python
from maybempi import MPI  # instead of: from mpi4py import MPI

# mpi4py.MPI under mpirun/mpiexec/srun, a serial stand-in otherwise
comm = MPI.COMM_WORLD
total = comm.allreduce(local_total, op=MPI.SUM)  # local_total in a serial run
```

## Why

Importing `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 (Open
MPI, MPICH, Intel MPI, PMIx/`srun`, MVAPICH2, Cray), without importing
mpi4py. In a serial run it returns a stand-in with the module constants
and communicator methods of mpi4py, which give the results MPI gives on
one process: `allreduce(x)` is `x`, `gather(x)` is `[x]`,
`Allreduce(send, recv)` copies. Methods it does not implement raise
`AttributeError` instead of silently doing nothing.

- `MAYBEMPI=0`/`1` overrides the decision.
- `maybempi.is_serial(MPI)` tells the stand-in from mpi4py.
- `maybempi.local_rank()` gives the node-local rank before `MPI_Init`,
  e.g. to pick a GPU.
- The `maybempi` command prints what it decides, and why:
  `mpirun -n 2 maybempi --init`.

maybempi is pure Python and has no dependencies.

## Install

``` bash
pip install maybempi            # serial runs only
pip install "maybempi[mpi]"     # with mpi4py, for MPI runs
```

## Development

With [uv](https://docs.astral.sh/uv/):

``` bash
make install    # uv sync --extra dev, plus the pre-commit hooks
make lint       # ruff check, ruff format --check, pyright
make test       # pytest with coverage
mpiexec -n 2 .venv/bin/python -m pytest src/maybempi/tests/mpi   # tests under a real launcher
```

Commit messages follow [Conventional
Commits](https://www.conventionalcommits.org/); see
[CONTRIBUTING.md](CONTRIBUTING.md).

The documentation in `docs/` is an [Astro](https://astro.build/) +
[Starlight](https://starlight.astro.build/) site with the executed
notebooks from `tutorials/` and an API reference generated from the
docstrings. It needs Node 22 or newer:

``` bash
make docs-install     # npm packages and the Python docs extra
make docs-notebooks   # execute tutorials/*.ipynb and convert them to pages
make docs-dev         # live preview at http://localhost:4321/maybempi/
```

`README.md` is rendered from `README.qmd` with
[Quarto](https://quarto.org/): `make readme`.

## Releases

Before merging a release to `main`, update the version in
`pyproject.toml`, `src/maybempi/__init__.py` and `CITATION.cff`
(including its release date), and add the release notes to
`CHANGELOG.md`. The push to `main` creates a GitHub release with a
`vX.Y.Z` tag and publishes the package to PyPI with trusted publishing
(OIDC). See the [publishing
guide](https://max-models.github.io/maybempi/development/publishing/).
