KratosMultiphysics
KRATOS Multiphysics (Kratos) is a framework for building parallel, multi-disciplinary simulation software, aiming at modularity, extensibility, and high performance. Kratos is written in C++, and counts with an extensive Python interface.
Protected Member Functions | List of all members
Kratos::MPIDataCommunicator Class Reference

Wrapper for common MPI calls within Kratos. More...

#include <mpi_data_communicator.h>

Inheritance diagram for Kratos::MPIDataCommunicator:
Collaboration diagram for Kratos::MPIDataCommunicator:

Public Member Functions

Type Definitions
 KRATOS_CLASS_POINTER_DEFINITION (MPIDataCommunicator)
 Pointer definition of MPIDataCommunicator. More...
 
Life Cycle
 MPIDataCommunicator (MPI_Comm MPIComm)
 Constructor accepting an MPI_Comm object. More...
 
 ~MPIDataCommunicator () override
 Destructor. More...
 
Inquiry
int Rank () const override
 Get the parallel rank for this DataCommunicator. More...
 
int Size () const override
 Get the parallel size of this DataCommunicator. More...
 
bool IsDistributed () const override
 Check whether this DataCommunicator is aware of parallelism. More...
 
bool IsDefinedOnThisRank () const override
 Check whether this DataCommunicator involves the current rank. More...
 
bool IsNullOnThisRank () const override
 Check whether this DataCommunicator is MPI_COMM_NULL for the current rank. More...
 
const DataCommunicatorGetSubDataCommunicator (const std::vector< int > &rRanks, const std::string &rNewCommunicatorName) const override
 Get a sub-data communicator. More...
 
Helper functions for error checking in MPI
bool BroadcastErrorIfTrue (bool Condition, const int SourceRank) const override
 This function throws an error on ranks != Sourcerank if Condition evaluates to true. More...
 
bool BroadcastErrorIfFalse (bool Condition, const int SourceRank) const override
 This function throws an error on ranks != Sourcerank if Condition evaluates to false. More...
 
bool ErrorIfTrueOnAnyRank (bool Condition) const override
 This function throws an error on ranks where Condition evaluates to false, if it evaluated to true on a different rank. More...
 
bool ErrorIfFalseOnAnyRank (bool Condition) const override
 This function throws an error on ranks where Condition evaluates to true, if it evaluated to false on a different rank. More...
 
Input and output
std::string Info () const override
 Turn back information as a string. More...
 
void PrintInfo (std::ostream &rOStream) const override
 Print information about this object. More...
 
void PrintData (std::ostream &rOStream) const override
 Print object's data. More...
 
- Public Member Functions inherited from Kratos::DataCommunicator
 KRATOS_CLASS_POINTER_DEFINITION (DataCommunicator)
 Pointer definition of DataCommunicator. More...
 
 DataCommunicator ()
 Default constructor. More...
 
virtual ~DataCommunicator ()
 Destructor. More...
 
template<typename TObject >
void Broadcast (TObject &rBroadcastObject, const int SourceRank) const
 Synchronize a buffer to the value held by the broadcasting rank. More...
 
template<typename TObject >
TObject SendRecv (const TObject &rSendObject, const int SendDestination, const int SendTag, const int RecvSource, const int RecvTag) const
 Exchange data with other ranks. More...
 
template<class TObject >
TObject SendRecv (const TObject &rSendObject, const int SendDestination, const int RecvSource) const
 Exchange data with other ranks. More...
 
template<class TObject >
void SendRecv (const TObject &rSendObject, const int SendDestination, const int SendTag, TObject &rRecvObject, const int RecvSource, const int RecvTag) const
 Exchange data with other ranks. More...
 
template<class TObject >
void SendRecv (const TObject &rSendObject, const int SendDestination, TObject &rRecvObject, const int RecvSource) const
 Exchange data with other ranks. More...
 
template<typename TObject >
void Send (const TObject &rSendValues, const int SendDestination, const int SendTag=0) const
 Exchange data with other ranks. More...
 
template<typename TObject >
void Recv (TObject &rRecvObject, const int RecvSource, const int RecvTag=0) const
 Exchange data with other ranks. More...
 

Static Public Member Functions

Access
static MPI_Comm GetMPICommunicator (const DataCommunicator &rDataCommunicator)
 Get the underlying MPI_Comm instance. More...
 
- Static Public Member Functions inherited from Kratos::DataCommunicator
static DataCommunicatorGetDefault ()
 Convenience function to retrieve the current default DataCommunicator. More...
 
static DataCommunicator::UniquePointer Create ()
 Create a new DataCommunicator as a copy of this one. More...
 

Protected Member Functions

void BroadcastImpl (std::string &rBroadcastValues, const int SourceRank) const override
 Synchronize a buffer to the value held by the broadcasting rank (string version). More...
 
std::string SendRecvImpl (const std::string &rSendValues, const int SendDestination, const int SendTag, const int RecvSource, const int RecvTag) const override
 Exchange data with other ranks (string version). More...
 
void SendRecvImpl (const std::string &rSendValues, const int SendDestination, const int SendTag, std::string &rRecvValues, const int RecvSource, const int RecvTag) const override
 Exchange data with other ranks (string version). More...
 
void SendImpl (const std::string &rSendValues, const int SendDestination, const int SendTag=0) const override
 Send data to other ranks (string version). More...
 
void RecvImpl (std::string &rRecvValues, const int RecvSource, const int RecvTag=0) const override
 Receive data from other ranks (string version). More...
 
- Protected Member Functions inherited from Kratos::DataCommunicator
template<class TObject >
void BroadcastImpl (TObject &rBroadcastObject, const int SourceRank) const
 Synchronize a buffer to the value held by the broadcasting rank (generic version). More...
 
template<class TObject >
TObject SendRecvImpl (const TObject &rSendObject, const int SendDestination, const int SendTag, const int RecvSource, const int RecvTag) const
 Exchange data with other ranks (generic version). More...
 
template<class TObject >
void SendImpl (const TObject &rSendObject, const int SendDestination, const int SendTag) const
 Exchange data with other ranks (generic version). More...
 
template<class TObject >
void RecvImpl (TObject &rRecvObject, const int RecvSource, const int RecvTag=0) const
 Exchange data with other ranks (generic version). More...
 

Operations

void Barrier () const override
 Pause program execution until all threads reach this call. More...
 
bool AndReduce (const bool Value, const int Root) const override
 
Kratos::Flags AndReduce (const Kratos::Flags Values, const Kratos::Flags Mask, const int Root) const override
 
bool OrReduce (const bool Value, const int Root) const override
 
Kratos::Flags OrReduce (const Kratos::Flags Values, const Kratos::Flags Mask, const int Root) const override
 
bool AndReduceAll (const bool Value) const override
 
Kratos::Flags AndReduceAll (const Kratos::Flags Values, const Kratos::Flags Mask) const override
 
bool OrReduceAll (const bool Value) const override
 
Kratos::Flags OrReduceAll (const Kratos::Flags Values, const Kratos::Flags Mask) const override
 
static MPIDataCommunicator::UniquePointer Create (MPI_Comm MPIComm)
 Create a new MPIDataCommunicator using the provided MPI_Comm object. More...
 

Detailed Description

Wrapper for common MPI calls within Kratos.

This class is designed to isolate the Kratos core and applications from direct calls to MPI routines.

For function operating on std::vectors, no effort is made to resize inconsistent vectors. The sizes of the sending and receiving buffers will only be checked if the code is compiled in Debug mode. In that case, a meaningful error explaining the inconsistency will be produced. This is done for efficiency (size checks can force multi-stage communications).

See also
DataCommunicator in the Kratos Core for the full interface and a serial do-nothing implementation.

Constructor & Destructor Documentation

◆ MPIDataCommunicator()

Kratos::MPIDataCommunicator::MPIDataCommunicator ( MPI_Comm  MPIComm)
explicit

Constructor accepting an MPI_Comm object.

◆ ~MPIDataCommunicator()

Kratos::MPIDataCommunicator::~MPIDataCommunicator ( )
override

Destructor.

Member Function Documentation

◆ AndReduce() [1/2]

bool Kratos::MPIDataCommunicator::AndReduce ( const bool  Value,
const int  Root 
) const
overridevirtual

Reimplemented from Kratos::DataCommunicator.

◆ AndReduce() [2/2]

Kratos::Flags Kratos::MPIDataCommunicator::AndReduce ( const Kratos::Flags  Values,
const Kratos::Flags  Mask,
const int  Root 
) const
overridevirtual

Reimplemented from Kratos::DataCommunicator.

◆ AndReduceAll() [1/2]

bool Kratos::MPIDataCommunicator::AndReduceAll ( const bool  Value) const
overridevirtual

Reimplemented from Kratos::DataCommunicator.

◆ AndReduceAll() [2/2]

Kratos::Flags Kratos::MPIDataCommunicator::AndReduceAll ( const Kratos::Flags  Values,
const Kratos::Flags  Mask 
) const
overridevirtual

Reimplemented from Kratos::DataCommunicator.

◆ Barrier()

void Kratos::MPIDataCommunicator::Barrier ( ) const
overridevirtual

Pause program execution until all threads reach this call.

Wrapper for MPI_Barrier.

Reimplemented from Kratos::DataCommunicator.

◆ BroadcastErrorIfFalse()

bool Kratos::MPIDataCommunicator::BroadcastErrorIfFalse ( bool  Condition,
const int  SourceRank 
) const
overridevirtual

This function throws an error on ranks != Sourcerank if Condition evaluates to false.

This method is intended as a helper function to force ranks to stop after an error is detected on a given rank. A typical use case would be to completely stop the simulation if an error is detected on the root process. The intended usage is something like:

KRATOS_ERROR_IF_NOT( data_communicator_instance.BroadcastErrorIfFalse(Condition, Root) ) << "Detailed error message in Root rank";

If an error is detected, ranks other than Root will fail with a generic error message. Failing on the Root rank is left to the caller, so that a detailed error message can be produced.

Note
This method should be called from all ranks, it will deadlock if called within an if(rank == some_rank) statement.
See also
MPIDataCommunicator.
Parameters
ConditionThe condition to check.
SourceRankThe rank where the condition is meaningful.
Returns
The result of evaluating Condition.

Reimplemented from Kratos::DataCommunicator.

◆ BroadcastErrorIfTrue()

bool Kratos::MPIDataCommunicator::BroadcastErrorIfTrue ( bool  Condition,
const int  SourceRank 
) const
overridevirtual

This function throws an error on ranks != Sourcerank if Condition evaluates to true.

This method is intended as a helper function to force ranks to stop after an error is detected on a given rank. A typical use case would be to completely stop the simulation if an error is detected on the root process. The intended usage is something like:

KRATOS_ERROR_IF( data_communicator_instance.BroadcastErrorIfTrue(Condition, Root) ) << "Detailed error message in Root rank";

If an error is detected, ranks other than Root will fail with a generic error message. Failing on the Root rank is left to the caller, so that a detailed error message can be produced.

Note
This method should be called from all ranks, it will deadlock if called within an if(rank == some_rank) statement.
See also
MPIDataCommunicator.
Parameters
ConditionThe condition to check.
SourceRankThe rank where the condition is meaningful.
Returns
The result of evaluating Condition.

Reimplemented from Kratos::DataCommunicator.

◆ BroadcastImpl()

void Kratos::MPIDataCommunicator::BroadcastImpl ( std::string &  rBuffer,
const int  SourceRank 
) const
overrideprotectedvirtual

Synchronize a buffer to the value held by the broadcasting rank (string version).

This is a wrapper for MPI_Bcast.

Parameters
[in/out]The broadcast value (input on SourceRank, output on all other ranks).
[in]SourceRankThe rank transmitting the value.

Reimplemented from Kratos::DataCommunicator.

◆ Create()

MPIDataCommunicator::UniquePointer Kratos::MPIDataCommunicator::Create ( MPI_Comm  MPIComm)
static

Create a new MPIDataCommunicator using the provided MPI_Comm object.

The new MPIDataCommunicator instance is returned as a unique pointer, since it is responsible for managing the lifetime of the underlying MPI_Comm, and in particular calling MPI_Comm_free once it goes out of scope (this is only required/done if Comm is not one of the predefined MPI_COMM types).

◆ ErrorIfFalseOnAnyRank()

bool Kratos::MPIDataCommunicator::ErrorIfFalseOnAnyRank ( bool  Condition) const
overridevirtual

This function throws an error on ranks where Condition evaluates to true, if it evaluated to false on a different rank.

This method is intended as a helper function to force ranks to stop after an error is detected on one or more ranks. The intended usage is something like:

KRATOS_ERROR_IF_NOT( data_communicator_instance.ErrorIfFalseOnAnyRank(Condition) ) << "Detailed error message in ranks where Condition == false.";

If an error is detected, ranks other than those where it was detected will fail with a generic error message. Failing on the ranks where the condition is false is left to the caller, so that a detailed error message can be produced.

Note
This method should be called from all ranks, it will deadlock if called within an if(rank == some_rank) statement.
See also
MPIDataCommunicator.
Parameters
ConditionThe condition to check.
Returns
The result of evaluating Condition.

Reimplemented from Kratos::DataCommunicator.

◆ ErrorIfTrueOnAnyRank()

bool Kratos::MPIDataCommunicator::ErrorIfTrueOnAnyRank ( bool  Condition) const
overridevirtual

This function throws an error on ranks where Condition evaluates to false, if it evaluated to true on a different rank.

This method is intended as a helper function to force ranks to stop after an error is detected on one or more ranks. The intended usage is something like:

KRATOS_ERROR_IF( data_communicator_instance.ErrorIfTrueOnAnyRank(Condition) ) << "Detailed error message in ranks where Condition == true.";

If an error is detected, ranks other than those where it was detected will fail with a generic error message. Failing on the ranks where the condition is true is left to the caller, so that a detailed error message can be produced.

Note
This method should be called from all ranks, it will deadlock if called within an if(rank == some_rank) statement.
See also
MPIDataCommunicator.
Parameters
ConditionThe condition to check.
Returns
The result of evaluating Condition.

Reimplemented from Kratos::DataCommunicator.

◆ GetMPICommunicator()

MPI_Comm Kratos::MPIDataCommunicator::GetMPICommunicator ( const DataCommunicator rDataCommunicator)
static

Get the underlying MPI_Comm instance.

Note
This method does not exist in the base class as it would introduce a dependency to MPI in the Kratos core.

◆ GetSubDataCommunicator()

const DataCommunicator & Kratos::MPIDataCommunicator::GetSubDataCommunicator ( const std::vector< int > &  rRanks,
const std::string &  rNewCommunicatorName 
) const
overridevirtual

Get a sub-data communicator.

This function returns a sub-data communicator based on the provided ranks and a new communicator name.

Parameters
rRanksThe ranks to include in the sub-communicator.
rNewCommunicatorNameThe name of the new sub-communicator.
Returns
The sub-data communicator.

Reimplemented from Kratos::DataCommunicator.

◆ Info()

std::string Kratos::MPIDataCommunicator::Info ( ) const
overridevirtual

Turn back information as a string.

Reimplemented from Kratos::DataCommunicator.

◆ IsDefinedOnThisRank()

bool Kratos::MPIDataCommunicator::IsDefinedOnThisRank ( ) const
overridevirtual

Check whether this DataCommunicator involves the current rank.

In MPI, if the rank is not involved in communication, the communicator is MPI_COMM_NULL and is not a valid argument for most MPI calls.

Returns
True if the DataCommunicator is defined on the current rank, otherwise false.

Reimplemented from Kratos::DataCommunicator.

◆ IsDistributed()

bool Kratos::MPIDataCommunicator::IsDistributed ( ) const
overridevirtual

Check whether this DataCommunicator is aware of parallelism.

Returns
True if the DataCommunicator is distributed, otherwise false.

Reimplemented from Kratos::DataCommunicator.

◆ IsNullOnThisRank()

bool Kratos::MPIDataCommunicator::IsNullOnThisRank ( ) const
overridevirtual

Check whether this DataCommunicator is MPI_COMM_NULL for the current rank.

In MPI, if the rank is not involved in communication, the communicator is MPI_COMM_NULL and is not a valid argument for most MPI calls.

Returns
True if the DataCommunicator is MPI_COMM_NULL, otherwise false.

Reimplemented from Kratos::DataCommunicator.

◆ KRATOS_CLASS_POINTER_DEFINITION()

Kratos::MPIDataCommunicator::KRATOS_CLASS_POINTER_DEFINITION ( MPIDataCommunicator  )

Pointer definition of MPIDataCommunicator.

◆ OrReduce() [1/2]

bool Kratos::MPIDataCommunicator::OrReduce ( const bool  Value,
const int  Root 
) const
overridevirtual

Reimplemented from Kratos::DataCommunicator.

◆ OrReduce() [2/2]

Kratos::Flags Kratos::MPIDataCommunicator::OrReduce ( const Kratos::Flags  Values,
const Kratos::Flags  Mask,
const int  Root 
) const
overridevirtual

Reimplemented from Kratos::DataCommunicator.

◆ OrReduceAll() [1/2]

bool Kratos::MPIDataCommunicator::OrReduceAll ( const bool  Value) const
overridevirtual

Reimplemented from Kratos::DataCommunicator.

◆ OrReduceAll() [2/2]

Kratos::Flags Kratos::MPIDataCommunicator::OrReduceAll ( const Kratos::Flags  Values,
const Kratos::Flags  Mask 
) const
overridevirtual

Reimplemented from Kratos::DataCommunicator.

◆ PrintData()

void Kratos::MPIDataCommunicator::PrintData ( std::ostream &  rOStream) const
overridevirtual

Print object's data.

Reimplemented from Kratos::DataCommunicator.

◆ PrintInfo()

void Kratos::MPIDataCommunicator::PrintInfo ( std::ostream &  rOStream) const
overridevirtual

Print information about this object.

Reimplemented from Kratos::DataCommunicator.

◆ Rank()

int Kratos::MPIDataCommunicator::Rank ( ) const
overridevirtual

Get the parallel rank for this DataCommunicator.

This function serves as a wrapper for MPI_Comm_rank.

Returns
The parallel rank of the current process.

Reimplemented from Kratos::DataCommunicator.

◆ RecvImpl()

void Kratos::MPIDataCommunicator::RecvImpl ( std::string &  rRecvValues,
const int  RecvSource,
const int  RecvTag = 0 
) const
overrideprotectedvirtual

Receive data from other ranks (string version).

This is a wrapper for MPI_Recv.

Parameters
[out]rRecvValuesReceived string from rank RecvSource.
[in]RecvSourceRank the string is expected from.
[in]RecvTagMessage tag for received values.

Reimplemented from Kratos::DataCommunicator.

◆ SendImpl()

void Kratos::MPIDataCommunicator::SendImpl ( const std::string &  rSendValues,
const int  SendDestination,
const int  SendTag = 0 
) const
overrideprotectedvirtual

Send data to other ranks (string version).

This is a wrapper for MPI_Send.

Parameters
[in]rSendValuesString to send to rank SendDestination.
[in]SendDestinationRank the string will be sent to.
[in]SendTagMessage tag for sent values.

Reimplemented from Kratos::DataCommunicator.

◆ SendRecvImpl() [1/2]

std::string Kratos::MPIDataCommunicator::SendRecvImpl ( const std::string &  rSendValues,
const int  SendDestination,
const int  SendTag,
const int  RecvSource,
const int  RecvTag 
) const
overrideprotectedvirtual

Exchange data with other ranks (string version).

This is a wrapper for MPI_Sendrecv.

Parameters
[in]rSendValuesString to send to rank SendDestination.
[in]SendDestinationRank the string will be sent to.
[in]SendTagMessage tag for sent values.
[in]RecvSourceRank the string is expected from.
[in]RecvTagMessage tag for received values.
Returns
Received string from rank RecvSource.

Reimplemented from Kratos::DataCommunicator.

◆ SendRecvImpl() [2/2]

void Kratos::MPIDataCommunicator::SendRecvImpl ( const std::string &  rSendValues,
const int  SendDestination,
const int  SendTag,
std::string &  rRecvValues,
const int  RecvSource,
const int  RecvTag 
) const
overrideprotectedvirtual

Exchange data with other ranks (string version).

This is a wrapper for MPI_Sendrecv.

Parameters
[in]rSendValuesString to send to rank SendDestination.
[in]SendDestinationRank the string will be sent to.
[in]SendTagMessage tag for sent values.
[out]rRecvValuesReceived string from rank RecvSource.
[in]RecvSourceRank the string is expected from.
[in]RecvTagMessage tag for received values.

Reimplemented from Kratos::DataCommunicator.

◆ Size()

int Kratos::MPIDataCommunicator::Size ( ) const
overridevirtual

Get the parallel size of this DataCommunicator.

This function serves as a wrapper for MPI_Comm_size.

Returns
The parallel size of the communicator.

Reimplemented from Kratos::DataCommunicator.


The documentation for this class was generated from the following files: