Debian Codegraph @

packages/muelu/research/max/XpetraSplitting/Xpetra_RegionHandler_def.hpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // MueLu: A package for multigrid based preconditioning
4 //
5 // Copyright 2012 NTESS and the MueLu contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 /*
11 * Xpetra_RegionHandler_def.hpp
12 *
13 * Created on: August 17, 2017
14 * Author: Massimiliano Lupo Pasini ([email protected])
15 */
16 #ifndef XPETRA_REGIONHANDLER_DEF_HPP
17 #define XPETRA_REGIONHANDLER_DEF_HPP
18
19 #include "Xpetra_RegionHandler_decl.hpp"
20 #include <algorithm>
21
22 namespace Xpetra {
23
24 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
25 RegionHandler<Scalar, LocalOrdinal, GlobalOrdinal, Node>::RegionHandler(const std::string &file_name, RCP<const Teuchos::Comm<int> > comm)
26 : comm_(comm) {
27 ReadFileInfo(file_name);
28
29 // Nodes are shuffled so that regions are sorted in ascending labeling order
30 std::sort(nodes_.begin(), nodes_.end(), compareRegions<GlobalOrdinal>);
31 nodes_sorted_by_regions_ = true;
32
33 if (comm_->getRank() == 0)
34 std::cout << "Started NodesToRegion" << std::endl;
35 NodesToRegion();
36 if (comm_->getRank() == 0)
37 std::cout << "Finished NodesToRegion" << std::endl;
38 ComputeProcRegions();
39 if (comm_->getRank() == 0)
40 std::cout << "Started RowMaps" << std::endl;
41 CreateRowMaps();
42 if (comm_->getRank() == 0)
43 std::cout << "Finished RowMaps" << std::endl;
44
45 num_region_nodes_.clear();
46 num_region_nodes_.resize(num_total_regions_);
47
48 // For each region, the following loop counts the number of region nodes and stores them
49 for (GlobalOrdinal region_idx = 1; region_idx <= num_total_regions_; ++region_idx) {
50 checkerNode<GlobalOrdinal> unaryPredicate(region_idx);
51 typename Array<std::tuple<GlobalOrdinal, GlobalOrdinal> >::iterator nodes_iterator1;
52 typename Array<std::tuple<GlobalOrdinal, GlobalOrdinal> >::iterator nodes_iterator2;
53 nodes_iterator1 = std::find_if<typename Array<std::tuple<GlobalOrdinal, GlobalOrdinal> >::iterator, checkerNode<GlobalOrdinal> >(nodes_.begin(), nodes_.end(), unaryPredicate);
54 nodes_iterator2 = std::find_if_not<typename Array<std::tuple<GlobalOrdinal, GlobalOrdinal> >::iterator, checkerNode<GlobalOrdinal> >(nodes_iterator1, nodes_.end(), unaryPredicate);
55 num_region_nodes_[region_idx - 1] = nodes_iterator2 - nodes_iterator1;
56 }
57 }
58
59 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
60 void RegionHandler<Scalar, LocalOrdinal, GlobalOrdinal, Node>::ReadFileInfo(const std::string &file_name) {
61 std::ifstream input_file_(file_name, std::ifstream::in);
62 std::string line;
63 TEUCHOS_TEST_FOR_EXCEPTION(!input_file_.good(), Exceptions::RuntimeError, "Cannot read \"" << file_name << "\"");
64
65 GlobalOrdinal line_index = 0;
66
67 // The information contained in the file is imported and stored in a Teuchos::Array of tuples.
68 // The first field of the tuple is the composite node index, the second field of the tuple is the region index
69 while (std::getline(input_file_, line)) {
70 std::istringstream is(line);
71 GlobalOrdinal number;
72 Array<GlobalOrdinal> node;
73 std::tuple<GlobalOrdinal, GlobalOrdinal> node_region;
74 Array<GlobalOrdinal> composite_info;
75
76 node.clear();
77 composite_info.clear();
78
79 if (1 == line_index) {
80 while (is >> number)
81 composite_info.push_back(number);
82
83 TEUCHOS_TEST_FOR_EXCEPTION(composite_info.size() != 2, Exceptions::RuntimeError, "The composite information must be a couple of integers: nTotal of nodes + nTotal of regions \n");
84 num_total_nodes_ = composite_info[0];
85 num_total_regions_ = composite_info[1];
86 } else if (line_index > 2) {
87 while (is >> number) {
88 node.push_back(number);
89 }
90 TEUCHOS_TEST_FOR_EXCEPTION(node.size() != 2, Exceptions::RuntimeError, "The node information must be a couple of integers: Node index + Region idnex \n");
91 node_region = std::make_tuple(node[0], node[1]);
92 nodes_.push_back(node_region);
93 node.clear();
94 }
95 line_index++;
96 }
97 input_file_.close();
98 }
99
100 // This routines computes the way regions are partitioned across processes
101 // The partitioning policies of course depends on whether the number of processes
102 // exceeds the number of regions or not.
103 // ASSUMPTION: A PROCESS CANNOT OWN CHUNKS OF MULTIPLE REGIONS. EITHER A PROCESS IS CONFINED INSIDE A SINGLE REGION
104 // OR IT MUST POSSESS ENTIRE REGIONS.
105 // The distribution of regions (or portions of them) across processes is conductes so to guarantee load balancing
106 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
107 void RegionHandler<Scalar, LocalOrdinal, GlobalOrdinal, Node>::ComputeProcRegions() {
108 int tot_num_proc = comm_->getSize();
109 int myPID = comm_->getRank();
110
111 regions_per_proc_.clear();
112 procs_per_region_.clear();
113
114 // If the number of processes instantiate is smaller than the total number of regions,
115 // then each process owns entire regions. The number of regions per process is calculates so to guarantee
116 // load balancing. After an initial distribution of regions, leftover regions that have not been assigned to any process yet are
117 // distributed in a round-robin fashion
118 if (tot_num_proc < num_total_regions_) {
119 int min_nregions_proc = std::floor(static_cast<double>(num_total_regions_) / static_cast<double>(tot_num_proc));
120 int num_leftover_regions = num_total_regions_ % tot_num_proc;
121
122 for (int i = 1; i <= min_nregions_proc; ++i)
123 regions_per_proc_.push_back(myPID * min_nregions_proc + i);
124
125 if (num_leftover_regions >= myPID + 1 && num_leftover_regions != 0)
126 regions_per_proc_.push_back(min_nregions_proc * tot_num_proc + (myPID + 1));
127
128 for (int procID = 0; procID < comm_->getSize(); ++procID) {
129 Array<GlobalOrdinal> proc;
130 proc.clear();
131 proc.push_back(procID);
132 for (int i = 1; i <= min_nregions_proc; ++i) {
133 GlobalOrdinal region_index = (procID)*min_nregions_proc + i;
134 std::tuple<GlobalOrdinal, Array<GlobalOrdinal> > tuple_aux = std::make_tuple(region_index, proc);
135 procs_per_region_.push_back(tuple_aux);
136 }
137
138 if (num_leftover_regions >= procID + 1 && num_leftover_regions != 0) {
139 GlobalOrdinal region_index = min_nregions_proc * tot_num_proc + (procID + 1);
140 std::tuple<GlobalOrdinal, Array<GlobalOrdinal> > tuple_aux = std::make_tuple(region_index, proc);
141 procs_per_region_.push_back(tuple_aux);
142 }
143 }
144 TEUCHOS_TEST_FOR_EXCEPTION(!(procs_per_region_.size() == num_total_regions_), Exceptions::RuntimeError, "PID: " << comm_->getRank() << " - Number of regions detected does not match with the initially declared one \n procs_per_region_ tracks " << procs_per_region_.size() << " regions whereas num_total_regions_ = " << num_total_regions_ << "\n");
145 }
146 // This is easy: if the number of regions coincides with the total number of processes instantiated, then
147 // a one-to-one relation between processes and regions is created
148 else if (tot_num_proc == num_total_regions_) {
149 regions_per_proc_.push_back(myPID + 1);
150
151 for (int i = 0; i < num_total_regions_; ++i) {
152 GlobalOrdinal region_index = i + 1;
153 Array<GlobalOrdinal> proc;
154 proc.clear();
155 proc.push_back(i);
156 std::tuple<GlobalOrdinal, Array<GlobalOrdinal> > tuple_aux = std::make_tuple(region_index, proc);
157 procs_per_region_.push_back(tuple_aux);
158 }
159 }
160 // If the number of processes exceeds the number of regions in the domain,
161 // then each process is given a subset of a region.
162 // N.B.: A SINGLE PROCESS IS NOT ALLOWED TO OWN CHUNCKS OF MULTIPLE REGIONS.
163 // IN THIS CONFIGURATION EACH PROCESS IS CONFINED TO A SINGLE REGION
164 else if (tot_num_proc > num_total_regions_) {
165 int num_procs_region = std::ceil(static_cast<double>(tot_num_proc) / static_cast<double>(num_total_regions_));
166 int num_regions_extra_proc = tot_num_proc % num_total_regions_;
167 int proc_count = 0;
168 std::tuple<int, Array<GlobalOrdinal> > region_tuple;
169
170 for (int i = 1; i <= num_total_regions_; ++i) {
171 Array<GlobalOrdinal> procs;
172 procs.clear();
173 if (i <= num_regions_extra_proc || num_regions_extra_proc == 0)
174 for (int j = 1; j <= num_procs_region; ++j) {
175 procs.push_back(proc_count);
176 proc_count++;
177 }
178 else
179 for (int j = 1; j <= num_procs_region - 1; ++j) {
180 procs.push_back(proc_count);
181 proc_count++;
182 }
183 std::sort(procs.begin(), procs.end());
184 region_tuple = std::make_tuple(i, procs);
185 procs_per_region_.push_back(region_tuple);
186 }
187 regions_per_proc_.clear();
188 }
189 }
190
191 // This routine associates a globally indexed node with the list of regions it belongs to
192 // This is helpful to spot which nodes lie on a interregion interface. In fact, these nodes must have
193 // the list of regions with more than one element
194 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
195 void RegionHandler<Scalar, LocalOrdinal, GlobalOrdinal, Node>::NodesToRegion() {
196 nodesToRegion_.clear();
197 interfaceNodes_.clear();
198 Array<std::tuple<GlobalOrdinal, GlobalOrdinal> > nodes_reordered;
199 nodes_reordered = nodes_;
200 std::sort(nodes_reordered.begin(), nodes_reordered.end(), compareNodes<GlobalOrdinal>);
201
202 typename Array<std::tuple<GlobalOrdinal, GlobalOrdinal> >::iterator node_iterator;
203 node_iterator = nodes_reordered.begin();
204
205 while (node_iterator != nodes_reordered.end()) {
206 GlobalOrdinal current_node = std::get<0>(*(node_iterator));
207 Array<GlobalOrdinal> regions;
208 regions.clear();
209 regions.push_back(std::get<1>(*node_iterator));
210
211 typename Array<std::tuple<GlobalOrdinal, GlobalOrdinal> >::iterator next_node_iterator = node_iterator + 1;
212
213 while (next_node_iterator != nodes_reordered.end()) {
214 GlobalOrdinal next_node = std::get<0>(*(next_node_iterator));
215 if (current_node == next_node) {
216 // As long as the information spanned regards the same node,
217 // the algorithm keeps on increasing the list of regions the given mesh node belong to
218 regions.push_back(std::get<1>(*(next_node_iterator)));
219 next_node_iterator++;
220 } else {
221 // When the mesh node label changes, then the algorithm
222 // stops recording information about the previous node and it starts recording information for the new one
223 node_iterator = next_node_iterator;
224 break;
225 }
226 }
227 std::tuple<GlobalOrdinal, Array<GlobalOrdinal> > new_tuple;
228 std::sort(regions.begin(), regions.end());
229 new_tuple = std::make_tuple(current_node, regions);
230 nodesToRegion_.push_back(new_tuple);
231
232 if (regions.size() > 1)
233 interfaceNodes_.push_back(new_tuple);
234
235 if (next_node_iterator == nodes_reordered.end())
236 break;
237 }
238 TEUCHOS_TEST_FOR_EXCEPTION(!(nodesToRegion_.size() == num_total_nodes_), Exceptions::RuntimeError, "Number of nodes detected does not match with the initially declared one \n"
239 << "nodesToRegion tracks " << nodesToRegion_.size() << " nodes whereas num_total_nodes_ =" << num_total_nodes_ << "\n");
240 }
241
242 // This routine creates row maps for composite and region matrices
243 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
244 void RegionHandler<Scalar, LocalOrdinal, GlobalOrdinal, Node>::CreateRowMaps() {
245 TEUCHOS_TEST_FOR_EXCEPTION((procs_per_region_.empty() && regions_per_proc_.empty()), Exceptions::RuntimeError, "Process ID: " << comm_->getRank() << " - Information about region partitioning across processors is not consistent: incorrect values for number of processors or number of regions \n");
246 Array<GlobalOrdinal> elements;
247 Array<GlobalOrdinal> region_elements;
248 Array<Array<GlobalOrdinal> > elements_per_region;
249 Array<Array<std::tuple<GlobalOrdinal, GlobalOrdinal> > > regionToAll;
250 int myPID = comm_->getRank();
251
252 elements.clear();
253 region_elements.clear();
254 elements_per_region.clear();
255 elements_per_region.resize(num_total_regions_);
256 regionToAll.clear();
257 regionToAll.resize(num_total_regions_);
258
259 TEUCHOS_TEST_FOR_EXCEPTION(!nodes_sorted_by_regions_, Exceptions::RuntimeError, "Nodes are not sorted by regions in ascending order \n");
260 TEUCHOS_TEST_FOR_EXCEPTION(num_total_nodes_ > nodes_.size(), Exceptions::RuntimeError, "Number of nodes declared in input file does not match with the effective number of nodes provided\n"
261 << "num_total_nodes_ =" << num_total_nodes_ << " whereas nodes_ tracks " << nodes_.size() << " nodes \n");
262 TEUCHOS_TEST_FOR_EXCEPTION(num_total_regions_ != std::get<1>(*(nodes_.end() - 1)), Exceptions::RuntimeError, "Number of regions declared in input file does not match with the effective number of regions provided\n");
263
264 if (!(regions_per_proc_.empty())) {
265 typename Array<GlobalOrdinal>::iterator iter_array;
266 for (iter_array = regions_per_proc_.begin(); iter_array != regions_per_proc_.end(); ++iter_array) {
267 region_elements.clear();
268 checkerNode<GlobalOrdinal> unaryPredicate(*iter_array);
269 typename Array<std::tuple<GlobalOrdinal, GlobalOrdinal> >::iterator nodes_iterator1;
270 typename Array<std::tuple<GlobalOrdinal, GlobalOrdinal> >::iterator nodes_iterator2;
271
272 // We position an iterator at the beginning of the information associated with the a region owned by the calling process
273 // and another iterator right at the end of the information
274 nodes_iterator1 = std::find_if<typename Array<std::tuple<GlobalOrdinal, GlobalOrdinal> >::iterator, checkerNode<GlobalOrdinal> >(nodes_.begin(), nodes_.end(), unaryPredicate);
275 nodes_iterator2 = std::find_if_not<typename Array<std::tuple<GlobalOrdinal, GlobalOrdinal> >::iterator, checkerNode<GlobalOrdinal> >(nodes_iterator1, nodes_.end(), unaryPredicate);
276
277 // Coun the number of mesh nodes inside a region
278 int num_region_nodes = nodes_iterator2 - nodes_iterator1;
279
280 typename Array<std::tuple<GlobalOrdinal, GlobalOrdinal> >::iterator nodes_iterator_aux;
281
282 // The policy assumes that in the input file the indexBase for the node label is 1
283 GlobalOrdinal region_node_label = 1;
284 for (nodes_iterator_aux = nodes_iterator1; nodes_iterator_aux != nodes_iterator2; ++nodes_iterator_aux) {
285 GlobalOrdinal node = std::get<0>(*nodes_iterator_aux);
286 checkerNodesToRegion<GlobalOrdinal> unaryPredicateNode(node);
287 typename Array<std::tuple<GlobalOrdinal, Array<GlobalOrdinal> > >::iterator nodes_to_region_iterator;
288 nodes_to_region_iterator = std::find_if<typename Array<std::tuple<GlobalOrdinal, Array<GlobalOrdinal> > >::iterator, checkerNodesToRegion<GlobalOrdinal> >(nodesToRegion_.begin(), nodesToRegion_.end(), unaryPredicateNode);
289 Array<GlobalOrdinal> nodal_regions = std::get<1>(*nodes_to_region_iterator);
290
291 // By default, I choose that a node is owned by the process associated with the region that shows up first in its list of beloning
292 // This guarantees that each row of the composite stiffness matrix is owned only by a single process, as Trilinos requires
293 if (*iter_array == nodal_regions[0])
294 elements.push_back(node);
295
296 // Nodes on the interface still belong to multiple regions, so
297 // it is important to keep track of this for the row maps of region matrices
298 region_elements.push_back(region_node_label);
299
300 // If a process owns a region (or even a portion of it), we provide to it a map
301 // from region indices to composite indices for all the nodes inside that region,
302 // even if a specific node is not owned by the calling process
303 regionToAll[*iter_array - 1].push_back(std::make_tuple(region_node_label, std::get<0>(*nodes_iterator_aux)));
304 region_node_label++;
305 }
306
307 // C++ indexing starts from 0, so everything is shofted backward by one to make it consistent with programming language's policies
308 for (typename Array<GlobalOrdinal>::iterator iter = region_elements.begin(); iter != region_elements.end(); ++iter)
309 *iter = *iter - 1;
310
311 elements_per_region[*iter_array - 1] = region_elements;
312 TEUCHOS_TEST_FOR_EXCEPTION((num_region_nodes != regionToAll[*iter_array - 1].size()), Exceptions::RuntimeError, "Process ID: " << comm_->getRank() << " - Number of region nodes does not match with number of nodes stored in regionToAll \n"
313 << "num_region_nodes= " << num_region_nodes << " whereas regionToAll[" << *iter_array - 1 << "].size()= " << regionToAll[*iter_array - 1].size() << "\n");
314 }
315 TEUCHOS_TEST_FOR_EXCEPTION((num_total_regions_ != regionToAll.size()), Exceptions::RuntimeError, "regionToAll size has been corrupted\n"
316 << "num_total_regions_ = " << num_total_regions_ << " whereas regionToAll.size() = " << regionToAll.size() << "\n");
317 } else {
318 // The code enters the scope of these curly brackets if the number of processes exceeds the number of regions in the domain.
319 // Therefore, each process owns only a subregion (or at most a single entire region).
320 // In this situation, the calling process must identify the region it is associated with
321 bool region_found = false;
322 GlobalOrdinal myRegion = -1;
323 TEUCHOS_TEST_FOR_EXCEPTION(!(procs_per_region_.size() == num_total_regions_), Exceptions::RuntimeError, "Process ID: " << comm_->getRank() << " - Number of total regions does not match with regionHandler structures \n");
324
325 Array<GlobalOrdinal> region_procs;
326 while (!region_found) {
327 typename Array<GlobalOrdinal>::iterator iter_proc;
328 for (GlobalOrdinal region_index = 1; region_index <= procs_per_region_.size(); ++region_index) {
329 region_procs = std::get<1>(procs_per_region_[region_index - 1]);
330 iter_proc = std::find(region_procs.begin(), region_procs.end(), myPID);
331 if (iter_proc != region_procs.end()) {
332 myRegion = region_index;
333 region_found = true;
334 }
335 }
336 }
337
338 TEUCHOS_TEST_FOR_EXCEPTION((myRegion == -1 || !region_found), Exceptions::RuntimeError, ("Region containing PROC ID: " + std::to_string(myPID) + " NOT FOUND \n"));
339 region_procs = std::get<1>(procs_per_region_[myRegion - 1]);
340
341 checkerNode<GlobalOrdinal> unaryPredicate(myRegion);
342 typename Array<std::tuple<GlobalOrdinal, GlobalOrdinal> >::iterator nodes_iterator1;
343 typename Array<std::tuple<GlobalOrdinal, GlobalOrdinal> >::iterator nodes_iterator2;
344 nodes_iterator1 = std::find_if<typename Array<std::tuple<GlobalOrdinal, GlobalOrdinal> >::iterator, checkerNode<GlobalOrdinal> >(nodes_.begin(), nodes_.end(), unaryPredicate);
345 nodes_iterator2 = std::find_if_not<typename Array<std::tuple<GlobalOrdinal, GlobalOrdinal> >::iterator, checkerNode<GlobalOrdinal> >(nodes_iterator1, nodes_.end(), unaryPredicate);
346
347 int num_region_nodes = nodes_iterator2 - nodes_iterator1;
348 int num_region_procs = region_procs.size();
349
350 if (num_region_nodes < num_region_procs) {
351 Array<GlobalOrdinal> region_procs_reduced;
352 region_procs_reduced.clear();
353 for (int i = 0; i < num_region_nodes; ++i)
354 region_procs_reduced.push_back(region_procs[i]);
355
356 typename Array<GlobalOrdinal>::iterator proc_iterator;
357 proc_iterator = std::find<typename Array<GlobalOrdinal>::iterator, GlobalOrdinal>(region_procs_reduced.begin(), region_procs_reduced.end(), myPID);
358
359 if (proc_iterator != region_procs_reduced.end()) // This reasoning works because the PROC ID for each region has been previously sorted in ascending order
360 {
361 GlobalOrdinal node = std::get<0>(*(nodes_iterator1 + (proc_iterator - region_procs_reduced.begin() + 1)));
362 GlobalOrdinal region_node_label = proc_iterator - region_procs_reduced.begin() + 1;
363 checkerNodesToRegion<GlobalOrdinal> unaryPredicateNode(node);
364 typename Array<std::tuple<GlobalOrdinal, Array<GlobalOrdinal> > >::iterator nodes_to_region_iterator;
365 nodes_to_region_iterator = std::find_if<typename Array<std::tuple<GlobalOrdinal, Array<GlobalOrdinal> > >::iterator, checkerNodesToRegion<GlobalOrdinal> >(nodesToRegion_.begin(), nodesToRegion_.end(), unaryPredicateNode);
366 Array<GlobalOrdinal> nodal_regions = std::get<1>(*nodes_to_region_iterator);
367
368 // The follolwing if statement is necessary to guarantee uniqueness of the RowMap used for the composite matrix
369 if (myRegion == nodal_regions[0])
370 elements.push_back(node);
371
372 // Although a process does not own a row in the composite matrix, it may still happen that it owns the row
373 // from a region matrix perspective
374 region_elements.push_back(region_node_label);
375 }
376
377 // If a process owns a region (or even a portion of it), we provide to it a map
378 // from region indices to composite indices for all the nodes inside that region,
379 // even if a specific node is not owned by the calling process
380 // If a process owns something of a region, then the process has a global view of who owns what for that region
381 // Although this may seem more information than what actually needed, it is important for the computation of the collapsing.
382 // If the collapsing is not calculated, then this structure actually overestimates what a process needs to know.
383 for (proc_iterator = region_procs_reduced.begin(); proc_iterator != region_procs_reduced.end(); ++proc_iterator) {
384 GlobalOrdinal node = std::get<0>(*(nodes_iterator1 + (proc_iterator - region_procs_reduced.begin() + 1)));
385 GlobalOrdinal region_node_label = proc_iterator - region_procs_reduced.begin() + 1;
386 checkerNodesToRegion<GlobalOrdinal> unaryPredicateNode(node);
387 typename Array<std::tuple<GlobalOrdinal, Array<GlobalOrdinal> > >::iterator nodes_to_region_iterator;
388 nodes_to_region_iterator = std::find_if<typename Array<std::tuple<GlobalOrdinal, Array<GlobalOrdinal> > >::iterator, checkerNodesToRegion<GlobalOrdinal> >(nodesToRegion_.begin(), nodesToRegion_.end(), unaryPredicateNode);
389 regionToAll[myRegion - 1].push_back(std::make_tuple(region_node_label, node));
390 }
391
392 } else if (num_region_nodes == num_region_procs) {
393 typename Array<GlobalOrdinal>::iterator proc_iterator;
394 proc_iterator = std::find<typename Array<GlobalOrdinal>::iterator, GlobalOrdinal>(region_procs.begin(), region_procs.end(), myPID);
395
396 if (proc_iterator != region_procs.end()) // This reasoning works because the PROC ID for each region has been previously sorted in ascending order
397 {
398 GlobalOrdinal node = std::get<0>(*(nodes_iterator1 + (proc_iterator - region_procs.begin() + 1)));
399 GlobalOrdinal region_node_label = proc_iterator - region_procs.begin() + 1;
400 checkerNodesToRegion<GlobalOrdinal> unaryPredicateNode(node);
401 typename Array<std::tuple<GlobalOrdinal, Array<GlobalOrdinal> > >::iterator nodes_to_region_iterator;
402 nodes_to_region_iterator = std::find_if<typename Array<std::tuple<GlobalOrdinal, Array<GlobalOrdinal> > >::iterator, checkerNodesToRegion<GlobalOrdinal> >(nodesToRegion_.begin(), nodesToRegion_.end(), unaryPredicateNode);
403 Array<GlobalOrdinal> nodal_regions = std::get<1>(*nodes_to_region_iterator);
404 if (myRegion == nodal_regions[0])
405 elements.push_back(node);
406
407 region_elements.push_back(region_node_label);
408 }
409
410 // If a process owns a region (or even a portion of it), we provide to it a map
411 // from region indices to composite indices for all the nodes inside that region,
412 // even if a specific node is not owned by the calling process
413 for (proc_iterator = region_procs.begin(); proc_iterator != region_procs.end(); ++proc_iterator) {
414 GlobalOrdinal node = std::get<0>(*(nodes_iterator1 + (proc_iterator - region_procs.begin() + 1)));
415 GlobalOrdinal region_node_label = proc_iterator - region_procs.begin() + 1;
416 checkerNodesToRegion<GlobalOrdinal> unaryPredicateNode(node);
417 typename Array<std::tuple<GlobalOrdinal, Array<GlobalOrdinal> > >::iterator nodes_to_region_iterator;
418 nodes_to_region_iterator = std::find_if<typename Array<std::tuple<GlobalOrdinal, Array<GlobalOrdinal> > >::iterator, checkerNodesToRegion<GlobalOrdinal> >(nodesToRegion_.begin(), nodesToRegion_.end(), unaryPredicateNode);
419 regionToAll[myRegion - 1].push_back(std::make_tuple(region_node_label, node));
420 }
421 } else {
422 typename Array<GlobalOrdinal>::iterator proc_iterator;
423 proc_iterator = std::find<typename Array<GlobalOrdinal>::iterator, GlobalOrdinal>(region_procs.begin(), region_procs.end(), myPID);
424
425 int num_nodes_proc = std::ceil(static_cast<double>(num_region_nodes) / static_cast<double>(num_region_procs));
426 int num_procs_extra_node = num_region_nodes % num_region_procs;
427
428 if (proc_iterator - region_procs.begin() + 1 <= num_procs_extra_node || num_procs_extra_node == 0) {
429 int init_node = num_nodes_proc * (proc_iterator - region_procs.begin());
430 for (int i = 0; i < num_nodes_proc; ++i) {
431 GlobalOrdinal node = std::get<0>(*(nodes_iterator1 + init_node + i));
432 GlobalOrdinal region_node_label = init_node + i + 1;
433 checkerNodesToRegion<GlobalOrdinal> unaryPredicateNode(node);
434 typename Array<std::tuple<GlobalOrdinal, Array<GlobalOrdinal> > >::iterator nodes_to_region_iterator;
435 nodes_to_region_iterator = std::find_if<typename Array<std::tuple<GlobalOrdinal, Array<GlobalOrdinal> > >::iterator, checkerNodesToRegion<GlobalOrdinal> >(nodesToRegion_.begin(), nodesToRegion_.end(), unaryPredicateNode);
436 Array<GlobalOrdinal> nodal_regions = std::get<1>(*nodes_to_region_iterator);
437 if (myRegion == nodal_regions[0])
438 elements.push_back(node);
439
440 region_elements.push_back(region_node_label);
441 }
442 } else {
443 int init_node = num_nodes_proc * num_procs_extra_node + (proc_iterator - region_procs.begin() - num_procs_extra_node) * (num_nodes_proc - 1);
444 for (int i = 0; i < num_nodes_proc - 1; ++i) {
445 GlobalOrdinal node = std::get<0>(*(nodes_iterator1 + init_node + i));
446 GlobalOrdinal region_node_label = init_node + i + 1;
447 checkerNodesToRegion<GlobalOrdinal> unaryPredicateNode(node);
448 typename Array<std::tuple<GlobalOrdinal, Array<GlobalOrdinal> > >::iterator nodes_to_region_iterator;
449 nodes_to_region_iterator = std::find_if<typename Array<std::tuple<GlobalOrdinal, Array<GlobalOrdinal> > >::iterator, checkerNodesToRegion<GlobalOrdinal> >(nodesToRegion_.begin(), nodesToRegion_.end(), unaryPredicateNode);
450 Array<GlobalOrdinal> nodal_regions = std::get<1>(*nodes_to_region_iterator);
451 if (myRegion == nodal_regions[0])
452 elements.push_back(node);
453
454 region_elements.push_back(region_node_label);
455 }
456 }
457
458 // If a process owns a region (or even a portion of it), we provide to it a map
459 // from region indices to composite indices for all the nodes inside that region,
460 // even if a specific node is not owned by the calling process
461 for (proc_iterator = region_procs.begin(); proc_iterator != region_procs.end(); ++proc_iterator) {
462 if (proc_iterator - region_procs.begin() + 1 <= num_procs_extra_node || num_procs_extra_node == 0) {
463 int init_node = num_nodes_proc * (proc_iterator - region_procs.begin());
464 for (int i = 0; i < num_nodes_proc; ++i) {
465 GlobalOrdinal node = std::get<0>(*(nodes_iterator1 + init_node + i));
466 GlobalOrdinal region_node_label = init_node + i + 1;
467 checkerNodesToRegion<GlobalOrdinal> unaryPredicateNode(node);
468 typename Array<std::tuple<GlobalOrdinal, Array<GlobalOrdinal> > >::iterator nodes_to_region_iterator;
469 nodes_to_region_iterator = std::find_if<typename Array<std::tuple<GlobalOrdinal, Array<GlobalOrdinal> > >::iterator, checkerNodesToRegion<GlobalOrdinal> >(nodesToRegion_.begin(), nodesToRegion_.end(), unaryPredicateNode);
470 regionToAll[myRegion - 1].push_back(std::make_tuple(region_node_label, node));
471 }
472 } else {
473 int init_node = num_nodes_proc * num_procs_extra_node + (proc_iterator - region_procs.begin() - num_procs_extra_node) * (num_nodes_proc - 1);
474 for (int i = 0; i < num_nodes_proc - 1; ++i) {
475 GlobalOrdinal node = std::get<0>(*(nodes_iterator1 + init_node + i));
476 GlobalOrdinal region_node_label = init_node + i + 1;
477 checkerNodesToRegion<GlobalOrdinal> unaryPredicateNode(node);
478 typename Array<std::tuple<GlobalOrdinal, Array<GlobalOrdinal> > >::iterator nodes_to_region_iterator;
479 nodes_to_region_iterator = std::find_if<typename Array<std::tuple<GlobalOrdinal, Array<GlobalOrdinal> > >::iterator, checkerNodesToRegion<GlobalOrdinal> >(nodesToRegion_.begin(), nodesToRegion_.end(), unaryPredicateNode);
480 regionToAll[myRegion - 1].push_back(std::make_tuple(region_node_label, node));
481 }
482 }
483 }
484 }
485 TEUCHOS_TEST_FOR_EXCEPTION((num_total_regions_ != regionToAll.size()), Exceptions::RuntimeError, "Process ID: " << comm_->getRank() << " - regionToAll size has been corrupted\n"
486 << "num_total_regions_ = " << num_total_regions_ << " whereas regionToAll.size()= " << regionToAll.size() << "\n");
487
488 // C++ indexing starts from 0, so everything is shofted backward by one to make it consistent with programming language's policies
489 for (typename Array<GlobalOrdinal>::iterator iter = region_elements.begin(); iter != region_elements.end(); ++iter)
490 *iter = *iter - 1;
491
492 elements_per_region[myRegion - 1] = region_elements;
493 }
494
495 // C++ indexing starts from 0, so everything is shofted backward by one to make it consistent with programming language's policies
496 for (typename Array<GlobalOrdinal>::iterator iter = elements.begin(); iter != elements.end(); ++iter)
497 *iter = *iter - 1;
498
499 maps_.composite_map_ = elements;
500 maps_.region_maps_ = elements_per_region;
501 maps_.regionToAll_ = regionToAll;
502
503 for (int i = 0; i < comm_->getSize(); ++i) {
504 comm_->barrier();
505
506 if (comm_->getRank() == i) {
507 std::cout << "Proc " << i << std::endl;
508 for (int i = 0; i < regionToAll.size(); ++i) {
509 Teuchos::Array<std::tuple<GlobalOrdinal, GlobalOrdinal> > currArray = regionToAll[i];
510 for (int j = 0; j < currArray.size(); ++j) {
511 std::cout << std::get<0>(currArray[j]) << "\t" << std::get<1>(currArray[j]) << std::endl;
512 }
513 }
514 }
515
516 comm_->barrier();
517 }
518 }
519
520 // Get methods to allow a user to interface with private members of the RegionHandler class
521 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
522 Array<GlobalOrdinal> RegionHandler<Scalar, LocalOrdinal, GlobalOrdinal, Node>::GetRegionRowMap(GlobalOrdinal region_index) const {
523 TEUCHOS_TEST_FOR_EXCEPTION(region_index >= num_total_regions_, Exceptions::RuntimeError, "Value of region index exceeds total number of regions stored \n"
524 << "Trying to access informaiton about region " << region_index << " when the total number of regions stored is " << num_total_regions_ << "\n");
525 return maps_.region_maps_[region_index];
526 }
527
528 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
529 Array<Array<std::tuple<GlobalOrdinal, GlobalOrdinal> > > RegionHandler<Scalar, LocalOrdinal, GlobalOrdinal, Node>::GetRegionToAll() const {
530 return maps_.regionToAll_;
531 }
532
533 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
534 Array<std::tuple<GlobalOrdinal, GlobalOrdinal> > RegionHandler<Scalar, LocalOrdinal, GlobalOrdinal, Node>::GetRegionToAll(GlobalOrdinal region_index) const {
535 TEUCHOS_TEST_FOR_EXCEPTION(region_index >= num_total_regions_, Exceptions::RuntimeError, "Value of region index exceeds total number of regions stored \n"
536 << "Trying to access informaiton about region " << region_index << " when the total number of regions stored is " << num_total_regions_ << "\n");
537 return maps_.regionToAll_[region_index];
538 }
539
540 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
541 void RegionHandler<Scalar, LocalOrdinal, GlobalOrdinal, Node>::printView() const {
542 if (0 == comm_->getRank()) {
543 std::cout << "Total number of mesh nodes: " << num_total_nodes_ << std::endl;
544 std::cout << "Total number of mesh regions: " << num_total_regions_ << std::endl;
545 std::cout << "Number of rows in nodes_ structure: " << nodes_.size() << std::endl;
546 for (int i = 0; i < nodes_.size(); ++i) {
547 std::cout << std::get<0>(nodes_[i]) << "\t" << std::get<1>(nodes_[i]) << std::endl;
548 }
549 }
550 }
551
552 // Print methods
553 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
554 void RegionHandler<Scalar, LocalOrdinal, GlobalOrdinal, Node>::printNodesToRegion() const {
555 if (0 == comm_->getRank()) {
556 std::cout << "Total number of mesh nodes: " << num_total_nodes_ << std::endl;
557 std::cout << "Total number of mesh regions: " << num_total_regions_ << std::endl;
558 std::cout << "Number of rows in nodes_ structure: " << nodes_.size() << std::endl;
559 for (int i = 0; i < nodesToRegion_.size(); ++i) {
560 std::cout << "Node " << std::get<0>(nodesToRegion_[i]) << "\t belongs to regions: " << std::get<1>(nodesToRegion_[i]) << std::endl;
561 }
562 }
563 }
564
565 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
566 void RegionHandler<Scalar, LocalOrdinal, GlobalOrdinal, Node>::printInactive() const {
567 if (maps_.composite_map_.empty())
568 std::cout << "INACTIVE PROC ID: " << comm_->getRank() << std::endl;
569 }
570
571 } // namespace Xpetra
572
573 #endif