Skip to main content

Simple decorators and utilities for MPI parallel computing

Project description

mpitools

Development Status Version

⚠️ Development Notice: This package is in active development. The API may change significantly between versions until v1.0.0. Use in production environments is not recommended.

A Python package providing simple decorators and utilities for MPI (Message Passing Interface) parallel computing. Built on top of mpi4py, mpitools makes it easy to write parallel code with minimal boilerplate.

Features

  • Decorators for work distribution: Execute functions on specific ranks or groups of processes
  • Communication decorators: Broadcast, gather, and reduce operations made simple
  • Error handling: Graceful error handling across all MPI processes
  • Task queue system: Distributed task processing with the queue submodule

Installation

git clone git@github.com:erik-a-bensen/mpitools.git
cd mpitools
pip install .

Requirements:

  • Python 3.7+
  • mpi4py
  • An MPI implementation (OpenMPI, MPICH, etc.)

Quick Start

Basic Usage

from mpitools import setup_mpi, broadcast_from_main, gather_to_main, eval_on_main

# Initialize MPI environment
comm, rank, size = setup_mpi()

# Execute only on rank 0, broadcast result to all processes
@broadcast_from_main()
def load_config():
    return {"num_iterations": 1000, "tolerance": 1e-6}

# Execute on all processes, gather results to rank 0
@gather_to_main()
def compute_partial_sum():
    return sum(range(rank * 100, (rank + 1) * 100))

# Execute only on rank 0
@eval_on_main()
def save_results(data):
    with open("results.txt", "w") as f:
        f.write(str(data))

# Usage
config = load_config()  # Same config on all processes
partial_sums = compute_partial_sum()  # List of sums on rank 0, None elsewhere
save_results(partial_sums)  # Only saves on rank 0

Task Queue System

from mpitools import setup_mpi
from mpitools.queue import MPIQueue, Task

# Initialize MPI environment
comm, rank, size = setup_mpi()

# Define a task class
class MyTask(Task):
    def __init__(self, task_id: str, data: int):
        super().__init__(task_id)
        self.data = data

    def execute(self):
        # Perform some computation
        result = self.data * 2  # Example computation
        return result

# Create a distributed task queue
queue = MPIQueue()

# Add tasks to the queue
if rank == 0:
    tasks = [MyTask(f"task_{i}", i) for i in range(10)]
    queue.add_tasks(tasks)

# Run the task queue
results = queue.run()

Error Handling

from mpitools import abort_on_error

@abort_on_error()  # Aborts all processes if any process encounters an error
def risky_computation():
    # If this fails on any process, all processes will terminate
    result = 1 / some_calculation()
    return result

Core Decorators

Error Handling

  • @abort_on_error() - Abort all processes if any process raises an exception

Work Distribution

  • @eval_on_main() - Execute only on rank 0
  • @eval_on_workers() - Execute only on worker ranks (1, 2, ...)
  • @eval_on_single(rank) - Execute only on specified rank
  • @eval_on_select([ranks]) - Execute only on specified ranks

Communication

  • @broadcast_from_main() - Execute on rank 0, broadcast result to all
  • @broadcast_from_process(rank) - Execute on specified rank, broadcast to all
  • @gather_to_main() - Execute on all, gather results to rank 0
  • @gather_to_process(rank) - Execute on all, gather results to specified rank
  • @gather_to_all() - Execute on all, gather results to all processes

Reduction Operations

  • @reduce_to_main(op='sum') - Execute on all, reduce to rank 0
  • @reduce_to_process(rank, op='sum') - Execute on all, reduce to specified rank
  • @reduce_to_all(op='sum') - Execute on all, reduce to all processes

Supported reduction operations: 'sum', 'prod', 'max', 'min', 'land', 'band', 'lor', 'bor', 'lxor', 'bxor', 'maxloc', 'minloc'

Running MPI Programs

# Run with 4 processes
mpirun -n 4 python your_script.py

# Run with specific hosts
mpirun -n 4 -H host1,host2 python your_script.py

Documentation

Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

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

mpi4pytools-0.1.0.tar.gz (9.7 kB view details)

Uploaded Source

Built Distribution

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

mpi4pytools-0.1.0-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

Details for the file mpi4pytools-0.1.0.tar.gz.

File metadata

  • Download URL: mpi4pytools-0.1.0.tar.gz
  • Upload date:
  • Size: 9.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.11

File hashes

Hashes for mpi4pytools-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7ba893cc9df86cde258af2f02d4eb087c31fbfc010d950a82329bc9318f2df6a
MD5 1891c5c410117f983e9270276665d7fd
BLAKE2b-256 749094d6ab91fbf7b8afe99bb54975ffa42649026c3034842935116997f17ec0

See more details on using hashes here.

File details

Details for the file mpi4pytools-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: mpi4pytools-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.11

File hashes

Hashes for mpi4pytools-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7bb37b4c8b42d4f18f8d647895f37101b25fe3776540fdb1f555a8b595a215b5
MD5 54248683a52e704b4f832d0501e25deb
BLAKE2b-256 67bfe1c5922e1958da1675561b33d29b8a30f50f78a04df4ec5d2734472106ac

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