GCC Code Coverage Report


Directory: gridformat/
File: gridformat/vtk/parallel.hpp
Date: 2026-10-18 00:09:19
Exec Total Coverage
Lines: 150 156 96.2%
Functions: 58 74 78.4%
Branches: 154 254 60.6%

Line Branch Exec Source
1 // SPDX-FileCopyrightText: 2022-2023 Dennis Gläser <dennis.glaeser@iws.uni-stuttgart.de>
2 // SPDX-License-Identifier: MIT
3 /*!
4 * \file
5 * \ingroup VTK
6 * \brief Helper function for writing parallel VTK files
7 */
8 #ifndef GRIDFORMAT_VTK_PARALLEL_HPP_
9 #define GRIDFORMAT_VTK_PARALLEL_HPP_
10
11 #include <array>
12 #include <vector>
13 #include <string>
14 #include <utility>
15 #include <concepts>
16 #include <algorithm>
17 #include <iterator>
18 #include <numeric>
19 #include <tuple>
20 #include <cmath>
21
22 #include <gridformat/common/math.hpp>
23 #include <gridformat/common/exceptions.hpp>
24 #include <gridformat/common/concepts.hpp>
25 #include <gridformat/common/ranges.hpp>
26 #include <gridformat/common/field.hpp>
27 #include <gridformat/grid/grid.hpp>
28
29 #include <gridformat/parallel/communication.hpp>
30 #include <gridformat/parallel/helpers.hpp>
31
32 #include <gridformat/xml/element.hpp>
33 #include <gridformat/vtk/attributes.hpp>
34
35 namespace GridFormat::PVTK {
36
37 //! Return the piece filename (w/o extension) for the given rank
38 12754 inline std::string piece_basefilename(const std::string& par_filename, int rank) {
39
1/2
✓ Branch 2 taken 12754 times.
✗ Branch 3 not taken.
12754 const std::string base_name = par_filename.substr(0, par_filename.find_last_of("."));
40
2/4
✓ Branch 2 taken 12754 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 12754 times.
✗ Branch 6 not taken.
25508 return base_name + "-" + std::to_string(rank);
41 12754 }
42
43 //! Helper to add a PDataArray child to an xml element
44 template<typename Encoder, typename DataFormat>
45 class PDataArrayHelper {
46 public:
47 8484 PDataArrayHelper(const Encoder& e,
48 const DataFormat& df,
49 XMLElement& element)
50 8484 : _encoder(e)
51 8484 , _data_format(df)
52 8484 , _element(element)
53 8484 {}
54
55 46928 void add(const std::string& name, const Field& field) {
56 // vtk always uses 3d, this assumes that the field
57 // is transformed accordingly in the piece writers
58 static constexpr std::size_t vtk_space_dim = 3;
59
1/2
✓ Branch 1 taken 23464 times.
✗ Branch 2 not taken.
46928 const auto layout = field.layout();
60
2/4
✓ Branch 1 taken 23464 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 23464 times.
✗ Branch 5 not taken.
46928 std::size_t ncomps = std::pow(vtk_space_dim, field.layout().dimension() - 1);
61
62 // leave higher-dimensional vectors untouched
63
8/10
✓ Branch 1 taken 23464 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 7910 times.
✓ Branch 4 taken 15554 times.
✓ Branch 6 taken 7910 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 144 times.
✓ Branch 9 taken 7766 times.
✓ Branch 10 taken 144 times.
✓ Branch 11 taken 23320 times.
46928 if (layout.dimension() == 2 && layout.extent(1) > vtk_space_dim)
64
1/2
✓ Branch 1 taken 144 times.
✗ Branch 2 not taken.
288 ncomps = layout.extent(1);
65
66
2/4
✓ Branch 1 taken 23464 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 23464 times.
✗ Branch 5 not taken.
93856 XMLElement& arr = _element.add_child("PDataArray");
67
2/4
✓ Branch 1 taken 23464 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 23464 times.
✗ Branch 5 not taken.
93856 arr.set_attribute("Name", name);
68
4/8
✓ Branch 1 taken 23464 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 23464 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 23464 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 23464 times.
✗ Branch 11 not taken.
140784 arr.set_attribute("type", VTK::attribute_name(field.precision()));
69
3/6
✓ Branch 1 taken 23464 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 23464 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 23464 times.
✗ Branch 8 not taken.
140784 arr.set_attribute("format", VTK::data_format_name(_encoder, _data_format));
70
2/4
✓ Branch 1 taken 23464 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 23464 times.
✗ Branch 5 not taken.
93856 arr.set_attribute("NumberOfComponents", ncomps);
71 46928 }
72
73 private:
74 const Encoder& _encoder;
75 const DataFormat& _data_format;
76 XMLElement& _element;
77 };
78
79 template<std::size_t dim>
80 class StructuredGridMapper {
81 public:
82 using Location = std::array<std::size_t, dim>;
83
84 4240 explicit StructuredGridMapper(std::vector<Location>&& map)
85 4240 : _map{std::move(map)}
86 4240 {}
87
88 35163 const auto& location(std::integral auto rank) const {
89 35163 return _map[rank];
90 }
91
92 35163 std::ranges::view auto ranks_below(const Location& loc, unsigned direction) const {
93 return std::views::filter(
94
1/2
✓ Branch 1 taken 17789 times.
✗ Branch 2 not taken.
35163 std::views::iota(std::size_t{0}, _map.size()),
95 128266 [&, loc=loc, direction=direction] (const std::size_t rank) {
96 58796 const auto& rank_loc = _map[rank];
97
4/4
✓ Branch 0 taken 16102 times.
✓ Branch 1 taken 5214 times.
✓ Branch 2 taken 110744 times.
✓ Branch 3 taken 23140 times.
155200 for (unsigned dir = 0; dir < dim; ++dir)
98
12/12
✓ Branch 0 taken 9200 times.
✓ Branch 1 taken 6902 times.
✓ Branch 4 taken 3986 times.
✓ Branch 5 taken 5214 times.
✓ Branch 6 taken 3986 times.
✓ Branch 7 taken 12116 times.
✓ Branch 8 taken 79340 times.
✓ Branch 9 taken 31404 times.
✓ Branch 12 taken 26456 times.
✓ Branch 13 taken 52884 times.
✓ Branch 14 taken 26456 times.
✓ Branch 15 taken 84288 times.
126846 if (dir != direction && rank_loc[dir] != loc[dir]) {
99 30442 return false;
100 }
101 28354 return rank_loc[direction] < loc[direction];
102 }
103
1/2
✓ Branch 1 taken 17789 times.
✗ Branch 2 not taken.
70326 );
104 }
105
106 private:
107 std::vector<Location> _map;
108 };
109
110 //! Helper for finding the locations of the sub-grids
111 //! associated with each rank in structured parallel grids
112 template<typename T, std::size_t dim>
113 class StructuredGridMapperHelper {
114 public:
115 using Origin = std::array<T, dim>;
116 using Basis = std::array<Origin, dim>;
117
118 4240 explicit StructuredGridMapperHelper(std::integral auto ranks,
119 const Basis& basis,
120 T default_epsilon = 1e-6)
121 4240 : _basis(basis)
122
1/2
✓ Branch 1 taken 2172 times.
✗ Branch 2 not taken.
8480 , _origins(ranks)
123
1/2
✓ Branch 1 taken 2172 times.
✗ Branch 2 not taken.
8480 , _set(ranks, false)
124 4240 , _default_epsilon{default_epsilon} {
125
1/2
✓ Branch 1 taken 2172 times.
✗ Branch 2 not taken.
4240 std::ranges::fill(_reverse, false);
126 4240 }
127
128 5712 void reverse(int direction) {
129 5712 _reverse[direction] = !_reverse[direction];
130 5712 }
131
132 12625 void set_origin_for(int rank, Origin origin) {
133
2/4
✓ Branch 1 taken 6417 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 6417 times.
12625 if (_set[rank])
134 ✗ throw ValueError("Origin for given rank already set");
135 12625 _origins[rank] = std::move(origin);
136
1/2
✓ Branch 1 taken 6417 times.
✗ Branch 2 not taken.
12625 _set[rank] = true;
137 12625 }
138
139 4240 auto make_mapper() const {
140
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2172 times.
4240 if (_origins.empty())
141 ✗ throw InvalidState("No origins have been set");
142
143
1/2
✓ Branch 2 taken 2172 times.
✗ Branch 3 not taken.
4240 std::vector<std::array<std::size_t, dim>> map(_origins.size());
144
2/2
✓ Branch 1 taken 5996 times.
✓ Branch 2 taken 2172 times.
16024 for (unsigned dir = 0; dir < dim; ++dir) {
145
1/2
✓ Branch 1 taken 5996 times.
✗ Branch 2 not taken.
11784 auto ordinates = _get_ordinates(dir);
146
1/2
✓ Branch 1 taken 5996 times.
✗ Branch 2 not taken.
11784 const auto eps = _epsilon(ordinates);
147
1/2
✓ Branch 2 taken 5996 times.
✗ Branch 3 not taken.
11784 _sort_ordinates(ordinates, eps, _reverse[dir]);
148
149
2/2
✓ Branch 1 taken 3207 times.
✓ Branch 2 taken 2789 times.
11784 if (ordinates.size() > 1) {
150
2/2
✓ Branch 1 taken 10566 times.
✓ Branch 2 taken 3207 times.
27229 for (unsigned rank = 0; rank < _origins.size(); ++rank) {
151
1/2
✓ Branch 2 taken 10566 times.
✗ Branch 3 not taken.
20918 const auto rank_origin_ordinate = _get_ordinate(_origins[rank], dir);
152
1/2
✓ Branch 1 taken 10566 times.
✗ Branch 2 not taken.
36446 auto it = std::ranges::find_if(ordinates, [&] (const T o) {
153 using std::abs;
154 15849 return abs(o - rank_origin_ordinate) < eps;
155 });
156
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 10566 times.
20918 if (it == ordinates.end())
157 ✗ throw InvalidState("Could not determine rank ordinate");
158 41836 map[rank][dir] = std::distance(ordinates.begin(), it);
159 }
160 } else {
161
2/2
✓ Branch 1 taken 7226 times.
✓ Branch 2 taken 2789 times.
19721 for (unsigned rank = 0; rank < _origins.size(); ++rank)
162 14248 map[rank][dir] = 0;
163 }
164 }
165 8480 return StructuredGridMapper<dim>{std::move(map)};
166 4240 }
167
168 4240 Origin compute_origin() const {
169
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2172 times.
4240 if (_origins.empty())
170 ✗ throw InvalidState("No origins have been set");
171
172 Origin result;
173
2/2
✓ Branch 0 taken 5996 times.
✓ Branch 1 taken 2172 times.
16024 for (unsigned dir = 0; dir < dim; ++dir) {
174
1/2
✓ Branch 1 taken 5996 times.
✗ Branch 2 not taken.
11784 auto ordinates = _get_ordinates(dir);
175
1/2
✓ Branch 2 taken 5996 times.
✗ Branch 3 not taken.
11784 _sort_ordinates(ordinates, _reverse[dir]);
176 11784 result[dir] = ordinates[0];
177 }
178 4240 return result;
179 }
180
181 private:
182 23568 auto _get_ordinates(unsigned int axis) const {
183 23568 std::vector<T> result;
184
1/2
✓ Branch 2 taken 11992 times.
✗ Branch 3 not taken.
23568 result.reserve(_origins.size());
185
2/4
✓ Branch 1 taken 11992 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 11992 times.
✗ Branch 5 not taken.
47136 std::ranges::copy(
186
1/2
✓ Branch 1 taken 11992 times.
✗ Branch 2 not taken.
58316 std::views::transform(_origins, [&] (const Origin& o) {
187 35584 return _get_ordinate(o, axis);
188 }),
189 std::back_inserter(result)
190 );
191 23568 return result;
192 ✗ }
193
194 91250 auto _get_ordinate(const Origin& o, unsigned int axis) const {
195 91250 return dot_product(o, _basis[axis]);
196 }
197
198 11784 void _sort_ordinates(std::vector<T>& ordinates, bool reverse) const {
199
2/2
✓ Branch 1 taken 5990 times.
✓ Branch 2 taken 6 times.
11784 if (ordinates.size() > 1)
200
2/4
✓ Branch 1 taken 5990 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 5990 times.
✗ Branch 5 not taken.
11778 _sort_ordinates(ordinates, _epsilon(ordinates), reverse);
201 11784 }
202
203 23562 void _sort_ordinates(std::vector<T>& ordinates, const T& eps, bool reverse) const {
204
2/2
✓ Branch 1 taken 11980 times.
✓ Branch 2 taken 6 times.
23562 if (ordinates.size() > 1) {
205
3/6
✓ Branch 1 taken 11980 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 11980 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 11980 times.
✗ Branch 8 not taken.
46728 auto [it, _] = Ranges::sort_and_unique(ordinates, {}, [&] (T a, T b) {
206 using std::abs;
207 23592 return abs(a - b) < eps;
208 });
209
1/2
✓ Branch 4 taken 11980 times.
✗ Branch 5 not taken.
23556 ordinates.erase(it, ordinates.end());
210
2/2
✓ Branch 0 taken 5712 times.
✓ Branch 1 taken 6268 times.
23556 if (reverse)
211
1/2
✓ Branch 1 taken 5712 times.
✗ Branch 2 not taken.
11424 std::ranges::reverse(ordinates);
212 }
213 23562 }
214
215 23562 T _epsilon(const std::vector<T>& ordinates) const {
216
1/2
✓ Branch 1 taken 11986 times.
✗ Branch 2 not taken.
23562 const auto max = *std::ranges::max_element(ordinates);
217
1/2
✓ Branch 1 taken 11986 times.
✗ Branch 2 not taken.
23562 const auto min = *std::ranges::min_element(ordinates);
218 23562 const auto size = max - min;
219
220
2/2
✓ Branch 0 taken 6414 times.
✓ Branch 1 taken 5572 times.
23562 if (size > _default_epsilon) {
221
1/2
✓ Branch 2 taken 6414 times.
✗ Branch 3 not taken.
12622 std::vector<T> dx; dx.reserve(ordinates.size());
222
2/4
✓ Branch 1 taken 6414 times.
✗ Branch 2 not taken.
✓ Branch 6 taken 6414 times.
✗ Branch 7 not taken.
12622 std::adjacent_difference(ordinates.begin(), ordinates.end(), std::back_inserter(dx));
223
1/2
✓ Branch 1 taken 6414 times.
✗ Branch 2 not taken.
33754 std::ranges::for_each(dx, [] (auto& v) { v = std::abs(v); });
224
1/2
✓ Branch 1 taken 6414 times.
✗ Branch 2 not taken.
12622 std::ranges::sort(dx);
225
1/2
✓ Branch 5 taken 16980 times.
✗ Branch 6 not taken.
33540 for (T _dx : dx)
226
2/2
✓ Branch 0 taken 6414 times.
✓ Branch 1 taken 10566 times.
33540 if (_dx > 1e-8*size)
227 12622 return _dx*0.1;
228
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6414 times.
12622 }
229
230 10940 return _default_epsilon;
231 }
232
233 const Basis& _basis;
234 std::vector<Origin> _origins;
235 std::vector<bool> _set;
236 std::array<bool, dim> _reverse;
237 T _default_epsilon;
238 };
239
240
241 template<typename Communicator>
242 class StructuredParallelGridHelper {
243 public:
244 6417 StructuredParallelGridHelper(const Communicator& comm, int root_rank = 0)
245 6417 : _comm{comm}
246 6417 , _root_rank{root_rank}
247 6417 {}
248
249 template<Concepts::Scalar CT,
250 std::size_t dim,
251 Concepts::StaticallySizedMDRange<2> B = std::array<std::array<CT, dim>, dim>>
252 12625 auto compute_extents_and_origin(const std::vector<CT>& all_origins,
253 const std::vector<std::size_t>& all_extents,
254 const std::array<bool, dim>& is_negative_axis,
255 const B& basis = GridDetail::standard_basis<CT, dim>()) const {
256
1/2
✓ Branch 1 taken 6414 times.
✗ Branch 2 not taken.
12625 const auto num_ranks = Parallel::size(_comm);
257
1/2
✓ Branch 1 taken 6417 times.
✗ Branch 2 not taken.
25250 std::vector<std::array<std::size_t, dim>> pieces_begin(num_ranks);
258
1/2
✓ Branch 1 taken 6417 times.
✗ Branch 2 not taken.
25250 std::vector<std::array<std::size_t, dim>> pieces_end(num_ranks);
259 std::array<std::size_t, dim> whole_extent;
260 std::array<CT, dim> origin;
261
262 // only the root computes the extents, but all ranks must throw if this fails
263
3/4
✓ Branch 1 taken 6417 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 6415 times.
✓ Branch 5 taken 2 times.
19043 Parallel::invoke_collectively(_comm, [&] () {
264
4/4
✓ Branch 1 taken 520 times.
✓ Branch 2 taken 939 times.
✓ Branch 4 taken 1652 times.
✓ Branch 5 taken 3306 times.
6417 if (Parallel::rank(_comm) == _root_rank) {
265
6/12
✓ Branch 1 taken 520 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 520 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 520 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 1652 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 1652 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 1652 times.
✗ Branch 17 not taken.
19546 const auto default_epsilon = 1e-6*std::ranges::max(all_origins | std::views::transform([] (CT v) {
266 using std::abs;
267 17792 return abs(v);
268 }));
269
2/4
✓ Branch 1 taken 520 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1652 times.
✗ Branch 5 not taken.
2172 const auto mapper_helper = _make_mapper_helper(basis, all_origins, is_negative_axis, default_epsilon);
270
2/4
✓ Branch 1 taken 520 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1652 times.
✗ Branch 5 not taken.
2172 const auto rank_mapper = mapper_helper.make_mapper();
271
2/4
✓ Branch 1 taken 520 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1652 times.
✗ Branch 5 not taken.
2172 origin = mapper_helper.compute_origin();
272
273
4/4
✓ Branch 0 taken 1039 times.
✓ Branch 1 taken 519 times.
✓ Branch 2 taken 4956 times.
✓ Branch 3 taken 1652 times.
8166 for (unsigned dir = 0; dir < dim; ++dir) {
274
4/4
✓ Branch 0 taken 2915 times.
✓ Branch 1 taken 1038 times.
✓ Branch 2 taken 14874 times.
✓ Branch 3 taken 4956 times.
23783 for (int rank = 0; rank < num_ranks; ++rank) {
275
2/4
✓ Branch 2 taken 2915 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 14874 times.
✗ Branch 7 not taken.
17789 auto ranks_below = rank_mapper.ranks_below(rank_mapper.location(rank), dir);
276
6/12
✓ Branch 1 taken 2915 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2915 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 2915 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 14874 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 14874 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 14874 times.
✗ Branch 17 not taken.
17789 const std::size_t offset = std::accumulate(
277 std::ranges::begin(ranks_below),
278 std::ranges::end(ranks_below),
279 std::size_t{0},
280 5282 [&] (const std::size_t current, int r) {
281
2/4
✓ Branch 1 taken 1149 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4133 times.
✗ Branch 5 not taken.
5282 return current + Parallel::access_gathered<dim>(all_extents, _comm, {dir, r});
282 }
283 );
284 17789 pieces_begin[rank][dir] = offset;
285
3/4
✓ Branch 1 taken 2914 times.
✓ Branch 2 taken 1 times.
✓ Branch 6 taken 14874 times.
✗ Branch 7 not taken.
17789 pieces_end[rank][dir] = offset + Parallel::access_gathered<dim>(all_extents, _comm, {dir, rank});
286 }
287
288
2/4
✓ Branch 3 taken 1038 times.
✗ Branch 4 not taken.
✓ Branch 10 taken 4956 times.
✗ Branch 11 not taken.
11988 whole_extent[dir] = (*std::max_element(
289 pieces_end.begin(), pieces_end.end(),
290 11794 [&] (const auto& a1, const auto& a2) { return a1[dir] < a2[dir]; }
291 5994 ))[dir];
292 }
293 2173 }
294 }, "Computing the extents of the parallel grid");
295
296 return std::make_tuple(
297 12623 std::move(pieces_begin),
298 12623 std::move(pieces_end),
299 12623 std::move(whole_extent),
300 12623 std::move(origin)
301 25246 );
302 12627 }
303
304 private:
305 template<Concepts::StaticallySizedMDRange<2> Basis, Concepts::Scalar CT, std::size_t dim>
306 4240 auto _make_mapper_helper(const Basis& basis,
307 const std::vector<CT>& all_origins,
308 const std::array<bool, dim>& is_negative_axis,
309 CT default_eps) const {
310 4240 StructuredGridMapperHelper<CT, dim> helper(Parallel::size(_comm), basis, default_eps);
311
2/4
✓ Branch 1 taken 2172 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2172 times.
✗ Branch 5 not taken.
16656 std::ranges::for_each(Parallel::ranks(_comm), [&] (int rank) {
312 6417 helper.set_origin_for(rank, Parallel::access_gathered<dim>(all_origins, _comm, rank));
313 });
314
315
2/2
✓ Branch 0 taken 5996 times.
✓ Branch 1 taken 2172 times.
16024 for (unsigned dir = 0; dir < dim; ++dir)
316
2/2
✓ Branch 1 taken 2856 times.
✓ Branch 2 taken 3140 times.
11784 if (is_negative_axis[dir])
317 5712 helper.reverse(dir);
318
319 4240 return helper;
320 ✗ }
321
322 const Communicator& _comm;
323 int _root_rank;
324 };
325
326 } // namespace GridFormat::PVTK
327
328 #endif // GRIDFORMAT_VTK_PARALLEL_HPP_
329