Debian Codegraph @

packages/seacas/libraries/ioss/src/Ioss_FaceGenerator.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_CodeTypes.h"
8 #include "Ioss_CommSet.h"
9 #include "Ioss_ElementBlock.h"
10 #include "Ioss_ElementTopology.h"
11 #include "Ioss_FaceGenerator.h"
12 #include "Ioss_NodeBlock.h"
13 #include "Ioss_Property.h"
14 #include "Ioss_Region.h"
15 #include <cassert>
16 #include <fmt/ostream.h>
17 #include <iosfwd>
18 #include <numeric>
19 #include <stdint.h>
20
21 #include "Ioss_Utils.h"
22 #include "robin_set.h"
23
24 // Options for generating hash function key...
25 #define USE_MURMUR
26 // #define USE_RANDOM
27
28 #define DO_TIMING 0
29
30 namespace {
31 #ifdef SEACAS_HAVE_MPI
32 template <typename T> void generate_index(std::vector<T> &index)
33 {
34 T sum = 0;
35 for (auto &idx : index) {
36 T cnt = idx;
37 idx = sum;
38 sum += cnt;
39 }
40 }
41 #endif
42
43 #if defined(USE_MURMUR)
44 uint64_t MurmurHash64A(size_t key);
45 #endif
46
47 void create_face(Ioss::FaceUnorderedSet &faces, size_t id, std::array<size_t, 4> &conn,
48 size_t element, int local_face)
49 {
50 Ioss::Face face(id, conn);
51 auto face_iter = faces.insert(face);
52
53 (*(face_iter.first)).add_element(element, local_face);
54 }
55
56 template <typename INT>
57 void internal_generate_faces(Ioss::ElementBlock *eb, Ioss::FaceUnorderedSet &faces,
58 const std::vector<INT> &ids, const std::vector<size_t> &hash_ids,
59 bool local_ids, INT /*dummy*/)
60 {
61 const Ioss::ElementTopology *topo = eb->topology();
62
63 // Only handle continuum elements at this time...
64 if (topo->parametric_dimension() != 3) {
65 return;
66 }
67
68 std::vector<INT> connectivity;
69 eb->get_field_data("connectivity_raw", connectivity);
70
71 std::vector<INT> elem_ids;
72 if (!local_ids) {
73 eb->get_field_data("ids", elem_ids);
74 }
75
76 int num_face_per_elem = topo->number_faces();
77 assert(num_face_per_elem <= 6);
78 std::array<Ioss::IntVector, 6> face_conn;
79 std::array<int, 6> face_node_count{};
80 for (int face = 0; face < num_face_per_elem; face++) {
81 face_conn[face] = topo->face_connectivity(face + 1);
82 face_node_count[face] = topo->face_type(face + 1)->number_corner_nodes();
83 }
84
85 int num_node_per_elem = topo->number_nodes();
86 size_t num_elem = eb->entity_count();
87
88 for (size_t elem = 0, offset = 0; elem < num_elem; elem++, offset += num_node_per_elem) {
89 auto elem_id = local_ids ? eb->get_offset() + elem + 1 : elem_ids[elem];
90 for (int face = 0; face < num_face_per_elem; face++) {
91 size_t id = 0;
92 assert(face_node_count[face] <= 4);
93 std::array<size_t, 4> conn = {{0, 0, 0, 0}};
94 for (int j = 0; j < face_node_count[face]; j++) {
95 size_t fnode = offset + face_conn[face][j];
96 size_t lnode = connectivity[fnode]; // local since "connectivity_raw"
97 conn[j] = local_ids ? lnode : ids[lnode - 1]; // Convert to global
98 id += hash_ids[lnode - 1];
99 }
100 create_face(faces, id, conn, elem_id, face);
101 }
102 }
103 }
104
105 template <typename INT>
106 void resolve_parallel_faces(IOSS_MAYBE_UNUSED Ioss::Region &region,
107 IOSS_MAYBE_UNUSED Ioss::FaceUnorderedSet &faces,
108 IOSS_MAYBE_UNUSED const std::vector<size_t> &hash_ids, INT /*dummy*/)
109 {
110 IOSS_PAR_UNUSED(region);
111 IOSS_PAR_UNUSED(faces);
112 IOSS_PAR_UNUSED(hash_ids);
113
114 #ifdef SEACAS_HAVE_MPI
115 size_t proc_count = region.get_database()->util().parallel_size();
116
117 if (proc_count > 1) {
118 // If parallel, resolve faces on processor boundaries.
119 // For each boundary face, need to check whether all of the nodes on
120 // the face are shared with the same processor. If so, then that face
121 // is *possibly* shared with that processor.
122 //
123 // With the current continuum element only restriction, then a face
124 // can only be shared with one other processor...
125
126 // get nodal communication data CommSet...
127 std::vector<std::pair<INT, INT>> proc_entity;
128 {
129 // entity_processor consists of node,proc, node,proc, entries.
130 std::vector<INT> entity_processor;
131 Ioss::CommSet *css = region.get_commset("commset_node");
132 css->get_field_data("entity_processor_raw", entity_processor);
133
134 proc_entity.reserve(entity_processor.size() / 2);
135 for (size_t i = 0; i < entity_processor.size(); i += 2) {
136 // Converts from 1-based to 0-based local nodes.
137 proc_entity.push_back(std::make_pair(entity_processor[i + 1], entity_processor[i] - 1));
138 }
139 }
140
141 // 'id_span' gives index into proc_entity for all nodes.
142 // 'id_span[local_node_id] .. id_span[local_node_id+1]' gives
143 // the location in 'proc_entity' of the sharing information
144 // for node 'local_node_id'
145 std::vector<size_t> id_span(hash_ids.size() + 1);
146 for (const auto &pe : proc_entity) {
147 INT node = pe.second;
148 assert(node >= 0 && node < (INT)id_span.size() - 1);
149 id_span[node]++;
150 }
151 generate_index(id_span);
152
153 // Each boundary face ...
154 // .. See if all of its nodes are shared with same processor.
155 // .. Iterate face nodes
156 // .. Determine shared proc.
157 // .. (for now, use a vector[proc] = count
158 // .. if potentially shared with 'proc', then count == num_nodes_face
159
160 std::vector<INT> potential_count(proc_count);
161 std::vector<int> shared_nodes(proc_count);
162 for (auto &face : faces) {
163 if (face.element_count() == 1) {
164 // On 'boundary' -- try to determine whether on processor or exterior
165 // boundary
166 int face_node_count = 0;
167 for (auto &gnode : face.connectivity_) {
168 if (gnode > 0) {
169 auto node = region.get_database()->node_global_to_local(gnode, true) - 1;
170 face_node_count++;
171 size_t begin = id_span[node];
172 size_t end = id_span[node + 1];
173 for (size_t j = begin; j < end; j++) {
174 assert(proc_entity[j].second == node);
175 int proc = proc_entity[j].first;
176 shared_nodes[proc]++;
177 }
178 }
179 }
180 for (size_t i = 0; i < proc_count; i++) {
181 if (shared_nodes[i] == face_node_count) {
182 potential_count[i]++;
183 }
184 shared_nodes[i] = 0; // Reset for next trip through face.connectivity_ loop
185 }
186 }
187 }
188
189 std::vector<INT> potential_offset(potential_count.begin(), potential_count.end());
190 generate_index(potential_offset);
191
192 size_t potential = potential_offset[proc_count - 1] + potential_count[proc_count - 1];
193 std::vector<int64_t> potential_faces(6 * potential);
194
195 for (auto &face : faces) {
196 if (face.element_count() == 1) {
197 // On 'boundary' -- try to determine whether on processor or exterior
198 // boundary
199 int face_node_count = 0;
200 for (auto &gnode : face.connectivity_) {
201 if (gnode > 0) {
202 auto node = region.get_database()->node_global_to_local(gnode, true) - 1;
203 face_node_count++;
204 size_t begin = id_span[node];
205 size_t end = id_span[node + 1];
206 for (size_t j = begin; j < end; j++) {
207 assert(proc_entity[j].second == node);
208 int proc = proc_entity[j].first;
209 shared_nodes[proc]++;
210 }
211 }
212 }
213 for (size_t i = 0; i < proc_count; i++) {
214 if (shared_nodes[i] == face_node_count) {
215 size_t offset = potential_offset[i];
216 potential_faces[6 * offset + 0] = face.hashId_;
217 potential_faces[6 * offset + 1] = face.connectivity_[0];
218 potential_faces[6 * offset + 2] = face.connectivity_[1];
219 potential_faces[6 * offset + 3] = face.connectivity_[2];
220 potential_faces[6 * offset + 4] = face.connectivity_[3];
221 potential_faces[6 * offset + 5] = face.element[0];
222 assert(face.element_count() == 1);
223 potential_offset[i]++;
224 }
225 shared_nodes[i] = 0; // Reset for next trip through face.connectivity_ loop
226 }
227 }
228 }
229
230 // Regenerate potential_offset since it was modified above...
231 std::copy(potential_count.begin(), potential_count.end(), potential_offset.begin());
232 generate_index(potential_offset);
233
234 // Now need to send to the other processors...
235 // For now, use all-to-all; optimization is just send to processors with
236 // data...
237 std::vector<INT> check_count(proc_count);
238 MPI_Alltoall(Data(potential_count), 1, Ioss::mpi_type((INT)0), Data(check_count), 1,
239 Ioss::mpi_type((INT)0), region.get_database()->util().communicator());
240
241 const int values_per_face = 6; // id, 4-node-conn, element
242 auto sum = std::accumulate(check_count.begin(), check_count.end(), 0);
243 std::vector<int64_t> check_faces(values_per_face * sum);
244
245 std::vector<INT> check_offset(check_count.begin(), check_count.end());
246 generate_index(check_offset);
247
248 // Need to adjust counts and offsets to account for sending 6 values per
249 // face...
250 for (size_t i = 0; i < proc_count; i++) {
251 potential_count[i] *= values_per_face;
252 potential_offset[i] *= values_per_face;
253 check_count[i] *= values_per_face;
254 check_offset[i] *= values_per_face;
255 }
256
257 Ioss::MY_Alltoallv(potential_faces, potential_count, potential_offset, check_faces,
258 check_count, check_offset, region.get_database()->util().communicator());
259
260 // Now iterate the check_faces and see if any of them match one
261 // of this processors faces... If so, then mark as shared and
262 // add the element...
263 for (size_t i = 0; i < check_faces.size(); i += values_per_face) {
264 size_t id = check_faces[i + 0];
265 std::array<size_t, 4> conn{};
266 conn[0] = check_faces[i + 1];
267 conn[1] = check_faces[i + 2];
268 conn[2] = check_faces[i + 3];
269 conn[3] = check_faces[i + 4];
270 size_t element = check_faces[i + 5];
271 Ioss::Face face(id, conn);
272 auto face_iter = faces.find(face, face.hashId_);
273 if (face_iter != faces.end()) {
274 // we have a match... This is a shared interior face
275 (*face_iter).add_element(element); // Already has face multiplied in.
276
277 int proc = 0;
278 for (size_t j = 0; j < check_count.size(); j++) {
279 if (check_count[j] > 0 && check_offset[j] == (INT)i) {
280 break;
281 }
282 proc++;
283 }
284 }
285 }
286 }
287 #endif
288 }
289 } // namespace
290
291 namespace Ioss {
292 Face::Face(std::array<size_t, 4> conn) : connectivity_(conn)
293 {
294 for (auto &node : connectivity_) {
295 hashId_ += Ioss::FaceGenerator::id_hash(node);
296 }
297 }
298
299 void Face::face_element_error(size_t element_id) const
300 {
301 std::ostringstream errmsg;
302 fmt::print(errmsg,
303 "ERROR: Face {} has more than two elements using it.\n"
304 " The element/local_face are: {}:{}, {}:{}, and {}:{}.\n"
305 " The face connectivity is {} {} {} {}.\n",
306 hashId_, element[0] / 10, element[0] % 10, element[1] / 10, element[1] % 10,
307 element_id / 10, element_id % 10, connectivity_[0], connectivity_[1],
308 connectivity_[2], connectivity_[3]);
309 IOSS_ERROR(errmsg);
310 }
311
312 size_t FaceGenerator::id_hash(size_t global_id)
313 {
314 #if defined(USE_RANDOM)
315 std::mt19937_64 rng;
316 rng.seed(global_id);
317 return rng();
318 #elif defined(USE_MURMUR)
319 return MurmurHash64A(global_id);
320 #else
321 return global_id;
322 #endif
323 }
324
325 FaceGenerator::FaceGenerator(Ioss::Region &region) : region_(region) {}
326
327 FaceUnorderedSet &FaceGenerator::faces(const Ioss::ElementBlock *block)
328 {
329 const auto &name = block->name();
330 return faces_[name];
331 }
332
333 void FaceGenerator::progress(const std::string &output) const
334 {
335 region_.get_database()->progress(output);
336 }
337
338 void FaceGenerator::clear(const Ioss::ElementBlock *block)
339 {
340 const auto &name = block->name();
341 faces_[name].clear();
342 }
343
344 template IOSS_EXPORT void FaceGenerator::generate_faces(int, bool, bool);
345 template IOSS_EXPORT void FaceGenerator::generate_faces(int64_t, bool, bool);
346
347 template <typename INT>
348 void FaceGenerator::generate_faces(INT /*dummy*/, bool block_by_block, bool local_ids)
349 {
350 progress(__func__);
351 if (block_by_block) {
352 const auto &ebs = region_.get_element_blocks();
353 generate_block_faces(ebs, INT(0), local_ids);
354 }
355 else {
356 generate_model_faces(INT(0), local_ids);
357 }
358 }
359
360 template <typename INT> void FaceGenerator::hash_node_ids(const std::vector<INT> &node_ids)
361 {
362 progress(__func__);
363 hashIds_.reserve(node_ids.size());
364 for (auto &id : node_ids) {
365 hashIds_.push_back(id_hash(id));
366 }
367 }
368
369 void FaceGenerator::hash_local_node_ids(size_t count)
370 {
371 hashIds_.reserve(count);
372 for (size_t i = 1; i <= count; i++) {
373 hashIds_.push_back(id_hash(i));
374 }
375 }
376
377 template IOSS_EXPORT void FaceGenerator::generate_block_faces(const Ioss::ElementBlockContainer &,
378 int, bool);
379 template IOSS_EXPORT void FaceGenerator::generate_block_faces(const Ioss::ElementBlockContainer &,
380 int64_t, bool);
381
382 template <typename INT>
383 void FaceGenerator::generate_block_faces(const Ioss::ElementBlockContainer &ebs, INT /*dummy*/,
384 bool local_ids)
385 {
386 progress(__func__);
387 // Convert ids into hashed-ids
388 Ioss::NodeBlock *nb = region_.get_node_blocks()[0];
389
390 #if DO_TIMING
391 auto starth = std::chrono::steady_clock::now();
392 #endif
393 std::vector<INT> ids;
394 if (local_ids) {
395 hash_local_node_ids(nb->entity_count());
396 }
397 else {
398 nb->get_field_data("ids", ids);
399 hash_node_ids(ids);
400 }
401 #if DO_TIMING
402 auto endh = std::chrono::steady_clock::now();
403 #endif
404
405 for (const auto &eb : ebs) {
406 progress("\tgenerate_block_faces: " + eb->name());
407 const std::string &name = eb->name();
408 size_t numel = eb->entity_count();
409 size_t reserve = 2.0 * numel;
410 faces_[name].reserve(reserve);
411 faces_[name].max_load_factor(0.9);
412 internal_generate_faces(eb, faces_[name], ids, hashIds_, local_ids, (INT)0);
413 }
414 progress("\tgenerate_block_faces: end of blocks");
415
416 #if DO_TIMING
417 auto endf = std::chrono::steady_clock::now();
418 #endif
419 for (const auto &eb : ebs) {
420 resolve_parallel_faces(region_, faces_[eb->name()], hashIds_, (INT)0);
421 }
422 #if DO_TIMING
423 size_t face_count = 0;
424 for (const auto &eb : ebs) {
425 face_count += faces_[eb->name()].size();
426 }
427 auto endp = std::chrono::steady_clock::now();
428 auto diffh = endh - starth;
429 auto difff = endf - endh;
430 fmt::print(
431 "Node ID hash time: \t{:.6} ms\t{:12} nodes/second\n"
432 "Face generation time:\t{:.6} ms\t{:12} faces/second\n",
433 std::chrono::duration<double, std::milli>(diffh).count(),
434 fmt::group_digits(INT(hashIds_.size() / std::chrono::duration<double>(diffh).count())),
435 std::chrono::duration<double, std::milli>(difff).count(),
436 fmt::group_digits(INT(face_count / std::chrono::duration<double>(difff).count())));
437 #ifdef SEACAS_HAVE_MPI
438 auto diffp = endp - endf;
439 size_t proc_count = region_.get_database()->util().parallel_size();
440
441 if (proc_count > 1) {
442 fmt::print("Parallel time: \t{:.6} ms\t{:12} faces/second.\n",
443 std::chrono::duration<double, std::milli>(diffp).count(),
444 fmt::group_digits(INT(face_count / std::chrono::duration<double>(diffp).count())));
445 }
446 #endif
447 fmt::print("Total time: \t{:.6} ms\n\n",
448 std::chrono::duration<double, std::milli>(endp - starth).count());
449 #endif
450 progress("\tgenerate_block_faces: end of routine");
451 hashIds_.clear();
452 }
453
454 template <typename INT> void FaceGenerator::generate_model_faces(INT /*dummy*/, bool local_ids)
455 {
456 progress(__func__);
457 // Convert ids into hashed-ids
458 Ioss::NodeBlock *nb = region_.get_node_blocks()[0];
459
460 std::vector<INT> ids;
461 if (local_ids) {
462 ids.resize(nb->entity_count());
463 std::iota(ids.begin(), ids.end(), 1);
464 }
465 else {
466 nb->get_field_data("ids", ids);
467 }
468 #if DO_TIMING
469 auto starth = std::chrono::steady_clock::now();
470 #endif
471 hash_node_ids(ids);
472 #if DO_TIMING
473 auto endh = std::chrono::steady_clock::now();
474 #endif
475
476 auto &my_faces = faces_["ALL"];
477 size_t numel = region_.get_property("element_count").get_int();
478
479 size_t reserve = 3.2 * numel;
480 my_faces.reserve(reserve);
481 const auto &ebs = region_.get_element_blocks();
482 for (const auto &eb : ebs) {
483 internal_generate_faces(eb, my_faces, ids, hashIds_, local_ids, (INT)0);
484 }
485
486 #if DO_TIMING
487 auto endf = std::chrono::steady_clock::now();
488 #endif
489 resolve_parallel_faces(region_, my_faces, hashIds_, (INT)0);
490
491 #if DO_TIMING
492 auto endp = std::chrono::steady_clock::now();
493 auto diffh = endh - starth;
494 auto difff = endf - endh;
495 fmt::print("Node ID hash time: \t{:.3f} ms\t{:.3} nodes/second\n"
496 "Face generation time:\t{:.3f} ms\t{:.3} faces/second.\n",
497 std::chrono::duration<double, std::milli>(diffh).count(),
498 hashIds_.size() / std::chrono::duration<double>(diffh).count(),
499 std::chrono::duration<double, std::milli>(difff).count(),
500 my_faces.size() / std::chrono::duration<double>(difff).count());
501 #ifdef SEACAS_HAVE_MPI
502 auto diffp = endp - endf;
503 size_t proc_count = region_.get_database()->util().parallel_size();
504
505 if (proc_count > 1) {
506 fmt::print("Parallel time: \t{:.3f} ms\t{:.3} faces/second.\n",
507 std::chrono::duration<double, std::milli>(diffp).count(),
508 my_faces.size() / std::chrono::duration<double>(diffp).count());
509 }
510 #endif
511 fmt::print("Total time: \t{:.3f} ms\n\n",
512 std::chrono::duration<double, std::milli>(endp - starth).count());
513 #endif
514 hashIds_.clear();
515 }
516 } // namespace Ioss
517
518 namespace {
519 #if defined(USE_MURMUR)
520 //-----------------------------------------------------------------------------
521 // MurmurHash2 was written by Austin Appleby, and is placed in the public
522 // domain. The author hereby disclaims copyright to this source code.
523
524 // Note - This code makes a few assumptions about how your machine behaves -
525
526 // 1. We can read a 4-byte value from any address without crashing
527 // 2. sizeof(int) == 4
528
529 // And it has a few limitations -
530
531 // 1. It will not work incrementally.
532 // 2. It will not produce the same results on little-endian and big-endian
533 // machines.
534
535 //-----------------------------------------------------------------------------
536 // MurmurHash2, 64-bit versions, by Austin Appleby
537
538 // The same caveats as 32-bit MurmurHash2 apply here - beware of alignment
539 // and endian-ness issues if used across multiple platforms.
540
541 // 64-bit hash for 64-bit platforms
542 #define BIG_CONSTANT(x) (x##LLU)
543 uint64_t MurmurHash64A(const size_t key)
544 {
545 // NOTE: Not general purpose -- optimized for single 'size_t key'
546 const uint64_t m = BIG_CONSTANT(0xc6a4a7935bd1e995) * 8;
547 const int r = 47;
548 const int seed = 24713;
549
550 uint64_t h = seed ^ (m);
551
552 uint64_t k = key;
553
554 k *= m;
555 k ^= k >> r;
556 k *= m;
557
558 h ^= k;
559 h *= m;
560
561 h ^= h >> r;
562 h *= m;
563 h ^= h >> r;
564
565 return h;
566 }
567 #endif
568 } // namespace