preCICE v3.1.1
Loading...
Searching...
No Matches
Parallel.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <iosfwd>
4#include <memory>
5#include <optional>
6#include <string>
7#include <vector>
8
9#include "logging/Logger.hpp"
11
12#ifndef PRECICE_NO_MPI
13#include <mpi.h>
14#endif // not PRECICE_NO_MPI
15
16namespace precice {
17namespace logging {
18class Logger;
19} // namespace logging
20
21namespace utils {
22
24class Parallel {
25public:
26#ifndef PRECICE_NO_MPI
28#else
30#define MPI_COMM_NULL nullptr
31#endif
32
33 struct CommState;
34
36
41 struct CommState {
43 Communicator comm = MPI_COMM_NULL;
44
47
49 bool _owning = true;
50
53 CommState() = default;
54
55 CommState(const CommState &) = delete;
56 CommState &operator=(const CommState &) = delete;
57
58 CommState(CommState &&) noexcept;
59 CommState &operator=(CommState &&) noexcept;
60
62 ~CommState() noexcept;
63
65
68
70 static CommStatePtr world();
71
73 static CommStatePtr null();
74
76 static CommStatePtr self();
77
80
88
90
93
95 Rank rank() const;
96
98 int size() const;
99
101 bool isNull() const;
102
104
107
111 void synchronize() const;
112
114 void print(std::ostream &out) const;
115
117 };
118
121
123 static bool isMPIInitialized();
124
141 static void initializeOrDetectMPI(std::optional<Communicator> userProvided = std::nullopt);
142
151 static void finalizeOrCleanupMPI();
152
160 static void initializeTestingMPI(
161 int * argc,
162 char ***argv);
163
165 static void finalizeTestingMPI();
166
168
171
179 static void splitCommunicator(std::optional<int> group = std::nullopt);
180
189 static void resetCommState();
190
195 static void resetManagedMPI();
196
201 static void popState();
203
206
210 static Rank getProcessRank();
211
213 static CommStatePtr current();
214
216
217private:
218 static logging::Logger _log;
219
221
224
228 Provided,
229 Managed,
230 Unmanaged,
231 Testing
232 };
233
235
245 static void pushState(CommStatePtr newState);
246};
247
250
251} // namespace utils
252} // namespace precice
253
254template <>
255struct fmt::formatter<precice::utils::Parallel::CommState> : ostream_formatter {
256};
std::ostream & out
std::nullptr_t MPI_Comm
Definition MPI_Mock.hpp:9
Utility class for managing MPI operations.
Definition Parallel.hpp:24
static bool isMPIInitialized()
Return true if MPI is initialized.
Definition Parallel.cpp:179
static InitializationState _initState
Definition Parallel.hpp:234
static void finalizeTestingMPI()
Unconditionally finalizes MPI environment.
Definition Parallel.cpp:261
static void resetCommState()
Definition Parallel.cpp:156
static void finalizeOrCleanupMPI()
Finalized a managed MPI environment or cleans up after an non-managed session.
Definition Parallel.cpp:230
static CommStatePtr current()
Returns an owning pointer to the current CommState.
Definition Parallel.cpp:147
static Rank getProcessRank()
Definition Parallel.cpp:299
static void resetManagedMPI()
Definition Parallel.cpp:161
static void pushState(CommStatePtr newState)
Definition Parallel.cpp:167
InitializationState
Kind of initialization that took place.
Definition Parallel.hpp:226
@ Unmanaged
preCICE manages the lifetime of the MPI environment
@ Managed
Communicator was provided by the user.
@ Testing
preCICE was initialized in an existing MPI environment
static CommStatePtr _currentState
Definition Parallel.hpp:220
static bool _mpiInitializedByPrecice
Flag to saveguard against reinitializing MPI, which is forbidden.
Definition Parallel.hpp:223
static void initializeOrDetectMPI(std::optional< Communicator > userProvided=std::nullopt)
Definition Parallel.cpp:190
static void initializeTestingMPI(int *argc, char ***argv)
Definition Parallel.cpp:249
static void splitCommunicator(std::optional< int > group=std::nullopt)
Splits and creates a local MPI communicator according to groupName.
Definition Parallel.cpp:272
static logging::Logger _log
Definition Parallel.hpp:218
std::ostream & operator<<(std::ostream &out, const RangePreview< Iter > &rp)
Allows streaming of RangePreview objects.
Main namespace of the precice library.
int Rank
Definition Types.hpp:37
STL namespace.
static CommStatePtr world()
returns a commstate containing MPI_COMM_WORLD
Definition Parallel.cpp:89
void print(std::ostream &out) const
pretty printer for comms
Definition Parallel.cpp:127
int size() const
Returns size of comm.
Definition Parallel.cpp:60
Rank rank() const
Returns the current rank in comm.
Definition Parallel.cpp:51
static CommStatePtr fromComm(Communicator comm)
returns the commstate representing comm
Definition Parallel.cpp:112
static CommStatePtr self()
returns the commstate representing MPI_COMM_SELF
Definition Parallel.cpp:103
static CommStatePtr null()
returns an blank commstate representing MPI_COMM_NULL
Definition Parallel.cpp:98
CommState(const CommState &)=delete
CommState & operator=(const CommState &)=delete
bool _owning
Whether this state owns the communicator and has to free it.
Definition Parallel.hpp:49
bool isNull() const
Returns weather the comm is NULL.
Definition Parallel.cpp:80
Communicator comm
The native communicator that represents this state.
Definition Parallel.hpp:43
CommStatePtr parent
A shared pointer to the parent CommState.
Definition Parallel.hpp:46
static CommStatePtr fromExtern(Communicator comm)
Definition Parallel.cpp:119