Debian Codegraph @

packages/seacas/libraries/ioss/src/exodus/Ioex_DecompositionData.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_DecompositionUtils.h"
9 #include "exodus/Ioex_DecompositionData.h"
10
11 #if defined PARALLEL_AWARE_EXODUS
12 #include "Ioss_ElementTopology.h"
13 #include "Ioss_Field.h"
14 #include "Ioss_IOFactory.h"
15 #include "Ioss_Map.h"
16 #include "Ioss_PropertyManager.h"
17 #include "Ioss_Region.h"
18 #include "Ioss_SmartAssert.h"
19 #include "Ioss_Sort.h"
20 #include "Ioss_Utils.h"
21 #include "exodus/Ioex_Utils.h"
22
23 #include <algorithm>
24 #include <cassert>
25 #include <climits>
26 #include <cmath>
27 #include <cstdlib>
28 #include <cstring>
29 #include <fmt/ostream.h>
30 #include <iostream>
31 #include <iterator>
32 #include <map>
33 #include <numeric>
34 #include <utility>
35
36 #if !defined(NO_PARMETIS_SUPPORT)
37 #include <parmetis.h>
38 #endif
39
40 #if !defined(NO_ZOLTAN_SUPPORT)
41 #include <zoltan.h>
42 #include <zoltan_cpp.h>
43 #endif
44
45 namespace {
46
47 // ZOLTAN Callback functions...
48 #if !defined(NO_ZOLTAN_SUPPORT)
49 int zoltan_num_dim(void *data, int *ierr)
50 {
51 // Return dimensionality of coordinate data.
52 Ioex::DecompositionDataBase *zdata = reinterpret_cast<Ioex::DecompositionDataBase *>(data);
53
54 *ierr = ZOLTAN_OK;
55 return zdata->spatial_dimension();
56 }
57
58 int zoltan_num_obj(void *data, int *ierr)
59 {
60 // Return number of objects (element count) on this processor...
61 Ioex::DecompositionDataBase *zdata = reinterpret_cast<Ioex::DecompositionDataBase *>(data);
62
63 *ierr = ZOLTAN_OK;
64 return zdata->decomp_elem_count();
65 }
66
67 void zoltan_obj_list(void *data, int ngid_ent, int /*nlid_ent*/, ZOLTAN_ID_PTR gids,
68 ZOLTAN_ID_PTR lids, int wdim, float *wgts, int *ierr)
69 {
70 // Return list of object IDs, both local and global.
71 Ioex::DecompositionDataBase *zdata = reinterpret_cast<Ioex::DecompositionDataBase *>(data);
72
73 // At the time this is called, we don't have much information
74 // These routines are the ones that are developing that
75 // information...
76 size_t element_count = zdata->decomp_elem_count();
77 size_t element_offset = zdata->decomp_elem_offset();
78
79 *ierr = ZOLTAN_OK;
80
81 if (lids != nullptr) {
82 std::iota(lids, lids + element_count, 0);
83 }
84
85 if (wdim != 0) {
86 if (zdata->weights().empty()) {
87 std::fill(wgts, wgts + element_count, 1.0);
88 }
89 else {
90 std::copy(zdata->weights().begin(), zdata->weights().end(), &wgts[0]);
91 }
92 }
93
94 if (ngid_ent == 1) {
95 std::iota(gids, gids + element_count, element_offset);
96 }
97 else if (ngid_ent == 2) {
98 int64_t *global_ids = reinterpret_cast<int64_t *>(gids);
99 std::iota(global_ids, global_ids + element_count, element_offset);
100 }
101 else {
102 *ierr = ZOLTAN_FATAL;
103 }
104 return;
105 }
106
107 void zoltan_geom(void *data, int /*ngid_ent*/, int /*nlid_ent*/, int /*nobj*/,
108 ZOLTAN_ID_PTR /*gids*/, ZOLTAN_ID_PTR /*lids*/, int /*ndim*/, double *geom,
109 int *ierr)
110 {
111 // Return coordinates for objects.
112 Ioex::DecompositionDataBase *zdata = reinterpret_cast<Ioex::DecompositionDataBase *>(data);
113
114 std::copy(zdata->centroids().begin(), zdata->centroids().end(), &geom[0]);
115
116 *ierr = ZOLTAN_OK;
117 }
118 #endif
119
120 } // namespace
121
122 namespace Ioex {
123 template DecompositionData<int>::DecompositionData(const Ioss::PropertyManager &props,
124 Ioss_MPI_Comm communicator);
125 template DecompositionData<int64_t>::DecompositionData(const Ioss::PropertyManager &props,
126 Ioss_MPI_Comm communicator);
127
128 template <typename INT>
129 DecompositionData<INT>::DecompositionData(const Ioss::PropertyManager &props,
130 Ioss_MPI_Comm communicator)
131 : DecompositionDataBase(communicator), m_decomposition(props, communicator)
132 {
133 Ioss::ParallelUtils pu(comm_);
134 m_processor = pu.parallel_rank();
135 m_processorCount = pu.parallel_size();
136 }
137
138 template <typename INT>
139 void DecompositionData<INT>::decompose_model(int filePtr, const std::string &filename)
140 {
141 m_decomposition.show_progress(__func__);
142 // Initial decomposition is linear where processor #p contains
143 // elements from (#p * #element/#proc) to (#p+1 * #element/#proc)
144
145 ex_init_params info{};
146 ex_get_init_ext(filePtr, &info);
147
148 size_t globalElementCount = info.num_elem;
149 size_t globalNodeCount = info.num_nodes;
150 m_decomposition.m_spatialDimension = info.num_dim;
151 el_blocks.resize(info.num_elem_blk);
152
153 // Generate element_dist/node_dist -- size proc_count + 1
154 // processor p contains all elements/nodes from X_dist[p] .. X_dist[p+1]
155 m_decomposition.generate_entity_distributions(globalNodeCount, globalElementCount);
156
157 if (!m_decomposition.m_lineDecomp) {
158 generate_adjacency_list(filePtr, m_decomposition);
159 }
160
161 #if IOSS_DEBUG_OUTPUT
162 fmt::print(Ioss::DebugOut(), "Processor {} has {} elements; offset = {}\n", m_processor,
163 fmt::group_digits(decomp_elem_count()), fmt::group_digits(decomp_elem_offset()));
164 fmt::print(Ioss::DebugOut(), "Processor {} has {} nodes; offset = {}\n", m_processor,
165 fmt::group_digits(decomp_node_count()), fmt::group_digits(decomp_node_offset()));
166 #endif
167
168 if (!m_decomposition.m_lineDecomp && m_decomposition.needs_centroids()) {
169 // Get my coordinate data using direct exodus calls
170 size_t size = decomp_node_count();
171 if (size == 0) {
172 size = 1; // Workaround for ambiguity in ex_get_partial_coord
173 }
174
175 std::vector<double> x(size);
176 std::vector<double> y;
177 std::vector<double> z;
178 if (m_decomposition.m_spatialDimension > 1) {
179 y.resize(size);
180 }
181 if (m_decomposition.m_spatialDimension > 2) {
182 z.resize(size);
183 }
184
185 m_decomposition.show_progress("\tex_get_partial_coord");
186 ex_get_partial_coord(filePtr, decomp_node_offset() + 1, decomp_node_count(), Data(x), Data(y),
187 Data(z));
188
189 m_decomposition.calculate_element_centroids(x, y, z);
190 }
191
192 if (m_decomposition.m_method == "MAP") {
193 // Need to read and store the element_to_processor map data into `m_elementToProc` vector.
194 auto pos = m_decomposition.m_decompExtra.find(",");
195 std::string map_name = m_decomposition.m_decompExtra.substr(0, pos);
196 bool map_read = false;
197 int map_count = ex_inquire_int(filePtr, EX_INQ_ELEM_MAP);
198 if (map_count > 0) {
199 int max_name_length = ex_inquire_int(filePtr, EX_INQ_DB_MAX_USED_NAME_LENGTH);
200 max_name_length = max_name_length < 32 ? 32 : max_name_length;
201 char **names = Ioex::get_name_array(map_count, max_name_length);
202 ex_get_names(filePtr, EX_ELEM_MAP, names);
203
204 for (int i = 0; i < map_count; i++) {
205 if (std::string(names[i]) == map_name) {
206 m_decomposition.m_elementToProc.resize(decomp_elem_count());
207 if (sizeof(INT) == 4) {
208 ex_get_partial_num_map(filePtr, EX_ELEM_MAP, i + 1, decomp_elem_offset() + 1,
209 decomp_elem_count(), Data(m_decomposition.m_elementToProc));
210 }
211 else {
212 std::vector<INT> tmp_map(decomp_elem_count());
213 ex_get_partial_num_map(filePtr, EX_ELEM_MAP, i + 1, decomp_elem_offset() + 1,
214 decomp_elem_count(), Data(tmp_map));
215 std::copy(tmp_map.begin(), tmp_map.end(), m_decomposition.m_elementToProc.begin());
216 }
217 map_read = true;
218 break;
219 }
220 }
221 Ioex::delete_name_array(names, map_count);
222 }
223
224 if (!map_read) {
225 if (m_processor == 0) {
226 fmt::print(stderr,
227 "\nERROR: Element decomposition map '{}' could not be read from file.\n",
228 map_name);
229 }
230 exit(EXIT_FAILURE);
231 }
232 }
233
234 if (m_decomposition.m_method == "VARIABLE") {
235 // Need to read and store the element_to_processor variable data into `m_elementToProc`
236 // vector.
237
238 auto pos = m_decomposition.m_decompExtra.find(",");
239 std::string var_name = m_decomposition.m_decompExtra.substr(0, pos);
240 int var_index = 0;
241 int var_count = ex_inquire_int(filePtr, EX_INQ_NUM_ELEM_BLOCK_VAR);
242 if (var_count > 0) {
243 int max_name_length = ex_inquire_int(filePtr, EX_INQ_DB_MAX_USED_NAME_LENGTH);
244 max_name_length = max_name_length < 32 ? 32 : max_name_length;
245 char **names = Ioex::get_name_array(var_count, max_name_length);
246 ex_get_variable_names(filePtr, EX_ELEM_BLOCK, var_count, names);
247
248 for (int i = 0; i < var_count; i++) {
249 if (Ioss::Utils::str_equal(std::string(names[i]), var_name)) {
250 var_index = i + 1;
251 break;
252 }
253 }
254 Ioex::delete_name_array(names, var_count);
255 }
256
257 if (var_index == 0) {
258 if (m_processor == 0) {
259 fmt::print(stderr,
260 "\nERROR: Element decomposition variable '{}' does not exist on database.\n",
261 var_name);
262 }
263 exit(EXIT_FAILURE);
264 }
265
266 size_t block_count = el_blocks.size();
267 for (size_t i = 0; i < block_count; i++) {
268 size_t count = get_block_element_count(i);
269 size_t offset = get_block_element_offset(i);
270 std::vector<double> file_data(count);
271 ex_get_partial_var(filePtr, 1, EX_ELEM_BLOCK, var_index, el_blocks[i].id_, offset + 1,
272 count, Data(file_data));
273
274 for (double value : file_data) {
275 m_decomposition.m_elementToProc.push_back((int)value);
276 }
277 }
278 }
279
280 if (m_decomposition.m_lineDecomp) {
281 // For first iteration of this, we do the line-decomp modified decomposition on a single rank
282 // and then communicate the m_elementToProc vector to each of the ranks. This is then used
283 // do do the parallel distributions/decomposition of the elements assuming a "guided"
284 // decomposition.
285 std::vector<int> element_to_proc_global{};
286
287 m_decomposition.show_progress("***LINE_DECOMPOSE BEGIN***");
288 if (m_processor == 0) {
289 Ioss::PropertyManager properties;
290 Ioss::DatabaseIO *dbi = Ioss::IOFactory::create(
291 "exodus", filename, Ioss::READ_RESTART, Ioss::ParallelUtils::comm_self(), properties);
292
293 // Set integer size to match what the caller is using
294 if (sizeof(INT) == 8) {
295 dbi->set_int_byte_size_api(Ioss::USE_INT64_API);
296 }
297 else {
298 dbi->set_int_byte_size_api(Ioss::USE_INT32_API);
299 }
300
301 Ioss::Region region(dbi, "line_decomp_region");
302
303 Ioss::DecompUtils::line_decompose(region, m_processorCount, m_decomposition.m_method,
304 m_decomposition.m_decompExtra, element_to_proc_global,
305 INT(0));
306
307 if (m_decomposition.m_showHWM || m_decomposition.m_showProgress) {
308 auto work_per_rank =
309 Ioss::DecompUtils::get_work_per_rank(element_to_proc_global, m_processorCount);
310 Ioss::DecompUtils::output_decomposition_statistics(work_per_rank);
311 }
312 }
313 // Now broadcast the parts of the `element_to_proc_global`
314 // vector to the owning ranks in the initial linear
315 // decomposition...
316
317 std::vector<int> sendcounts(m_processorCount);
318 std::vector<int> displs(m_processorCount);
319 m_decomposition.m_elementToProc.resize(decomp_elem_count());
320
321 // calculate send counts and displacements
322 int sum = 0;
323 int rem = globalElementCount % m_processorCount;
324 for (int i = 0; i < m_processorCount; i++) {
325 sendcounts[i] = globalElementCount / m_processorCount;
326 if (rem > 0) {
327 sendcounts[i]++;
328 rem--;
329 }
330 displs[i] = sum;
331 sum += sendcounts[i];
332 }
333 MPI_Scatterv(Data(element_to_proc_global), Data(sendcounts), Data(displs), MPI_INT,
334 Data(m_decomposition.m_elementToProc), decomp_elem_count(), MPI_INT, 0,
335 m_decomposition.m_comm);
336 m_decomposition.m_method = "LINE_DECOMP";
337 m_decomposition.show_progress("***LINE_DECOMPOSE END***");
338 }
339
340 if (m_decomposition.m_lineDecomp) {
341 // Do not combine into previous if block since we want to release memory for
342 // the local vectors in that block before allocating the large adjacency vector.
343 generate_adjacency_list(filePtr, m_decomposition);
344 }
345
346 #if !defined(NO_ZOLTAN_SUPPORT)
347 float version = 0.0;
348 Zoltan_Initialize(0, nullptr, &version);
349
350 Zoltan zz(comm_);
351
352 // Register Zoltan Callback functions...
353 zz.Set_Num_Obj_Fn(zoltan_num_obj, this);
354 zz.Set_Obj_List_Fn(zoltan_obj_list, this);
355 zz.Set_Num_Geom_Fn(zoltan_num_dim, this);
356 zz.Set_Geom_Multi_Fn(zoltan_geom, this);
357 #endif
358
359 m_decomposition.decompose_model(
360 #if !defined(NO_ZOLTAN_SUPPORT)
361 zz,
362 #endif
363 el_blocks);
364
365 if (info.num_node_sets > 0) {
366 get_nodeset_data(filePtr, info.num_node_sets);
367 }
368
369 if (info.num_side_sets > 0) {
370 // Create elemGTL map which is used for sidesets (also element sets)
371 build_global_to_local_elem_map();
372 get_sideset_data(filePtr, info.num_side_sets);
373 }
374
375 // Have all the decomposition data needed
376 // Can now populate the Ioss metadata...
377 m_decomposition.show_progress("\tFinished with Ioex::decompose_model");
378
379 if (m_decomposition.m_showHWM || m_decomposition.m_showProgress) {
380 int64_t min, max, avg;
381 m_decomposition.m_pu.hwm_memory_stats(min, max, avg);
382 int64_t MiB = 1024 * 1024;
383 if (m_processor == 0) {
384 fmt::print(Ioss::DebugOut(),
385 "\n\tHigh Water Memory at end of Decomposition: {}M {}M {}M\n", min / MiB,
386 max / MiB, avg / MiB);
387 }
388 }
389 }
390
391 template <typename INT>
392 void DecompositionData<INT>::generate_adjacency_list(int filePtr,
393 Ioss::Decomposition<INT> &decomposition)
394 {
395 m_decomposition.show_progress(__func__);
396 // Range of elements currently handled by this processor [)
397 size_t p_start = decomp_elem_offset();
398 size_t p_end = p_start + decomp_elem_count();
399
400 size_t block_count = el_blocks.size();
401
402 std::vector<ex_block> ebs(block_count);
403 std::vector<INT> ids(block_count);
404 ex_get_ids(filePtr, EX_ELEM_BLOCK, Data(ids));
405
406 size_t sum = 0; // Size of adjacency vector.
407 size_t offset = 0;
408
409 // Get the global element block index list at this time also.
410 // The global element at index 'I' (0-based) is on block B
411 // if global_block_index[B] <= I && global_block_index[B+1] < I
412 decomposition.m_fileBlockIndex.resize(block_count + 1);
413
414 for (size_t b = 0; b < block_count; b++) {
415 el_blocks[b].id_ = ids[b];
416 ebs[b].id = ids[b];
417 ebs[b].type = EX_ELEM_BLOCK;
418 ex_get_block_param(filePtr, &ebs[b]);
419
420 // Range of elements in element block b [)
421 size_t b_start = offset; // offset is index of first element in this block...
422 offset += ebs[b].num_entry;
423 size_t b_end = b_start + ebs[b].num_entry;
424
425 if (b_start < p_end && p_start < b_end) {
426 // Some of this blocks elements are on this processor...
427 size_t overlap = std::min(b_end, p_end) - std::max(b_start, p_start);
428 size_t element_nodes = ebs[b].num_nodes_per_entry;
429
430 sum += overlap * element_nodes;
431 }
432 decomposition.m_fileBlockIndex[b + 1] = decomposition.m_fileBlockIndex[b] + ebs[b].num_entry;
433 el_blocks[b].topologyType = ebs[b].topology;
434 if (ebs[b].num_entry == 0 && (std::strcmp(ebs[b].topology, "nullptr") == 0)) {
435 el_blocks[b].topologyType = "sphere";
436 }
437 el_blocks[b].globalCount = ebs[b].num_entry;
438 el_blocks[b].nodesPerEntity = ebs[b].num_nodes_per_entry;
439 el_blocks[b].attributeCount = ebs[b].num_attribute;
440 }
441
442 // Check that the number of elements matches the m_fileBlockIndex[b+1] entry.
443 // Reading a corrupt mesh in which there are elements not in an element block
444 // can cause hard to track down problems...
445 if (decomposition.m_globalElementCount != decomposition.m_fileBlockIndex[block_count]) {
446 std::ostringstream errmsg;
447 fmt::print(errmsg,
448 "ERROR: The sum of the element counts in each element block gives a total of {} "
449 "elements.\n"
450 " This does not match the total element count of {} which indicates a "
451 "corrupt mesh description.\n"
452 " Contact [email protected] for more details.\n",
453 decomposition.m_fileBlockIndex[block_count], decomposition.m_globalElementCount);
454 IOSS_ERROR(errmsg);
455 }
456
457 // Make sure 'sum' can fit in INT...
458 INT tmp_sum = (INT)sum;
459 if ((size_t)tmp_sum != sum) {
460 std::ostringstream errmsg;
461 fmt::print(
462 errmsg,
463 "ERROR: The decomposition of this mesh requires 64-bit integers, but is being\n"
464 " run with 32-bit integer code. Please rerun with the property INTEGER_SIZE_API\n"
465 " set to 8. The details of how to do this vary with the code that is being run.\n"
466 " Contact [email protected] for more details.\n");
467 IOSS_ERROR(errmsg);
468 }
469
470 decomposition.m_pointer.reserve(decomp_elem_count() + 1);
471 decomposition.m_adjacency.reserve(sum);
472
473 // Now, populate the vectors...
474 offset = 0;
475 sum = 0; // Size of adjacency vector.
476
477 for (const auto &block : ebs) {
478 // Range of elements in element block b [)
479 size_t b_start = offset; // offset is index of first element in this block...
480 offset += block.num_entry;
481 size_t b_end = b_start + block.num_entry;
482
483 m_decomposition.show_progress("\tex_get_partial_conn loop");
484 if (b_start < p_end && p_start < b_end) {
485 // Some of this blocks elements are on this processor...
486 size_t overlap = std::min(b_end, p_end) - std::max(b_start, p_start);
487 size_t element_nodes = block.num_nodes_per_entry;
488 int64_t id = block.id;
489
490 // Get the connectivity (raw) for this portion of elements...
491 std::vector<INT> connectivity(overlap * element_nodes);
492 size_t blk_start = std::max(b_start, p_start) - b_start + 1;
493 #if IOSS_DEBUG_OUTPUT
494 fmt::print(Ioss::DebugOut(), "Processor {} has {} elements on element block {}\n",
495 m_processor, fmt::group_digits(overlap), id);
496 #endif
497 ex_get_partial_conn(filePtr, EX_ELEM_BLOCK, id, blk_start, overlap, Data(connectivity),
498 nullptr, nullptr);
499 size_t el = 0;
500 for (size_t elem = 0; elem < overlap; elem++) {
501 decomposition.m_pointer.push_back(decomposition.m_adjacency.size());
502 for (size_t k = 0; k < element_nodes; k++) {
503 INT node = connectivity[el++] - 1; // 0-based node
504 decomposition.m_adjacency.push_back(node);
505 }
506 }
507 sum += overlap * element_nodes;
508 }
509 else {
510 int64_t id = block.id;
511 ex_get_partial_conn(filePtr, EX_ELEM_BLOCK, id, 0, 0, nullptr, nullptr, nullptr);
512 }
513 }
514 decomposition.m_pointer.push_back(decomposition.m_adjacency.size());
515 }
516
517 template <typename INT>
518 void
519 DecompositionData<INT>::get_common_set_data(int filePtr, ex_entity_type set_type,
520 std::vector<Ioss::SetDecompositionData> &entity_sets,
521 const std::string &set_type_name)
522 {
523 int root = 0; // Root processor that reads all entityset bulk data (entitylists)
524 size_t set_count = entity_sets.size();
525
526 std::vector<ex_set> sets(set_count);
527 std::vector<INT> ids(set_count);
528 ex_get_ids(filePtr, set_type, Data(ids));
529
530 for (size_t i = 0; i < set_count; i++) {
531 entity_sets[i].id_ = ids[i];
532 sets[i].id = ids[i];
533 sets[i].type = set_type;
534 sets[i].entry_list = nullptr;
535 sets[i].extra_list = nullptr;
536 sets[i].distribution_factor_list = nullptr;
537 }
538
539 ex_get_sets(filePtr, sets.size(), Data(sets));
540
541 // Get total length of nset entitylists...
542 size_t entitylist_size = 0;
543 for (size_t i = 0; i < set_count; i++) {
544 entitylist_size += sets[i].num_entry;
545 entity_sets[i].fileCount = sets[i].num_entry;
546 entity_sets[i].distributionFactorCount = sets[i].num_distribution_factor;
547 }
548
549 // Calculate the max "buffer" size usable for storing entityset
550 // entitylists. This is basically the space used to store the file
551 // decomposition nodal coordinates. The "nodeCount/2*2" is to
552 // equalize the nodeCount among processors since some procs have 1
553 // more node than others. For small models, assume we can handle
554 // at least 10000 entities.
555 size_t node_memory = ((decomp_node_count() + 1) / 2) * 2 * 3 * sizeof(double) / sizeof(INT);
556 size_t max_size = std::max((size_t)100000, node_memory);
557 if (1.05 * max_size > entitylist_size) {
558 max_size = entitylist_size;
559 }
560 else {
561 // Adjust so incremental reads are all about same size...
562 // Reduces size of largest broadcast...
563 int splits = (entitylist_size + max_size - 1) / max_size;
564 max_size = (entitylist_size + splits - 1) / splits;
565 }
566
567 size_t one = 1;
568 if (entitylist_size >= one << 31) {
569 std::ostringstream errmsg;
570 fmt::print(errmsg,
571 "ERROR: The sum of the {} entity counts is larger than 2.1 Billion "
572 " which cannot be correctly handled with the current IOSS decomposition "
573 "implementation.\n"
574 " Contact [email protected] for more details.\n",
575 set_type_name);
576 IOSS_ERROR(errmsg);
577 }
578
579 std::vector<INT> entitylist(max_size);
580 std::vector<INT> set_entities_read(set_count);
581
582 size_t offset = 0; // What position are we filling in entitylist.
583 int64_t remain = max_size; // Amount of space left in entitylist.
584 size_t ibeg = 0;
585 size_t total_read = 0;
586 for (size_t i = 0; i < set_count; i++) {
587 int64_t entitys_to_read = sets[i].num_entry;
588 do {
589 int64_t to_read = std::min(remain, entitys_to_read);
590 if (m_processor == root) {
591 #if IOSS_DEBUG_OUTPUT
592 fmt::print(Ioss::DebugOut(), "{} {} reading {} entities from offset {}\n", set_type_name,
593 sets[i].id, fmt::group_digits(to_read),
594 fmt::group_digits(set_entities_read[i] + 1));
595 #endif
596 // Read the entitylists on root processor.
597 ex_get_partial_set(filePtr, set_type, sets[i].id, set_entities_read[i] + 1, to_read,
598 &entitylist[offset], nullptr);
599 }
600 total_read += to_read;
601 entitys_to_read -= to_read;
602 remain -= to_read;
603 offset += to_read;
604 if (remain == 0 || total_read == entitylist_size) {
605 // entitylist is full at this point...
606 // * Broadcast data to other processors
607 // * Each processor extracts the entities it manages.
608 m_decomposition.show_progress("\tBroadcast entitylist begin");
609 Ioss::ParallelUtils pu(comm_);
610 pu.broadcast(entitylist, root);
611 m_decomposition.show_progress("\tBroadcast entitylist end");
612
613 // Each processor now has same list of entities in entitysets (i_beg ... i)
614 // Determine which of these are owned by the current
615 // processor...
616 offset = 0; // Just got new list of entities; starting at beginning.
617 for (size_t j = ibeg; j <= i; j++) {
618 size_t set_offset = set_entities_read[j];
619 size_t ns_beg = offset;
620 size_t num_in_this_set = sets[j].num_entry - set_offset;
621 size_t ns_end = std::min(ns_beg + num_in_this_set, max_size);
622
623 for (size_t n = ns_beg; n < ns_end; n++) {
624 INT entity = entitylist[n];
625 // See if entity owned by this processor...
626 bool owned = (set_type == EX_NODE_SET) ? i_own_node(entity) : i_own_elem(entity);
627 if (owned) {
628 // Save entity in this processors entitylist for this set.
629 // The saved data is this entities location in the global
630 // entitylist for this set.
631 entity_sets[j].entitylist_map.push_back(n - offset + set_offset);
632 }
633 }
634 offset = ns_end;
635 set_entities_read[j] += ns_end - ns_beg;
636 }
637 remain = max_size;
638 offset = 0;
639 ibeg = (entitys_to_read == 0) ? i + 1 : i;
640 }
641 } while (entitys_to_read > 0);
642 }
643
644 // Each processor knows how many of the entityset entities it
645 // owns;
646 //
647 // The first processor with non-zero entity count is the
648 // "root" for this entityset.
649 //
650 // A split communicator is created
651 // containing only the ranks that have non-zero entity count
652 {
653 std::vector<int> has_entitys_local(set_count);
654 for (size_t i = 0; i < set_count; i++) {
655 has_entitys_local[i] = entity_sets[i].entitylist_map.empty() ? 0 : 1;
656 }
657
658 std::vector<int> has_entitys(set_count * m_processorCount);
659 MPI_Allgather(Data(has_entitys_local), has_entitys_local.size(), MPI_INT, Data(has_entitys),
660 has_entitys_local.size(), MPI_INT, comm_);
661
662 for (size_t i = 0; i < set_count; i++) {
663 entity_sets[i].hasEntities.resize(m_processorCount);
664 entity_sets[i].root_ = m_processorCount;
665 for (int p = 0; p < m_processorCount; p++) {
666 if (p < entity_sets[i].root_ && has_entitys[p * set_count + i] != 0) {
667 entity_sets[i].root_ = p;
668 }
669 entity_sets[i].hasEntities[p] = has_entitys[p * set_count + i];
670 }
671 int color = entity_sets[i].hasEntities[m_processor] ? 1 : MPI_UNDEFINED;
672 MPI_Comm_split(comm_, color, m_processor, &entity_sets[i].setComm_);
673 }
674 }
675 }
676
677 template <typename INT>
678 void DecompositionData<INT>::get_nodeset_data(int filePtr, size_t set_count)
679 {
680 // Issues:
681 // 1. Large node count in nodeset(s) that could overwhelm a single
682 // processor. For example, every node in the model is in a
683 // nodeset.
684 // -- Cannot blindly read all nodeset data on proc 0 and
685 // broadcast.
686 //
687 // 2. Lots of small nodesets. Communication involving all
688 // processors would result in lots of overhead if done on a
689 // nodeset by nodeset basis.
690 //
691 // 3. Application code will be requesting nodeset data on a
692 // nodeset-by-nodeset basis. If communication needed each
693 // time, this could cause lots of slowdown/overhead...
694 // However, if cache the data, it could eat up memory.
695 //
696 // 4. Many models only need nodeset node list and then do nothing
697 // else with the nodeset data.
698 //
699 // 5. Only the processors that own any of the nodes in a nodeset
700 // should read that nodeset.
701 // -- What if nodelist is big and non-sorted so that a
702 // processors nodes cannot be read in a single contiguous
703 // read...
704 //
705 // 6. Alternatively, only processor 0 reads, but only communicates
706 // to the processors that have nodes in the nodeset.
707 // == REMEMBER: nodes are shared, so a node could be sent to
708 // multiple processors.
709 //
710 // Assumptions:
711 // 1. Since we have already read coordinate data, we probably have
712 // some extra memory we can afford to use without increasing
713 // the high-water mark.
714 // -- Read the nodeset node lists in groups of size
715 // (3*globNodeCount/procCount*sizeof(double)/sizeof(INT)) or
716 // less.
717
718 int root = 0; // Root processor that reads all nodeset bulk data (nodelists)
719 int old_par_setting = ex_set_parallel(filePtr, 0);
720
721 node_sets.resize(set_count);
722 get_common_set_data(filePtr, EX_NODE_SET, node_sets, "NodeSet");
723
724 // Check nodeset distribution factors to determine whether they
725 // are all constant or if they contain varying values that must
726 // be communicated. If constant or empty, then they can be
727 // "read" with no communication.
728 std::vector<double> df_valcon(2 * set_count);
729 if (m_processor == root) {
730 for (size_t i = 0; i < set_count; i++) {
731 df_valcon[2 * i + 0] = 1.0;
732 df_valcon[2 * i + 1] = 1;
733 if (node_sets[i].df_count() > 0) {
734 std::vector<double> df(node_sets[i].df_count());
735 ex_get_set_dist_fact(filePtr, EX_NODE_SET, node_sets[i].id(), Data(df));
736 double val = df[0];
737 df_valcon[2 * i] = val;
738 for (size_t j = 1; j < node_sets[i].df_count(); j++) {
739 if (val != df[j]) {
740 df_valcon[2 * i + 1] = 0;
741 }
742 }
743 }
744 }
745 }
746
747 // Tell other processors
748 Ioss::ParallelUtils pu(comm_);
749 pu.broadcast(df_valcon, root);
750
751 for (size_t i = 0; i < set_count; i++) {
752 node_sets[i].distributionFactorCount = node_sets[i].ioss_count();
753 node_sets[i].distributionFactorValue = df_valcon[2 * i + 0];
754 node_sets[i].distributionFactorConstant = (df_valcon[2 * i + 1] == 1.0);
755 }
756 ex_set_parallel(filePtr, old_par_setting);
757 }
758
759 template <typename INT>
760 void DecompositionData<INT>::get_sideset_data(int filePtr, size_t set_count)
761 {
762 m_decomposition.show_progress(__func__);
763
764 int root = 0; // Root processor that reads all sideset bulk data (nodelists)
765 int old_par_setting = ex_set_parallel(filePtr, 0);
766
767 side_sets.resize(set_count);
768 get_common_set_data(filePtr, EX_SIDE_SET, side_sets, "SideSet");
769
770 // Check sideset distribution factors to determine whether they
771 // are all constant or if they contain varying values that must
772 // be communicated. If constant or empty, then they can be
773 // "read" with no communication.
774
775 std::vector<double> df_valcon(3 * set_count);
776 // df_valcon[3*i + 0] = if df constant, this is the constant value
777 // df_valcon[3*i + 1] = 1 if df constant, 0 if variable
778 // df_valcon[3*i + 2] = value = nodecount if all faces have same node count;
779 // = -1 if variable
780 // (0 if df values are constant)
781
782 if (m_processor == root) {
783 for (size_t i = 0; i < set_count; i++) {
784 df_valcon[3 * i + 0] = 1.0;
785 df_valcon[3 * i + 1] = 1;
786 df_valcon[3 * i + 2] = 0;
787 if (side_sets[i].df_count() > 0) {
788 std::vector<double> df(side_sets[i].df_count());
789 // TODO: For large sideset, split into multiple reads to avoid
790 // peaking the memory
791 ex_get_set_dist_fact(filePtr, EX_SIDE_SET, side_sets[i].id(), Data(df));
792 double val = df[0];
793 df_valcon[3 * i] = val;
794 for (size_t j = 1; j < side_sets[i].df_count(); j++) {
795 if (val != df[j]) {
796 df_valcon[3 * i + 1] = 0;
797 break;
798 }
799 }
800 Ioss::Utils::clear(df);
801 if (df_valcon[3 * i + 1] == 1.0) { // df are constant.
802 df_valcon[3 * i + 2] = 0.0;
803 }
804 else {
805
806 // To determine the size of the df field on the
807 // ioss-decomp sidesets, need to know how many nodes per
808 // side there are for all sides in the sideset. Here we
809 // check to see if it is a constant number to avoid
810 // communicating the entire list for all sidesets. If not
811 // constant, then we will have to communicate.
812 std::vector<int> nodes_per_face(side_sets[i].file_count());
813 ex_get_side_set_node_count(filePtr, side_sets[i].id(), Data(nodes_per_face));
814 int nod_per_face = nodes_per_face[0];
815 for (size_t j = 1; j < nodes_per_face.size(); j++) {
816 if (nodes_per_face[j] != nod_per_face) {
817 nod_per_face = -1;
818 break;
819 }
820 }
821 df_valcon[3 * i + 2] = static_cast<double>(nod_per_face);
822 }
823 }
824 }
825 }
826
827 // Tell other processors
828 {
829 m_decomposition.show_progress("\tBroadcast df_valcon begin");
830 Ioss::ParallelUtils pu(comm_);
831 pu.broadcast(df_valcon, root);
832 m_decomposition.show_progress("\tBroadcast df_valcon end");
833 for (size_t i = 0; i < set_count; i++) {
834 side_sets[i].distributionFactorValue = df_valcon[3 * i + 0];
835 side_sets[i].distributionFactorConstant = (df_valcon[3 * i + 1] == 1.0);
836 side_sets[i].distributionFactorValsPerEntity = static_cast<int>(df_valcon[3 * i + 2]);
837 }
838 }
839
840 // See if need to communicate the nodes_per_side data on any
841 // sidesets... If not, then the size of those sidesets can be
842 // set here...
843 size_t count = 0;
844 for (size_t i = 0; i < set_count; i++) {
845 if (side_sets[i].distributionFactorValsPerEntity < 0) {
846 count += side_sets[i].file_count();
847 }
848 else {
849 side_sets[i].distributionFactorCount =
850 side_sets[i].ioss_count() * side_sets[i].distributionFactorValsPerEntity;
851 }
852 }
853
854 if (count > 0) {
855 // At least 1 sideset has variable number of nodes per side...
856 std::vector<int> nodes_per_face(count); // not INT
857 if (m_processor == root) {
858 size_t offset = 0;
859 for (size_t i = 0; i < set_count; i++) {
860 if (side_sets[i].distributionFactorValsPerEntity < 0) {
861 ex_get_side_set_node_count(filePtr, side_sets[i].id(), &nodes_per_face[offset]);
862 offset += side_sets[i].file_count();
863 }
864 }
865 }
866
867 {
868 // Broadcast this data to all other processors...
869 m_decomposition.show_progress("\tBroadcast nodes_per_face begin");
870 Ioss::ParallelUtils pu(comm_);
871 pu.broadcast(nodes_per_face, root);
872 m_decomposition.show_progress("\tBroadcast nodes_per_face end");
873
874 // Each processor now has a list of the number of nodes per
875 // face for all sidesets that have a variable number. This can
876 // be used to determine the df field size on the ioss_decomp.
877 size_t offset = 0;
878 for (size_t i = 0; i < set_count; i++) {
879 if (side_sets[i].distributionFactorValsPerEntity < 0) {
880 int *npf = &nodes_per_face[offset];
881 offset += side_sets[i].file_count();
882 size_t my_count = 0;
883 for (size_t j = 0; j < side_sets[i].ioss_count(); j++) {
884 my_count += npf[side_sets[i].entitylist_map[j]];
885 }
886 side_sets[i].distributionFactorCount = my_count;
887 }
888 }
889 }
890 }
891 ex_set_parallel(filePtr, old_par_setting);
892 }
893
894 template <typename INT>
895 int DecompositionData<INT>::get_node_coordinates(int filePtr, double *ioss_data,
896 const Ioss::Field &field) const
897 {
898 m_decomposition.show_progress(__func__);
899 std::vector<double> tmp(decomp_node_count());
900
901 int ierr = 0;
902 if (field.get_name() == "mesh_model_coordinates_x") {
903 m_decomposition.show_progress("\tex_get_partial_coord X");
904 ierr = ex_get_partial_coord_component(filePtr, decomp_node_offset() + 1, decomp_node_count(),
905 1, Data(tmp));
906 if (ierr >= 0) {
907 communicate_node_data(Data(tmp), ioss_data, 1);
908 }
909 }
910
911 else if (field.get_name() == "mesh_model_coordinates_y") {
912 m_decomposition.show_progress("\tex_get_partial_coord Y");
913 ierr = ex_get_partial_coord_component(filePtr, decomp_node_offset() + 1, decomp_node_count(),
914 2, Data(tmp));
915 if (ierr >= 0) {
916 communicate_node_data(Data(tmp), ioss_data, 1);
917 }
918 }
919
920 else if (field.get_name() == "mesh_model_coordinates_z") {
921 m_decomposition.show_progress("\tex_get_partial_coord Z");
922 ierr = ex_get_partial_coord_component(filePtr, decomp_node_offset() + 1, decomp_node_count(),
923 3, Data(tmp));
924 if (ierr >= 0) {
925 communicate_node_data(Data(tmp), ioss_data, 1);
926 }
927 }
928
929 else if (field.get_name() == "mesh_model_coordinates") {
930 // Data required by upper classes store x0, y0, z0, ... xn,
931 // yn, zn. Data stored in exodusII file is x0, ..., xn, y0,
932 // ..., yn, z0, ..., zn so we have to allocate some scratch
933 // memory to read in the data and then map into supplied
934 // 'data'
935
936 std::vector<double> ioss_tmp(ioss_node_count());
937
938 // This implementation trades off extra communication for
939 // reduced memory overhead.
940 // * This method uses 'ioss_node_count' extra memory; 3
941 // reads; and 3 communicate_node_data calls.
942 //
943 // * Other method uses 6*ioss_node_count extra memory; 1 read;
944 // and 1 communicate_node_data call.
945 //
946 // * NOTE: The read difference is not real since the ex_get_partial_coord
947 // function does 3 reads internally.
948
949 for (int d = 0; d < m_decomposition.m_spatialDimension; d++) {
950 m_decomposition.show_progress("\tex_get_partial_coord XYZ");
951 ierr = ex_get_partial_coord_component(filePtr, decomp_node_offset() + 1,
952 decomp_node_count(), d + 1, Data(tmp));
953 if (ierr < 0) {
954 return ierr;
955 }
956
957 communicate_node_data(Data(tmp), Data(ioss_tmp), 1);
958
959 size_t index = d;
960 for (size_t i = 0; i < ioss_node_count(); i++) {
961 ioss_data[index] = ioss_tmp[i];
962 index += m_decomposition.m_spatialDimension;
963 }
964 }
965 }
966 return ierr;
967 }
968
969 #ifndef DOXYGEN_SKIP_THIS
970 template void DecompositionData<int>::get_block_connectivity(int filePtr, int *data, int64_t id,
971 size_t blk_seq, size_t nnpe) const;
972 template void DecompositionData<int64_t>::get_block_connectivity(int filePtr, int64_t *data,
973 int64_t id, size_t blk_seq,
974 size_t nnpe) const;
975 #endif
976 /// relates DecompositionData::get_block_connectivity
977 template <typename INT>
978 void DecompositionData<INT>::get_block_connectivity(int filePtr, INT *data, int64_t id,
979 size_t blk_seq, size_t nnpe) const
980 {
981 m_decomposition.show_progress(__func__);
982 Ioss::BlockDecompositionData blk = el_blocks[blk_seq];
983
984 // Determine number of file decomp elements are in this block and the offset into the block.
985 size_t count = get_block_element_count(blk_seq);
986 size_t offset = get_block_element_offset(blk_seq);
987
988 if (m_decomposition.m_method == "LINEAR") {
989 ex_get_partial_conn(filePtr, EX_ELEM_BLOCK, id, offset + 1, count, data, nullptr, nullptr);
990 }
991 else {
992 std::vector<INT> file_conn(count * nnpe);
993 m_decomposition.show_progress("\tex_get_partial_conn");
994 ex_get_partial_conn(filePtr, EX_ELEM_BLOCK, id, offset + 1, count, Data(file_conn), nullptr,
995 nullptr);
996 m_decomposition.communicate_block_data(Data(file_conn), data, blk, nnpe);
997 }
998
999 for (size_t i = 0; i < blk.iossCount * nnpe; i++) {
1000 data[i] = node_global_to_local(data[i]);
1001 }
1002 }
1003
1004 template <typename INT>
1005 int DecompositionData<INT>::get_var(int filePtr, int step, ex_entity_type type, int var_index,
1006 ex_entity_id id, int64_t num_entity,
1007 std::vector<double> &data) const
1008 {
1009 m_decomposition.show_progress(__func__);
1010 if (type == EX_ELEM_BLOCK) {
1011 return get_elem_var(filePtr, step, var_index, id, num_entity, data);
1012 }
1013 else if (type == EX_NODAL) {
1014 return get_node_var(filePtr, step, var_index, id, num_entity, data);
1015 }
1016 else if (type == EX_NODE_SET || type == EX_SIDE_SET) {
1017 return get_set_var(filePtr, step, var_index, type, id, num_entity, data);
1018 }
1019 else {
1020 assert(1 == 0);
1021 return -1;
1022 }
1023 }
1024
1025 /// relates DecompositionData::get_user_map
1026 template <typename INT>
1027 int DecompositionData<INT>::get_user_map(int filePtr, ex_entity_type obj_type, ex_entity_id id,
1028 int map_index, size_t offset, size_t count,
1029 void *map_data) const
1030 {
1031 m_decomposition.show_progress(__func__);
1032 if (obj_type == EX_ELEM_MAP) {
1033 return get_elem_map(filePtr, id, map_index, offset, count, map_data);
1034 }
1035 else if (obj_type == EX_NODE_MAP) {
1036 // Does not use `id`
1037 return get_node_map(filePtr, map_index, offset, count, map_data);
1038 }
1039 else {
1040 assert(1 == 0);
1041 return -1;
1042 }
1043 }
1044
1045 template <typename INT>
1046 int DecompositionData<INT>::get_attr(int filePtr, ex_entity_type obj_type, ex_entity_id id,
1047 size_t attr_count, double *attrib) const
1048 {
1049 m_decomposition.show_progress(__func__);
1050 if (attr_count == 1) {
1051 return get_one_attr(filePtr, obj_type, id, 1, attrib);
1052 }
1053
1054 if (obj_type == EX_ELEM_BLOCK) {
1055 return get_elem_attr(filePtr, id, attr_count, attrib);
1056 }
1057 else if (obj_type == EX_NODAL) {
1058 return get_node_attr(filePtr, id, attr_count, attrib);
1059 }
1060 else if (obj_type == EX_NODE_SET || obj_type == EX_SIDE_SET) {
1061 return get_set_attr(filePtr, obj_type, id, attr_count, attrib);
1062 }
1063 else {
1064 assert(1 == 0);
1065 return -1;
1066 }
1067 }
1068
1069 template <typename INT>
1070 int DecompositionData<INT>::get_one_attr(int filePtr, ex_entity_type obj_type, ex_entity_id id,
1071 int attrib_index, double *attrib) const
1072 {
1073 m_decomposition.show_progress(__func__);
1074 if (obj_type == EX_ELEM_BLOCK) {
1075 return get_one_elem_attr(filePtr, id, attrib_index, attrib);
1076 }
1077 else if (obj_type == EX_NODAL) {
1078 return get_one_node_attr(filePtr, id, attrib_index, attrib);
1079 }
1080 else if (obj_type == EX_NODE_SET || obj_type == EX_SIDE_SET) {
1081 return get_one_set_attr(filePtr, obj_type, id, attrib_index, attrib);
1082 }
1083 else {
1084 assert(1 == 0);
1085 return -1;
1086 }
1087 }
1088
1089 #ifndef DOXYGEN_SKIP_THIS
1090 template void DecompositionDataBase::communicate_node_data(int *file_data, int *ioss_data,
1091 size_t comp_count) const;
1092 template void DecompositionDataBase::communicate_node_data(int64_t *file_data, int64_t *ioss_data,
1093 size_t comp_count) const;
1094 template void DecompositionDataBase::communicate_node_data(double *file_data, double *ioss_data,
1095 size_t comp_count) const;
1096 #endif
1097
1098 template <typename T>
1099 void DecompositionDataBase::communicate_node_data(T *file_data, T *ioss_data,
1100 size_t comp_count) const
1101 {
1102 if (int_size() == sizeof(int)) {
1103 const DecompositionData<int> *this32 = dynamic_cast<const DecompositionData<int> *>(this);
1104 Ioss::Utils::check_dynamic_cast(this32);
1105 this32->communicate_node_data(file_data, ioss_data, comp_count);
1106 }
1107 else {
1108 const DecompositionData<int64_t> *this64 =
1109 dynamic_cast<const DecompositionData<int64_t> *>(this);
1110 Ioss::Utils::check_dynamic_cast(this64);
1111 this64->communicate_node_data(file_data, ioss_data, comp_count);
1112 }
1113 }
1114
1115 #ifndef DOXYGEN_SKIP_THIS
1116 template void DecompositionDataBase::communicate_element_data(int *file_data, int *ioss_data,
1117 size_t comp_count) const;
1118 template void DecompositionDataBase::communicate_element_data(int64_t *file_data,
1119 int64_t *ioss_data,
1120 size_t comp_count) const;
1121 template void DecompositionDataBase::communicate_element_data(double *file_data,
1122 double *ioss_data,
1123 size_t comp_count) const;
1124 #endif
1125
1126 template <typename T>
1127 void DecompositionDataBase::communicate_element_data(T *file_data, T *ioss_data,
1128 size_t comp_count) const
1129 {
1130 if (int_size() == sizeof(int)) {
1131 const DecompositionData<int> *this32 = dynamic_cast<const DecompositionData<int> *>(this);
1132 Ioss::Utils::check_dynamic_cast(this32);
1133 this32->communicate_element_data(file_data, ioss_data, comp_count);
1134 }
1135 else {
1136 const DecompositionData<int64_t> *this64 =
1137 dynamic_cast<const DecompositionData<int64_t> *>(this);
1138 Ioss::Utils::check_dynamic_cast(this64);
1139 this64->communicate_element_data(file_data, ioss_data, comp_count);
1140 }
1141 }
1142
1143 void DecompositionDataBase::get_node_entity_proc_data(void *entity_proc,
1144 const Ioss::MapContainer &node_map,
1145 bool do_map) const
1146 {
1147 if (int_size() == sizeof(int)) {
1148 const DecompositionData<int> *this32 = dynamic_cast<const DecompositionData<int> *>(this);
1149 Ioss::Utils::check_dynamic_cast(this32);
1150 this32->m_decomposition.get_node_entity_proc_data(reinterpret_cast<int *>(entity_proc),
1151 node_map, do_map);
1152 }
1153 else {
1154 const DecompositionData<int64_t> *this64 =
1155 dynamic_cast<const DecompositionData<int64_t> *>(this);
1156 Ioss::Utils::check_dynamic_cast(this64);
1157 this64->m_decomposition.get_node_entity_proc_data(reinterpret_cast<int64_t *>(entity_proc),
1158 node_map, do_map);
1159 }
1160 }
1161
1162 int DecompositionDataBase::get_set_mesh_double(int filePtr, ex_entity_type type, ex_entity_id id,
1163 const Ioss::Field &field, double *ioss_data) const
1164 {
1165 if (int_size() == sizeof(int)) {
1166 const DecompositionData<int> *this32 = dynamic_cast<const DecompositionData<int> *>(this);
1167 Ioss::Utils::check_dynamic_cast(this32);
1168 return this32->get_set_mesh_var(filePtr, type, id, field, ioss_data);
1169 }
1170 else {
1171 const DecompositionData<int64_t> *this64 =
1172 dynamic_cast<const DecompositionData<int64_t> *>(this);
1173 Ioss::Utils::check_dynamic_cast(this64);
1174 return this64->get_set_mesh_var(filePtr, type, id, field, ioss_data);
1175 }
1176 }
1177
1178 int DecompositionDataBase::get_set_mesh_var(int filePtr, ex_entity_type type, ex_entity_id id,
1179 const Ioss::Field &field, void *ioss_data) const
1180 {
1181 if (int_size() == sizeof(int)) {
1182 const DecompositionData<int> *this32 = dynamic_cast<const DecompositionData<int> *>(this);
1183 Ioss::Utils::check_dynamic_cast(this32);
1184 return this32->get_set_mesh_var(filePtr, type, id, field, (int *)ioss_data);
1185 }
1186 else {
1187 const DecompositionData<int64_t> *this64 =
1188 dynamic_cast<const DecompositionData<int64_t> *>(this);
1189 Ioss::Utils::check_dynamic_cast(this64);
1190 return this64->get_set_mesh_var(filePtr, type, id, field, (int64_t *)ioss_data);
1191 }
1192 }
1193
1194 void DecompositionDataBase::get_block_connectivity(int filePtr, void *data, int64_t id,
1195 size_t blk_seq, size_t nnpe) const
1196 {
1197 if (int_size() == sizeof(int)) {
1198 const DecompositionData<int> *this32 = dynamic_cast<const DecompositionData<int> *>(this);
1199 Ioss::Utils::check_dynamic_cast(this32);
1200 this32->get_block_connectivity(filePtr, reinterpret_cast<int *>(data), id, blk_seq, nnpe);
1201 }
1202 else {
1203 const DecompositionData<int64_t> *this64 =
1204 dynamic_cast<const DecompositionData<int64_t> *>(this);
1205 Ioss::Utils::check_dynamic_cast(this64);
1206 this64->get_block_connectivity(filePtr, reinterpret_cast<int64_t *>(data), id, blk_seq, nnpe);
1207 }
1208 }
1209
1210 const Ioss::SetDecompositionData &DecompositionDataBase::get_decomp_set(ex_entity_type type,
1211 ex_entity_id id) const
1212 {
1213 if (type == EX_NODE_SET) {
1214 for (const auto &node_set : node_sets) {
1215 if (node_set.id() == id) {
1216 return node_set;
1217 }
1218 }
1219 }
1220 else if (type == EX_SIDE_SET) {
1221 for (const auto &side_set : side_sets) {
1222 if (side_set.id() == id) {
1223 return side_set;
1224 }
1225 }
1226 }
1227
1228 if (type != EX_NODE_SET && type != EX_SIDE_SET) {
1229 std::ostringstream errmsg;
1230 fmt::print(errmsg,
1231 "ERROR: Invalid set type specified in get_decomp_set. Only node set or side set "
1232 "supported\n");
1233 IOSS_ERROR(errmsg);
1234 }
1235 else {
1236 std::string typestr = type == EX_NODE_SET ? "node set" : "side set";
1237 std::ostringstream errmsg;
1238 fmt::print(errmsg, "ERROR: Count not find {} {}\n", typestr, id);
1239 IOSS_ERROR(errmsg);
1240 }
1241 return node_sets[0];
1242 }
1243
1244 template <typename INT>
1245 size_t DecompositionData<INT>::get_block_seq(ex_entity_type type, ex_entity_id id) const
1246 {
1247 m_decomposition.show_progress(__func__);
1248 if (type == EX_ELEM_BLOCK) {
1249 for (size_t i = 0; i < el_blocks.size(); i++) {
1250 if (el_blocks[i].id() == id) {
1251 return i;
1252 }
1253 }
1254 }
1255 return el_blocks.size();
1256 }
1257
1258 template <typename INT>
1259 size_t DecompositionData<INT>::get_block_element_count(size_t blk_seq) const
1260 {
1261 m_decomposition.show_progress(__func__);
1262 // Determine number of file decomp elements are in this block;
1263 size_t bbeg = std::max(m_decomposition.m_fileBlockIndex[blk_seq], decomp_elem_offset());
1264 size_t bend = std::min(m_decomposition.m_fileBlockIndex[blk_seq + 1],
1265 decomp_elem_offset() + decomp_elem_count());
1266 size_t count = 0;
1267 if (bend > bbeg) {
1268 count = bend - bbeg;
1269 }
1270 return count;
1271 }
1272
1273 template <typename INT>
1274 size_t DecompositionData<INT>::get_block_element_offset(size_t blk_seq) const
1275 {
1276 m_decomposition.show_progress(__func__);
1277 size_t offset = 0;
1278 if (decomp_elem_offset() > m_decomposition.m_fileBlockIndex[blk_seq]) {
1279 offset = decomp_elem_offset() - m_decomposition.m_fileBlockIndex[blk_seq];
1280 }
1281 return offset;
1282 }
1283
1284 template <typename INT>
1285 int DecompositionData<INT>::get_set_var(int filePtr, int step, int var_index, ex_entity_type type,
1286 ex_entity_id id, int64_t /*num_entity*/,
1287 std::vector<double> &ioss_data) const
1288 {
1289 m_decomposition.show_progress(__func__);
1290 // Find set corresponding to the specified id...
1291 auto &set = get_decomp_set(type, id);
1292
1293 std::vector<double> file_data;
1294 int ierr = 0;
1295 if (m_processor == set.root_) {
1296 // Read the set data from the file..
1297 file_data.resize(set.file_count());
1298 ierr = ex_get_var(filePtr, step, type, var_index, id, set.file_count(), Data(file_data));
1299 }
1300 else {
1301 ierr = ex_get_partial_var(filePtr, step, type, var_index, id, 0, 0, nullptr);
1302 }
1303
1304 if (ierr >= 0) {
1305 communicate_set_data(Data(file_data), Data(ioss_data), set, 1);
1306 }
1307
1308 return ierr;
1309 }
1310
1311 template <typename INT>
1312 int DecompositionData<INT>::get_set_attr(int filePtr, ex_entity_type type, ex_entity_id id,
1313 size_t comp_count, double *ioss_data) const
1314 {
1315 m_decomposition.show_progress(__func__);
1316 // Find set corresponding to the specified id...
1317 auto &set = get_decomp_set(type, id);
1318
1319 std::vector<double> file_data;
1320 int ierr = 0;
1321 if (m_processor == set.root_) {
1322 // Read the set data from the file..
1323 file_data.resize(set.file_count() * comp_count);
1324 ierr = ex_get_attr(filePtr, type, id, Data(file_data));
1325 }
1326
1327 if (ierr >= 0) {
1328 communicate_set_data(Data(file_data), ioss_data, set, comp_count);
1329 }
1330
1331 return ierr;
1332 }
1333
1334 template <typename INT>
1335 int DecompositionData<INT>::get_one_set_attr(int filePtr, ex_entity_type type, ex_entity_id id,
1336 int attr_index, double *ioss_data) const
1337 {
1338 m_decomposition.show_progress(__func__);
1339 // Find set corresponding to the specified id...
1340 auto &set = get_decomp_set(type, id);
1341
1342 std::vector<double> file_data;
1343 int ierr = 0;
1344 if (m_processor == set.root_) {
1345 // Read the set data from the file..
1346 file_data.resize(set.file_count());
1347 ierr = ex_get_one_attr(filePtr, type, id, attr_index, Data(file_data));
1348 }
1349
1350 if (ierr >= 0) {
1351 communicate_set_data(Data(file_data), ioss_data, set, 1);
1352 }
1353
1354 return ierr;
1355 }
1356
1357 template <typename INT>
1358 int DecompositionData<INT>::get_node_var(int filePtr, int step, int var_index, ex_entity_id id,
1359 int64_t /*num_entity*/,
1360 std::vector<double> &ioss_data) const
1361 {
1362 m_decomposition.show_progress(__func__);
1363 std::vector<double> file_data(decomp_node_count());
1364 m_decomposition.show_progress("\tex_get_partial_var");
1365 int ierr = ex_get_partial_var(filePtr, step, EX_NODAL, var_index, id, decomp_node_offset() + 1,
1366 decomp_node_count(), Data(file_data));
1367
1368 if (ierr >= 0) {
1369 communicate_node_data(Data(file_data), Data(ioss_data), 1);
1370 }
1371 return ierr;
1372 }
1373
1374 template <typename INT>
1375 int DecompositionData<INT>::get_node_attr(int filePtr, ex_entity_id id, size_t comp_count,
1376 double *ioss_data) const
1377 {
1378 m_decomposition.show_progress(__func__);
1379 std::vector<double> file_data(decomp_node_count() * comp_count);
1380 int ierr = ex_get_partial_attr(filePtr, EX_NODAL, id, decomp_node_offset() + 1,
1381 decomp_node_count(), Data(file_data));
1382
1383 if (ierr >= 0) {
1384 communicate_node_data(Data(file_data), ioss_data, comp_count);
1385 }
1386 return ierr;
1387 }
1388
1389 template <typename INT>
1390 int DecompositionData<INT>::get_one_node_attr(int filePtr, ex_entity_id id, int attr_index,
1391 double *ioss_data) const
1392 {
1393 m_decomposition.show_progress(__func__);
1394 std::vector<double> file_data(decomp_node_count());
1395 int ierr = ex_get_partial_one_attr(filePtr, EX_NODAL, id, decomp_node_offset() + 1,
1396 decomp_node_count(), attr_index, Data(file_data));
1397
1398 if (ierr >= 0) {
1399 communicate_node_data(Data(file_data), ioss_data, 1);
1400 }
1401 return ierr;
1402 }
1403
1404 template <typename INT>
1405 int DecompositionData<INT>::get_elem_var(int filePtr, int step, int var_index, ex_entity_id id,
1406 int64_t /*num_entity*/,
1407 std::vector<double> &ioss_data) const
1408 {
1409 m_decomposition.show_progress(__func__);
1410 // Find blk_seq corresponding to block the specified id...
1411 size_t blk_seq = get_block_seq(EX_ELEM_BLOCK, id);
1412 size_t count = get_block_element_count(blk_seq);
1413 size_t offset = get_block_element_offset(blk_seq);
1414 int ierr = 0;
1415 m_decomposition.show_progress("\tex_get_partial_var (elem)");
1416 if (m_decomposition.m_method == "LINEAR") {
1417 ierr = ex_get_partial_var(filePtr, step, EX_ELEM_BLOCK, var_index, id, offset + 1, count,
1418 Data(ioss_data));
1419 }
1420 else {
1421 std::vector<double> file_data(count);
1422 ierr = ex_get_partial_var(filePtr, step, EX_ELEM_BLOCK, var_index, id, offset + 1, count,
1423 Data(file_data));
1424
1425 if (ierr >= 0) {
1426 m_decomposition.communicate_block_data(Data(file_data), Data(ioss_data), el_blocks[blk_seq],
1427 1);
1428 }
1429 }
1430 return ierr;
1431 }
1432
1433 template <typename INT>
1434 int DecompositionData<INT>::get_elem_attr(int filePtr, ex_entity_id id, size_t comp_count,
1435 double *ioss_data) const
1436 {
1437 m_decomposition.show_progress(__func__);
1438 // Find blk_seq corresponding to block the specified id...
1439 size_t blk_seq = get_block_seq(EX_ELEM_BLOCK, id);
1440 size_t count = get_block_element_count(blk_seq);
1441 size_t offset = count == 0 ? 0 : get_block_element_offset(blk_seq);
1442 int ierr = 0;
1443 if (m_decomposition.m_method == "LINEAR") {
1444 ierr = ex_get_partial_attr(filePtr, EX_ELEM_BLOCK, id, offset + 1, count, ioss_data);
1445 }
1446 else {
1447 std::vector<double> file_data(count * comp_count);
1448 ierr = ex_get_partial_attr(filePtr, EX_ELEM_BLOCK, id, offset + 1, count, Data(file_data));
1449 if (ierr >= 0) {
1450 m_decomposition.communicate_block_data(Data(file_data), ioss_data, el_blocks[blk_seq],
1451 comp_count);
1452 }
1453 }
1454 return ierr;
1455 }
1456
1457 template <typename INT>
1458 int DecompositionData<INT>::get_one_elem_attr(int filePtr, ex_entity_id id, int attr_index,
1459 double *ioss_data) const
1460 {
1461 m_decomposition.show_progress(__func__);
1462 // Find blk_seq corresponding to block the specified id...
1463 size_t blk_seq = get_block_seq(EX_ELEM_BLOCK, id);
1464 size_t count = get_block_element_count(blk_seq);
1465 size_t offset = get_block_element_offset(blk_seq);
1466
1467 std::vector<double> file_data(count);
1468 int ierr = ex_get_partial_one_attr(filePtr, EX_ELEM_BLOCK, id, offset + 1, count, attr_index,
1469 Data(file_data));
1470
1471 if (ierr >= 0) {
1472 m_decomposition.communicate_block_data(Data(file_data), ioss_data, el_blocks[blk_seq], 1);
1473 }
1474
1475 return ierr;
1476 }
1477
1478 template <typename INT>
1479 int DecompositionData<INT>::get_elem_map(int filePtr, ex_entity_id id, int map_index,
1480 size_t offset, size_t count, void *ioss_data) const
1481 {
1482 m_decomposition.show_progress(__func__);
1483 // Reading an element blocks worth of map data and returning in `ioss_data`
1484 // The map is the `map_index`th map on the database.
1485 // Find blk_seq corresponding to block the specified id...
1486 size_t blk_seq = get_block_seq(EX_ELEM_BLOCK, id);
1487 size_t eb_count = get_block_element_count(blk_seq);
1488 size_t eb_offset = count == 0 ? 0 : get_block_element_offset(blk_seq);
1489 int ierr = 0;
1490 if (m_decomposition.m_method == "LINEAR") {
1491 ierr = ex_get_partial_num_map(filePtr, EX_ELEM_MAP, map_index, offset + eb_offset + 1,
1492 eb_count, (INT *)ioss_data);
1493 }
1494 else {
1495 std::vector<INT> file_data(eb_count);
1496 ierr = ex_get_partial_num_map(filePtr, EX_ELEM_MAP, map_index, offset + eb_offset + 1,
1497 eb_count, Data(file_data));
1498 if (ierr >= 0) {
1499 m_decomposition.communicate_block_data(Data(file_data), (INT *)ioss_data,
1500 el_blocks[blk_seq], 1);
1501 }
1502 }
1503 return ierr;
1504 }
1505
1506 template <typename INT>
1507 int DecompositionData<INT>::get_node_map(int filePtr, int map_index, size_t offset, size_t count,
1508 void *ioss_data) const
1509 {
1510 return -1;
1511 }
1512
1513 template int DecompositionData<int>::get_set_mesh_var(int filePtr, ex_entity_type type,
1514 ex_entity_id id, const Ioss::Field &field,
1515 int *ioss_data) const;
1516 template int DecompositionData<int64_t>::get_set_mesh_var(int filePtr, ex_entity_type type,
1517 ex_entity_id id,
1518 const Ioss::Field &field,
1519 int64_t *ioss_data) const;
1520 template int DecompositionData<int>::get_set_mesh_var(int filePtr, ex_entity_type type,
1521 ex_entity_id id, const Ioss::Field &field,
1522 double *ioss_data) const;
1523 template int DecompositionData<int64_t>::get_set_mesh_var(int filePtr, ex_entity_type type,
1524 ex_entity_id id,
1525 const Ioss::Field &field,
1526 double *ioss_data) const;
1527
1528 template <typename INT>
1529 template <typename T>
1530 int DecompositionData<INT>::get_set_mesh_var(int filePtr, ex_entity_type type, ex_entity_id id,
1531 const Ioss::Field &field, T *ioss_data) const
1532 {
1533 m_decomposition.show_progress(__func__);
1534 // SideSet Distribution Factor data can be very complicated.
1535 // For some sanity, handle all requests for those in a separate routine...
1536 if (type == EX_SIDE_SET && field.get_name() == "distribution_factors") {
1537 return handle_sset_df(filePtr, id, field, ioss_data);
1538 }
1539
1540 auto &set = get_decomp_set(type, id);
1541
1542 std::vector<T> file_data;
1543 int ierr = 0;
1544
1545 // These fields call back into this routine and are handled on all
1546 // processors; not just the root processor.
1547 if (field.get_name() == "element_side") {
1548 // SideSet only...
1549 if (type == EX_SIDE_SET) {
1550 // Interleave the "ids" and "sides" fields...
1551 std::vector<T> tmp(set.ioss_count());
1552 Ioss::Field elem_field("ids", Ioss::Field::INTEGER, "scalar", Ioss::Field::MESH,
1553 tmp.size());
1554 get_set_mesh_var(filePtr, type, id, elem_field, Data(tmp));
1555 for (size_t i = 0; i < tmp.size(); i++) {
1556 ioss_data[2 * i] = tmp[i];
1557 }
1558 Ioss::Field side_field("sides", Ioss::Field::INTEGER, "scalar", Ioss::Field::MESH,
1559 tmp.size());
1560 get_set_mesh_var(filePtr, type, id, side_field, Data(tmp));
1561 for (size_t i = 0; i < tmp.size(); i++) {
1562 ioss_data[2 * i + 1] = tmp[i];
1563 }
1564 }
1565 else {
1566 return -1;
1567 }
1568 return ierr;
1569 }
1570 else if (field.get_name() == "element_side_raw") {
1571 // SideSet only...
1572 if (type == EX_SIDE_SET) {
1573 // Interleave the "ids" and "sides" fields...
1574 std::vector<T> tmp(set.ioss_count());
1575 Ioss::Field elem_field("ids_raw", Ioss::Field::INTEGER, "scalar", Ioss::Field::MESH,
1576 tmp.size());
1577 get_set_mesh_var(filePtr, type, id, elem_field, Data(tmp));
1578 for (size_t i = 0; i < tmp.size(); i++) {
1579 ioss_data[2 * i] = tmp[i];
1580 }
1581 Ioss::Field side_field("sides", Ioss::Field::INTEGER, "scalar", Ioss::Field::MESH,
1582 tmp.size());
1583 get_set_mesh_var(filePtr, type, id, side_field, Data(tmp));
1584 for (size_t i = 0; i < tmp.size(); i++) {
1585 ioss_data[2 * i + 1] = tmp[i];
1586 }
1587 }
1588 else {
1589 return -1;
1590 }
1591 return ierr;
1592 }
1593
1594 // If the requested field is "distribution_factors" see if
1595 // they are constant and the read/comm can be skipped...
1596 if (field.get_name() == "distribution_factors" && set.distributionFactorConstant) {
1597 // Fill in the ioss decomp with the constant value...
1598 for (size_t i = 0; i < set.distributionFactorCount; i++) {
1599 ioss_data[i] = set.distributionFactorValue;
1600 }
1601 return 0;
1602 }
1603
1604 if (m_processor == set.root_) {
1605 // Read the nodeset data from the file..
1606 // KLUGE: Unknown why, but the following calls to ex_get_set are unable to handle a count() *
1607 // int_size > 2.1 billion. when run on a parallel file. This works fine in a serial run, but
1608 // in parallel even though only a single processor is calling the routine, it fails.
1609 const size_t max_size = 250000000;
1610 if (field.get_name() == "ids" || field.get_name() == "ids_raw") {
1611 file_data.resize(set.file_count());
1612 if (set.file_count() < max_size) {
1613 ierr = ex_get_set(filePtr, type, id, Data(file_data), nullptr);
1614 if (ierr < 0) {
1615 Ioex::exodus_error(filePtr, __LINE__, __func__, __FILE__);
1616 }
1617 }
1618 else {
1619 size_t iter = (set.file_count() + max_size - 1) / max_size;
1620 size_t count = (set.file_count() + iter - 1) / iter;
1621 int old_par_setting = ex_set_parallel(filePtr, 0);
1622 size_t start = 1;
1623 for (size_t i = 0; i < iter; i++) {
1624 if ((start + count - 1) > set.file_count()) {
1625 count = set.file_count() - start + 1;
1626 }
1627 ierr =
1628 ex_get_partial_set(filePtr, type, id, start, count, &file_data[start - 1], nullptr);
1629 if (ierr < 0) {
1630 Ioex::exodus_error(filePtr, __LINE__, __func__, __FILE__);
1631 }
1632 start += count;
1633 }
1634 ex_set_parallel(filePtr, old_par_setting);
1635 }
1636 }
1637 else if (field.get_name() == "sides") {
1638 // SideSet only...
1639 if (type == EX_SIDE_SET) {
1640 file_data.resize(set.file_count());
1641 if (set.file_count() < max_size) {
1642 ierr = ex_get_set(filePtr, type, id, nullptr, Data(file_data));
1643 if (ierr < 0) {
1644 Ioex::exodus_error(filePtr, __LINE__, __func__, __FILE__);
1645 }
1646 }
1647 else {
1648 size_t iter = (set.file_count() + max_size - 1) / max_size;
1649 size_t count = (set.file_count() + iter - 1) / iter;
1650 int old_par_setting = ex_set_parallel(filePtr, 0);
1651 size_t start = 1;
1652 for (size_t i = 0; i < iter; i++) {
1653 if ((start + count - 1) > set.file_count()) {
1654 count = set.file_count() - start + 1;
1655 }
1656 ierr = ex_get_partial_set(filePtr, type, id, start, count, nullptr,
1657 &file_data[start - 1]);
1658 if (ierr < 0) {
1659 Ioex::exodus_error(filePtr, __LINE__, __func__, __FILE__);
1660 }
1661 start += count;
1662 }
1663 ex_set_parallel(filePtr, old_par_setting);
1664 }
1665 }
1666 else {
1667 return -1;
1668 }
1669 }
1670 else if (field.get_name() == "distribution_factors") {
1671 ex_set set_param[1];
1672 set_param[0].id = id;
1673 set_param[0].type = type;
1674 set_param[0].entry_list = nullptr;
1675 set_param[0].extra_list = nullptr;
1676 set_param[0].distribution_factor_list = nullptr;
1677 ierr = ex_get_sets(filePtr, 1, set_param);
1678 if (ierr < 0) {
1679 Ioex::exodus_error(filePtr, __LINE__, __func__, __FILE__);
1680 }
1681
1682 if (set_param[0].num_distribution_factor == 0) {
1683 // This should have been caught above.
1684 assert(1 == 0 && "Internal error in get_set_mesh_var");
1685 }
1686 else {
1687 if (type == EX_NODE_SET) {
1688 file_data.resize(set_param[0].num_distribution_factor);
1689 set_param[0].distribution_factor_list = Data(file_data);
1690 ierr = ex_get_sets(filePtr, 1, set_param);
1691 if (ierr < 0) {
1692 Ioex::exodus_error(filePtr, __LINE__, __func__, __FILE__);
1693 }
1694 }
1695 else {
1696 assert(1 == 0 && "Internal error -- should not be here -- sset df");
1697 }
1698 }
1699 }
1700 else {
1701 assert(1 == 0 && "Unrecognized field name in get_set_mesh_var");
1702 }
1703 }
1704 communicate_set_data(Data(file_data), ioss_data, set, 1);
1705
1706 // Map global 0-based index to local 1-based index.
1707 if (field.get_name() == "ids" || field.get_name() == "ids_raw") {
1708 if (type == EX_NODE_SET) {
1709 for (size_t i = 0; i < set.ioss_count(); i++) {
1710 ioss_data[i] = node_global_to_local(ioss_data[i]);
1711 }
1712 }
1713 else if (type == EX_SIDE_SET) {
1714 for (size_t i = 0; i < set.ioss_count(); i++) {
1715 ioss_data[i] = elem_global_to_local(ioss_data[i]);
1716 }
1717 }
1718 else {
1719 assert(1 == 0);
1720 }
1721 }
1722 return ierr;
1723 }
1724
1725 template <typename INT>
1726 template <typename T>
1727 int DecompositionData<INT>::handle_sset_df(int filePtr, ex_entity_id id, const Ioss::Field &field,
1728 T *ioss_data) const
1729 {
1730 m_decomposition.show_progress(__func__);
1731
1732 // SideSet Distribution Factor data can be very complicated.
1733 // For some sanity, handle all requests for those here. Only handles sidesets
1734 // distribution_factors field.
1735 assert(field.get_name() == "distribution_factors");
1736
1737 auto &set = get_decomp_set(EX_SIDE_SET, id);
1738
1739 // See if df are constant and the read/comm can be skipped...
1740 if (set.distributionFactorConstant) {
1741 // Fill in the ioss decomp with the constant value...
1742 for (size_t i = 0; i < set.distributionFactorCount; i++) {
1743 ioss_data[i] = set.distributionFactorValue;
1744 }
1745 return 0;
1746 }
1747
1748 // See if this set only exists on a single processor.
1749 // In that case, the file_data is the same as the ioss_data
1750 // and we can read the data directly into ioss_data and return...
1751 size_t proc_active = std::accumulate(set.hasEntities.begin(), set.hasEntities.end(), 0);
1752 if (proc_active == 1) {
1753 if (m_processor == set.root_) {
1754 ex_set set_param[1];
1755 set_param[0].id = id;
1756 set_param[0].type = EX_SIDE_SET;
1757 set_param[0].entry_list = nullptr;
1758 set_param[0].extra_list = nullptr;
1759 set_param[0].distribution_factor_list = nullptr;
1760 ex_get_sets(filePtr, 1, set_param);
1761 if (set_param[0].num_distribution_factor == 0) {
1762 // This should have been caught above.
1763 assert(1 == 0 && "Internal error in handle_sset_df");
1764 }
1765 else {
1766 // Read data directly into ioss_data.
1767 set_param[0].distribution_factor_list = ioss_data;
1768 ex_get_sets(filePtr, 1, set_param);
1769 }
1770 }
1771 return 0;
1772 }
1773
1774 // At this point, we have nonconstant distribution factors on
1775 // a sideset split among 2 or more processors...
1776 // Two alternatives exist:
1777 // 1. Constant face topology in sideset (e.g., all quad or all tri) [EASY, COMMON]
1778 // 2. Non-constant face topology in sideset (e.g., mix of quad/tri/...) [HARD, RARE?]
1779
1780 if (set.distributionFactorValsPerEntity > 0) {
1781 // Constant face topology in sideset
1782 // Simply read the values in the file decomposition and
1783 // communicate with a comp count of set.distributionFactorValsPerEntity.
1784 int ierr = 0;
1785 std::vector<T> file_data;
1786 if (m_processor == set.root_) {
1787 file_data.resize(set.distributionFactorValsPerEntity * set.fileCount);
1788
1789 ex_set set_param[1];
1790 set_param[0].id = id;
1791 set_param[0].type = EX_SIDE_SET;
1792 set_param[0].entry_list = nullptr;
1793 set_param[0].extra_list = nullptr;
1794 set_param[0].distribution_factor_list = Data(file_data);
1795 ierr = ex_get_sets(filePtr, 1, set_param);
1796 }
1797 if (ierr >= 0) {
1798 communicate_set_data(Data(file_data), ioss_data, set, set.distributionFactorValsPerEntity);
1799 }
1800
1801 return ierr;
1802 }
1803
1804 // non-constant face topology in sideset, non-constant df on faces.
1805 // Get total number of df on file for this sset...
1806 size_t df_count = 0;
1807 if (m_processor == set.root_) {
1808 ex_set set_param[1];
1809 set_param[0].id = id;
1810 set_param[0].type = EX_SIDE_SET;
1811 set_param[0].entry_list = nullptr;
1812 set_param[0].extra_list = nullptr;
1813 set_param[0].distribution_factor_list = nullptr;
1814 ex_get_sets(filePtr, 1, set_param);
1815 df_count = set_param[0].num_distribution_factor;
1816 }
1817
1818 // Get the node-count-per-face for all faces in this set...
1819 std::vector<int> nodes_per_face(set.file_count() + 1);
1820 if (m_processor == set.root_) {
1821 ex_get_side_set_node_count(filePtr, set.id(), Data(nodes_per_face));
1822 nodes_per_face[set.file_count()] = df_count;
1823 }
1824
1825 // Send this data to the other processors
1826
1827 // NOTE That a processor either sends or receives, but never both,
1828 // so this will not cause a deadlock...
1829 if (m_processor != set.root_ && set.hasEntities[m_processor]) {
1830 MPI_Status status{};
1831 int result = MPI_Recv(Data(nodes_per_face), nodes_per_face.size(), MPI_INT, set.root_, 222,
1832 comm_, &status);
1833
1834 if (result != MPI_SUCCESS) {
1835 std::ostringstream errmsg;
1836 fmt::print(errmsg,
1837 "ERROR: MPI_Recv error on processor {} receiving nodes_per_face sideset data",
1838 m_processor);
1839 IOSS_ERROR(errmsg);
1840 }
1841 df_count = nodes_per_face.back();
1842 }
1843
1844 if (set.root_ == m_processor) {
1845 // Sending data to other processors...
1846 for (int i = m_processor + 1; i < m_processorCount; i++) {
1847 if (set.hasEntities[i]) {
1848 // Send same data to all active processors...
1849 MPI_Send(Data(nodes_per_face), nodes_per_face.size(), MPI_INT, i, 222, comm_);
1850 }
1851 }
1852 }
1853
1854 // Now, read the df on the root processor and send it to the other active
1855 // processors for this set...
1856 std::vector<double> file_data;
1857 if (m_processor == set.root_) {
1858 file_data.resize(df_count);
1859
1860 ex_set set_param[1];
1861 set_param[0].id = id;
1862 set_param[0].type = EX_SIDE_SET;
1863 set_param[0].entry_list = nullptr;
1864 set_param[0].extra_list = nullptr;
1865 set_param[0].distribution_factor_list = Data(file_data);
1866 ex_get_sets(filePtr, 1, set_param);
1867 }
1868
1869 // Send this data to the other processors
1870
1871 if (m_processor != set.root_ && set.hasEntities[m_processor]) {
1872 file_data.resize(df_count);
1873 MPI_Status status{};
1874 int result =
1875 MPI_Recv(Data(file_data), file_data.size(), MPI_DOUBLE, set.root_, 333, comm_, &status);
1876
1877 if (result != MPI_SUCCESS) {
1878 std::ostringstream errmsg;
1879 fmt::print(errmsg,
1880 "ERROR: MPI_Recv error on processor {} receiving nodes_per_face sideset data",
1881 m_processor);
1882 IOSS_ERROR(errmsg);
1883 }
1884 }
1885
1886 if (set.root_ == m_processor) {
1887 // Sending data to other processors...
1888 for (int i = m_processor + 1; i < m_processorCount; i++) {
1889 if (set.hasEntities[i]) {
1890 // Send same data to all active processors...
1891 MPI_Send(Data(file_data), file_data.size(), MPI_DOUBLE, i, 333, comm_);
1892 }
1893 }
1894 }
1895
1896 // Now, each active processor for this set needs to step through the df
1897 // data in file_data and transfer the data it owns to ioss_data.
1898 if (set.hasEntities[m_processor]) {
1899 // Convert nodes_per_face into an offset into the df array...
1900 Ioss::Utils::generate_index(nodes_per_face);
1901
1902 size_t k = 0;
1903 for (size_t i = 0; i < set.ioss_count(); i++) {
1904 size_t index = set.entitylist_map[i];
1905 size_t beg = nodes_per_face[index];
1906 size_t end = nodes_per_face[index + 1];
1907 for (size_t j = beg; j < end; j++) {
1908 ioss_data[k++] = file_data[j];
1909 }
1910 }
1911 }
1912 return 0;
1913 }
1914
1915 template void DecompositionData<int>::create_implicit_global_map(
1916 const std::vector<int> &owning_proc, std::vector<int64_t> &global_implicit_map,
1917 Ioss::Map &node_map, int64_t *locally_owned_count, int64_t *processor_offset);
1918 template void DecompositionData<int64_t>::create_implicit_global_map(
1919 const std::vector<int> &owning_proc, std::vector<int64_t> &global_implicit_map,
1920 Ioss::Map &node_map, int64_t *locally_owned_count, int64_t *processor_offset);
1921
1922 template <typename INT>
1923 void DecompositionData<INT>::create_implicit_global_map(const std::vector<int> &owning_proc,
1924 std::vector<int64_t> &global_implicit_map,
1925 Ioss::Map &node_map,
1926 int64_t *locally_owned_count,
1927 int64_t *processor_offset)
1928 {
1929 m_decomposition.show_progress(__func__);
1930 // Used on composed output database...
1931 // If the node is locally owned, then its position is basically
1932 // determined by removing all shared nodes from the list and
1933 // then compressing the list. This location plus the proc_offset
1934 // gives its location in the global-implicit file.
1935 //
1936 // If it is shared, then need to communicate with the owning
1937 // processor to determine where that processor is putting it.
1938 //
1939 // First, iterate nodeOwningProcessor list and set the implicit
1940 // map for all locally-owned nodes and also determine how many
1941 // of my nodes are owned by which other processors.
1942
1943 global_implicit_map.resize(owning_proc.size());
1944
1945 std::vector<int64_t> snd_count(m_processorCount);
1946 std::vector<int64_t> rcv_count(m_processorCount);
1947
1948 size_t position = 0;
1949 for (size_t i = 0; i < global_implicit_map.size(); i++) {
1950 snd_count[owning_proc[i]]++;
1951 if (owning_proc[i] == m_processor) {
1952 global_implicit_map[i] = position++;
1953 }
1954 }
1955 snd_count[m_processor] = 0;
1956
1957 // The number of locally-owned nodes on this processor is 'position'
1958 *locally_owned_count = position;
1959
1960 MPI_Allgather(locally_owned_count, 1, MPI_LONG_LONG_INT, Data(rcv_count), 1, MPI_LONG_LONG_INT,
1961 comm_);
1962 m_decomposition.show_progress("\tAllgather finished");
1963
1964 // Determine the offset of the nodes on this processor. The offset is the
1965 // total number of locally-owned nodes on all processors prior to this processor.
1966 *processor_offset = 0;
1967 for (int i = 0; i < m_processor; i++) {
1968 *processor_offset += rcv_count[i];
1969 }
1970
1971 for (auto &i : global_implicit_map) {
1972 i += *processor_offset + 1;
1973 }
1974
1975 // Now, tell the other processors how many nodes I will be sending
1976 // them (Nodes they own that I share with them)
1977 MPI_Alltoall(Data(snd_count), 1, MPI_LONG_LONG_INT, Data(rcv_count), 1, MPI_LONG_LONG_INT,
1978 comm_);
1979 m_decomposition.show_progress("\tCommunication 1 finished");
1980
1981 std::vector<int64_t> snd_offset(snd_count);
1982 Ioss::Utils::generate_index(snd_offset);
1983 std::vector<int64_t> snd_list(*snd_offset.rbegin() + *snd_count.rbegin());
1984
1985 {
1986 std::vector<int64_t> tmp_disp(snd_offset);
1987 // Now create the list of nodes to send...
1988 for (size_t i = 0; i < global_implicit_map.size(); i++) {
1989 if (owning_proc[i] != m_processor) {
1990 int64_t global_id = node_map.map()[i + 1];
1991 snd_list[tmp_disp[owning_proc[i]]++] = global_id;
1992 }
1993 }
1994 }
1995
1996 std::vector<int64_t> rcv_offset(rcv_count);
1997 Ioss::Utils::generate_index(rcv_offset);
1998 std::vector<int64_t> rcv_list(*rcv_offset.rbegin() + *rcv_count.rbegin());
1999
2000 Ioss::MY_Alltoallv(snd_list, snd_count, snd_offset, rcv_list, rcv_count, rcv_offset, comm_);
2001 m_decomposition.show_progress("\tCommunication 2 finished");
2002
2003 // Iterate rcv_list and convert global ids to the global-implicit position...
2004 for (auto &i : rcv_list) {
2005 int64_t local_id = node_map.global_to_local(i) - 1;
2006 int64_t rcv_position = global_implicit_map[local_id];
2007 i = rcv_position;
2008 }
2009
2010 // Send the data back now...
2011 Ioss::MY_Alltoallv(rcv_list, rcv_count, rcv_offset, snd_list, snd_count, snd_offset, comm_);
2012 m_decomposition.show_progress("\tCommunication 3 finished");
2013
2014 // Fill in the remaining portions of the global_implicit_map...
2015 std::vector<int64_t> tmp_disp(snd_offset);
2016 for (size_t i = 0; i < global_implicit_map.size(); i++) {
2017 if (owning_proc[i] != m_processor) {
2018 int64_t implicit = snd_list[tmp_disp[owning_proc[i]]++];
2019 global_implicit_map[i] = implicit;
2020 }
2021 }
2022 }
2023
2024 ElementBlockBatchReader::ElementBlockBatchReader(const DecompositionDataBase *decompDB)
2025 : m_decompositionDB(decompDB), m_batchOffset(decompDB->el_blocks)
2026 {
2027 }
2028
2029 size_t
2030 ElementBlockBatchReader::get_connectivity_size(const std::vector<int64_t> &blockSubsetIndex) const
2031 {
2032 size_t connSize = m_batchOffset.get_connectivity_ioss_offset_size(blockSubsetIndex);
2033 return connSize;
2034 }
2035
2036 template <typename INT>
2037 std::vector<size_t> ElementBlockBatchReader::get_connectivity_file_offset(
2038 const std::vector<int64_t> &blockSubsetIndex) const
2039 {
2040 const DecompositionData<INT> *decompData =
2041 dynamic_cast<const DecompositionData<INT> *>(m_decompositionDB);
2042 Ioss::Utils::check_dynamic_cast(decompData);
2043
2044 std::vector<size_t> offset(blockSubsetIndex.size() + 1, 0);
2045
2046 for (size_t i = 0; i < blockSubsetIndex.size(); i++) {
2047 int64_t blk_seq = blockSubsetIndex[i];
2048 const Ioss::BlockDecompositionData &blk = m_decompositionDB->el_blocks[blk_seq];
2049
2050 // Determine number of file decomp elements are in this block and the offset into the block.
2051 size_t count = decompData->get_block_element_count(blk_seq);
2052
2053 int npe = blk.nodesPerEntity;
2054 offset[i + 1] = count * npe;
2055 }
2056
2057 for (size_t i = 1; i <= blockSubsetIndex.size(); ++i) {
2058 offset[i] += offset[i - 1];
2059 }
2060
2061 return offset;
2062 }
2063
2064 template <typename INT>
2065 std::vector<size_t> ElementBlockBatchReader::get_connectivity_impl(
2066 int filePtr, const std::vector<int64_t> &blockSubsetIndex, void *data) const
2067 {
2068 const DecompositionData<INT> *decompData =
2069 dynamic_cast<const DecompositionData<INT> *>(m_decompositionDB);
2070 Ioss::Utils::check_dynamic_cast(decompData);
2071
2072 decompData->m_decomposition.show_progress(__func__);
2073
2074 INT *connData = reinterpret_cast<INT *>(data);
2075
2076 std::vector<size_t> retval;
2077 std::vector<size_t> fileConnOffset = get_connectivity_file_offset<INT>(blockSubsetIndex);
2078
2079 if (decompData->m_decomposition.m_method == "LINEAR") {
2080 for (size_t i = 0; i < blockSubsetIndex.size(); i++) {
2081 int64_t blk_seq = blockSubsetIndex[i];
2082 const Ioss::BlockDecompositionData &blk = m_decompositionDB->el_blocks[blk_seq];
2083 int64_t id = blk.id();
2084 size_t offset = decompData->get_block_element_offset(blk_seq);
2085 size_t count = decompData->get_block_element_count(blk_seq);
2086
2087 ex_get_partial_conn(filePtr, EX_ELEM_BLOCK, id, offset + 1, count,
2088 &connData[fileConnOffset[i]], nullptr, nullptr);
2089 }
2090
2091 retval = fileConnOffset;
2092 }
2093 else {
2094 std::vector<INT> file_conn(fileConnOffset[blockSubsetIndex.size()]);
2095 std::vector<int> blockComponentCount(blockSubsetIndex.size(), 0);
2096
2097 for (size_t i = 0; i < blockSubsetIndex.size(); i++) {
2098 int64_t blk_seq = blockSubsetIndex[i];
2099 const Ioss::BlockDecompositionData &blk = m_decompositionDB->el_blocks[blk_seq];
2100 int64_t id = blk.id();
2101 size_t offset = decompData->get_block_element_offset(blk_seq);
2102 size_t count = decompData->get_block_element_count(blk_seq);
2103
2104 blockComponentCount[i] = blk.nodesPerEntity;
2105 ex_get_partial_conn(filePtr, EX_ELEM_BLOCK, id, offset + 1, count,
2106 &file_conn[fileConnOffset[i]], nullptr, nullptr);
2107 }
2108
2109 retval = decompData->m_decomposition.communicate_entity_data(
2110 Data(file_conn), connData, m_decompositionDB->el_blocks, blockSubsetIndex, fileConnOffset,
2111 blockComponentCount);
2112 }
2113
2114 size_t length = retval[blockSubsetIndex.size()];
2115
2116 for (size_t i = 0; i < length; i++) {
2117 connData[i] = decompData->node_global_to_local(connData[i]);
2118 }
2119
2120 return retval;
2121 }
2122
2123 std::vector<size_t> ElementBlockBatchReader::get_connectivity(
2124 int filePtr, const std::vector<int64_t> &blockSubsetIndex, void *data) const
2125 {
2126 std::vector<size_t> offset;
2127
2128 if (m_decompositionDB->int_size() == sizeof(int)) {
2129 offset = get_connectivity_impl<int>(filePtr, blockSubsetIndex, data);
2130 }
2131 else {
2132 offset = get_connectivity_impl<int64_t>(filePtr, blockSubsetIndex, data);
2133 }
2134
2135 return offset;
2136 }
2137
2138 std::vector<size_t>
2139 ElementBlockBatchReader::get_offset(const std::vector<int64_t> &blockSubsetIndex,
2140 const std::vector<int> &blockComponentCount) const
2141 {
2142 std::vector<size_t> offset =
2143 m_batchOffset.get_ioss_offset(blockSubsetIndex, blockComponentCount);
2144 return offset;
2145 }
2146
2147 std::vector<int> ElementBlockBatchReader::get_connectivity_component_count(
2148 const std::vector<int64_t> &blockSubsetIndex) const
2149 {
2150 std::vector<int> componentCount =
2151 m_batchOffset.get_connectivity_ioss_component_count(blockSubsetIndex);
2152 return componentCount;
2153 }
2154
2155 template <typename INT>
2156 std::vector<size_t>
2157 ElementBlockBatchReader::get_file_offset(const std::vector<int64_t> &blockSubsetIndex,
2158 const std::vector<int> &blockComponentCount) const
2159 {
2160 const DecompositionData<INT> *decompData =
2161 dynamic_cast<const DecompositionData<INT> *>(m_decompositionDB);
2162 Ioss::Utils::check_dynamic_cast(decompData);
2163
2164 std::vector<size_t> offset(blockSubsetIndex.size() + 1, 0);
2165
2166 for (size_t i = 0; i < blockSubsetIndex.size(); i++) {
2167 int64_t blk_seq = blockSubsetIndex[i];
2168
2169 // Determine number of file decomp elements are in this block and the offset into the block.
2170 size_t count = decompData->get_block_element_count(blk_seq);
2171
2172 offset[i + 1] = count * blockComponentCount[i];
2173 }
2174
2175 for (size_t i = 1; i <= blockSubsetIndex.size(); ++i) {
2176 offset[i] += offset[i - 1];
2177 }
2178
2179 return offset;
2180 }
2181
2182 void ElementBlockBatchReader::get_field_data(int filePtr, void *data,
2183 const std::vector<int64_t> &blockSubsetIndex,
2184 size_t step,
2185 const std::vector<BlockFieldData> &block_data) const
2186 {
2187 if (m_decompositionDB->int_size() == sizeof(int)) {
2188 get_field_data_impl<int>(filePtr, data, blockSubsetIndex, step, block_data);
2189 }
2190 else {
2191 get_field_data_impl<int64_t>(filePtr, data, blockSubsetIndex, step, block_data);
2192 }
2193 }
2194
2195 std::vector<int> ElementBlockBatchReader::get_block_component_count(
2196 const std::vector<int64_t> &blockSubsetIndex,
2197 const std::vector<BlockFieldData> &blockFieldData) const
2198 {
2199 size_t num_blocks = blockSubsetIndex.size();
2200 std::vector<int> blockComponentCount(num_blocks);
2201
2202 for (size_t blk_seq = 0; blk_seq < blockSubsetIndex.size(); blk_seq++) {
2203 blockComponentCount[blk_seq] = blockFieldData[blk_seq].comp_count;
2204 }
2205
2206 return blockComponentCount;
2207 }
2208
2209 template <typename INT>
2210 void ElementBlockBatchReader::load_field_data(int filePtr, double *fileData,
2211 const std::vector<int64_t> &blockSubsetIndex,
2212 size_t step,
2213 const std::vector<BlockFieldData> &blockFieldData,
2214 const std::vector<int> &blockComponentCount,
2215 const std::vector<size_t> &fileConnOffset) const
2216 {
2217 const DecompositionData<INT> *decompData =
2218 dynamic_cast<const DecompositionData<INT> *>(m_decompositionDB);
2219 Ioss::Utils::check_dynamic_cast(decompData);
2220
2221 decompData->m_decomposition.show_progress("\tex_get_partial_var (elem)");
2222
2223 for (size_t i = 0; i < blockSubsetIndex.size(); i++) {
2224 int64_t blk_seq = blockSubsetIndex[i];
2225 const Ioss::BlockDecompositionData &blk = decompData->el_blocks[blk_seq];
2226 int64_t id = blk.id();
2227 size_t offset = decompData->get_block_element_offset(blk_seq);
2228 size_t count = decompData->get_block_element_count(blk_seq);
2229
2230 for (size_t comp = 0; comp < blockFieldData[i].comp_count; comp++) {
2231 size_t var_index = blockFieldData[i].var_index[comp];
2232 size_t file_index = fileConnOffset[i] + count * comp;
2233
2234 int ierr = ex_get_partial_var(filePtr, step, EX_ELEM_BLOCK, var_index, id, offset + 1,
2235 count, &fileData[file_index]);
2236
2237 if (ierr < 0) {
2238 Ioex::exodus_error(filePtr, __LINE__, __func__, __FILE__);
2239 }
2240 }
2241 }
2242 }
2243
2244 template <typename INT>
2245 void ElementBlockBatchReader::get_field_data_impl(
2246 int filePtr, void *iossData, const std::vector<int64_t> &blockSubsetIndex, size_t step,
2247 const std::vector<BlockFieldData> &blockFieldData) const
2248 {
2249 const DecompositionData<INT> *decompData =
2250 dynamic_cast<const DecompositionData<INT> *>(m_decompositionDB);
2251 Ioss::Utils::check_dynamic_cast(decompData);
2252
2253 decompData->m_decomposition.show_progress(__func__);
2254
2255 double *data = reinterpret_cast<double *>(iossData);
2256
2257 std::vector<int> blockComponentCount =
2258 get_block_component_count(blockSubsetIndex, blockFieldData);
2259 std::vector<size_t> fileConnOffset =
2260 get_file_offset<INT>(blockSubsetIndex, blockComponentCount);
2261
2262 if (decompData->m_decomposition.m_method == "LINEAR") {
2263 load_field_data<INT>(filePtr, data, blockSubsetIndex, step, blockFieldData,
2264 blockComponentCount, fileConnOffset);
2265 }
2266 else {
2267 size_t numBlocks = blockSubsetIndex.size();
2268 std::vector<double> fileData(fileConnOffset[numBlocks]);
2269 load_field_data<INT>(filePtr, Data(fileData), blockSubsetIndex, step, blockFieldData,
2270 blockComponentCount, fileConnOffset);
2271
2272 (void)decompData->m_decomposition.communicate_entity_data(
2273 Data(fileData), data, decompData->el_blocks, blockSubsetIndex, fileConnOffset,
2274 blockComponentCount);
2275 }
2276 }
2277
2278 } // namespace Ioex
2279 #else
2280 IOSS_MAYBE_UNUSED const char ioss_exodus_decomposition_data_unused_symbol_dummy = '\0';
2281 #endif