preCICE v3.1.1
Loading...
Searching...
No Matches
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
12{
13 PRECICE_TEST(""_on(1_rank).setupIntraComm());
14
15 BOOST_TEST(!utils::IntraComm::isPrimary());
16 BOOST_TEST(!utils::IntraComm::isSecondary());
17 BOOST_TEST(!utils::IntraComm::isParallel());
18
19 BOOST_TEST(utils::IntraComm::getRank() == context.rank);
20 BOOST_TEST(utils::IntraComm::getSize() == context.size);
21
22 { // ranks
23 auto ranksRange = utils::IntraComm::allRanks();
24 std::vector<int> ranks(ranksRange.begin(), ranksRange.end());
25 BOOST_TEST(ranks.size() == 1);
26 BOOST_TEST(ranks.front() == 0);
27 }
28
29 { // secondary ranks
30 auto secondaryRanks = utils::IntraComm::allSecondaryRanks();
31 BOOST_TEST((secondaryRanks.begin() == secondaryRanks.end()));
32 }
33
34 BOOST_TEST(!static_cast<bool>(utils::IntraComm::getCommunication()));
35}
36
37BOOST_AUTO_TEST_CASE(ParallelConfig)
38{
39 PRECICE_TEST(""_on(3_ranks).setupIntraComm());
40
41 BOOST_TEST(utils::IntraComm::isPrimary() == context.isPrimary());
42 BOOST_TEST(utils::IntraComm::isSecondary() != context.isPrimary());
43 BOOST_TEST(utils::IntraComm::isParallel());
44
45 BOOST_TEST(utils::IntraComm::getRank() == context.rank);
46 BOOST_TEST(utils::IntraComm::getSize() == context.size);
47
48 { // ranks
49 auto ranksRange = utils::IntraComm::allRanks();
50 std::vector<int> ranks(ranksRange.begin(), ranksRange.end());
51 std::vector<int> expected{0, 1, 2};
52 BOOST_TEST(ranks == expected, boost::test_tools::per_element());
53 }
54
55 { // secondary ranks
56 auto secondaryRanks = utils::IntraComm::allSecondaryRanks();
57 std::vector<int> ranks(secondaryRanks.begin(), secondaryRanks.end());
58 std::vector<int> expected{1, 2};
59 BOOST_TEST(ranks == expected, boost::test_tools::per_element());
60 }
61
62 BOOST_TEST(static_cast<bool>(utils::IntraComm::getCommunication()));
63}
64
65BOOST_AUTO_TEST_CASE(Parallell2norm)
66{
67 PRECICE_TEST(""_on(3_ranks).setupIntraComm());
68
69 const double norm = 16.881943016134134;
70 if (context.isPrimary()) {
71 Eigen::VectorXd v(3);
72 v << 1, 2, 3;
73 BOOST_TEST(utils::IntraComm::l2norm(v) == norm);
74 }
75 if (context.isRank(1)) {
76 Eigen::VectorXd v(2);
77 v << 4, 5;
78 BOOST_TEST(utils::IntraComm::l2norm(v) == norm);
79 }
80 if (context.isRank(2)) {
81 Eigen::VectorXd v(4);
82 v << 6, 7, 8, 9;
83 BOOST_TEST(utils::IntraComm::l2norm(v) == norm);
84 }
85}
86
88{
89 PRECICE_TEST(""_on(1_rank).setupIntraComm());
90
91 const double norm = 16.881943016134134;
92 Eigen::VectorXd v(9);
93 v << 1, 2, 3, 4, 5, 6, 7, 8, 9;
94 BOOST_TEST(utils::IntraComm::l2norm(v) == norm);
95}
96
98{
99 PRECICE_TEST(""_on(3_ranks).setupIntraComm());
100
101 if (context.isPrimary()) {
102 Eigen::VectorXd u(3), v(3);
103 u << 1, 2, 3;
104 v << 9, 8, 7;
105 BOOST_TEST(utils::IntraComm::dot(u, v) == 165);
106 }
107 if (context.isRank(1)) {
108 Eigen::VectorXd u(2), v(2);
109 u << 4, 5;
110 v << 6, 5;
111 BOOST_TEST(utils::IntraComm::dot(u, v) == 165);
112 }
113 if (context.isRank(2)) {
114 Eigen::VectorXd u(4), v(4);
115 u << 6, 7, 8, 9;
116 v << 4, 3, 2, 1;
117 BOOST_TEST(utils::IntraComm::dot(u, v) == 165);
118 }
119}
120
122{
123 PRECICE_TEST(""_on(1_rank).setupIntraComm());
124
125 Eigen::VectorXd u(9), v(9);
126 u << 1, 2, 3, 4, 5, 6, 7, 8, 9;
127 v << 9, 8, 7, 6, 5, 4, 3, 2, 1;
128 BOOST_TEST(utils::IntraComm::dot(u, v) == 165);
129}
130
131BOOST_AUTO_TEST_CASE(ParallelReduceSum)
132{
133 PRECICE_TEST(""_on(3_ranks).setupIntraComm());
134
135 if (context.isPrimary()) {
136 {
137 std::vector<double> in{1, 2, 3}, out{-1, -1, -1};
139 BOOST_TEST(out == (std::vector<double>{12, 15, 18}), boost::test_tools::per_element());
140 }
141 {
142 int in = 1, out = -1;
144 BOOST_TEST(out == 9);
145 }
146 {
147 double in = 1.1, out = -1;
149 BOOST_TEST(out == 9.9);
150 }
151 }
152 if (context.isRank(1)) {
153 {
154 std::vector<double> in{4, 5, 6}, out{-1, -1, -1};
155 auto expected = out;
157 BOOST_TEST(testing::equals(out, expected), boost::test_tools::per_element());
158 }
159 {
160 int in = 3, out = -1;
162 BOOST_TEST(out == -1);
163 }
164 {
165 double in = 3.3, out = -1;
167 BOOST_TEST(out == -1);
168 }
169 }
170 if (context.isRank(2)) {
171 {
172 std::vector<double> in{7, 8, 9}, out{-1, -1, -1};
173 auto expected = out;
175 BOOST_TEST(testing::equals(out, expected), boost::test_tools::per_element());
176 }
177 {
178 int in = 5, out = -1;
180 BOOST_TEST(out == -1);
181 }
182 {
183 double in = 5.5, out = -1;
185 BOOST_TEST(out == -1);
186 }
187 }
188}
189
190BOOST_AUTO_TEST_CASE(SerialReduceSum)
191{
192 PRECICE_TEST(""_on(1_rank).setupIntraComm());
193
194 {
195 std::vector<double> in{1, 2, 3}, out{-1, -1, -1};
197 BOOST_TEST(testing::equals(out, in), boost::test_tools::per_element());
198 }
199 {
200 int in = 1, out = -1;
202 BOOST_TEST(out == in);
203 }
204 {
205 double in = 1.1, out = -1;
207 BOOST_TEST(out == in);
208 }
209}
210
211BOOST_AUTO_TEST_CASE(ParallelAllReduceSum)
212{
213 PRECICE_TEST(""_on(3_ranks).setupIntraComm());
214
215 if (context.isPrimary()) {
216 {
217 std::vector<double> in{1, 2, 3}, out{-1, -1, -1};
219 BOOST_TEST(out == (std::vector<double>{12, 15, 18}), boost::test_tools::per_element());
220 }
221 {
222 int in = 1, out = -1;
224 BOOST_TEST(out == 9);
225 }
226 {
227 double in = 1.1, out = -1;
229 BOOST_TEST(out == 9.9);
230 }
231 }
232 if (context.isRank(1)) {
233 {
234 std::vector<double> in{4, 5, 6}, out{-1, -1, -1};
236 BOOST_TEST(out == (std::vector<double>{12, 15, 18}), boost::test_tools::per_element());
237 }
238 {
239 int in = 3, out = -1;
241 BOOST_TEST(out == 9);
242 }
243 {
244 double in = 3.3, out = -1;
246 BOOST_TEST(out == 9.9);
247 }
248 }
249 if (context.isRank(2)) {
250 {
251 std::vector<double> in{7, 8, 9}, out{-1, -1, -1};
253 BOOST_TEST(out == (std::vector<double>{12, 15, 18}), boost::test_tools::per_element());
254 }
255 {
256 int in = 5, out = -1;
258 BOOST_TEST(out == 9);
259 }
260 {
261 double in = 5.5, out = -1;
263 BOOST_TEST(out == 9.9);
264 }
265 }
266}
267
268BOOST_AUTO_TEST_CASE(SerialAllReduceSum)
269{
270 PRECICE_TEST(""_on(1_rank).setupIntraComm());
271
272 {
273 std::vector<double> in{1, 2, 3}, out{-1, -1, -1};
275 BOOST_TEST(testing::equals(out, in), boost::test_tools::per_element());
276 }
277 {
278 int in = 1, out = -1;
280 BOOST_TEST(out == in);
281 }
282 {
283 double in = 1.1, out = -1;
285 BOOST_TEST(out == in);
286 }
287}
288
289BOOST_AUTO_TEST_CASE(ParallelBroadcast)
290{
291 PRECICE_TEST(""_on(3_ranks).setupIntraComm());
292
293 if (context.isPrimary()) {
294 {
295 std::vector<double> in{1, 2, 3};
297 BOOST_TEST(in == (std::vector<double>{1, 2, 3}), boost::test_tools::per_element());
298 }
299 {
300 bool in = true;
302 BOOST_TEST(in);
303 }
304 {
305 double in = 9.9;
307 BOOST_TEST(in == 9.9);
308 }
309 } else {
310 {
311 std::vector<double> out{-1, -1, -1};
313 BOOST_TEST(out == (std::vector<double>{1, 2, 3}), boost::test_tools::per_element());
314 }
315 {
316 bool out = false;
318 BOOST_TEST(out);
319 }
320 {
321 double out = 9.9;
323 BOOST_TEST(out == 9.9);
324 }
325 }
326}
327
328BOOST_AUTO_TEST_CASE(SerialBroadcast)
329{
330 PRECICE_TEST(""_on(1_rank).setupIntraComm());
331 {
332 std::vector<double> in{1, 2, 3};
334 BOOST_TEST(in == (std::vector<double>{1, 2, 3}), boost::test_tools::per_element());
335 }
336 {
337 bool in = true;
339 BOOST_TEST(in);
340 }
341 {
342 double in = 9.9;
344 BOOST_TEST(in == 9.9);
345 }
346}
347
349
std::ostream & out
BOOST_AUTO_TEST_CASE(SerialConfig)
BOOST_AUTO_TEST_SUITE(PreProcess)
BOOST_AUTO_TEST_SUITE_END()
#define PRECICE_TEST(...)
Definition Testing.hpp:27
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:65
Main namespace of the precice library.
T size(T... args)