preCICE v3.1.2
Loading...
Searching...
No Matches
TestContext.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <stdexcept>
4#include <string>
5#include <type_traits>
6#include <utility>
7#include <vector>
8
11#include "utils/Parallel.hpp"
12
13namespace precice {
14namespace testing {
15
19struct Ranks {
20 int value;
21};
22
29inline constexpr Ranks operator""_ranks(unsigned long long value)
30{
31 return (value <= 1) ? throw std::runtime_error{"Cannot create a single rank with _ranks()! Use _rank() instead!"} : Ranks{static_cast<int>(value)};
32}
33
40inline constexpr Ranks operator""_rank(unsigned long long value)
41{
42 return (value == 1) ? Ranks{1} : throw std::runtime_error{"Cannot create multiple ranks with _rank()! Use _ranks() instead!"};
43}
44
49
51 int size = 1;
52
54 bool initIntraComm = false;
55
58 : name(std::move(n)){};
59
69 {
70 size = rsize.value;
71 return *this;
72 }
73
79 {
80 initIntraComm = true;
81 return *this;
82 }
83};
84
86inline ParticipantState operator""_on(const char *name, std::size_t)
87{
88 return ParticipantState{name};
89}
90
91static_assert(std::is_same<ParticipantState &, decltype(""_on(1_rank))>::value, "");
92static_assert(std::is_same<ParticipantState &, decltype(""_on(2_ranks))>::value, "");
93
99enum class Require {
101 PETSc,
103 Events,
104};
105
110enum struct ConnectionType {
113};
114
138
153public:
155
158
161
163 int size = 1;
164
166 bool invalid = false;
167
170
172 TestContext() = default;
173
182 template <class... T>
184 : _simple(true)
185 {
186 Participants participants{"Serial"_on(ranks)};
187 initialize(participants);
188 }
189
199 template <class... T>
200 TestContext(Ranks ranks, T... args)
201 : _simple(true)
202 {
203 Participants participants{"Serial"_on(ranks)};
204 handleOptions(participants, args...);
205 initialize(participants);
206 }
207
214 template <class... T>
215 TestContext(T... args)
216 {
217 Participants participants;
218 handleOptions(participants, args...);
219 initialize(participants);
220 }
221
223
227 ~TestContext() noexcept;
228
237 std::string config() const;
238
245 std::string prefix(const std::string &filename) const;
246
248 bool hasSize(int size) const;
249
251 bool isNamed(const std::string &name) const;
252
254 bool isRank(Rank rank) const;
255
257 auto comm()
258 {
259 return &(_contextComm->comm);
260 }
261
265 bool isPrimary() const;
266
276 m2n::PtrM2N connectPrimaryRanks(const std::string &acceptor, const std::string &requestor, const ConnectionOptions &options = ConnectionOptions{}) const;
277
279 std::string describe() const;
280
281private:
283 bool _petsc = false;
284
286 bool _events = false;
287
289 bool _simple = false;
290
292 bool _initIntraComm = false;
293
296
299
302 void handleOption(Participants &participants, ParticipantState participant);
303 void handleOption(Participants &participants, testing::Require requirement);
304
305 template <class LastOption>
306 void handleOptions(Participants &participants, LastOption &last)
307 {
308 handleOption(participants, last);
309 }
310
311 template <class NextOption, class... Rest>
312 void handleOptions(Participants &participants, NextOption &next, Rest &... rest)
313 {
314 handleOption(participants, next);
315 handleOptions(participants, rest...);
316 }
318
323 void setContextFrom(const ParticipantState &p);
324
327
329 void initialize(const Participants &participants);
330
334 void initializeMPI(const Participants &participants);
335
337 void initializeIntraComm();
338
340 void initializePetsc();
341
343 void initializeEvents();
344
346};
347
348} // namespace testing
349} // namespace precice
std::string prefix
std::string name
void handleOption(Participants &participants, ParticipantState participant)
bool _petsc
whether to initialize PETSc
bool isRank(Rank rank) const
Check whether this context has a given rank inside the Participant.
void handleOptions(Participants &participants, NextOption &next, Rest &... rest)
void initialize(const Participants &participants)
Main entrypoint.
TestContext(Ranks ranks, T... args)
bool _initIntraComm
whether to initialize an intra-participant connection
bool _simple
whether this Context was created with a Ranks constructor
m2n::PtrM2N connectPrimaryRanks(const std::string &acceptor, const std::string &requestor, const ConnectionOptions &options=ConnectionOptions{}) const
void initializePetsc()
Initialize PETSc if required.
bool invalid
whether this context is valid or not
std::vector< ParticipantState > Participants
void initializeMPI(const Participants &participants)
bool isNamed(const std::string &name) const
Check whether this context has a given name.
int size
the size of the Communicator of the current participant
std::string describe() const
Provides a user- and log-friendly description of the current context.
bool _events
whether to initialize events
bool hasSize(int size) const
Check whether this context has a given size.
utils::Parallel::CommStatePtr _contextComm
the MPI communicator of the context
void initializeIntraComm()
Initialize the intra-participant communication connection if requested.
std::vector< std::string > _names
contains the name of every known Participant
void initializeEvents()
Initialize Events if required.
Rank rank
the rank of the current participant
void handleOptions(Participants &participants, LastOption &last)
auto comm()
Returns a pointer to the MPI communicator of this context.
TestContext()=default
Create a context representing an unnamed serial Participant.
std::string name
the name of the current participant
void setContextFrom(const ParticipantState &p)
std::string config() const
@ PETSc
Require to initialize PETSc. This implies the initialization of Events.
@ Events
Require to initialize Event.
Main namespace of the precice library.
int Rank
Definition Types.hpp:37
STL namespace.
Represents a ParticipantState in a test.
int size
the amount of ranks this participant runs on
std::string name
the name of the participant
ParticipantState & operator()(Ranks rsize)
bool initIntraComm
whether to initialize an intra-participant communication for this participant
ParticipantState(std::string n)
Constructs a serial participant with a given name.
ParticipantState & setupIntraComm()