Skip to main content

Profiler for MPI python programs

Project description

Enables:

  1. using cProfile on each launched MPI process.

  2. measuring the communication overhead of your MPI setup.

mpiprof provides two complementary pieces:

  1. A small runner you can invoke as a module to profile each MPI rank with Python’s cProfile, writing a separate .pstats file per rank: mpiexec -n 4 python3 -m mpiprof your_script.py arg1 arg2.

  2. An optional MPIProfiler class that wraps a mpi4py.MPI.Comm and records basic timing/count statistics of MPI calls and where they came from. You can write the results to disk by calling write_statistics().

Installation

  • From pypi:

    pip install mpiprof
  • From a local checkout, by cloning this repository:

    git clone https://github.com/JohannesBuchner/mpiprof.py
    cd mpiprof.py/
    pip install .

Requirements

  • Python 3.8+

  • mpi4py

  • An MPI runtime if you plan to launch under mpiexec/srun/etc.

Usage: per-rank cProfile runner

Run your script under mpiexec (or srun) using the module form:

  • mpiexec -n 4 python3 -m mpiprof your_script.py arg1 arg2

This writes one profile file per rank named mpiprof.<rank>.pstats in the current working directory.

Options:

  • -o / --outfile sets the output path or pattern (default: mpiprof.{rank}.pstats). The literal substring {rank} will be replaced with the MPI rank.

Signal handling:

  • mpiprof installs SIGINT and SIGTERM handlers to dump the profile before exiting, so Ctrl-C or a clean termination should still produce output. If the MPI launcher escalates to SIGKILL immediately, no tool can save a profile.

Usage: MPIProfiler for mpi4py

The MPIProfiler wraps a communicator and measures wall-clock time and call counts for common blocking MPI calls. It is intentionally lightweight and safe to leave in production runs (low overhead on the measured calls). Nonblocking calls are counted but not timed precisely (the time recorded is call overhead, not the transfer time).

Example:

from mpi4py import MPI
from mpiprof import MPIProfiler

comm = MPIProfiler(MPI.COMM_WORLD)

# Your MPI code as usual, using comm instead of MPI.COMM_WORLD:
rank = comm.Get_rank()
size = comm.Get_size()
data = rank

# Simple collective
total = comm.allreduce(data, op=MPI.SUM)

# Point-to-point
if rank == 0:
    comm.send(b"hello", dest=1, tag=0)
elif rank == 1:
    msg = comm.recv(source=0, tag=0)

# Write stats at the end (one file per rank)
comm.write_statistics()  # default name: mpiprof.stats.<rank>.json

Notes:

  • The wrapper exposes common methods (e.g., send, recv, bcast, reduce, allreduce, gather, scatter, barrier) and forwards any other attributes to the underlying communicator. It tries to be case-insensitive to match mpi4py idioms (both Send and send are supported).

  • For nonblocking operations (Isend, Irecv), the wrapper records the call count but cannot attribute data transfer time unless you also wrap and time Wait/Waitall. A simple wait wrapper is provided to time individual requests returned by the wrapper.

Rank detection

The runner tries to detect the rank via common environment variables:

  • OMPI_COMM_WORLD_RANK, PMIX_RANK (Open MPI)

  • PMI_RANK (MPICH, Intel MPI)

  • MV2_COMM_WORLD_RANK (MVAPICH2)

  • SLURM_PROCID (scheduler fallback)

  • default: 0 if none found

Output files

  • Runner: mpiprof.<rank>.pstats (or the pattern you set with -o). You can analyze it with pstats or tools like snakeviz:

    • python3 -m pstats mpiprof.0.pstats

    • snakeviz mpiprof.0.pstats

  • MPIProfiler: mpiprof.stats.<rank>.json with operation counts and total wall-clock time per operation.

Limitations

  • The runner cannot save profiles if the process is killed by SIGKILL.

  • MPIProfiler’s accounting for nonblocking calls is approximate unless you consistently call wait/waitall on the requests returned by the wrapper’s nonblocking methods.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mpiprof-0.1.2.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mpiprof-0.1.2-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file mpiprof-0.1.2.tar.gz.

File metadata

  • Download URL: mpiprof-0.1.2.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for mpiprof-0.1.2.tar.gz
Algorithm Hash digest
SHA256 d652fe74d1f67d9bfc0b5b1f3cbd31bcc5b31e366d18e7181d35029047f59d14
MD5 5a0224e0c2cb7fc6fe52c293232e5f30
BLAKE2b-256 6d7b6ca7dc69b7e83b1e02aff262a6d86a438a39b20f57085d876b374e930f28

See more details on using hashes here.

File details

Details for the file mpiprof-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: mpiprof-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for mpiprof-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 16d3912a461467269c342af3c46acf1faa5632e1b29f738b9ae7b057e8d48b13
MD5 88007ad50fb8b26968bd66bb58923ac5
BLAKE2b-256 fbbf6d5be0f86facdfa66abd2a0817efdba352f1311f8c714e8e8d01b11ef0d7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page