preCICE v3.2.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TestContext.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <set>
4#include <stdexcept>
5#include <string>
6#include <type_traits>
7#include <utility>
8#include <vector>
9
10#include "m2n/SharedPointer.hpp"
12#include "utils/Parallel.hpp"
13
14namespace precice::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
57 constexpr explicit ParticipantState(std::string_view n)
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 constexpr 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,
105 Ginkgo,
106};
107
109struct TestSetup {
110
118 template <class... T>
119 TestSetup(T... args)
120 {
121 (handleOption(args), ...);
122 }
123
127 void handleOption(Ranks ranks);
128 void handleOption(testing::Require requirement);
130
132 int totalRanks() const;
133
135 bool petsc = false;
136
138 bool events = false;
139
141 bool ginkgo = false;
142
145};
146
151enum struct ConnectionType {
154};
155
179
194public:
196
199
202
204 int size = 1;
205
207 bool invalid = false;
208
218 TestContext(TestSetup setup);
219
223 ~TestContext() noexcept;
224
233 std::string config() const;
234
241 std::string prefix(const std::string &filename) const;
242
244 bool hasSize(int size) const;
245
247 bool isNamed(const std::string &name) const;
248
250 bool isRank(Rank rank) const;
251
253 auto comm()
254 {
255 return &(_contextComm->comm);
256 }
257
261 bool isPrimary() const;
262
272 m2n::PtrM2N connectPrimaryRanks(const std::string &acceptor, const std::string &connector, const ConnectionOptions &options = ConnectionOptions{}) const;
273
275 std::string describe() const;
276
277private:
279
281 bool _initIntraComm = false;
282
285
288
293 void setContextFrom(const ParticipantState &p);
294
297
299 void initialize(const Participants &participants);
300
304 void initializeMPI(const Participants &participants);
305
307 void initializeIntraComm();
308
310 void initializePetsc();
311
313 void initializeEvents();
314
316 void initializeGinkgo();
318};
319
320} // namespace precice::testing
std::string prefix
std::string name
bool isRank(Rank rank) const
Check whether this context has a given rank inside the Participant.
void initialize(const Participants &participants)
Main entrypoint.
bool _initIntraComm
whether this context needs to initialize the intracomm
void initializePetsc()
Initialize PETSc if required.
bool invalid
whether this context is valid or not
m2n::PtrM2N connectPrimaryRanks(const std::string &acceptor, const std::string &connector, const ConnectionOptions &options=ConnectionOptions{}) const
std::set< std::string > _names
contains the name of every known Participant
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 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.
void initializeGinkgo()
Initialize Ginkgo if required.
void initializeEvents()
Initialize Events if required.
Rank rank
the rank of the current participant
auto comm()
Returns a pointer to the MPI communicator of this context.
std::string name
the name of the current participant
void setContextFrom(const ParticipantState &p)
contains the testing framework.
Definition helper.hpp:9
@ PETSc
Require to initialize PETSc. This implies the initialization of Events.
@ Events
Require to initialize Event.
@ Ginkgo
Ginkgo initialization.
PETSc related utilities.
Definition Petsc.cpp:81
int Rank
Definition Types.hpp:37
STL namespace.
Represents a ParticipantState in a test.
int size
the amount of ranks this participant runs on
constexpr ParticipantState & setupIntraComm()
constexpr ParticipantState(std::string_view n)
Constructs a serial participant with a given name.
bool initIntraComm
whether to initialize an intra-participant communication for this participant
constexpr ParticipantState & operator()(Ranks rsize)
std::string_view name
the name of the participant
Contains the setup description of a test including participants and requirements.
int totalRanks() const
total amount of ranks required by this setup
std::vector< ParticipantState > participants
All known participants.
bool events
whether to initialize events
bool ginkgo
whether to initialize Ginkgo (the device)
void handleOption(ParticipantState participants)