Debian Codegraph @

packages/seacas/libraries/ioss/src/utest/Utst_map.C

raw
1 // Copyright(C) 1999-2024 National Technology & Engineering Solutions
2 // of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
3 // NTESS, the U.S. Government retains certain rights in this software.
4 //
5 // See packages/seacas/LICENSE for details
6
7 #include "Ioss_ConcreteVariableType.h"
8 #include "Ioss_Field.h"
9 #include "Ioss_Map.h"
10 #include <catch2/catch_test_macros.hpp>
11 #include <cstddef>
12 #include <numeric>
13 #include <random>
14 #include <stdint.h>
15 #include <string>
16 #include <vector>
17
18 template <typename INT> void initialize_data(std::vector<INT> &init)
19 {
20 std::random_device rd;
21 std::mt19937 g(rd());
22 std::shuffle(init.begin(), init.end(), g);
23 }
24
25 template <typename INT>
26 void verify_global_to_local(const Ioss::Map &my_map, const std::vector<INT> &init)
27 {
28 size_t count = my_map.map().size() - 1;
29 CHECK(count == init.size());
30
31 for (size_t i = 0; i < count; i++) {
32 INT global = init[i];
33 CHECK((size_t)my_map.global_to_local(global) == i + 1);
34 }
35 }
36
37 template <typename INT> void test_reorder(Ioss::Map &my_map, std::vector<INT> &init, size_t offset)
38 {
39 // The map coming in has already been defined using 'init' and is
40 // sequential from 'offset+1' to 'offset+count+1'
41 //
42 // Redefine the map with a shuffle of the original ids -- reorder
43 // Then map the shuffled ordering back to the original order
44 // using the `map_field_to_db_scalar_order` function and verify.
45 initialize_data(init);
46
47 my_map.set_map(Data(init), init.size(), 0, false);
48 CHECK(!my_map.is_sequential());
49
50 // Check that we get the *original* ordering back from global_to_local.
51 size_t count = init.size();
52 for (size_t i = 0; i < count; i++) {
53 CHECK(my_map.global_to_local(init[i]) == int(init[i] - offset));
54 }
55
56 // Check that we get the the `reordered` vector has been put into `db` order.
57 std::vector<double> reordered(count);
58 my_map.map_field_to_db_scalar_order(Data(init), reordered, 0, count, 1, 0);
59 for (size_t i = 0; i < count; i++) {
60 CHECK(reordered[i] == offset + i + 1);
61 }
62 }
63
64 TEST_CASE("test random ids")
65 {
66 // Create a map of random ids and make verify global to local works.
67 size_t count = 128;
68 Ioss::Map my_map;
69 my_map.set_size(count);
70
71 std::vector<int> init(count);
72 std::iota(init.begin(), init.end(), 2511);
73
74 initialize_data(init);
75
76 for (auto &e : init) {
77 e = 11 * e;
78 }
79
80 my_map.set_map(Data(init), init.size(), 0, true);
81
82 CHECK(!my_map.is_sequential());
83 CHECK(!my_map.is_sequential(true));
84 CHECK_NOTHROW(verify_global_to_local(my_map, init));
85 }
86
87 TEST_CASE("test sequential map with offset")
88 {
89 // Simple sequential map 'offset+1 .. offset+1+count'
90 // Should verify that reverseMap and reorderMap are empty, but
91 // not possible with current API...
92 size_t count = 128;
93 Ioss::Map my_map;
94 my_map.set_size(count);
95
96 std::vector<int64_t> init(count);
97 #if defined(__IOSS_WINDOWS__)
98 std::vector<size_t> offsets{0, 123, 858993459};
99 #else
100 std::vector<size_t> offsets{0, 123, 8589934592};
101 #endif
102 std::vector<std::string> sections{"offset0", "offset123", "offsetBIG"};
103
104 for (size_t i = 0; i < offsets.size(); i++) {
105 SECTION(sections[i])
106 {
107 std::size_t offset = offsets[i];
108 std::iota(init.begin(), init.end(), int64_t(offset) + 1);
109
110 my_map.set_map(Data(init), init.size(), 0, true);
111
112 CHECK(my_map.is_sequential());
113 CHECK(my_map.is_sequential(true));
114 CHECK_NOTHROW(verify_global_to_local(my_map, init));
115
116 SECTION("Reorder-1")
117 {
118 test_reorder(my_map, init, offset);
119
120 SECTION("Reorder-2") { test_reorder(my_map, init, offset); }
121 }
122 }
123 }
124 }
125
126 TEST_CASE("test segmented map creation")
127 {
128 Ioss::Map my_map;
129 size_t segments = 4;
130
131 size_t count = 128;
132 my_map.set_size(count);
133
134 size_t seg_size = count / segments;
135 CHECK(count % segments == 0);
136
137 std::vector<int> init(count);
138
139 std::vector<size_t> offsets{0, 123};
140 std::vector<std::string> sections{"offset0", "offset123"};
141
142 for (size_t i = 0; i < offsets.size(); i++) {
143 SECTION(sections[i])
144 {
145 std::size_t offset = offsets[i];
146 std::iota(init.begin(), init.end(), int(offset) + 1);
147
148 for (size_t j = 0; j < segments; j++) {
149 my_map.set_map(&init[j * seg_size], seg_size, j * seg_size, true);
150 my_map.set_map(&init[j * seg_size], 0, j * seg_size,
151 true); // make sure handles empty segments
152 }
153
154 CHECK(my_map.is_sequential()); // Based on m_map[0] setting.
155 CHECK(my_map.is_sequential(true)); // Based on checking all values.
156 CHECK_NOTHROW(verify_global_to_local(my_map, init));
157 }
158 }
159 }
160
161 TEST_CASE("test reverse segmented map creation")
162 {
163 Ioss::Map my_map;
164 size_t segments = 4;
165
166 size_t count = 128;
167 my_map.set_size(count);
168
169 size_t seg_size = count / segments;
170 CHECK(count % segments == 0);
171
172 std::vector<int> init(count);
173
174 std::vector<size_t> offsets{0, 123};
175 std::vector<std::string> sections{"offset0", "offset123"};
176
177 for (size_t i = 0; i < offsets.size(); i++) {
178 SECTION(sections[i])
179 {
180 std::size_t offset = offsets[i];
181 std::iota(init.begin(), init.end(), int(offset) + 1);
182
183 for (size_t j = 0; j < segments; j++) {
184 size_t k = segments - j - 1;
185 my_map.set_map(&init[k * seg_size], seg_size, k * seg_size, true);
186 my_map.set_map(&init[k * seg_size], 0, k * seg_size,
187 true); // make sure handles empty segments
188 }
189
190 CHECK(my_map.is_sequential());
191 CHECK(my_map.is_sequential(true));
192 CHECK_NOTHROW(verify_global_to_local(my_map, init));
193 }
194 }
195 }
196
197 TEST_CASE("test segment gap")
198 {
199 // Each segment is sequential, but there is a gap between each segment.
200 // Make sure mapping can detect the gap...
201 size_t segments = 4;
202 size_t count = 128;
203 size_t seg_size = count / segments;
204 CHECK(count % segments == 0);
205
206 Ioss::Map my_map;
207 my_map.set_size(count);
208
209 std::vector<int> init(count);
210
211 std::vector<size_t> offsets{0, 123};
212 std::vector<std::string> sections{"offset0", "offset123"};
213
214 for (size_t ii = 0; ii < offsets.size(); ii++) {
215 SECTION(sections[ii])
216 {
217 std::size_t offset = offsets[ii];
218 for (size_t j = 0; j < segments; j++) {
219 size_t seg_begin = j * seg_size;
220 size_t seg_end = seg_begin + seg_size;
221
222 for (size_t i = seg_begin; i < seg_end; i++) {
223 init[i] = i + j + offset + 1;
224 }
225 }
226
227 for (size_t j = 0; j < segments; j++) {
228 size_t i = segments - j - 1;
229 my_map.set_map(&init[i * seg_size], seg_size, i * seg_size, true);
230 }
231 CHECK(!my_map.is_sequential());
232 CHECK(!my_map.is_sequential(true));
233 CHECK_NOTHROW(verify_global_to_local(my_map, init));
234 }
235 }
236 }
237
238 TEST_CASE("test small reverse")
239 {
240 std::vector<int> init{1, 3};
241
242 size_t count = init.size();
243 Ioss::Map my_map;
244 my_map.set_size(count);
245
246 my_map.set_map(&init[1], 1, 1, true);
247 my_map.set_map(&init[0], 1, 0, true);
248
249 CHECK(!my_map.is_sequential());
250 CHECK(!my_map.is_sequential(true));
251 CHECK_NOTHROW(verify_global_to_local(my_map, init));
252 }
253
254 TEST_CASE("test small swap front back")
255 {
256 size_t count = 16;
257 Ioss::Map my_map;
258 my_map.set_size(count);
259
260 // Two segments each sequential 0..7 8..15
261 std::vector<int> init{9, 10, 11, 12, 13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8};
262
263 // Build map with segment 2 first and then segment 1.
264 my_map.set_map(&init[8], 8, 8, true);
265 my_map.set_map(&init[0], 8, 0, true);
266
267 CHECK(!my_map.is_sequential());
268 CHECK(!my_map.is_sequential(true));
269 CHECK_NOTHROW(verify_global_to_local(my_map, init));
270 }
271
272 TEST_CASE("test map_data sequential")
273 {
274 size_t count = 128;
275 Ioss::Map my_map;
276 my_map.set_size(count);
277
278 std::vector<int> init(count);
279 std::vector<size_t> offsets{0, 123};
280 std::vector<std::string> sections{"offset0", "offset123"};
281
282 for (size_t ii = 0; ii < offsets.size(); ii++) {
283 SECTION(sections[ii])
284 {
285 std::size_t offset = offsets[ii];
286 std::iota(init.begin(), init.end(), int(offset) + 1);
287
288 my_map.set_map(Data(init), init.size(), 0, true);
289
290 CHECK(my_map.is_sequential());
291 CHECK(my_map.is_sequential(true));
292 CHECK_NOTHROW(verify_global_to_local(my_map, init));
293
294 Ioss::StorageInitializer();
295 Ioss::Field int_field("int_field", Ioss::Field::INTEGER, "invalid", Ioss::Field::MESH, count);
296
297 SECTION("explicit map")
298 {
299 // Now try 'map_data' call which is basically a bulk local-global.
300 // Pass in local ids, returns global ids. So if pass in 1..count, should get back 'init'
301 std::vector<int> local(count);
302 std::iota(local.begin(), local.end(), 1);
303 my_map.map_data(Data(local), int_field, count);
304 CHECK(init == local);
305
306 SECTION("reverse_map")
307 {
308 // If now reverse 'local', should get the original 1..count back
309 my_map.reverse_map_data(Data(local), int_field, count);
310 std::vector<int> seq(count);
311 std::iota(seq.begin(), seq.end(), 1);
312 CHECK(local == seq);
313 }
314 }
315
316 SECTION("implicit map")
317 {
318 // Now try 'map_data' call which is basically a bulk local-global.
319 // Pass in local ids, returns global ids. So if pass in 1..count, should get back 'init'
320 std::vector<int> local(count);
321 my_map.map_implicit_data(Data(local), int_field, count, 0);
322 CHECK(init == local);
323 }
324 }
325 }
326 }
327
328 TEST_CASE("test map_data random")
329 {
330 size_t count = 128;
331 Ioss::Map my_map;
332 my_map.set_size(count);
333
334 std::vector<int> init(count);
335 std::iota(init.begin(), init.end(), 2511);
336
337 initialize_data(init);
338
339 for (auto &e : init) {
340 e = 11 * e;
341 }
342
343 my_map.set_map(Data(init), init.size(), 0, true);
344
345 CHECK(!my_map.is_sequential());
346 CHECK(!my_map.is_sequential(true));
347 CHECK_NOTHROW(verify_global_to_local(my_map, init));
348
349 Ioss::StorageInitializer();
350 Ioss::Field int_field("int_field", Ioss::Field::INTEGER, "invalid", Ioss::Field::MESH, count);
351
352 SECTION("explicit map")
353 {
354 // Now try 'map_data' call which is basically a bulk local-global.
355 // Pass in local ids, returns global ids. So if pass in 1..count, should get back 'init'
356 std::vector<int> local(count);
357 std::iota(local.begin(), local.end(), 1);
358 my_map.map_data(Data(local), int_field, count);
359 CHECK(init == local);
360
361 SECTION("reverse_map")
362 {
363 // If now reverse 'local', should get the original 1..count back
364 my_map.reverse_map_data(Data(local), int_field, count);
365 std::vector<int> seq(count);
366 std::iota(seq.begin(), seq.end(), 1);
367 CHECK(local == seq);
368 }
369 }
370
371 SECTION("implicit map")
372 {
373 // Now try 'map_data' call which is basically a bulk local-global.
374 // Pass in local ids, returns global ids. So if pass in 1..count, should get back 'init'
375 std::vector<int> local(count);
376 my_map.map_implicit_data(Data(local), int_field, count, 0);
377 CHECK(init == local);
378 }
379 }