28 other.comm = MPI_COMM_NULL;
29 _owning = other._owning;
30 parent = std::move(other.parent);
36 other.comm = MPI_COMM_NULL;
37 _owning = other._owning;
38 parent = std::move(other.parent);
63 int communicatorSize = 1;
67 return communicatorSize;
83 return comm == MPI_COMM_NULL;
100 return std::make_shared<CommState>();
105#ifndef PRECICE_NO_MPI
106 return fromComm(MPI_COMM_SELF);
129 if (comm == MPI_COMM_NULL) {
130 out <<
"COMM_NULL:invalid";
133#ifndef PRECICE_NO_MPI
134 if (comm == MPI_COMM_SELF) {
135 out <<
"COMM_SELF:1/1";
139 out << rank() <<
'/' << size();
170 PRECICE_ASSERT(newState !=
nullptr,
"pushState cannot to be called with nullptr!");
181#ifndef PRECICE_NO_MPI
192#ifndef PRECICE_NO_MPI
194 "MPI cannot be initialized twice. You need to handle the MPI lifetime yourself.");
200 PRECICE_ASSERT(isInit,
"A user-provided comm can only exist if MPI has been initialized.");
208 MPI_Init(
nullptr,
nullptr);
232#ifndef PRECICE_NO_MPI
253#ifndef PRECICE_NO_MPI
255 MPI_Init(argc, argv);
265#ifndef PRECICE_NO_MPI
274#ifndef PRECICE_NO_MPI
278 constexpr int keepTheSameOrder{0};
282 auto err = MPI_Comm_split(
current()->comm, group.
value_or(MPI_UNDEFINED), keepTheSameOrder, &newComm);
302#ifndef PRECICE_NO_MPI
#define PRECICE_DEBUG(...)
#define PRECICE_TRACE(...)
int MPI_Comm_rank(MPI_Comm comm, int *rank)
int MPI_Barrier(MPI_Comm comm)
static MPI_Comm MPI_COMM_WORLD
int MPI_Comm_size(MPI_Comm comm, int *size)
#define PRECICE_ASSERT(...)
static bool isMPIInitialized()
Return true if MPI is initialized.
static InitializationState _initState
static void finalizeTestingMPI()
Unconditionally finalizes MPI environment.
std::shared_ptr< CommState > CommStatePtr
static void resetCommState()
static void finalizeOrCleanupMPI()
Finalized a managed MPI environment or cleans up after an non-managed session.
static CommStatePtr current()
Returns an owning pointer to the current CommState.
static Rank getProcessRank()
static void resetManagedMPI()
static void pushState(CommStatePtr newState)
InitializationState
Kind of initialization that took place.
@ Unmanaged
preCICE manages the lifetime of the MPI environment
@ Managed
Communicator was provided by the user.
@ Provided
Not initialized.
@ Testing
preCICE was initialized in an existing MPI environment
static CommStatePtr _currentState
static bool _mpiInitializedByPrecice
Flag to saveguard against reinitializing MPI, which is forbidden.
static void initializeOrDetectMPI(std::optional< Communicator > userProvided=std::nullopt)
static void initializeTestingMPI(int *argc, char ***argv)
static void splitCommunicator(std::optional< int > group=std::nullopt)
Splits and creates a local MPI communicator according to groupName.
static logging::Logger _log
contains precice-related utilities.
std::ostream & operator<<(std::ostream &out, const RangePreview< Iter > &rp)
Allows streaming of RangePreview objects.
static CommStatePtr world()
returns a commstate containing MPI_COMM_WORLD
void print(std::ostream &out) const
pretty printer for comms
int size() const
Returns size of comm.
Rank rank() const
Returns the current rank in comm.
static CommStatePtr fromComm(Communicator comm)
returns the commstate representing comm
static CommStatePtr self()
returns the commstate representing MPI_COMM_SELF
static CommStatePtr null()
returns an blank commstate representing MPI_COMM_NULL
CommState & operator=(const CommState &)=delete
bool _owning
Whether this state owns the communicator and has to free it.
bool isNull() const
Returns weather the comm is NULL.
Communicator comm
The native communicator that represents this state.
CommStatePtr parent
A shared pointer to the parent CommState.
static CommStatePtr fromExtern(Communicator comm)
~CommState() noexcept
Frees the communicator if allowed.