Using ParallelZone with mpi4py

Note

mpi4py is not installed by ParallelZone, so should be installed separately. See mpi4py documentation for more information.

Warning

Python support for ParallelZone is currently in beta. APIs are not considered stable at this time and are subject to change.

mpi4py is the most popular package that provides Python bindings for the MPI standard. Here, we describe how you can use mpi4py along with ParallelZone.

MPI Initialization and Finalization

The MPI standard requires MPI_Init or MPI_Init_thread function to be called by each process to initialize the MPI execution environment. mpi4py performs initialization (if necessary) automatically when MPI module is imported with from mpi4py import MPI. By default thread support is enabled with MPI_THREAD_MULTIPLE option, which is also the required thread support level for ParallelZone. If MPI is already initialized, i.e. by ParallelZone, mpi4py basically uses the same MPI environment. Finalization is also handled automatically by mpi4py when Python processes exit.

Therefore, for a general use case, where MPI_COMM_WORLD refers to the same set of resources for mpi4py and ParallelZone, they are compatible with each other.

Note

It is possible to disable automatic initialization of mpi4py or change the thread support level manually. See the mpi4py documentation for more details. However, note that it is not possible to pass a communicator to mpi4py explicitly.

Passing mpi4py Communicator to ParallelZone

If a user is interested in only sharing a subset of resources with ParallelZone, then the user needs to initialize ParallelZone by passing the communicator created by mpi4py. An example, where the user passes MPI_COMM_SELF as the communicator for ParallelZone is given below.

from mpi4py import MPI
import cppyy.ll

pz_comm = cppyy.ll.cast["MPI_Comm"](MPI._handleof(MPI.COMM_SELF))
pz_world = parallelzone.runtime.RuntimeView(pz_comm)