Debian Codegraph @

packages/seacas/libraries/ioss/src/Ioss_Decomposition.C

raw
1 /*
2 * Copyright(C) 1999-2024 National Technology & Engineering Solutions
3 * of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
4 * NTESS, the U.S. Government retains certain rights in this software.
5 *
6 * See packages/seacas/LICENSE for details
7 */
8
9 #include "Ioss_Decomposition.h"
10 #include "Ioss_ElementTopology.h"
11 #include "Ioss_Enumerate.h"
12 #include "Ioss_ParallelUtils.h"
13 #include "Ioss_Sort.h"
14 #include "Ioss_Utils.h"
15 #include <algorithm>
16 #include <cassert>
17 #include <fmt/ostream.h>
18 #include <fmt/ranges.h>
19 #include <numeric>
20
21 #if !defined(NO_ZOLTAN_SUPPORT)
22 extern "C" int Zoltan_get_global_id_type(char **name);
23 #endif
24
25 namespace {
26 template <typename INT>
27 inline std::vector<INT> get_entity_dist(size_t proc_count, size_t my_proc, size_t entity_count,
28 size_t *offset, size_t *count)
29 {
30 std::vector<INT> dist(proc_count + 1);
31
32 size_t per_proc = entity_count / proc_count;
33 size_t extra = entity_count % proc_count;
34
35 *count = per_proc + (my_proc < extra ? 1 : 0);
36
37 if (my_proc < extra) {
38 *offset = (per_proc + 1) * my_proc;
39 }
40 else {
41 *offset = (per_proc + 1) * extra + per_proc * (my_proc - extra);
42 }
43
44 // This processors range of elements is
45 // [element_offset..element_offset+element_count)
46
47 // Fill in element_dist vector. Range of elements on each processor...
48 size_t sum = 0;
49 for (size_t i = 0; i < proc_count; i++) {
50 dist[i] = sum;
51 sum += per_proc;
52 if (i < extra) {
53 sum++;
54 }
55 }
56 dist[proc_count] = sum;
57 return dist;
58 }
59
60 bool check_valid_decomp_method(const std::string &method)
61 {
62 const auto &valid_methods = Ioss::valid_decomp_methods();
63 return std::find(valid_methods.begin(), valid_methods.end(), method) != valid_methods.end();
64 }
65
66 std::string get_decomposition_method(const Ioss::PropertyManager &properties)
67 {
68 std::string method = "LINEAR";
69
70 if (properties.exists("DECOMPOSITION_METHOD")) {
71 method = properties.get("DECOMPOSITION_METHOD").get_string();
72 method = Ioss::Utils::uppercase(method);
73 }
74 else if (properties.exists("RESTART_DECOMPOSITION_METHOD")) {
75 method = properties.get("RESTART_DECOMPOSITION_METHOD").get_string();
76 method = Ioss::Utils::uppercase(method);
77 }
78 else if (properties.exists("MODEL_DECOMPOSITION_METHOD")) {
79 method = properties.get("MODEL_DECOMPOSITION_METHOD").get_string();
80 method = Ioss::Utils::uppercase(method);
81 }
82
83 if (!check_valid_decomp_method(method)) {
84 IOSS_ERROR(fmt::format("ERROR: Invalid decomposition method specified: '{}'\n"
85 " Valid methods: {}\n",
86 method, fmt::join(Ioss::valid_decomp_methods(), ", ")));
87 }
88 return method;
89 }
90
91 #if !defined(NO_PARMETIS_SUPPORT)
92 int get_common_node_count(const std::vector<Ioss::BlockDecompositionData> &el_blocks,
93 Ioss::ParallelUtils &pu)
94 {
95 // Determine number of nodes that elements must share to be
96 // considered connected. A 8-node hex-only mesh would have 4
97 // A 3D shell mesh should have 2. Basically, use the minimum
98 // number of nodes per side for all element blocks... Omit sphere
99 // elements; ignore bars(?)...
100
101 int common_nodes = INT_MAX;
102
103 for (const auto &block : el_blocks) {
104 if (block.global_count() == 0) {
105 continue;
106 }
107 std::string type = Ioss::Utils::lowercase(block.topologyType);
108 Ioss::ElementTopology *topology = Ioss::ElementTopology::factory(type, false);
109 if (topology != nullptr) {
110 Ioss::ElementTopology *boundary = topology->boundary_type(0);
111 if (boundary != nullptr) {
112 common_nodes = std::min(common_nodes, boundary->number_boundaries());
113 }
114 else {
115 // Different topologies on some element faces...
116 size_t nb = topology->number_boundaries();
117 for (size_t b = 1; b <= nb; b++) {
118 boundary = topology->boundary_type(b);
119 if (boundary != nullptr) {
120 common_nodes = std::min(common_nodes, boundary->number_boundaries());
121 }
122 }
123 }
124 }
125 }
126 common_nodes = std::max(1, common_nodes);
127 common_nodes = pu.global_minmax(common_nodes, Ioss::ParallelUtils::DO_MIN);
128
129 #if IOSS_DEBUG_OUTPUT
130 fmt::print(Ioss::DebugOut(), "Setting common_nodes to {}\n", common_nodes);
131 #endif
132 return common_nodes;
133 }
134 #endif
135 } // namespace
136
137 namespace Ioss {
138
139 IOSS_EXPORT const Ioss::NameList &valid_decomp_methods()
140 {
141 static const Ioss::NameList valid_methods{"EXTERNAL"
142 #ifdef SEACAS_HAVE_MPI
143 ,
144 "LINEAR",
145 "MAP",
146 "VARIABLE"
147 #if !defined(NO_ZOLTAN_SUPPORT)
148 ,
149 "BLOCK",
150 "CYCLIC",
151 "RANDOM",
152 "RCB",
153 "RIB",
154 "HSFC"
155 #endif
156 #if !defined(NO_PARMETIS_SUPPORT)
157 ,
158 "KWAY",
159 "KWAY_GEOM",
160 "GEOM_KWAY",
161 "METIS_SFC"
162 #endif
163 #endif
164 };
165 return valid_methods;
166 }
167
168 size_t
169 ElementBlockBatchOffset::get_ioss_element_size(const std::vector<int64_t> &blockSubsetIndex) const
170 {
171 size_t count = 0;
172
173 // Determine total number of ioss subset decomp elements
174 for (int64_t i : blockSubsetIndex) {
175 const Ioss::BlockDecompositionData &block = m_data[i];
176 count += (block.importMap.size() + block.localMap.size());
177 }
178
179 return count;
180 }
181
182 size_t ElementBlockBatchOffset::get_ioss_offset_size(
183 const std::vector<int64_t> &blockSubsetIndex,
184 const std::vector<int> &blockSubsetFieldComponentCount) const
185 {
186 size_t count = 0;
187
188 for (auto [i, blk_seq] : enumerate(blockSubsetIndex)) {
189 const Ioss::BlockDecompositionData &block = m_data[blk_seq];
190 // Determine total number of ioss decomp entries based on subset field component count per
191 // block.
192 count += blockSubsetFieldComponentCount[i] * (block.importMap.size() + block.localMap.size());
193 }
194
195 return count;
196 }
197
198 std::vector<size_t> ElementBlockBatchOffset::get_ioss_offset(
199 const std::vector<int64_t> &blockSubsetIndex,
200 const std::vector<int> &blockSubsetFieldComponentCount) const
201 {
202 std::vector<size_t> offset(blockSubsetIndex.size() + 1, 0);
203
204 for (auto [i, blk_seq] : enumerate(blockSubsetIndex)) {
205 const Ioss::BlockDecompositionData &block = m_data[blk_seq];
206
207 // Determine number of ioss decomp entries based on subset field component count per block.
208 offset[i + 1] =
209 blockSubsetFieldComponentCount[i] * (block.importMap.size() + block.localMap.size());
210 }
211
212 // Compute offsets
213 for (size_t i = 1; i <= blockSubsetIndex.size(); ++i) {
214 offset[i] += offset[i - 1];
215 }
216
217 return offset;
218 }
219
220 std::vector<size_t> ElementBlockBatchOffset::get_import_offset(
221 const std::vector<int64_t> &blockSubsetIndex,
222 const std::vector<int> &blockSubsetFieldComponentCount) const
223 {
224 std::vector<size_t> offset(blockSubsetIndex.size() + 1, 0);
225
226 for (auto [i, blk_seq] : enumerate(blockSubsetIndex)) {
227 const Ioss::BlockDecompositionData &block = m_data[blk_seq];
228
229 // Determine number of imported ioss decomp entries based on subset field component count per
230 // block.
231 offset[i + 1] = blockSubsetFieldComponentCount[i] * block.importMap.size();
232 }
233
234 // Compute offsets
235 for (size_t i = 1; i <= blockSubsetIndex.size(); ++i) {
236 offset[i] += offset[i - 1];
237 }
238
239 return offset;
240 }
241
242 std::vector<int> ElementBlockBatchOffset::get_connectivity_ioss_component_count(
243 const std::vector<int64_t> &blockSubsetIndex) const
244 {
245 std::vector<int> blockSubsetConnectivityComponentCount(blockSubsetIndex.size());
246
247 for (auto [i, blk_seq] : enumerate(blockSubsetIndex)) {
248 const Ioss::BlockDecompositionData &block = m_data[blk_seq];
249 blockSubsetConnectivityComponentCount[i] = block.nodesPerEntity;
250 }
251
252 return blockSubsetConnectivityComponentCount;
253 }
254
255 size_t ElementBlockBatchOffset::get_connectivity_ioss_offset_size(
256 const std::vector<int64_t> &blockSubsetIndex) const
257 {
258 return get_ioss_offset_size(blockSubsetIndex,
259 get_connectivity_ioss_component_count(blockSubsetIndex));
260 }
261
262 std::vector<size_t> ElementBlockBatchOffset::get_connectivity_ioss_offset(
263 const std::vector<int64_t> &blockSubsetIndex) const
264 {
265 return get_ioss_offset(blockSubsetIndex,
266 get_connectivity_ioss_component_count(blockSubsetIndex));
267 }
268
269 std::vector<size_t> ElementBlockBatchOffset::get_connectivity_import_offset(
270 const std::vector<int64_t> &blockSubsetIndex) const
271 {
272 return get_import_offset(blockSubsetIndex,
273 get_connectivity_ioss_component_count(blockSubsetIndex));
274 }
275
276 template IOSS_EXPORT Decomposition<int>::Decomposition(const Ioss::PropertyManager &props,
277 Ioss_MPI_Comm comm);
278 template IOSS_EXPORT Decomposition<int64_t>::Decomposition(const Ioss::PropertyManager &props,
279 Ioss_MPI_Comm comm);
280
281 template <typename INT>
282 Decomposition<INT>::Decomposition(const Ioss::PropertyManager &props, Ioss_MPI_Comm comm)
283 : m_comm(comm), m_pu(comm), m_processor(m_pu.parallel_rank()),
284 m_processorCount(m_pu.parallel_size())
285 {
286 m_method = get_decomposition_method(props);
287 if (m_method == "MAP" || m_method == "VARIABLE") {
288 m_decompExtra = props.get_optional("DECOMPOSITION_EXTRA", "processor_id");
289 }
290
291 Utils::check_set_bool_property(props, "RETAIN_FREE_NODES", m_retainFreeNodes);
292 Utils::check_set_bool_property(props, "DECOMP_SHOW_HWM", m_showHWM);
293 Utils::check_set_bool_property(props, "DECOMP_SHOW_PROGRESS", m_showProgress);
294 if (!m_showProgress) {
295 Utils::check_set_bool_property(props, "ENABLE_TRACING", m_showProgress);
296 }
297
298 if (props.exists("PARMETIS_COMMON_NODE_COUNT") &&
299 props.get("PARMETIS_COMMON_NODE_COUNT").get_int() > 0) {
300 m_commonNodeCount = props.get("PARMETIS_COMMON_NODE_COUNT").get_int();
301 }
302
303 if (props.exists("LINE_DECOMPOSITION")) {
304 // The value of the property should be a comma-separated list of surface/sideset names from
305 // which the lines will grow, or the value "ALL" for all surfaces in the model.
306 m_lineDecomp = true;
307 m_decompExtra = props.get("LINE_DECOMPOSITION").get_string();
308 }
309 }
310
311 template IOSS_EXPORT void
312 Decomposition<int>::generate_entity_distributions(size_t global_node_count,
313 size_t global_element_count);
314 template IOSS_EXPORT void
315 Decomposition<int64_t>::generate_entity_distributions(size_t global_node_count,
316 size_t global_element_count);
317
318 template <typename INT>
319 void Decomposition<INT>::generate_entity_distributions(size_t global_node_count,
320 size_t global_element_count)
321 {
322 show_progress(__func__);
323 m_globalNodeCount = global_node_count;
324 m_globalElementCount = global_element_count;
325
326 m_elementDist = get_entity_dist<INT>(m_processorCount, m_processor, m_globalElementCount,
327 &m_elementOffset, &m_elementCount);
328 m_nodeDist = get_entity_dist<INT>(m_processorCount, m_processor, m_globalNodeCount,
329 &m_nodeOffset, &m_nodeCount);
330 }
331
332 template <typename INT>
333 void Decomposition<INT>::get_element_block_communication(
334 std::vector<BlockDecompositionData> &el_blocks)
335 {
336 show_progress(__func__);
337 for (auto &block : el_blocks) {
338 block.exportCount.resize(m_processorCount);
339 block.exportIndex.resize(m_processorCount);
340 block.importCount.resize(m_processorCount);
341 block.importIndex.resize(m_processorCount);
342 }
343
344 // First iterate the local element indices and count number in
345 // each block.
346 for (auto loc_elem : localElementMap) {
347 size_t elem = loc_elem + m_elementOffset;
348 size_t b = Ioss::Utils::find_index_location(elem, m_fileBlockIndex);
349
350 assert(elem >= m_fileBlockIndex[b] && elem < m_fileBlockIndex[b + 1]);
351 size_t off = std::max(m_fileBlockIndex[b], m_elementOffset);
352 el_blocks[b].localMap.push_back(elem - off);
353 }
354
355 // Now iterate the imported element list...
356 // Find number of imported elements that are less than the current
357 // local_map[0]
358 size_t proc = 0;
359 std::vector<size_t> imp_index(el_blocks.size());
360 for (auto [i, elem] : enumerate(importElementMap)) {
361 while (i >= (size_t)importElementIndex[proc + 1]) {
362 proc++;
363 }
364
365 size_t b = Ioss::Utils::find_index_location((size_t)elem, m_fileBlockIndex);
366 size_t off = std::max(m_fileBlockIndex[b], m_elementOffset);
367
368 if (!el_blocks[b].localMap.empty() && (size_t)elem < el_blocks[b].localMap[0] + off) {
369 el_blocks[b].localIossOffset++;
370 el_blocks[b].importMap.push_back(imp_index[b]++);
371 }
372 else {
373 el_blocks[b].importMap.push_back(el_blocks[b].localMap.size() + imp_index[b]++);
374 }
375 el_blocks[b].importCount[proc]++;
376 }
377
378 // Now for the exported data...
379 proc = 0;
380 for (auto [i, elem] : enumerate(exportElementMap)) {
381 while (i >= (size_t)exportElementIndex[proc + 1]) {
382 proc++;
383 }
384
385 size_t b = Ioss::Utils::find_index_location((size_t)elem, m_fileBlockIndex);
386
387 size_t off = std::max(m_fileBlockIndex[b], m_elementOffset);
388 el_blocks[b].exportMap.push_back((size_t)elem - off);
389 el_blocks[b].exportCount[proc]++;
390 }
391
392 for (auto &block : el_blocks) {
393 block.iossCount = block.localMap.size() + block.importMap.size();
394 std::copy(block.exportCount.begin(), block.exportCount.end(), block.exportIndex.begin());
395 std::copy(block.importCount.begin(), block.importCount.end(), block.importIndex.begin());
396 Ioss::Utils::generate_index(block.exportIndex);
397 Ioss::Utils::generate_index(block.importIndex);
398 }
399 }
400
401 // ========================================================================
402 // Decompose model function (confusing with all the #if...
403 template IOSS_EXPORT void Decomposition<int>::decompose_model(
404 #if !defined(NO_ZOLTAN_SUPPORT)
405 Zoltan &zz,
406 #endif
407 std::vector<BlockDecompositionData> &element_blocks);
408 template IOSS_EXPORT void Decomposition<int64_t>::decompose_model(
409 #if !defined(NO_ZOLTAN_SUPPORT)
410 Zoltan &zz,
411 #endif
412 std::vector<BlockDecompositionData> &element_blocks);
413
414 template <typename INT>
415 void Decomposition<INT>::decompose_model(
416 #if !defined(NO_ZOLTAN_SUPPORT)
417 Zoltan &zz,
418 #endif
419 std::vector<BlockDecompositionData> &element_blocks)
420 {
421 show_progress(__func__);
422 if (m_processor == 0) {
423 fmt::print(Ioss::OUTPUT(),
424 "\nIOSS: Using decomposition method '{}' for {} elements on {} mpi ranks.\n",
425 m_method, fmt::group_digits(m_globalElementCount), m_processorCount);
426
427 if (!m_decompExtra.empty()) {
428 fmt::print(Ioss::OUTPUT(), "\tDecomposition extra data: '{}'.\n", m_decompExtra);
429 }
430
431 if ((size_t)m_processorCount > m_globalElementCount) {
432 fmt::print(Ioss::WarnOut(),
433 "Decomposing {} elements across {} mpi ranks will "
434 "result in some processors with *NO* elements.\n",
435 m_globalElementCount, m_processorCount);
436 }
437 }
438 #if !defined(NO_PARMETIS_SUPPORT)
439 if (m_method == "KWAY" || m_method == "GEOM_KWAY" || m_method == "KWAY_GEOM" ||
440 m_method == "METIS_SFC") {
441 metis_decompose((idx_t *)Data(m_pointer), (idx_t *)Data(m_adjacency), element_blocks);
442 }
443 #endif
444 #if !defined(NO_ZOLTAN_SUPPORT)
445 if (m_method == "RCB" || m_method == "RIB" || m_method == "HSFC" || m_method == "BLOCK" ||
446 m_method == "CYCLIC" || m_method == "RANDOM") {
447 zoltan_decompose(zz);
448 }
449 #endif
450 if (m_method == "LINEAR") {
451 if (m_globalElementCount > 0) {
452 simple_decompose();
453 }
454 else {
455 simple_node_decompose();
456 }
457 }
458 if (m_method == "VARIABLE") {
459 guided_decompose();
460 }
461 if (m_method == "MAP") {
462 guided_decompose();
463 }
464 if (m_method == "LINE_DECOMP") {
465 // Currently used for line decomposition with another decomposition type.
466 // The line-modified decomposition is done prior to this and builds the
467 // `m_elementToProc` which is then used here to decompose the elements...
468 guided_decompose();
469 }
470
471 show_progress("\tfinished with decomposition method");
472 Ioss::sort(importElementMap);
473 show_progress("\tfinished with sort");
474
475 std::copy(importElementCount.begin(), importElementCount.end(), importElementIndex.begin());
476 Ioss::Utils::generate_index(importElementIndex);
477
478 // Find the number of imported elements that precede the elements
479 // that remain locally owned...
480 m_importPreLocalElemIndex = 0;
481 for (size_t i = 0; i < importElementMap.size(); i++) {
482 if ((size_t)importElementMap[i] >= m_elementOffset) {
483 break;
484 }
485 m_importPreLocalElemIndex++;
486 }
487
488 // Determine size of this processors element blocks...
489 get_element_block_communication(element_blocks);
490
491 // Now need to determine the nodes that are on this processor,
492 // both owned and shared...
493 if (m_globalElementCount > 0) {
494 get_local_node_list();
495 get_shared_node_list();
496 }
497
498 show_progress("\tprior to releasing some temporary decomposition memory");
499
500 // Release some memory...
501 Ioss::Utils::clear(m_adjacency);
502 Ioss::Utils::clear(m_pointer);
503 Ioss::Utils::clear(m_elementDist);
504 Ioss::Utils::clear(m_nodeDist);
505 show_progress("\tIoss::decompose model finished");
506 }
507
508 template IOSS_EXPORT void Decomposition<int>::calculate_element_centroids(
509 const std::vector<double> &x, const std::vector<double> &y, const std::vector<double> &z);
510 template IOSS_EXPORT void Decomposition<int64_t>::calculate_element_centroids(
511 const std::vector<double> &x, const std::vector<double> &y, const std::vector<double> &z);
512
513 template <typename INT>
514 void Decomposition<INT>::calculate_element_centroids(const std::vector<double> &x,
515 const std::vector<double> &y,
516 const std::vector<double> &z)
517 {
518 // recv_count is the number of nodes that I need to recv from the other
519 // processors
520 // send_count is the number of nodes that I need to send to the other
521 // processors
522 show_progress(__func__);
523 std::vector<INT> recv_count(m_processorCount);
524 std::vector<INT> send_count(m_processorCount);
525
526 std::vector<int> owner; // Size is sum of element connectivity sizes (same as
527 // adjacency list)
528 owner.reserve(m_adjacency.size());
529
530 for (auto node : m_adjacency) {
531 INT owning_processor = Ioss::Utils::find_index_location(node, m_nodeDist);
532 owner.push_back(owning_processor);
533 recv_count[owning_processor]++;
534 }
535
536 // Zero out myProcessor entry in recv_count and sum the
537 // remainder...
538 recv_count[m_processor] = 0;
539
540 // Tell each processor how many nodes worth of data to send to
541 // every other processor...
542 MPI_Alltoall(Data(recv_count), 1, Ioss::mpi_type((INT)0), Data(send_count), 1,
543 Ioss::mpi_type((INT)0), m_comm);
544
545 send_count[m_processor] = 0;
546
547 std::vector<INT> recv_disp(m_processorCount);
548 std::vector<INT> send_disp(m_processorCount);
549 size_t sums = 0;
550 size_t sumr = 0;
551 for (int p = 0; p < m_processorCount; p++) {
552 recv_disp[p] = sumr;
553 sumr += recv_count[p];
554
555 send_disp[p] = sums;
556 sums += send_count[p];
557 }
558
559 #if IOSS_DEBUG_OUTPUT
560 fmt::print(Ioss::DebugOut(),
561 "Processor {} communicates {} nodes from and {} nodes to other processors\n",
562 m_processor, fmt::group_digits(sumr), fmt::group_digits(sums));
563 #endif
564 // Build the list telling the other processors which of their nodes I will
565 // need data from...
566 std::vector<INT> node_comm_recv(sumr);
567 std::vector<INT> node_comm_send(sums);
568 {
569 std::vector<INT> recv_tmp(m_processorCount);
570 for (auto [i, proc] : enumerate(owner)) {
571 if (proc != m_processor) {
572 INT node = m_adjacency[i];
573 size_t position = recv_disp[proc] + recv_tmp[proc]++;
574 node_comm_recv[position] = node;
575 }
576 }
577 }
578
579 assert(node_comm_recv.size() == sumr);
580
581 Ioss::MY_Alltoallv(node_comm_recv, recv_count, recv_disp, node_comm_send, send_count, send_disp,
582 m_comm);
583
584 Ioss::Utils::clear(node_comm_recv);
585
586 // At this point, 'node_comm_send' contains the list of nodes that I
587 // need to provide coordinate data for.
588
589 // DEBUG: == Check that all nodes in node_comm_send are in the range
590 // m_nodeOffset..m_nodeOffset+m_nodeCount
591 #ifndef NDEBUG
592 for (auto node : node_comm_send) {
593 assert((size_t)node >= m_nodeOffset && (size_t)node < m_nodeOffset + m_nodeCount);
594 }
595 #endif
596
597 // The total vector size I need to send data in is node_comm_send.size()*3
598 std::vector<double> coord_send;
599 coord_send.reserve(node_comm_send.size() * m_spatialDimension);
600 std::vector<double> coord_recv(sumr * m_spatialDimension);
601 for (auto node : node_comm_send) {
602 node -= m_nodeOffset;
603 coord_send.push_back(x[node]);
604 if (m_spatialDimension > 1) {
605 coord_send.push_back(y[node]);
606 }
607 if (m_spatialDimension > 2) {
608 coord_send.push_back(z[node]);
609 }
610 }
611 assert(coord_send.size() == node_comm_send.size() * m_spatialDimension);
612
613 // Send the coordinate data back to the processors that requested it...
614 for (int i = 0; i < m_processorCount; i++) {
615 send_count[i] *= m_spatialDimension;
616 recv_count[i] *= m_spatialDimension;
617 send_disp[i] *= m_spatialDimension;
618 recv_disp[i] *= m_spatialDimension;
619 }
620
621 Ioss::MY_Alltoallv(coord_send, send_count, send_disp, coord_recv, recv_count, recv_disp,
622 m_comm);
623
624 // Don't need coord_send data anymore ... clean out the vector.
625 Ioss::Utils::clear(coord_send);
626
627 // Should have all needed coordinate data at this time.
628 // Some in x,y,z vectors and some in coord_recv vector.
629
630 // Note that in the current data structure, adjacency contains the
631 // connectivity for all elements on this processor. 'owner' is a
632 // parallel datastructure containing the owning processor for that
633 // node. If it is off-processor, then its coordinates will be
634 // stored in coord_recv in processor order, but will be hit in the
635 // correct order... The 'pointer' array tells the number of nodes
636 // per element...
637
638 // Calculate the centroid into the DecompositionData structure 'centroids'
639 m_centroids.reserve(m_elementCount * m_spatialDimension);
640 std::vector<INT> recv_tmp(m_processorCount);
641
642 for (size_t i = 0; i < m_elementCount; i++) {
643 size_t nnpe = m_pointer[i + 1] - m_pointer[i];
644 double cx = 0.0;
645 double cy = 0.0;
646 double cz = 0.0;
647 for (INT jj = m_pointer[i]; jj < m_pointer[i + 1]; jj++) {
648 INT node = m_adjacency[jj];
649 INT proc = owner[jj];
650 if (proc == m_processor) {
651 cx += x[node - m_nodeOffset];
652 if (m_spatialDimension > 1) {
653 cy += y[node - m_nodeOffset];
654 }
655 if (m_spatialDimension > 2) {
656 cz += z[node - m_nodeOffset];
657 }
658 }
659 else {
660 INT coffset = recv_disp[proc] + recv_tmp[proc];
661 recv_tmp[proc] += m_spatialDimension;
662 cx += coord_recv[coffset + 0];
663 if (m_spatialDimension > 1) {
664 cy += coord_recv[coffset + 1];
665 }
666 if (m_spatialDimension > 2) {
667 cz += coord_recv[coffset + 2];
668 }
669 }
670 }
671 m_centroids.push_back(cx / nnpe);
672 if (m_spatialDimension > 1) {
673 m_centroids.push_back(cy / nnpe);
674 }
675 if (m_spatialDimension > 2) {
676 m_centroids.push_back(cz / nnpe);
677 }
678 }
679 }
680
681 template <typename INT> void Decomposition<INT>::guided_decompose()
682 {
683 show_progress(__func__);
684 assert(m_method == "MAP" || m_method == "VARIABLE" || m_method == "LINE_DECOMP");
685 // - Read my portion of the map / variable.
686 // - count # of exports to each rank
687 // -- exportElementCount[proc]
688 // - share that with each rank -- becomes their import count
689 // - build list of exported elements (exportElementMap)
690 // - communicate to all proc -- becomes importElementMap.
691 // Create `exportElementIndex` from `exportElementCount`
692
693 std::string label = m_method;
694
695 // If the "m_decompExtra" string contains a comma, then the
696 // value following the comma is either an integer "scale"
697 // which is divided into each entry in m_elementToProc, or it
698 // is the string "auto" which will automatically scale all
699 // values by the *integer* "max/processorCount"
700 //
701 // NOTE: integer division with *no* rounding is used.
702
703 // Get maximum value in the m_elementToProc vector for use in scaling or verifying
704 // `m_elementToProc` is populated in Ioex_Decomposition.C...
705 auto local_max = *std::max_element(m_elementToProc.begin(), m_elementToProc.end());
706 auto max_proc = m_pu.global_minmax(local_max, Ioss::ParallelUtils::DO_MAX);
707
708 // [0..m_processorCount).
709 double scale = 1.0;
710 auto pos = m_decompExtra.find(",");
711 if (m_method != "LINE_DECOMP" && pos != std::string::npos) {
712 // Extract the string following the comma...
713 auto scale_str = m_decompExtra.substr(pos + 1);
714 if (scale_str == "AUTO" || scale_str == "auto") {
715 scale = double(max_proc + 1) / (double)m_processorCount;
716 if (m_processor == 0) {
717 fmt::print(Ioss::OUTPUT(),
718 "IOSS: Element Processor {} automatic scaling factor = {:.5}\n", label, scale);
719 }
720 }
721 else {
722 scale = std::stod(scale_str);
723 }
724
725 if (scale < 1.0) {
726 IOSS_ERROR(fmt::format("ERROR: Processor {} scaling factor is {} which is not allowed.\n"
727 "\tIt must be >= 1.0. Scaling values is not possible.",
728 label, scale));
729 }
730
731 // Do the scaling (integer division...)
732 std::transform(m_elementToProc.begin(), m_elementToProc.end(), m_elementToProc.begin(),
733 [scale](int p) { return int(double(p) / scale); });
734 max_proc = int(double(max_proc) / scale);
735 }
736
737 // Check that values in the map/variable are in range
738 if (max_proc < m_processorCount - 1) {
739 if (m_processor == 0) {
740 fmt::print(Ioss::WarnOut(),
741 "Max value in element processor {} is {} which is\n"
742 "\tless than the processor count ({}). Make sure this is correct.\n",
743 label, max_proc, m_processorCount);
744 }
745 }
746 else if (max_proc >= m_processorCount) {
747 IOSS_ERROR(
748 fmt::format("ERROR: The element processor {} '{}' specifies a processor of {} which\n"
749 "\tis not valid for a decomposition on {} processors.",
750 label, m_decompExtra, max_proc, m_processorCount));
751 }
752
753 // Finally... Do the decomposition...
754 // Count exports to each rank...
755 exportElementCount.resize(m_processorCount + 1);
756 exportElementIndex.resize(m_processorCount + 1);
757 importElementCount.resize(m_processorCount + 1);
758 importElementIndex.resize(m_processorCount + 1);
759
760 for (const auto &proc : m_elementToProc) {
761 exportElementCount[proc]++;
762 }
763
764 size_t local = exportElementCount[m_processor];
765 localElementMap.reserve(local);
766 for (size_t i = 0; i < m_elementToProc.size(); i++) {
767 if (m_elementToProc[i] == m_processor) {
768 localElementMap.push_back(i);
769 }
770 }
771
772 exportElementCount[m_processor] = 0;
773 #if IOSS_DEBUG_OUTPUT
774 fmt::print(Ioss::DebugOut(), "[{}] Export Count: {}\n", m_processor,
775 fmt::join(exportElementCount, " "));
776 #endif
777 MPI_Alltoall(Data(exportElementCount), 1, Ioss::mpi_type((INT)0), Data(importElementCount), 1,
778 Ioss::mpi_type((INT)0), m_comm);
779 show_progress("\tguided_decompose Communication 1 finished");
780
781 // Now fill the vectors with the elements ...
782 size_t exp_size = std::accumulate(exportElementCount.begin(), exportElementCount.end(), 0);
783 exportElementMap.resize(exp_size);
784
785 std::copy(exportElementCount.begin(), exportElementCount.end(), exportElementIndex.begin());
786 Ioss::Utils::generate_index(exportElementIndex);
787
788 {
789 std::vector<INT> tmp_disp(exportElementIndex);
790 for (size_t i = 0; i < m_elementToProc.size(); i++) {
791 if (m_elementToProc[i] != m_processor) {
792 exportElementMap[tmp_disp[m_elementToProc[i]]++] = m_elementOffset + i;
793 }
794 }
795 }
796
797 m_elementToProc.clear();
798
799 size_t imp_size = std::accumulate(importElementCount.begin(), importElementCount.end(), 0);
800 importElementMap.resize(imp_size);
801 importElementIndex.resize(m_processorCount + 1);
802 std::copy(importElementCount.begin(), importElementCount.end(), importElementIndex.begin());
803 Ioss::Utils::generate_index(importElementIndex);
804
805 Ioss::MY_Alltoallv(exportElementMap, exportElementCount, exportElementIndex, importElementMap,
806 importElementCount, importElementIndex, m_comm);
807 show_progress("\tguided_decompose Communication 2 finished");
808
809 #if IOSS_DEBUG_OUTPUT
810 fmt::print(Ioss::DebugOut(), "Processor {}:\t{} local, {} imported and {} exported elements\n",
811 m_processor, fmt::group_digits(m_elementCount - exp_size),
812 fmt::group_digits(imp_size), fmt::group_digits(exp_size));
813 #endif
814 }
815
816 template <typename INT> void Decomposition<INT>::simple_decompose()
817 {
818 show_progress(__func__);
819 if (m_method == "LINEAR") {
820 // The "ioss_decomposition" is the same as the "file_decomposition"
821 // Nothing is imported or exported, everything stays "local"
822
823 size_t local = m_elementDist[m_processor + 1] - m_elementDist[m_processor];
824 assert(local == m_elementCount);
825 localElementMap.resize(local);
826 std::iota(localElementMap.begin(), localElementMap.end(), 0);
827
828 // All values are 0
829 exportElementCount.resize(m_processorCount + 1);
830 exportElementIndex.resize(m_processorCount + 1);
831 importElementCount.resize(m_processorCount + 1);
832 importElementIndex.resize(m_processorCount + 1);
833 }
834 }
835
836 template <typename INT> void Decomposition<INT>::simple_node_decompose()
837 {
838 // Used if there are no elements on the model...
839 show_progress(__func__);
840 if (m_method == "LINEAR") {
841 // The "ioss_decomposition" is the same as the "file_decomposition"
842 // Nothing is imported or exported, everything stays "local"
843
844 size_t local_elem = 0;
845
846 // All values are 0
847 localElementMap.resize(local_elem);
848 exportElementCount.resize(m_processorCount + 1);
849 exportElementIndex.resize(m_processorCount + 1);
850 importElementCount.resize(m_processorCount + 1);
851 importElementIndex.resize(m_processorCount + 1);
852
853 size_t local = m_nodeDist[m_processor + 1] - m_nodeDist[m_processor];
854 assert(local == m_nodeCount);
855
856 localNodeMap.resize(local);
857 nodeGTL.resize(local);
858 std::iota(localNodeMap.begin(), localNodeMap.end(), m_nodeOffset);
859 std::iota(nodeGTL.begin(), nodeGTL.end(), m_nodeOffset + 1);
860
861 // All values are 0
862 exportNodeCount.resize(m_processorCount + 1);
863 exportNodeIndex.resize(m_processorCount + 1);
864 importNodeCount.resize(m_processorCount + 1);
865 importNodeIndex.resize(m_processorCount + 1);
866 }
867 }
868
869 #if !defined(NO_PARMETIS_SUPPORT)
870 template <typename INT>
871 void Decomposition<INT>::metis_decompose(idx_t *pointer, idx_t *adjacency,
872 std::vector<BlockDecompositionData> &el_blocks)
873 {
874 show_progress(__func__);
875 std::vector<idx_t> elem_partition(m_elementCount);
876
877 // Determine whether sizeof(INT) matches sizeof(idx_t).
878 // If not, decide how to proceed...
879 if (sizeof(INT) == sizeof(idx_t)) {
880 internal_metis_decompose(el_blocks, (idx_t *)Data(m_elementDist), pointer, adjacency,
881 Data(elem_partition));
882 }
883
884 // Now know that they don't match... Are we widening or narrowing...
885 else if (sizeof(idx_t) > sizeof(INT)) {
886 assert(sizeof(idx_t) == 8);
887 // ... Widening; just create new wider arrays
888 std::vector<idx_t> dist_cv(m_elementDist.begin(), m_elementDist.end());
889 std::vector<idx_t> pointer_cv(m_pointer.begin(), m_pointer.end());
890 std::vector<idx_t> adjacency_cv(m_adjacency.begin(), m_adjacency.end());
891 internal_metis_decompose(el_blocks, Data(dist_cv), Data(pointer_cv), Data(adjacency_cv),
892 Data(elem_partition));
893 }
894
895 else if (sizeof(idx_t) < sizeof(INT)) {
896 // ... Narrowing. See if data range (#elements and/or #nodes) fits in
897 // 32-bit idx_t
898 // Can determine this by checking the pointer[
899 assert(sizeof(idx_t) == 4);
900 if (m_globalElementCount >= INT_MAX || m_globalNodeCount >= INT_MAX ||
901 m_pointer[m_elementCount] >= INT_MAX) {
902 // Can't narrow...
903 IOSS_ERROR(fmt::format(
904 "ERROR: The metis/parmetis libraries being used with this application only support\n"
905 " 32-bit integers, but the mesh being decomposed requires 64-bit integers.\n"
906 " You must either choose a different, non-metis decomposition method, or\n"
907 " rebuild your metis/parmetis libraries with 64-bit integer support.\n"
908 " Contact [email protected] for more details.\n"));
909 }
910 else {
911 // Should be able to narrow...
912 std::vector<idx_t> dist_cv(m_elementDist.begin(), m_elementDist.end());
913 std::vector<idx_t> pointer_cv(m_pointer.begin(), m_pointer.end());
914 std::vector<idx_t> adjacency_cv(m_adjacency.begin(), m_adjacency.end());
915 internal_metis_decompose(el_blocks, Data(dist_cv), Data(pointer_cv), Data(adjacency_cv),
916 Data(elem_partition));
917 }
918 }
919 // ------------------------------------------------------------------------
920 // Done with metis functions...
921 show_progress("\tDone with metis functions");
922
923 // Determine how many elements I send to the other processors...
924 // and how many remain local (on this processor)
925 exportElementCount.resize(m_processorCount + 1);
926 for (auto element : elem_partition) {
927 exportElementCount[element]++;
928 }
929
930 size_t local = exportElementCount[m_processor];
931 localElementMap.reserve(local);
932 for (size_t i = 0; i < elem_partition.size(); i++) {
933 if (elem_partition[i] == m_processor) {
934 localElementMap.push_back(i);
935 }
936 }
937
938 // Zero out the local element count so local elements aren't communicated.
939 exportElementCount[m_processor] = 0;
940
941 importElementCount.resize(m_processorCount + 1);
942 MPI_Alltoall(Data(exportElementCount), 1, Ioss::mpi_type((INT)0), Data(importElementCount), 1,
943 Ioss::mpi_type((INT)0), m_comm);
944 show_progress("\tmetis_decompose Communication 1 finished");
945
946 // Now fill the vectors with the elements ...
947 size_t exp_size = std::accumulate(exportElementCount.begin(), exportElementCount.end(), 0);
948
949 exportElementMap.resize(exp_size);
950 exportElementIndex.resize(m_processorCount + 1);
951 std::copy(exportElementCount.begin(), exportElementCount.end(), exportElementIndex.begin());
952 Ioss::Utils::generate_index(exportElementIndex);
953
954 {
955 std::vector<INT> tmp_disp(exportElementIndex);
956 for (size_t i = 0; i < elem_partition.size(); i++) {
957 if (elem_partition[i] != m_processor) {
958 exportElementMap[tmp_disp[elem_partition[i]]++] = m_elementOffset + i;
959 }
960 }
961 }
962 Ioss::Utils::clear(elem_partition);
963
964 size_t imp_size = std::accumulate(importElementCount.begin(), importElementCount.end(), 0);
965 importElementMap.resize(imp_size);
966 importElementIndex.resize(m_processorCount + 1);
967 std::copy(importElementCount.begin(), importElementCount.end(), importElementIndex.begin());
968 Ioss::Utils::generate_index(importElementIndex);
969
970 Ioss::MY_Alltoallv(exportElementMap, exportElementCount, exportElementIndex, importElementMap,
971 importElementCount, importElementIndex, m_comm);
972 show_progress("\tmetis_decompose Communication 2 finished");
973
974 #if IOSS_DEBUG_OUTPUT
975 fmt::print(Ioss::DebugOut(), "Processor {}:\t{} local, {} imported and {} exported elements\n",
976 m_processor, fmt::group_digits(m_elementCount - exp_size),
977 fmt::group_digits(imp_size), fmt::group_digits(exp_size));
978 #endif
979 }
980
981 template <typename INT>
982 void Decomposition<INT>::internal_metis_decompose(std::vector<BlockDecompositionData> &el_blocks,
983 idx_t *element_dist, idx_t *pointer,
984 idx_t *adjacency, idx_t *elem_partition)
985 {
986 idx_t wgt_flag = 0; // No weights
987 idx_t *elm_wgt = nullptr;
988 idx_t ncon = 1;
989 idx_t num_flag = 0; // Use C-based numbering
990 idx_t common_nodes =
991 m_commonNodeCount > 0 ? m_commonNodeCount : get_common_node_count(el_blocks, m_pu);
992
993 idx_t nparts = m_processorCount;
994 idx_t ndims = m_spatialDimension;
995 std::vector<real_t> tp_wgts(ncon * nparts, 1.0 / nparts);
996
997 std::vector<real_t> ub_vec(ncon, 1.01);
998
999 idx_t edge_cuts = 0;
1000
1001 std::vector<idx_t> options(3);
1002 options[0] = 1; // Use my values instead of default
1003 options[1] = 0; // PARMETIS_DBGLVL_TIME;
1004 options[2] = 1234567; // Random number seed
1005
1006 show_progress(__func__);
1007 if (m_method == "KWAY") {
1008 int rc = ParMETIS_V3_PartMeshKway(
1009 element_dist, pointer, adjacency, elm_wgt, &wgt_flag, &num_flag, &ncon, &common_nodes,
1010 &nparts, Data(tp_wgts), Data(ub_vec), Data(options), &edge_cuts, elem_partition, &m_comm);
1011 #if IOSS_DEBUG_OUTPUT
1012 fmt::print(Ioss::DebugOut(), "Edge Cuts = {}\n", edge_cuts);
1013 #endif
1014 if (rc != METIS_OK) {
1015 IOSS_ERROR(fmt::format("ERROR: Problem during call to ParMETIS_V3_PartMeshKWay "
1016 "decomposition\n"));
1017 }
1018 }
1019 else if (m_method == "GEOM_KWAY" || m_method == "KWAY_GEOM") {
1020
1021 idx_t *dual_xadj = nullptr;
1022 idx_t *dual_adjacency = nullptr;
1023 int rc = ParMETIS_V3_Mesh2Dual(element_dist, pointer, adjacency, &num_flag, &common_nodes,
1024 &dual_xadj, &dual_adjacency, &m_comm);
1025
1026 if (rc != METIS_OK) {
1027 IOSS_ERROR(
1028 fmt::format("ERROR: Problem during call to ParMETIS_V3_Mesh2Dual graph conversion\n"));
1029 }
1030
1031 if (sizeof(double) == sizeof(real_t)) {
1032 rc = ParMETIS_V3_PartGeomKway(element_dist, dual_xadj, dual_adjacency, elm_wgt, elm_wgt,
1033 &wgt_flag, &num_flag, &ndims, (real_t *)Data(m_centroids),
1034 &ncon, &nparts, Data(tp_wgts), Data(ub_vec), Data(options),
1035 &edge_cuts, elem_partition, &m_comm);
1036 }
1037 else {
1038 std::vector<real_t> centroids(m_centroids.begin(), m_centroids.end());
1039 rc = ParMETIS_V3_PartGeomKway(element_dist, dual_xadj, dual_adjacency, elm_wgt, elm_wgt,
1040 &wgt_flag, &num_flag, &ndims, Data(centroids), &ncon, &nparts,
1041 Data(tp_wgts), Data(ub_vec), Data(options), &edge_cuts,
1042 elem_partition, &m_comm);
1043 }
1044
1045 #if IOSS_DEBUG_OUTPUT
1046 fmt::print(Ioss::DebugOut(), "Edge Cuts = {}\n", edge_cuts);
1047 #endif
1048 METIS_Free(dual_xadj);
1049 METIS_Free(dual_adjacency);
1050
1051 if (rc != METIS_OK) {
1052 IOSS_ERROR(
1053 fmt::format("ERROR: Problem during call to ParMETIS_V3_PartGeomKWay decomposition\n"));
1054 }
1055 }
1056 else if (m_method == "METIS_SFC") {
1057 int rc = METIS_OK;
1058 if (sizeof(double) == sizeof(real_t)) {
1059 rc = ParMETIS_V3_PartGeom(element_dist, &ndims, (real_t *)Data(m_centroids), elem_partition,
1060 &m_comm);
1061 }
1062 else {
1063 std::vector<real_t> centroids(m_centroids.begin(), m_centroids.end());
1064 rc = ParMETIS_V3_PartGeom(element_dist, &ndims, Data(centroids), elem_partition, &m_comm);
1065 }
1066
1067 if (rc != METIS_OK) {
1068 IOSS_ERROR(
1069 fmt::format("ERROR: Problem during call to ParMETIS_V3_PartGeom decomposition\n"));
1070 }
1071 }
1072 m_centroids.clear();
1073 }
1074 #endif
1075
1076 #if !defined(NO_ZOLTAN_SUPPORT)
1077
1078 template <typename INT> void Decomposition<INT>::zoltan_decompose(Zoltan &zz)
1079 {
1080 show_progress(__func__);
1081 // Set Zoltan parameters
1082 std::string num_proc = std::to_string(m_processorCount);
1083 zz.Set_Param("DEBUG_LEVEL", "0");
1084 zz.Set_Param("NUM_GLOBAL_PARTS", num_proc);
1085
1086 int num_global = sizeof(INT) / sizeof(ZOLTAN_ID_TYPE);
1087 num_global = num_global < 1 ? 1 : num_global;
1088
1089 int lib_global_id_type_size = Zoltan_get_global_id_type(nullptr);
1090 if (lib_global_id_type_size != sizeof(ZOLTAN_ID_TYPE)) {
1091 IOSS_ERROR(fmt::format(
1092 "ERROR: The compile-time ZOLTAN_ID_TYPE size ({}) does not match the run-time "
1093 "ZOLTAN_ID_TYPE size ({}). There is an error in the build/link procedure for this "
1094 "application.\n",
1095 sizeof(ZOLTAN_ID_TYPE), lib_global_id_type_size));
1096 }
1097
1098 zz.Set_Param("NUM_GID_ENTRIES", std::to_string(num_global));
1099 zz.Set_Param("NUM_LID_ENTRIES", "0");
1100 zz.Set_Param("LB_METHOD", m_method);
1101 zz.Set_Param("REMAP", "0");
1102 zz.Set_Param("RETURN_LISTS", "ALL");
1103
1104 int changes = 0;
1105 int num_local = 0;
1106 int num_import = 1;
1107 int num_export = 1;
1108 ZOLTAN_ID_PTR import_global_ids = nullptr;
1109 ZOLTAN_ID_PTR import_local_ids = nullptr;
1110 ZOLTAN_ID_PTR export_global_ids = nullptr;
1111 ZOLTAN_ID_PTR export_local_ids = nullptr;
1112 int *import_procs = nullptr;
1113 int *import_to_part = nullptr;
1114 int *export_procs = nullptr;
1115 int *export_to_part = nullptr;
1116
1117 num_local = 1;
1118
1119 int rc = zz.LB_Partition(changes, num_global, num_local, num_import, import_global_ids,
1120 import_local_ids, import_procs, import_to_part, num_export,
1121 export_global_ids, export_local_ids, export_procs, export_to_part);
1122
1123 if (rc != ZOLTAN_OK) {
1124 IOSS_ERROR(fmt::format("ERROR: Problem during call to Zoltan LB_Partition.\n"));
1125 }
1126 show_progress("\tZoltan lb_partition finished");
1127
1128 #if IOSS_DEBUG_OUTPUT
1129 fmt::print(Ioss::DebugOut(), "Processor {}:\t{} local, {} imported and {} exported elements\n",
1130 m_processor, fmt::group_digits(m_elementCount - num_export),
1131 fmt::group_digits(num_import), fmt::group_digits(num_export));
1132 #endif
1133
1134 // Don't need centroid data anymore... Free up space
1135 Ioss::Utils::clear(m_centroids);
1136
1137 // Find all elements that remain locally owned...
1138 get_local_element_list(export_global_ids, num_export);
1139
1140 // Build exportElementMap and importElementMap...
1141 importElementMap.reserve(num_import);
1142 importElementIndex.resize(m_processorCount + 1);
1143 importElementCount.resize(m_processorCount + 1);
1144
1145 if (num_global == 1) {
1146 if (num_export > 0 && export_procs == nullptr) {
1147 IOSS_ERROR(
1148 fmt::format("ERROR: Internal error in zoltan_decompose. export_procs is null.\n"));
1149 }
1150
1151 std::vector<std::pair<int, int>> export_map;
1152 export_map.reserve(num_export);
1153 for (int i = 0; i < num_export; i++) {
1154 export_map.emplace_back(export_procs[i], export_global_ids[i]);
1155 }
1156
1157 Ioss::sort(export_map.begin(), export_map.end());
1158 exportElementMap.reserve(num_export);
1159 exportElementIndex.resize(m_processorCount + 1);
1160 exportElementCount.resize(m_processorCount + 1);
1161 for (auto elem_count : export_map) {
1162 exportElementMap.push_back(elem_count.second);
1163 exportElementCount[elem_count.first]++;
1164 }
1165
1166 for (int i = 0; i < num_import; i++) {
1167 importElementMap.push_back(import_global_ids[i]);
1168 importElementCount[import_procs[i]]++;
1169 }
1170 }
1171 else {
1172 if (num_export > 0 && export_procs == nullptr) {
1173 IOSS_ERROR(
1174 fmt::format("ERROR: Internal error in zoltan_decompose. export_procs is null.\n"));
1175 }
1176 std::vector<std::pair<int, int64_t>> export_map;
1177 export_map.reserve(num_export);
1178 auto *export_glob = reinterpret_cast<int64_t *>(export_global_ids);
1179 for (int i = 0; i < num_export; i++) {
1180 export_map.emplace_back(export_procs[i], export_glob[i]);
1181 }
1182
1183 Ioss::sort(export_map.begin(), export_map.end());
1184 exportElementMap.reserve(num_export);
1185 exportElementIndex.resize(m_processorCount + 1);
1186 exportElementCount.resize(m_processorCount + 1);
1187 for (auto elem_count : export_map) {
1188 exportElementMap.push_back(elem_count.second);
1189 exportElementCount[elem_count.first]++;
1190 }
1191
1192 auto *import_glob = reinterpret_cast<int64_t *>(import_global_ids);
1193 for (int i = 0; i < num_import; i++) {
1194 importElementMap.push_back(import_glob[i]);
1195 importElementCount[import_procs[i]]++;
1196 }
1197 }
1198
1199 std::copy(exportElementCount.begin(), exportElementCount.end(), exportElementIndex.begin());
1200 Ioss::Utils::generate_index(exportElementIndex);
1201
1202 zz.LB_Free_Part(&import_global_ids, &import_local_ids, &import_procs, &import_to_part);
1203 zz.LB_Free_Part(&export_global_ids, &export_local_ids, &export_procs, &export_to_part);
1204 }
1205 #endif
1206
1207 #if !defined(NO_ZOLTAN_SUPPORT)
1208 template <typename INT>
1209 void Decomposition<INT>::get_local_element_list(const ZOLTAN_ID_PTR &export_global_ids,
1210 size_t export_count)
1211 {
1212 show_progress(__func__);
1213 std::vector<size_t> elements(m_elementCount);
1214
1215 size_t global_id_size = sizeof(INT) / sizeof(int);
1216
1217 if (global_id_size == 1) {
1218 for (size_t i = 0; i < export_count; i++) {
1219 // flag all elements to be exported...
1220 size_t elem = export_global_ids[i];
1221 elements[elem - m_elementOffset] = 1;
1222 }
1223 }
1224 else {
1225 assert(global_id_size == 2);
1226 auto *export_glob = reinterpret_cast<int64_t *>(export_global_ids);
1227
1228 for (size_t i = 0; i < export_count; i++) {
1229 // flag all elements to be exported...
1230 size_t elem = export_glob[i];
1231 elements[elem - m_elementOffset] = 1;
1232 }
1233 }
1234
1235 localElementMap.reserve(m_elementCount - export_count);
1236 for (size_t i = 0; i < m_elementCount; i++) {
1237 if (elements[i] == 0) {
1238 localElementMap.push_back(i);
1239 }
1240 }
1241 }
1242 #endif
1243
1244 template IOSS_EXPORT void Decomposition<int>::build_global_to_local_elem_map();
1245 template IOSS_EXPORT void Decomposition<int64_t>::build_global_to_local_elem_map();
1246
1247 template <typename INT> void Decomposition<INT>::build_global_to_local_elem_map()
1248 {
1249 show_progress(__func__);
1250 // global_index is 1-based index into global list of elems
1251 // [1..global_elem_count]
1252 #if defined(DC_USE_HOPSCOTCH) || defined(DC_USE_ROBIN) || defined(DC_USE_VECTOR)
1253 elemGTL.reserve(localElementMap.size() + m_importPreLocalElemIndex + importElementMap.size());
1254 #endif
1255 for (size_t i = 0; i < localElementMap.size(); i++) {
1256 size_t global_index = localElementMap[i] + m_elementOffset + 1;
1257 size_t local_index = i + m_importPreLocalElemIndex + 1;
1258 #if defined(DC_USE_VECTOR)
1259 elemGTL.emplace_back(global_index, local_index);
1260 #else
1261 elemGTL.insert({global_index, local_index});
1262 #endif
1263 }
1264
1265 for (size_t i = 0; i < m_importPreLocalElemIndex; i++) {
1266 size_t global_index = importElementMap[i] + 1;
1267 size_t local_index = i + 1;
1268 #if defined(DC_USE_VECTOR)
1269 elemGTL.emplace_back(global_index, local_index);
1270 #else
1271 elemGTL.insert({global_index, local_index});
1272 #endif
1273 }
1274
1275 for (size_t i = m_importPreLocalElemIndex; i < importElementMap.size(); i++) {
1276 size_t global_index = importElementMap[i] + 1;
1277 size_t local_index = localElementMap.size() + i + 1;
1278 #if defined(DC_USE_VECTOR)
1279 elemGTL.emplace_back(global_index, local_index);
1280 #else
1281 elemGTL.insert({global_index, local_index});
1282 #endif
1283 }
1284 #if defined(DC_USE_VECTOR)
1285 Ioss::sort(elemGTL.begin(), elemGTL.end());
1286 #endif
1287 show_progress("build_global_to_local_elem_map end");
1288 }
1289
1290 template <typename INT> void Decomposition<INT>::get_local_node_list()
1291 {
1292 // Get the connectivity of all imported elements...
1293 // First, determine how many nodes the exporting processors are
1294 // going to send me and how many nodes my exported elements
1295 // have...
1296 show_progress(__func__);
1297
1298 std::vector<INT> export_conn_size(m_processorCount);
1299 std::vector<INT> import_conn_size(m_processorCount);
1300 for (int p = 0; p < m_processorCount; p++) {
1301 size_t el_begin = exportElementIndex[p];
1302 size_t el_end = exportElementIndex[p + 1];
1303 for (size_t i = el_begin; i < el_end; i++) {
1304 INT elem = exportElementMap[i] - m_elementOffset;
1305 size_t nnpe = m_pointer[elem + 1] - m_pointer[elem];
1306 export_conn_size[p] += nnpe;
1307 }
1308 }
1309
1310 MPI_Alltoall(Data(export_conn_size), 1, Ioss::mpi_type((INT)0), Data(import_conn_size), 1,
1311 Ioss::mpi_type((INT)0), m_comm);
1312 show_progress("\tCommunication 1 finished");
1313
1314 // Now fill the vectors with the nodes ...
1315 size_t exp_size = std::accumulate(export_conn_size.begin(), export_conn_size.end(), 0);
1316 size_t imp_size = std::accumulate(import_conn_size.begin(), import_conn_size.end(), 0);
1317 std::vector<INT> export_conn;
1318 export_conn.reserve(exp_size);
1319
1320 std::vector<INT> export_disp(m_processorCount);
1321 std::vector<INT> import_disp(m_processorCount);
1322 for (int p = 1; p < m_processorCount; p++) {
1323 export_disp[p] = export_disp[p - 1] + export_conn_size[p - 1];
1324 import_disp[p] = import_disp[p - 1] + import_conn_size[p - 1];
1325 }
1326
1327 for (int p = 0; p < m_processorCount; p++) {
1328 size_t el_begin = exportElementIndex[p];
1329 size_t el_end = exportElementIndex[p + 1];
1330 for (size_t i = el_begin; i < el_end; i++) {
1331 INT elem = exportElementMap[i] - m_elementOffset;
1332 for (INT n = m_pointer[elem]; n < m_pointer[elem + 1]; n++) {
1333 export_conn.push_back(m_adjacency[n]);
1334 }
1335 }
1336 }
1337
1338 // Count number of nodes on local elements...
1339 size_t node_sum = 0;
1340 for (auto elem : localElementMap) {
1341 node_sum += m_pointer[elem + 1] - m_pointer[elem];
1342 }
1343 // Also holds imported nodes...
1344 node_sum += imp_size;
1345
1346 std::vector<INT> nodes;
1347
1348 {
1349 std::vector<INT> import_conn(imp_size);
1350
1351 Ioss::MY_Alltoallv(export_conn, export_conn_size, export_disp, import_conn, import_conn_size,
1352 import_disp, m_comm);
1353 show_progress("\tCommunication 2 finished");
1354
1355 // Done with export_conn...
1356 Ioss::Utils::clear(export_conn);
1357
1358 // Find list of unique nodes used by the elements on this
1359 // processor... adjacency list contains connectivity for local
1360 // elements and import_conn contains connectivity for imported
1361 // elements.
1362
1363 // Nodes on Imported elements...
1364 nodes.reserve(node_sum);
1365 for (auto node : import_conn) {
1366 nodes.push_back(node);
1367 }
1368 }
1369
1370 // Nodes on local elements...
1371 for (auto elem : localElementMap) {
1372 for (INT n = m_pointer[elem]; n < m_pointer[elem + 1]; n++) {
1373 nodes.push_back(m_adjacency[n]);
1374 }
1375 }
1376
1377 // Now need to sort and Ioss::Utils::uniquify 'nodes'
1378 Ioss::Utils::uniquify(nodes);
1379 show_progress("\tUniquify finished");
1380
1381 // Determine owning 'file' processor for each node...
1382 nodeIndex.resize(m_processorCount + 1);
1383
1384 for (auto node : nodes) {
1385 INT owning_processor = Ioss::Utils::find_index_location(node, m_nodeDist);
1386 nodeIndex[owning_processor]++;
1387 }
1388 importNodeCount.resize(nodeIndex.size());
1389 std::copy(nodeIndex.begin(), nodeIndex.end(), importNodeCount.begin());
1390 exportNodeCount.resize(m_processorCount);
1391 Ioss::Utils::generate_index(nodeIndex);
1392
1393 // Tell other processors how many nodes I will be importing from
1394 // them...
1395 importNodeCount[m_processor] = 0;
1396 MPI_Alltoall(Data(importNodeCount), 1, Ioss::mpi_type((INT)0), Data(exportNodeCount), 1,
1397 Ioss::mpi_type((INT)0), m_comm);
1398 show_progress("\tCommunication 3 finished");
1399
1400 size_t import_sum = std::accumulate(importNodeCount.begin(), importNodeCount.end(), 0);
1401 size_t export_sum = std::accumulate(exportNodeCount.begin(), exportNodeCount.end(), 0);
1402
1403 std::vector<INT> import_nodes;
1404 import_nodes.reserve(import_sum);
1405 importNodeMap.reserve(import_sum);
1406 for (int p = 0; p < m_processorCount; p++) {
1407 size_t beg = nodeIndex[p];
1408 size_t end = nodeIndex[p + 1];
1409
1410 if (p == m_processor) {
1411 m_importPreLocalNodeIndex = beg;
1412 localNodeMap.reserve(end - beg);
1413 for (size_t n = beg; n < end; n++) {
1414 localNodeMap.push_back(nodes[n]);
1415 }
1416 }
1417 else {
1418 for (size_t n = beg; n < end; n++) {
1419 import_nodes.push_back(nodes[n]);
1420 importNodeMap.push_back(n);
1421 }
1422 }
1423 }
1424 assert(import_nodes.size() == import_sum);
1425 exportNodeMap.resize(export_sum);
1426 exportNodeIndex.resize(m_processorCount + 1);
1427 std::copy(exportNodeCount.begin(), exportNodeCount.end(), exportNodeIndex.begin());
1428 Ioss::Utils::generate_index(exportNodeIndex);
1429
1430 // Now send the list of nodes that I need to import from each
1431 // processor...
1432 importNodeIndex.resize(importNodeCount.size());
1433 std::copy(importNodeCount.begin(), importNodeCount.end(), importNodeIndex.begin());
1434 Ioss::Utils::generate_index(importNodeIndex);
1435
1436 Ioss::MY_Alltoallv(import_nodes, importNodeCount, importNodeIndex, exportNodeMap,
1437 exportNodeCount, exportNodeIndex, m_comm);
1438 Ioss::Utils::clear(import_nodes);
1439 show_progress("\tCommunication 4 finished");
1440
1441 if (m_retainFreeNodes) {
1442 // See if all nodes have been accounted for (i.e., process non-connected nodes)
1443 std::vector<bool> file_nodes(m_nodeCount);
1444 for (const auto &node : exportNodeMap) {
1445 file_nodes[node - m_nodeOffset] = true;
1446 }
1447 for (const auto &node : localNodeMap) {
1448 file_nodes[node - m_nodeOffset] = true;
1449 }
1450
1451 size_t found_count = 0;
1452 for (size_t i = 0; i < file_nodes.size(); i++) {
1453 if (!file_nodes[i]) {
1454 localNodeMap.push_back(i + m_nodeOffset);
1455 nodes.push_back(i + m_nodeOffset);
1456 found_count++;
1457 #if IOSS_DEBUG_OUTPUT
1458 fmt::print(Ioss::DebugOut(), "Processor {}:\tNode {} not connected to any elements\n",
1459 m_processor, i + m_nodeOffset + 1);
1460 #endif
1461 }
1462 }
1463
1464 if (found_count > 0) {
1465 nodes.shrink_to_fit();
1466 localNodeMap.shrink_to_fit();
1467 Ioss::sort(nodes.begin(), nodes.end());
1468 Ioss::sort(localNodeMap.begin(), localNodeMap.end());
1469 for (int proc = m_processor + 1; proc < m_processorCount + 1; proc++) {
1470 nodeIndex[proc] += found_count;
1471 }
1472
1473 assert((size_t)nodeIndex[m_processorCount] == nodes.size());
1474
1475 // Also need to update importNodeMap for all nodes being
1476 // imported from processors higher than m_processor...
1477 size_t beg = importNodeIndex[m_processor + 1];
1478 size_t end = importNodeIndex[m_processorCount];
1479 for (size_t i = beg; i < end; i++) {
1480 importNodeMap[i] += found_count;
1481 }
1482 }
1483 }
1484
1485 // Map that converts nodes from the global index (1-based) to a
1486 // local-per-processor index (1-based)
1487 #if IOSS_DEBUG_OUTPUT
1488 fmt::print(Ioss::DebugOut(), "Processor {}:\tNode Count = {}\n", m_processor,
1489 fmt::group_digits(nodes.size()));
1490 #endif
1491 nodeGTL.swap(nodes);
1492 for (size_t i = 0; i < nodeGTL.size(); i++) {
1493 nodeGTL[i]++; // convert from 0-based index to 1-based index
1494 }
1495 show_progress(__func__);
1496 }
1497
1498 template <typename INT> void Decomposition<INT>::get_shared_node_list()
1499 {
1500 // Need a list of all "shared" nodes (nodes on more than one
1501 // processor) and the list of processors that they are on for the
1502 // ioss decomposition.
1503 //
1504 // * iterate all local nodes (those that are in both file and ioss
1505 // decomposition)
1506 // on this processor and all exported nodes,
1507 // * put in a vector and sort on (id,proc).
1508 // * iterate and create a vector of all shared nodes and the
1509 // processor they are on..
1510 show_progress(__func__);
1511 size_t local_node_count = nodeIndex[m_processor + 1] - nodeIndex[m_processor];
1512 std::vector<std::pair<INT, int>> node_proc_list;
1513 node_proc_list.reserve(local_node_count + exportNodeMap.size());
1514
1515 for (auto local_node : localNodeMap) {
1516 node_proc_list.push_back(std::make_pair(local_node, m_processor));
1517 }
1518
1519 for (int p = 0; p < m_processorCount; p++) {
1520 if (p == m_processor) {
1521 continue;
1522 }
1523 size_t beg = exportNodeIndex[p];
1524 size_t end = exportNodeIndex[p + 1];
1525 for (size_t i = beg; i < end; i++) {
1526 node_proc_list.push_back(std::make_pair(exportNodeMap[i], p));
1527 }
1528 }
1529 Ioss::sort(node_proc_list.begin(), node_proc_list.end());
1530
1531 std::vector<std::pair<INT, int>> shared_nodes;
1532 for (size_t i = 0; i < node_proc_list.size(); i++) {
1533 INT node = node_proc_list[i].first;
1534 if (i + 1 < node_proc_list.size() && node_proc_list[i + 1].first == node) {
1535 shared_nodes.push_back(node_proc_list[i]);
1536 }
1537
1538 while (i + 1 < node_proc_list.size() && node_proc_list[i + 1].first == node) {
1539 shared_nodes.push_back(node_proc_list[++i]);
1540 }
1541 }
1542
1543 // The shared_nodes list contains all nodes that I know about that
1544 // are shared.
1545
1546 // Determine the counts...
1547 std::vector<INT> send_comm_map_count(m_processorCount);
1548 for (size_t i = 0; i < shared_nodes.size(); i++) {
1549 size_t beg = i;
1550 size_t end = ++i;
1551 while (i + 1 < shared_nodes.size() && shared_nodes[beg].first == shared_nodes[i + 1].first) {
1552 end = ++i;
1553 }
1554 for (size_t p = beg; p <= end; p++) {
1555 int proc = shared_nodes[p].second;
1556 for (size_t j = beg; j <= end; j++) {
1557 if (j == p) {
1558 continue;
1559 }
1560 assert(shared_nodes[p].first == shared_nodes[j].first);
1561 send_comm_map_count[proc] += 2;
1562 }
1563 }
1564 }
1565
1566 // Determine total count... (including m_processor for now just to
1567 // see whether it simplifies/complicates coding)
1568 std::vector<INT> send_comm_map_disp(m_processorCount + 1);
1569 std::copy(send_comm_map_count.begin(), send_comm_map_count.end(), send_comm_map_disp.begin());
1570 Ioss::Utils::generate_index(send_comm_map_disp);
1571
1572 std::vector<INT> send_comm_map(send_comm_map_disp[m_processorCount]);
1573 std::vector<INT> nc_offset(m_processorCount);
1574
1575 for (size_t i = 0; i < shared_nodes.size(); i++) {
1576 size_t beg = i;
1577 size_t end = ++i;
1578 while (i + 1 < shared_nodes.size() && shared_nodes[beg].first == shared_nodes[i + 1].first) {
1579 end = ++i;
1580 }
1581 for (size_t p = beg; p <= end; p++) {
1582 int proc = shared_nodes[p].second;
1583 for (size_t j = beg; j <= end; j++) {
1584 if (j == p) {
1585 continue;
1586 }
1587 assert(shared_nodes[p].first == shared_nodes[j].first);
1588 size_t location = send_comm_map_disp[proc] + nc_offset[proc];
1589 send_comm_map[location + 0] = shared_nodes[j].first;
1590 send_comm_map[location + 1] = shared_nodes[j].second;
1591 nc_offset[proc] += 2;
1592 }
1593 }
1594 }
1595
1596 // Tell other processors how many nodes/procs I am sending them...
1597 std::vector<INT> recv_comm_map_count(m_processorCount);
1598 MPI_Alltoall(Data(send_comm_map_count), 1, Ioss::mpi_type((INT)0), Data(recv_comm_map_count), 1,
1599 Ioss::mpi_type((INT)0), m_comm);
1600 show_progress("\tCommunication 1 finished");
1601
1602 std::vector<INT> recv_comm_map_disp(recv_comm_map_count);
1603 Ioss::Utils::generate_index(recv_comm_map_disp);
1604 m_nodeCommMap.resize(recv_comm_map_disp[m_processorCount - 1] +
1605 recv_comm_map_count[m_processorCount - 1]);
1606 Ioss::MY_Alltoallv(send_comm_map, send_comm_map_count, send_comm_map_disp, m_nodeCommMap,
1607 recv_comm_map_count, recv_comm_map_disp, m_comm);
1608 Ioss::Utils::clear(send_comm_map);
1609 show_progress("\tCommunication 2 finished");
1610
1611 // Map global 0-based index to local 1-based index.
1612 for (size_t i = 0; i < m_nodeCommMap.size(); i += 2) {
1613 m_nodeCommMap[i] = node_global_to_local(m_nodeCommMap[i] + 1);
1614 }
1615 #if IOSS_DEBUG_OUTPUT
1616 fmt::print(Ioss::DebugOut(), "Processor {} has {} shared nodes\n", m_processor,
1617 fmt::group_digits(m_nodeCommMap.size() / 2));
1618 #endif
1619 show_progress(__func__);
1620 }
1621 } // namespace Ioss