mpi

class laddu.mpi.MPI(*, trigger: bool = True)

Context manager for using the MPI backend.

__init__(self, \*, trigger: bool = True)

Initialize the MPI context manager to use MPI if trigger is True.

Examples

>>> import os
>>> from laddu.mpi import MPI
>>>
>>> def main():
...     pass
>>>
>>> if __name__ == '__main__':
...     with MPI(trigger=os.environ.get('MPI') == '1'):
...         main()
laddu.mpi.finalize_mpi()

Drop the MPI universe and finalize MPI at the end of a program

This should only be called once and should be called at the end of all laddu-related function calls. This must be called at the end of any program which uses MPI.

laddu.mpi.get_rank()

Get the rank of the current MPI process

Returns 0 if MPI is not enabled

laddu.mpi.get_size()

Get the total number of MPI processes (including the root process)

Returns 1 if MPI is not enabled

laddu.mpi.is_mpi_available()

Check if laddu was compiled with MPI support (returns True if it was).

Since laddu-mpi has the same namespace as laddu (they both are imported with import laddu), this method can be used to check if MPI capabilities are available without actually running any MPI code. While functions in the laddu.mpi module will raise an ModuleNotFoundError if MPI is not supported, its sometimes convenient to have a simple boolean check rather than a try-catch block, and this method provides that.

laddu.mpi.is_root()

Check if the current MPI process is the root process

This can be combined with laddu.mpi.using_mpi() to ensure valid results are only returned from the root rank process on the condition that MPI is enabled.

laddu.mpi.use_mpi(*, trigger=True)

Use the Message Passing Interface (MPI) to run on a distributed system

Parameters:

trigger (bool, default=True) – An optional parameter which allows MPI to only be used under some boolean condition.

Notes

You must have MPI installed for this to work, and you must call the program with mpirun <executable>, or bad things will happen.

MPI runs an identical program on each process, but gives the program an ID called its “rank”. Only the results of methods on the root process (rank 0) should be considered valid, as other processes only contain portions of each dataset. To ensure you don’t save or print data at other ranks, use the provided laddu.mpi.is_root() method to check if the process is the root process.

Once MPI is enabled, it cannot be disabled. If MPI could be toggled (which it can’t), the other processes will still run, but they will be independent of the root process and will no longer communicate with it. The root process stores no data, so it would be difficult (and convoluted) to get the results which were already processed via MPI.

Additionally, MPI must be enabled at the beginning of a script, at least before any other laddu functions are called. For this reason, it is suggested that you use the context manager laddu.mpi.MPI to ensure the MPI backend is used properly.

If laddu.mpi.use_mpi() is called multiple times, the subsequent calls will have no effect.

You must call laddu.mpi.finalize_mpi() before your program exits for MPI to terminate smoothly.

laddu.mpi.using_mpi()

Check if MPI is enabled

This can be combined with laddu.mpi.is_root() to ensure valid results are only returned from the root rank process on the condition that MPI is enabled.