This class is designed to abstract away the details of whether the project is built with MPI, or in single thread mode. More...
#include <ptems/parallelcomm.hpp>
Public Member Functions | |
ParallelComm () | |
Initialise a reference to the "world" communicator. More... | |
ParallelComm (ParallelComm &&rhs) | |
Move constructor. More... | |
ParallelComm & | operator= (ParallelComm &&rhs) |
Move assignment. More... | |
ParallelComm (const ParallelComm &rhs) | |
Copy constructor. More... | |
ParallelComm & | operator= (const ParallelComm &rhs) |
Copy assignment. More... | |
~ParallelComm () | |
Destroy the communicator object. More... | |
template<typename Dist > | |
auto | GenerateRandomNumber (Dist &dist) |
Generates a random number using the specified distribution. More... | |
void | DiscardRandomNumbers ([[maybe_unused]] unsigned long long amount) |
Discard the specified number of random numbers from the random number generator. More... | |
std::size_t | ProcessRank () const |
Gets the index of the current process in the communicator. More... | |
std::size_t | NumberProcessors () const |
Gets the number of processes in the communicator. More... | |
bool | IsMainProcess () const |
Gets whether the current process is the "main" process (first process in communicator) More... | |
template<typename T > | |
void | ThrowExceptionOnMain (const std::string &message) const |
Raises the specified exception on the main process (actually raises on all calling processes, but should only be called from code running only on main). More... | |
template<typename T > | |
void | CheckExceptionOnMain () const |
Checks if an exception was raised on the main process. More... | |
template<typename T = std::logic_error> | |
void | ThrowExceptionIfAny (const std::string &message, bool condition) const |
Raises the specified exception on all processors if the specified condition is true on ANY processor. More... | |
template<typename T = std::logic_error> | |
void | ThrowExceptionIfAll (const std::string &message, bool condition) const |
Raises the specified exception on all processors if the specified condition is true on ALL processors. More... | |
template<typename T > | |
std::enable_if< std::is_scalar< T >::value >::type | BroadcastFromMain (T &data) const |
Broadcast the specified value from the "main" process to all other processes. More... | |
template<typename T > | |
std::enable_if< std::is_scalar< T >::value >::type | BroadcastFrom (std::size_t processID, T &data) const |
Broadcast the specified value from the specified process to all other processes. More... | |
template<typename T > | |
auto | BroadcastFromMain ([[maybe_unused]] T &data) const -> decltype(data.data(), void()) |
Broadcast the specified C++ random access container from the "main" process to all other processes (this covers std::string, std::vector, etc. More... | |
template<typename T > | |
auto | BroadcastFrom ([[maybe_unused]] std::size_t processID, [[maybe_unused]] T &data) const -> decltype(data.data(), void()) |
Broadcast the specified C++ random access container from the specified process to all other processes (this covers std::string, std::vector, etc. More... | |
template<typename T > | |
void | BroadcastFromMain ([[maybe_unused]] T *data, [[maybe_unused]] std::size_t count) const |
Broadcast the specified array of data from the "main" process to all other processes. More... | |
template<typename T > | |
std::enable_if<!std::is_enum< T >::value >::type | BroadcastFrom ([[maybe_unused]] std::size_t processID, [[maybe_unused]] T *data, [[maybe_unused]] std::size_t count) const |
Broadcast the specified array of data from the specified process to all other processes. More... | |
template<typename T > | |
std::enable_if< std::is_enum< T >::value >::type | BroadcastFrom ([[maybe_unused]] std::size_t processID, [[maybe_unused]] T *data, [[maybe_unused]] std::size_t count) const |
Broadcast the specified array of data from the specified process to all other processes. More... | |
template<typename T > | |
void | Gather ([[maybe_unused]] T *data, [[maybe_unused]] std::size_t count) const |
Gather (concatenates) the specified number of elements from the array in each process into the array of the main (first) process. More... | |
template<typename T > | |
void | Gather ([[maybe_unused]] T *data, [[maybe_unused]] std::size_t offset, [[maybe_unused]] std::size_t count) const |
Gather the specified elements from the array in each process into the array of the main (first) process. More... | |
template<typename T > | |
void | Gather ([[maybe_unused]] T &data) const |
Gather (concatenates) the specified elements from the STL container in each process into the STL container of the main (first) process. More... | |
template<typename T > | |
void | AllGather (T *data, std::size_t count, T *out) const |
Gather (concatenates) the specified number of elements from the array in each process into the specified destination array of all processes. More... | |
template<typename T > | |
void | AllGather (T *data, std::size_t offset, std::size_t count, T *out) const |
Gather the specified elements from the array in each process into the destination array of all processes. More... | |
template<typename T > | |
void | AllGather ([[maybe_unused]] T &data) const |
Gather (concatenates) the specified elements from the STL container in each process into the STL container of all processes. More... | |
template<typename T > | |
void | Add (const T *data, T *output, std::size_t count) const |
Performs elementwise addition of the array from each process, and stores it into the destination array of the main process. More... | |
template<typename T > | |
void | AllAdd (const T *data, T *output, std::size_t count) const |
Performs elementwise addition of the array from each process, and stores it into the destination array of all processes. More... | |
template<typename T > | |
T | Add (T value) const |
Performs addition of the value from each process and returns the result on main process only. More... | |
template<typename T > | |
T | AllAdd (T value) const |
Performs addition of the value from each process and returns the value. More... | |
template<typename T > | |
T | Max (T value) const |
Performs maximum of the value from each process and returns the result on main process only. More... | |
template<typename T > | |
T | AllMax (T value) const |
Performs maximum of the value from each process and returns the value. More... | |
template<typename T > | |
T | Min (T value) const |
Performs minimum of the value from each process and returns the result on main process only. More... | |
template<typename T > | |
T | AllMin (T value) const |
Performs minimum of the value from each process and returns the value. More... | |
void | And (const bool *data, bool *output, std::size_t count) const |
Performs elementwise logical AND of the array from each process, and stores it into the destination array of the main process. More... | |
void | AllAnd (const bool *data, bool *output, std::size_t count) const |
Performs elementwise logical AND of the array from each process, and stores it into the destination array of all processes. More... | |
bool | And (bool value) const |
Performs elementwise logical AND of the boolean value from each process and returns the result on main process only. More... | |
bool | AllAnd (bool value) const |
Performs elementwise logical AND of the boolean value from each process and returns the value. More... | |
void | Or (const bool *data, bool *output, std::size_t count) const |
Performs elementwise logical AND of the array from each process, and stores it into the destination array of the main process. More... | |
void | AllOr (const bool *data, bool *output, std::size_t count) const |
Performs elementwise logical AND of the array from each process, and stores it into the destination array of all processes. More... | |
bool | Or (bool value) const |
Performs elementwise logical OR of the boolean value from each process and returns the result on main process only. More... | |
bool | AllOr (bool value) const |
Performs elementwise logical OR of the boolean value from each process and returns the value. More... | |
ParallelComm | Clone () const |
Creates a duplicate of the current communicator. More... | |
ParallelComm | Split (int colour, int key) const |
Splits the current communicator into multiple sub communicators. More... | |
Static Public Member Functions | |
static ParallelComm | World () |
The main "world" communicator. More... | |
This class is designed to abstract away the details of whether the project is built with MPI, or in single thread mode.
The general principle is to allow the rest of the code to use this class as the MPI communication assuming MPI is available. If, however, the application is built in single process mode this class "fakes" the MPI details (such as returning that only one process is available, the only process is the main process, and all gather/broadcast calls are no-ops).
It also manages a random number generator so that in a multiple processor system each processor uses the same seeded generator but SHOULD skip the generation of numbers generated by other processors
ptems::ParallelComm::ParallelComm | ( | ) |
Initialise a reference to the "world" communicator.
ptems::ParallelComm::ParallelComm | ( | ParallelComm && | rhs | ) |
Move constructor.
rhs | The ParallelComm object to move to this |
ptems::ParallelComm::ParallelComm | ( | const ParallelComm & | rhs | ) |
Copy constructor.
rhs | The ParallelComm object to copy to this |
ptems::ParallelComm::~ParallelComm | ( | ) |
Destroy the communicator object.
|
inline |
Performs elementwise addition of the array from each process, and stores it into the destination array of the main process.
T | The type of data to add |
data | Pointer to the array of data to add elementwise |
output | Pointer to the destination array to store the result (main process only) |
count | Number of elements in the input and output arrays |
|
inline |
Performs addition of the value from each process and returns the result on main process only.
value | Value from current process to add |
|
inline |
Performs elementwise addition of the array from each process, and stores it into the destination array of all processes.
T | The type of data to add |
data | Pointer to the array of data to add elementwise |
output | Pointer to the destination array to store the result |
count | Number of elements in the input and output arrays |
|
inline |
Performs addition of the value from each process and returns the value.
value | Value from current process to add |
|
inline |
Performs elementwise logical AND of the boolean value from each process and returns the value.
value | Boolean value from current process to AND |
|
inline |
Performs elementwise logical AND of the array from each process, and stores it into the destination array of all processes.
data | Pointer to the array of data to AND elementwise |
output | Pointer to the destination array to store the result |
count | Number of elements in the input and output arrays |
|
inline |
Gather (concatenates) the specified elements from the STL container in each process into the STL container of all processes.
The number of entries in each process can be different.
Wrapper around MPI_Allgatherv
T | The type of the STL container to gather |
data | The STL container containing data to gather. On the main process the gathered data is placed into this container. |
|
inline |
Gather (concatenates) the specified number of elements from the array in each process into the specified destination array of all processes.
Wrapper around MPI_Allgather
T | The type of data to gather |
data | Pointer to the array of data to gather. |
count | The number of elements to send. Must be the same for all processes. |
out | Array to gather the data into. The array in the main process MUST be large enough to store the data from all processes. This array can be the same as data. |
|
inline |
Gather the specified elements from the array in each process into the destination array of all processes.
The number of entries in each process can be different, and can be gathered into an arbitrary offset in the destination.
Wrapper around MPI_Allgatherv
T | The type of data to gather |
data | Pointer to the array of data to gather. |
offset | Offset into the destination array of the location to gather the data from this process |
count | The number of elements to send. Can be different for different processes. |
out | Array to gather the data into. The array in the main process MUST be large enough to store the data from all processes. This array can be the same as data. |
|
inline |
Performs maximum of the value from each process and returns the value.
value | Value from current process to add |
|
inline |
Performs minimum of the value from each process and returns the value.
value | Value from current process to add |
|
inline |
Performs elementwise logical OR of the boolean value from each process and returns the value.
value | Boolean value from current process to OR |
|
inline |
Performs elementwise logical AND of the array from each process, and stores it into the destination array of all processes.
data | Pointer to the array of data to AND elementwise |
output | Pointer to the destination array to store the result |
count | Number of elements in the input and output arrays |
|
inline |
Performs elementwise logical AND of the boolean value from each process and returns the result on main process only.
value | Boolean value from current process to AND |
|
inline |
Performs elementwise logical AND of the array from each process, and stores it into the destination array of the main process.
data | Pointer to the array of data to AND elementwise |
output | Pointer to the destination array to store the result (main process only) |
count | Number of elements in the input and output arrays |
|
inline |
Broadcast the specified C++ random access container from the specified process to all other processes (this covers std::string, std::vector, etc.
but not std::list etc.)
T | The container type to broadcast |
processID | The process ID to broadcast from |
data | Data to broadcast |
|
inline |
Broadcast the specified array of data from the specified process to all other processes.
T | The type of data to broadcast, where T is NOT an enumeration type |
processID | The process ID to broadcast from |
data | Pointer to the array of data to broadcast |
count | The size of the array |
|
inline |
Broadcast the specified array of data from the specified process to all other processes.
T | The type of data to broadcast, where T is an enumeration type |
processID | The process ID to broadcast from |
data | Pointer to the array of data to broadcast |
count | The size of the array |
|
inline |
Broadcast the specified value from the specified process to all other processes.
T | The type of data to broadcast |
processID | The process ID to broadcast from |
data | Data to broadcast |
|
inline |
Broadcast the specified C++ random access container from the "main" process to all other processes (this covers std::string, std::vector, etc.
but not std::list etc.)
T | The container type to broadcast |
data | Data to broadcast |
|
inline |
Broadcast the specified array of data from the "main" process to all other processes.
T | The type of data to broadcast, where T is NOT an enumeration type |
data | Pointer to the array of data to broadcast |
count | The size of the array |
|
inline |
Broadcast the specified value from the "main" process to all other processes.
T | The type of data to broadcast |
data | Data to broadcast |
|
inline |
Checks if an exception was raised on the main process.
Should be called on ALL processes (even main if no exception was raised) after any block of code that may have called ThrowExceptionOnMain
T | The exception type to raise is an exception occurred on main |
T | (which may not be the same as the type raised by ThrowExceptionOnMain) if an exception occurred on main |
ParallelComm ptems::ParallelComm::Clone | ( | ) | const |
Creates a duplicate of the current communicator.
|
inline |
Discard the specified number of random numbers from the random number generator.
|
inline |
Gather (concatenates) the specified elements from the STL container in each process into the STL container of the main (first) process.
The number of entries in each process can be different.
Wrapper around MPI_Gatherv
T | The type of the STL container to gather |
data | The STL container containing data to gather. On the main process the gathered data is placed into this container. |
|
inline |
Gather (concatenates) the specified number of elements from the array in each process into the array of the main (first) process.
Wrapper around MPI_Gather
T | The type of data to gather |
data | Pointer to the array of data to gather. On the main process the gathered data is placed into this array (the main process should already have the data in the first count entries). The array in the main process MUST be large enough to store the data from all processes. |
count | The number of elements to send. Must be the same for all processes. |
|
inline |
Gather the specified elements from the array in each process into the array of the main (first) process.
The number of entries in each process can be different, and can be gathered into an arbitrary offset in the main process.
Wrapper around MPI_Gatherv
T | The type of data to gather |
data | Pointer to the array of data to gather. On the main process the gathered data is placed into this array (the main process should already have the data in the correct location). The array in the main process MUST be large enough to store the data from all processes. |
offset | Offset into the array in the main process of the location to gather the data from this process |
count | The number of elements to send. Can be different for different processes. |
|
inline |
Generates a random number using the specified distribution.
The | type of the random number distribution |
dist | The random number distribution to use |
|
inline |
Gets whether the current process is the "main" process (first process in communicator)
|
inline |
Performs maximum of the value from each process and returns the result on main process only.
value | Value from current process to calculate max of |
|
inline |
Performs minimum of the value from each process and returns the result on main process only.
value | Value from current process to calculate min of |
|
inline |
Gets the number of processes in the communicator.
ParallelComm& ptems::ParallelComm::operator= | ( | const ParallelComm & | rhs | ) |
Copy assignment.
rhs | The ParallelComm object to move to this |
ParallelComm& ptems::ParallelComm::operator= | ( | ParallelComm && | rhs | ) |
Move assignment.
rhs | The ParallelComm object to move to this |
|
inline |
Performs elementwise logical OR of the boolean value from each process and returns the result on main process only.
value | Boolean value from current process to OR |
|
inline |
Performs elementwise logical AND of the array from each process, and stores it into the destination array of the main process.
data | Pointer to the array of data to AND elementwise |
output | Pointer to the destination array to store the result (main process only) |
count | Number of elements in the input and output arrays |
|
inline |
Gets the index of the current process in the communicator.
ParallelComm ptems::ParallelComm::Split | ( | int | colour, |
int | key | ||
) | const |
Splits the current communicator into multiple sub communicators.
Wrapper around MPI_Comm_split. All processes which passes the same value for colour will end up in the subcommunicator, ordered such that the lowest key of all processes in the same communicator is the "first" process.
colour | Index denoting subcommunicator process will be added to |
key | Key to use to indicate order of processes in new communicators |
|
inline |
Raises the specified exception on all processors if the specified condition is true on ALL processors.
T | The exception type to raise |
message | The exception error message |
condition | Condition that if true on ALL processors causes the excpetion to be thrown |
T | if condition is true on ALL process |
|
inline |
Raises the specified exception on all processors if the specified condition is true on ANY processor.
T | The exception type to raise |
message | The exception error message |
condition | Condition that if true on ANY processors causes the excpetion to be thrown |
T | if condition is true on ALL process |
|
inline |
Raises the specified exception on the main process (actually raises on all calling processes, but should only be called from code running only on main).
After any block of code which calls ThrowExceptionOnMain a call to CheckExceptionOnMain should be made on ALL processes (including main process if no exception was raised)
T | The exception type to raise |
message | The exception error message |
T | is always thrown |
|
static |
The main "world" communicator.