relentless.mpi.Communicator#
- class relentless.mpi.Communicator(comm=None, root=0)#
MPI communicator.
Wrapper around an MPI communicator. Methods will gracefully degrade to single-processor functions if not running under MPI or there is only a single rank in the communicator.
- Parameters:
comm (
mpi4py.MPI.Comm) – Communicator backend. Defaults toNone, which is converted tompi4py.MPI.COMM_WORLDif running under MPI.root (int) – Root rank index (defaults to 0).
- Raises:
IndexError – If the
rootrank does not lie within the valid range for the communicator.
Methods
barrier()Create barrier for all ranks in the MPI communicator.
bcast(data[, root])Broadcast Python object to all ranks.
bcast_numpy(data[, root])Broadcast NumPy array to all ranks.
load_json(filename[, root])Load a JSON file and broadcast.
loadtxt(filename[, root])Load text from a file and broadcast.
Attributes
mpi4py.MPI.CommMPI communicator.True if MPI communication is enabled.
Index of rank in the MPI communicator.
True if the MPI rank is the root rank.
Index of the root rank in the MPI communicator.
Number of ranks in the MPI communicator.
- barrier()#
Create barrier for all ranks in the MPI communicator.
- bcast(data, root=None)#
Broadcast Python object to all ranks.
Broadcasting is a one-to-all communication pattern that can be used to synchronize data across ranks. This method wraps around the
mpi4py.MPI.Comm.bcast()method, which only operates on Python data types that are picklable. If you need to broadcast a NumPy array that uses the Python buffer protocol, you should usebcast_numpy().- Parameters:
- Returns:
The broadcast data on all ranks.
- Return type:
Example
Broadcast an integer:
comm = relentless.mpi.Communicator() if comm.rank_is_root: x = 42 else: x = None x = comm.bcast(x)
- bcast_numpy(data, root=None)#
Broadcast NumPy array to all ranks.
Broadcasting is a one-to-all communication pattern that can be used to synchronize data across ranks. This method wraps around the
mpi4py.MPI.Comm.Bcast()method for NumPy arrays. The method will ensure thatdatahas the correct size and type on all ranks by first broadcasting this information; ifdatais not allocated in this way, it will be allocated automatically.- Parameters:
data (
numpy.ndarray) – NumPy array to broadcast.root (int) – Rank to broadcast from. If
None(default), broadcast from therootof the communicator.
- Returns:
The broadcast array on all ranks.
- Return type:
Examples
Broadcast an array:
comm = relentless.mpi.Communicator() if comm.rank_is_root: x = numpy.array([1,2,3], dtype=numpy.int32) else: x = None x = comm.bcast_numpy(x)
Broadcast an array with pre-existing storage:
comm = relentless.mpi.Communicator() if comm.rank_is_root: x = numpy.array([1,2,3], dtype=numpy.int32) else: x = numpy.empty(3, dtype=numpy.int32) x = comm.bcast_numpy(x)
- property comm#
mpi4py.MPI.CommMPI communicator.This attribute is
Noneif running on a single processor.
- load_json(filename, root=None)#
Load a JSON file and broadcast.
Data is loaded from file using
json.load()on therootrank, then broadcast to all ranks. This function can be called with essentially no overhead in single-processor calculations, but prevents oversubscribing file handles when running under MPI.- Parameters:
- Returns:
The data from
filename.- Return type:
Example
Load a file:
comm = relentless.mpi.Communicator() dat = comm.load_json("ensemble.json")
- loadtxt(filename, root=None, **kwargs)#
Load text from a file and broadcast.
Data is loaded from file using
numpy.loadtxt()on therootrank, then broadcast to all ranks. This function can be called with essentially no overhead in single-processor calculations, but prevents oversubscribing file handles when running under MPI.- Parameters:
filename (str) – Name of the file to load.
root (int) – Rank to broadcast from. If
None(default), broadcast from therootof the communicator.**kwargs – Optional keyword arguments to
numpy.loadtxt().
- Returns:
The data from
filename.- Return type:
Example
Load a file:
comm = relentless.mpi.Communicator() dat = comm.loadtxt("gr.dat")