Skip to main content

NuMPI

NuMPI is a collection of numerical tools for MPI-parallelized Python codes. NuMPI presently contains:

  • An (incomplete) stub implementation of the mpi4py interface to the MPI libraries. This allows running serial versions of MPI parallel code without having mpi4py (and hence a full MPI stack) installed.
  • Parallel file IO in numpy's .npy format using MPI I/O.
  • MPI-parallel L-BFGS optimizers:
    • l_bfgs — unconstrained, with a strong-Wolfe line search.
    • l_bfgs_bounded — box-constrained (lo <= x <= hi) with optional index pinning, two-loop recursion and projected Armijo backtracking.
    • l_bfgs_projected — a single linear equality <a, x> = target plus optional box bounds.
  • An MPI-parallel bound constrained conjugate gradients algorithm.
  • FFT engines with the interface of muGrid's FFTEngine: a serial engine based on numpy.fft, and muGrid's MPI-parallel engine for domain-decomposed grids.

Build status

Tests Flake8

Installation

python3 -m pip install NuMPI

Development Installation

Clone the repository.

To use the code, install the current package as editable:

pip install -e .[test]

Testing

You have to do a development installation to be able to run the tests.

From the main installation directory, run the test suite with pytest:

python -m pytest

This runs serially and exercises the MPI stub implementation, so it works without mpi4py installed. To run a single test file or test:

python -m pytest test/Optimization/test_cg.py
python -m pytest test/Optimization/test_cg.py::test_directions -s

To run the suite under real MPI at a given number of ranks (this is what CI does, for 1, 2, 4, 8 and 10 ranks):

mpirun --oversubscribe -n 4 python -m pytest --verbose

MPI Conventions

All of NuMPI's parallel algorithms operate on distributed arrays: each MPI rank holds a slice of the global data, and scalar quantities (energies, norms, convergence tolerances, Lagrange multipliers) are globally reduced — the same value on every rank. Understanding the split between local and global is essential to using the optimizers correctly; this section spells it out.

Distributed vs. global

Quantity Lives where
Iterate x, gradient grad, initial guess x0 local — each rank's own slice
Bounds bounds_lo, bounds_hi, zero_mask local — sliced to match x
LinearConstraint.a (weight vector) local
Scalar energy f(x) global (reduced)
LinearConstraint.target (right-hand side) global (same on every rank)
Lagrange multiplier, convergence tolerance, gtol, ftol global
callback(x) argument local slice of current iterate

User-supplied callbacks

The solvers call back into user code in a few places; each has a specific contract.

  • Objective fun(x) -> (energy, gradient) (when jac=True) or separate fun(x) -> energy and jac(x) -> gradient:

    • energy must be a globally reduced scalar. All ranks must return the same number. The standard way to do this is to compute a local quantity and reduce it with pnp.sum(...).item() (or equivalent), where pnp is the Reduction(comm) wrapper. Returning a local energy is the single most common MPI mistake: ranks will silently disagree in line-search acceptance tests and the optimisation will diverge or hang.
    • gradient is local — only the current rank's slice.
  • callback(x) receives the current local iterate. If the caller needs the global state (for plotting or logging from rank 0), they must gather explicitly.

  • hessp(x, d) (CG) returns a local Hessian-vector product.

Building distributed inputs

Use NuMPI.Tools.Reduction(comm) to obtain a pnp object whose sum, max, min, mean, dot methods perform MPI_Allreduce across the communicator. When mpi4py is not installed, NuMPI.MPIStub provides the same interface with a single "rank", so the same code runs serially too.

comm need not be an mpi4py communicator. Any object implementing allreduce(array, op) (with op one of "sum", "max", "min", returning an array of the same shape and dtype on every rank), compare(other) (returning "identical", "congruent", "similar" or "unequal"), size and rank is accepted; muGrid's Communicator (e.g. muGrid.Communicator.world()) is one, and works without mpi4py. A communicator with mpi4py's Allreduce is driven through mpi4py as before.

A typical setup with a communicator-provided subdomain looks like:

from NuMPI.Tools import Reduction
from NuMPI.Optimization import LinearConstraint, l_bfgs_projected

pnp = Reduction(comm)

# a_local: this rank's slice of the global weight vector, shape matching x
# target: global scalar, same on every rank
lc = LinearConstraint(a_local, target, pnp=pnp)

def fun(x):                      # x is the local slice
    # compute local integrand, then REDUCE for the scalar return
    local_energy = 0.5 * np.sum((x - y_local) ** 2)
    return pnp.sum(local_energy).item(), (x - y_local)   # gradient stays local

res = l_bfgs_projected(fun, x0_local, lc, jac=True,
                       bounds_lo=0.0, bounds_hi=1.0,
                       comm=comm, gtol=1e-5)

The returned res.x is the local slice of the solution; res.fun, res.multiplier, and res.max_grad are globally reduced scalars.

See NuMPI/Optimization/__init__.py for optimizer-specific notes and test/Optimization/MPIMinimizationProblems.py::MPI_Quadratic for a reference implementation of a distributed objective.

FFT engines

NuMPI.FFT.make_fft_engine returns an FFT engine for a (possibly domain-decomposed) grid. For a single process this is NumpyFFTEngine, which needs only numpy; for multiple processes it is muGrid.FFTEngine (install with pip install NuMPI[fft]; muGrid must be compiled with MPI support). Both share muGrid's conventions:

import numpy as np
from NuMPI.FFT import make_fft_engine

engine = make_fft_engine((nx, ny), communicator=comm)
real_field = engine.real_space_field("heights")
fourier_field = engine.fourier_space_field("heights_q")
real_field.p[...] = local_heights  # subdomain at engine.subdomain_locations
engine.fft(real_field, fourier_field)  # half-complex along the first axis
q = 2 * np.pi * (engine.fftfreq.T / pixel_size).T  # wavevectors
engine.ifft(fourier_field, real_field)
local_heights = real_field.p * engine.normalisation  # ifft is unnormalized

Development & Funding

Development of this project is funded by the European Research Council within Starting Grant 757343 and by the Deutsche Forschungsgemeinschaft within project EXC 2193.

Metadata

Release files for NuMPI 0.17.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for NuMPI 0.17.0
File Size Uploaded Provenance Actions
numpi-0.17.0.tar.gz 133.1 kB Without provenance Details for numpi-0.17.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for NuMPI 0.17.0
File Interpreter ABI Platform Actions
numpi-0.17.0-py3-none-any.whl Python 3 none any Details for numpi-0.17.0-py3-none-any.whl

Total release size: 211.7 kB

Release files / numpi-0.17.0.tar.gz

Download URL numpi-0.17.0.tar.gz
Size 133.1 kB
Tags Source
SHA-256 checksum
How to use checksums
582a85b2dee35dcbfa82d5eccd36c6236364eef11fa43a35ce72463fc893accd
BLAKE2b-256 checksum
How to use checksums
9a11391a82d57fd26ddf1ad8e0e6e3d997f5b408740adef8aaf1f42dc40ec7a6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / numpi-0.17.0-py3-none-any.whl

Download URL numpi-0.17.0-py3-none-any.whl
Size 78.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d42fc9c72dfad798318c40fe499f441f9b8bc09a3c01a8f9e19874424f6d2205
BLAKE2b-256 checksum
How to use checksums
32743b0c94654ef8fcdca9f6abeed2d82be246f106350b62091b0be9afdd5ea1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Security for NuMPI 0.17.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.17.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page