preCICE v3.2.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
IntraCommTest.cpp
Go to the documentation of this file.
1#include <boost/test/tools/context.hpp>
2#include "testing/Testing.hpp"
3#include "utils/IntraComm.hpp"
4
5using namespace precice;
6
7BOOST_AUTO_TEST_SUITE(UtilsTests)
8
10
11PRECICE_TEST_SETUP(""_on(1_rank).setupIntraComm())
12BOOST_AUTO_TEST_CASE(SerialConfig)
13{
15
16 BOOST_TEST(!utils::IntraComm::isPrimary());
17 BOOST_TEST(!utils::IntraComm::isSecondary());
18 BOOST_TEST(!utils::IntraComm::isParallel());
19
20 BOOST_TEST(utils::IntraComm::getRank() == context.rank);
21 BOOST_TEST(utils::IntraComm::getSize() == context.size);
22
23 { // ranks
24 auto ranksRange = utils::IntraComm::allRanks();
25 std::vector<int> ranks(ranksRange.begin(), ranksRange.end());
26 BOOST_TEST(ranks.size() == 1);
27 BOOST_TEST(ranks.front() == 0);
28 }
29
30 { // secondary ranks
31 auto secondaryRanks = utils::IntraComm::allSecondaryRanks();
32 BOOST_TEST((secondaryRanks.begin() == secondaryRanks.end()));
33 }
34
35 BOOST_TEST(!static_cast<bool>(utils::IntraComm::getCommunication()));
36}
37
38PRECICE_TEST_SETUP(""_on(3_ranks).setupIntraComm())
39BOOST_AUTO_TEST_CASE(ParallelConfig)
40{
42
43 BOOST_TEST(utils::IntraComm::isPrimary() == context.isPrimary());
44 BOOST_TEST(utils::IntraComm::isSecondary() != context.isPrimary());
45 BOOST_TEST(utils::IntraComm::isParallel());
46
47 BOOST_TEST(utils::IntraComm::getRank() == context.rank);
48 BOOST_TEST(utils::IntraComm::getSize() == context.size);
49
50 { // ranks
51 auto ranksRange = utils::IntraComm::allRanks();
52 std::vector<int> ranks(ranksRange.begin(), ranksRange.end());
53 std::vector<int> expected{0, 1, 2};
54 BOOST_TEST(ranks == expected, boost::test_tools::per_element());
55 }
56
57 { // secondary ranks
58 auto secondaryRanks = utils::IntraComm::allSecondaryRanks();
59 std::vector<int> ranks(secondaryRanks.begin(), secondaryRanks.end());
60 std::vector<int> expected{1, 2};
61 BOOST_TEST(ranks == expected, boost::test_tools::per_element());
62 }
63
64 BOOST_TEST(static_cast<bool>(utils::IntraComm::getCommunication()));
65}
66
67PRECICE_TEST_SETUP(""_on(3_ranks).setupIntraComm())
68BOOST_AUTO_TEST_CASE(Parallell2norm)
69{
71
72 const double norm = 16.881943016134134;
73 if (context.isPrimary()) {
74 Eigen::VectorXd v(3);
75 v << 1, 2, 3;
76 BOOST_TEST(utils::IntraComm::l2norm(v) == norm);
77 }
78 if (context.isRank(1)) {
79 Eigen::VectorXd v(2);
80 v << 4, 5;
81 BOOST_TEST(utils::IntraComm::l2norm(v) == norm);
82 }
83 if (context.isRank(2)) {
84 Eigen::VectorXd v(4);
85 v << 6, 7, 8, 9;
86 BOOST_TEST(utils::IntraComm::l2norm(v) == norm);
87 }
88}
89
90PRECICE_TEST_SETUP(""_on(1_rank).setupIntraComm())
91BOOST_AUTO_TEST_CASE(Seriall2norm)
92{
94
95 const double norm = 16.881943016134134;
96 Eigen::VectorXd v(9);
97 v << 1, 2, 3, 4, 5, 6, 7, 8, 9;
98 BOOST_TEST(utils::IntraComm::l2norm(v) == norm);
99}
100
101PRECICE_TEST_SETUP(""_on(3_ranks).setupIntraComm())
102BOOST_AUTO_TEST_CASE(Paralleldot)
103{
104 PRECICE_TEST();
105
106 if (context.isPrimary()) {
107 Eigen::VectorXd u(3), v(3);
108 u << 1, 2, 3;
109 v << 9, 8, 7;
110 BOOST_TEST(utils::IntraComm::dot(u, v) == 165);
111 }
112 if (context.isRank(1)) {
113 Eigen::VectorXd u(2), v(2);
114 u << 4, 5;
115 v << 6, 5;
116 BOOST_TEST(utils::IntraComm::dot(u, v) == 165);
117 }
118 if (context.isRank(2)) {
119 Eigen::VectorXd u(4), v(4);
120 u << 6, 7, 8, 9;
121 v << 4, 3, 2, 1;
122 BOOST_TEST(utils::IntraComm::dot(u, v) == 165);
123 }
124}
125
126PRECICE_TEST_SETUP(""_on(1_rank).setupIntraComm())
127BOOST_AUTO_TEST_CASE(Serialdot)
128{
129 PRECICE_TEST();
130
131 Eigen::VectorXd u(9), v(9);
132 u << 1, 2, 3, 4, 5, 6, 7, 8, 9;
133 v << 9, 8, 7, 6, 5, 4, 3, 2, 1;
134 BOOST_TEST(utils::IntraComm::dot(u, v) == 165);
135}
136
137PRECICE_TEST_SETUP(""_on(3_ranks).setupIntraComm())
138BOOST_AUTO_TEST_CASE(ParallelReduceSum)
139{
140 PRECICE_TEST();
141
142 if (context.isPrimary()) {
143 {
144 std::vector<double> in{1, 2, 3}, out{-1, -1, -1};
146 BOOST_TEST(out == (std::vector<double>{12, 15, 18}), boost::test_tools::per_element());
147 }
148 {
149 int in = 1, out = -1;
151 BOOST_TEST(out == 9);
152 }
153 {
154 double in = 1.1, out = -1;
156 BOOST_TEST(out == 9.9);
157 }
158 }
159 if (context.isRank(1)) {
160 {
161 std::vector<double> in{4, 5, 6}, out{-1, -1, -1};
162 auto expected = out;
164 BOOST_TEST(testing::equals(out, expected), boost::test_tools::per_element());
165 }
166 {
167 int in = 3, out = -1;
169 BOOST_TEST(out == -1);
170 }
171 {
172 double in = 3.3, out = -1;
174 BOOST_TEST(out == -1);
175 }
176 }
177 if (context.isRank(2)) {
178 {
179 std::vector<double> in{7, 8, 9}, out{-1, -1, -1};
180 auto expected = out;
182 BOOST_TEST(testing::equals(out, expected), boost::test_tools::per_element());
183 }
184 {
185 int in = 5, out = -1;
187 BOOST_TEST(out == -1);
188 }
189 {
190 double in = 5.5, out = -1;
192 BOOST_TEST(out == -1);
193 }
194 }
195}
196
197PRECICE_TEST_SETUP(""_on(1_rank).setupIntraComm())
198BOOST_AUTO_TEST_CASE(SerialReduceSum)
199{
200 PRECICE_TEST();
201
202 {
203 std::vector<double> in{1, 2, 3}, out{-1, -1, -1};
205 BOOST_TEST(testing::equals(out, in), boost::test_tools::per_element());
206 }
207 {
208 int in = 1, out = -1;
210 BOOST_TEST(out == in);
211 }
212 {
213 double in = 1.1, out = -1;
215 BOOST_TEST(out == in);
216 }
217}
218
219PRECICE_TEST_SETUP(""_on(3_ranks).setupIntraComm())
220BOOST_AUTO_TEST_CASE(ParallelAllReduceSum)
221{
222 PRECICE_TEST();
223
224 if (context.isPrimary()) {
225 {
226 std::vector<double> in{1, 2, 3}, out{-1, -1, -1};
228 BOOST_TEST(out == (std::vector<double>{12, 15, 18}), boost::test_tools::per_element());
229 }
230 {
231 int in = 1, out = -1;
233 BOOST_TEST(out == 9);
234 }
235 {
236 double in = 1.1, out = -1;
238 BOOST_TEST(out == 9.9);
239 }
240 }
241 if (context.isRank(1)) {
242 {
243 std::vector<double> in{4, 5, 6}, out{-1, -1, -1};
245 BOOST_TEST(out == (std::vector<double>{12, 15, 18}), boost::test_tools::per_element());
246 }
247 {
248 int in = 3, out = -1;
250 BOOST_TEST(out == 9);
251 }
252 {
253 double in = 3.3, out = -1;
255 BOOST_TEST(out == 9.9);
256 }
257 }
258 if (context.isRank(2)) {
259 {
260 std::vector<double> in{7, 8, 9}, out{-1, -1, -1};
262 BOOST_TEST(out == (std::vector<double>{12, 15, 18}), boost::test_tools::per_element());
263 }
264 {
265 int in = 5, out = -1;
267 BOOST_TEST(out == 9);
268 }
269 {
270 double in = 5.5, out = -1;
272 BOOST_TEST(out == 9.9);
273 }
274 }
275}
276
277PRECICE_TEST_SETUP(""_on(1_rank).setupIntraComm())
278BOOST_AUTO_TEST_CASE(SerialAllReduceSum)
279{
280 PRECICE_TEST();
281
282 {
283 std::vector<double> in{1, 2, 3}, out{-1, -1, -1};
285 BOOST_TEST(testing::equals(out, in), boost::test_tools::per_element());
286 }
287 {
288 int in = 1, out = -1;
290 BOOST_TEST(out == in);
291 }
292 {
293 double in = 1.1, out = -1;
295 BOOST_TEST(out == in);
296 }
297}
298
299PRECICE_TEST_SETUP(""_on(3_ranks).setupIntraComm())
300BOOST_AUTO_TEST_CASE(ParallelBroadcast)
301{
302 PRECICE_TEST();
303
304 if (context.isPrimary()) {
305 {
306 std::vector<double> in{1, 2, 3};
308 BOOST_TEST(in == (std::vector<double>{1, 2, 3}), boost::test_tools::per_element());
309 }
310 {
311 bool in = true;
313 BOOST_TEST(in);
314 }
315 {
316 double in = 9.9;
318 BOOST_TEST(in == 9.9);
319 }
320 } else {
321 {
322 std::vector<double> out{-1, -1, -1};
324 BOOST_TEST(out == (std::vector<double>{1, 2, 3}), boost::test_tools::per_element());
325 }
326 {
327 bool out = false;
329 BOOST_TEST(out);
330 }
331 {
332 double out = 9.9;
334 BOOST_TEST(out == 9.9);
335 }
336 }
337}
338
339PRECICE_TEST_SETUP(""_on(1_rank).setupIntraComm())
340BOOST_AUTO_TEST_CASE(SerialBroadcast)
341{
342 PRECICE_TEST();
343 {
344 std::vector<double> in{1, 2, 3};
346 BOOST_TEST(in == (std::vector<double>{1, 2, 3}), boost::test_tools::per_element());
347 }
348 {
349 bool in = true;
351 BOOST_TEST(in);
352 }
353 {
354 double in = 9.9;
356 BOOST_TEST(in == 9.9);
357 }
358}
359
361
BOOST_AUTO_TEST_CASE(testIQNIMVJPPWithSubsteps)
std::ostream & out
BOOST_AUTO_TEST_SUITE(PreProcess)
BOOST_AUTO_TEST_SUITE_END()
#define PRECICE_TEST()
Definition Testing.hpp:39
#define PRECICE_TEST_SETUP(...)
Creates and attaches a TestSetup to a Boost test case.
Definition Testing.hpp:29
static void allreduceSum(precice::span< const double > sendData, precice::span< double > rcvData)
static int getSize()
Number of ranks. This includes ranks from both participants, e.g. minimal size is 2.
Definition IntraComm.cpp:47
static double l2norm(const Eigen::VectorXd &vec)
The l2 norm of a vector is calculated on distributed data.
Definition IntraComm.cpp:67
static Rank getRank()
Current rank.
Definition IntraComm.cpp:42
static double dot(const Eigen::VectorXd &vec1, const Eigen::VectorXd &vec2)
static bool isPrimary()
True if this process is running the primary rank.
Definition IntraComm.cpp:52
static void broadcast(bool &value)
static auto allSecondaryRanks()
Returns an iterable range over salve ranks [1, _size)
Definition IntraComm.hpp:37
static auto allRanks()
Returns an iterable range over all ranks [0, _size)
Definition IntraComm.hpp:43
static bool isParallel()
True if this process is running in parallel.
Definition IntraComm.cpp:62
static bool isSecondary()
True if this process is running a secondary rank.
Definition IntraComm.cpp:57
static com::PtrCommunication & getCommunication()
Intra-participant communication.
Definition IntraComm.hpp:31
static void reduceSum(precice::span< const double > sendData, precice::span< double > rcvData)
T front(T... args)
boost::test_tools::predicate_result equals(const std::vector< float > &VectorA, const std::vector< float > &VectorB, float tolerance)
equals to be used in tests. Compares two std::vectors using a given tolerance. Prints both operands o...
Definition Testing.cpp:93
Main namespace of the precice library.
T size(T... args)