Tpetra parallel linear algebra Version of the Day
Loading...
Searching...
No Matches
Tpetra_CrsSingletonFilter_LinearProblem_def.hpp
Go to the documentation of this file.
1// @HEADER
2// *****************************************************************************
3// Tpetra: Templated Linear Algebra Services Package
4//
5// Copyright 2008 NTESS and the Tpetra contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9
10#ifndef TPETRA_CRSSINGLETONFILTER_LINEARPROBLEM_DEF_HPP
11#define TPETRA_CRSSINGLETONFILTER_LINEARPROBLEM_DEF_HPP
12
15
17
18namespace Tpetra {
19
20// Functor to create post-solve arrays
21template <class LocalLO_type, class ColIDView_type, class ColMapColor_type, class RowMapColor_type, class Counter_type>
22struct CreatePostSolveArraysFunctor {
23 LocalLO_type LocalRowIDofSingletonColData;
24 ColIDView_type ColSingletonRowLIDs;
25 ColIDView_type ColSingletonColLIDs;
26
27 LocalLO_type NewColProfilesData;
28 LocalLO_type ColProfilesData;
29 LocalLO_type ColHasRowWithSingletonData;
30 ColMapColor_type ColMapColors_Data;
31 RowMapColor_type RowMapColors_Data;
32 Counter_type numSingletonCols;
33
34 CreatePostSolveArraysFunctor(LocalLO_type localRowIDofSingletonColData,
35 ColIDView_type colSingletonRowLIDs, ColIDView_type colSingletonColLIDs,
36 LocalLO_type newColProfilesData, LocalLO_type colProfilesData, LocalLO_type colHasRowWithSingletonData,
37 ColMapColor_type colMapColors_Data, RowMapColor_type rowMapColors_Data, Counter_type in_numSingletonCols)
38 : LocalRowIDofSingletonColData(localRowIDofSingletonColData)
39 , ColSingletonRowLIDs(colSingletonRowLIDs)
40 , ColSingletonColLIDs(colSingletonColLIDs)
41 , NewColProfilesData(newColProfilesData)
42 , ColProfilesData(colProfilesData)
43 , ColHasRowWithSingletonData(colHasRowWithSingletonData)
44 , ColMapColors_Data(colMapColors_Data)
45 , RowMapColors_Data(rowMapColors_Data)
46 , numSingletonCols(in_numSingletonCols) {}
47
48 KOKKOS_INLINE_FUNCTION
49 void operator()(const size_t j) const {
50 int i = LocalRowIDofSingletonColData(j, 0);
51 if (ColProfilesData(j, 0) == 1 && RowMapColors_Data(i, 0) != 1) {
52 // These will be sorted by rowLIDs, so no need to be in any particular order
53 auto lclNumSingletonCols = Kokkos::atomic_fetch_add(&numSingletonCols(0), 1);
54 ColSingletonRowLIDs(lclNumSingletonCols) = i;
55 ColSingletonColLIDs(lclNumSingletonCols) = j;
56 }
57 // Also check for columns that were eliminated implicitly by
58 // having all associated row eliminated
59 else if (NewColProfilesData(j, 0) == 0 && ColHasRowWithSingletonData(j, 0) != 1 && RowMapColors_Data(i, 0) == 0) {
60 ColMapColors_Data(j, 0) = 1;
61 }
62 }
63};
64
65// Functors to construct or update reduced problem
66template <class LocalOrdinal, class GlobalOrdinal, class LocalMap_type, class LocalMatrix_type, class row_map_type, class entries_type, class values_type>
67struct ConstructReducedProblemFunctor {
68 LocalMap_type lclFullRowMap;
69 LocalMap_type lclFullColMap;
70 LocalMap_type lclReducedRowMap;
71 LocalMap_type lclReducedColMap;
72
73 LocalMatrix_type lclFullMatrix;
74
75 row_map_type lclReducedRowPtr;
76 entries_type lclReducedColInd;
77 values_type lclReducedValues;
78
79 // Constructor
80 ConstructReducedProblemFunctor(LocalMap_type lclFullRowMap_, LocalMap_type lclFullColMap_, LocalMap_type lclReducedRowMap_, LocalMap_type lclReducedColMap_,
81 LocalMatrix_type lclFullMatrix_, row_map_type lclReducedRowPtr_, entries_type lclReducedColInd_, values_type lclReducedValues_)
82 : lclFullRowMap(lclFullRowMap_)
83 , lclFullColMap(lclFullColMap_)
84 , lclReducedRowMap(lclReducedRowMap_)
85 , lclReducedColMap(lclReducedColMap_)
86 , lclFullMatrix(lclFullMatrix_)
87 , lclReducedRowPtr(lclReducedRowPtr_)
88 , lclReducedColInd(lclReducedColInd_)
89 , lclReducedValues(lclReducedValues_) {}
90
91 // Functor to count nonzero entries
92 KOKKOS_INLINE_FUNCTION
93 void operator()(const LocalOrdinal i, size_t& nnz) const {
94 const LocalOrdinal INVALID = Tpetra::Details::OrdinalTraits<LocalOrdinal>::invalid();
95
96 auto lclFullRowPtr = lclFullMatrix.graph.row_map;
97 auto lclFullColInd = lclFullMatrix.graph.entries;
98 auto lclFullValues = lclFullMatrix.values;
99
100 GlobalOrdinal glbRowID = lclFullRowMap.getGlobalElement(i);
101 LocalOrdinal lclRowID = lclReducedRowMap.getLocalElement(glbRowID);
102
103 if (lclRowID != INVALID) {
104 for (size_t j = lclFullRowPtr(i); j < lclFullRowPtr(i + 1); j++) {
105 GlobalOrdinal glbColID = lclFullColMap.getGlobalElement(lclFullColInd[j]);
106 LocalOrdinal lclColID = lclReducedColMap.getLocalElement(glbColID);
107 if (lclColID != INVALID) {
108 lclReducedRowPtr(1 + lclRowID)++;
109 }
110 }
111 }
112 nnz += lclReducedRowPtr(1 + lclRowID);
113 ;
114 }
115
116 // Functor to insert nonzero entries
117 KOKKOS_INLINE_FUNCTION
118 void operator()(const LocalOrdinal i) const {
119 const LocalOrdinal INVALID = Tpetra::Details::OrdinalTraits<LocalOrdinal>::invalid();
120
121 auto lclFullRowPtr = lclFullMatrix.graph.row_map;
122 auto lclFullColInd = lclFullMatrix.graph.entries;
123 auto lclFullValues = lclFullMatrix.values;
124
125 GlobalOrdinal glbRowID = lclFullRowMap.getGlobalElement(i);
126 LocalOrdinal lclRowID = lclReducedRowMap.getLocalElement(glbRowID);
127
128 if (lclRowID != INVALID) {
129 size_t k = lclReducedRowPtr(lclRowID);
130 for (size_t j = lclFullRowPtr(i); j < lclFullRowPtr(i + 1); j++) {
131 GlobalOrdinal glbColID = lclFullColMap.getGlobalElement(lclFullColInd[j]);
132 LocalOrdinal lclColID = lclReducedColMap.getLocalElement(glbColID);
133 if (lclColID != INVALID) {
134 lclReducedColInd[k] = lclColID;
135 lclReducedValues[k] = lclFullValues[j];
136 k++;
137 }
138 }
139 }
140 }
141};
142
143template <class LocalOrdinal, class GlobalOrdinal, class LocalMap_type, class LocalMatrix_type>
144struct UpdateReducedProblemFunctor {
145 LocalMap_type lclFullRowMap;
146 LocalMap_type lclFullColMap;
147 LocalMap_type lclReducedRowMap;
148 LocalMap_type lclReducedColMap;
149
150 LocalMatrix_type lclFullMatrix;
151 LocalMatrix_type lclReducedMatrix;
152
153 // Constructor
154 UpdateReducedProblemFunctor(LocalMap_type lclFullRowMap_, LocalMap_type lclFullColMap_,
155 LocalMap_type lclReducedRowMap_, LocalMap_type lclReducedColMap_,
156 LocalMatrix_type lclFullMatrix_, LocalMatrix_type lclReducedMatrix_)
157 : lclFullRowMap(lclFullRowMap_)
158 , lclFullColMap(lclFullColMap_)
159 , lclReducedRowMap(lclReducedRowMap_)
160 , lclReducedColMap(lclReducedColMap_)
161 , lclFullMatrix(lclFullMatrix_)
162 , lclReducedMatrix(lclReducedMatrix_) {}
163
164 // Functor to update nonzero entries
165 KOKKOS_INLINE_FUNCTION
166 void operator()(const LocalOrdinal i) const {
167 const LocalOrdinal INVALID = Tpetra::Details::OrdinalTraits<LocalOrdinal>::invalid();
168
169 auto lclFullRowPtr = lclFullMatrix.graph.row_map;
170 auto lclFullColInd = lclFullMatrix.graph.entries;
171 auto lclFullValues = lclFullMatrix.values;
172
173 auto lclReducedRowPtr = lclReducedMatrix.graph.row_map;
174 auto lclReducedColInd = lclReducedMatrix.graph.entries;
175 auto lclReducedValues = lclReducedMatrix.values;
176
177 GlobalOrdinal glbRowID = lclFullRowMap.getGlobalElement(i);
178 LocalOrdinal lclRowID = lclReducedRowMap.getLocalElement(glbRowID);
179
180 if (lclRowID != INVALID) {
181 size_t k = lclReducedRowPtr(lclRowID);
182 for (size_t j = lclFullRowPtr(i); j < lclFullRowPtr(i + 1); j++) {
183 GlobalOrdinal glbColID = lclFullColMap.getGlobalElement(lclFullColInd(j));
184 LocalOrdinal lclColID = lclReducedColMap.getLocalElement(glbColID);
185 if (lclColID != INVALID) {
186 // because ConstructReducedProblemFunctor and UpdateReducedProblemFunctor scan non-zeros in the same order (based on lclFullRowPtr)
187 // lclColID should match lclReducedColInd[k].
188 lclReducedColInd[k] = lclColID;
189 lclReducedValues[k] = lclFullValues[j];
190 k++;
191 }
192 }
193 }
194 }
195};
196
197// Functor to solve singleton problem
198template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node,
199 class LocalMap_type, class LocalMatrix_type, class LocalX_type, class LocalB_type,
200 class View_type_int, class View_type_scalar, class Err_type>
201struct SolveSingletonProblemFunctor {
202 using local_ordinal_type = LocalOrdinal;
204
205 local_ordinal_type NumVectors;
206 local_ordinal_type localNumSingletonCols;
207
208 Err_type error_code;
209 LocalMap_type lclFullRowMap;
210 LocalMap_type lclFullColMap;
211 LocalMap_type lclReducedRowMap;
212
213 LocalMatrix_type lclFullMatrix;
214
215 LocalX_type localExportX;
216 LocalB_type localRHS;
217
218 View_type_int ColSingletonColLIDs;
219 View_type_int ColSingletonRowLIDs;
220 View_type_int ColSingletonPivotLIDs;
221 View_type_scalar ColSingletonPivots;
222
223 // Constructor
224 SolveSingletonProblemFunctor(local_ordinal_type NumVectors_, local_ordinal_type localNumSingletonCols_, Err_type error_code_,
225 LocalMap_type lclFullRowMap_, LocalMap_type lclFullColMap_, LocalMap_type lclReducedRowMap_,
226 LocalMatrix_type lclFullMatrix_, LocalX_type localExportX_, LocalB_type localRHS_,
227 View_type_int colSingletonColLIDs_, View_type_int colSingletonRowLIDs_,
228 View_type_int colSingletonPivotLIDs_, View_type_scalar colSingletonPivots_)
229 : NumVectors(NumVectors_)
230 , localNumSingletonCols(localNumSingletonCols_)
231 , error_code(error_code_)
232 , lclFullRowMap(lclFullRowMap_)
233 , lclFullColMap(lclFullColMap_)
234 , lclReducedRowMap(lclReducedRowMap_)
235 , lclFullMatrix(lclFullMatrix_)
236 , localExportX(localExportX_)
237 , localRHS(localRHS_)
238 , ColSingletonColLIDs(colSingletonColLIDs_)
239 , ColSingletonRowLIDs(colSingletonRowLIDs_)
240 , ColSingletonPivotLIDs(colSingletonPivotLIDs_)
241 , ColSingletonPivots(colSingletonPivots_) {}
242
243 // Functor used in ConstructReducedProblem with parallel-for
244 KOKKOS_INLINE_FUNCTION
245 void operator()(const local_ordinal_type i) const {
246 const LocalOrdinal INVALID = Tpetra::Details::OrdinalTraits<LocalOrdinal>::invalid();
247 const impl_scalar_type zero(0);
248
249 auto lclFullRowPtr = lclFullMatrix.graph.row_map;
250 auto lclFullColInd = lclFullMatrix.graph.entries;
251 auto lclFullValues = lclFullMatrix.values;
252
253 GlobalOrdinal glbID = lclFullRowMap.getGlobalElement(i);
254 LocalOrdinal lclID = lclReducedRowMap.getLocalElement(glbID);
255
256 if (lclID == INVALID) {
257 if (lclFullRowPtr(i + 1) == lclFullRowPtr(i) + 1) {
258 // Singleton row
259 LocalOrdinal indX = lclFullColInd(lclFullRowPtr(i));
260 impl_scalar_type pivot = lclFullValues(lclFullRowPtr(i));
261 if (pivot == zero) {
262 Kokkos::atomic_exchange(&error_code(0), 1);
263 } else {
264 for (LocalOrdinal j = 0; j < NumVectors; j++) {
265 localExportX(indX, j) = localRHS(i, j) / pivot;
266 }
267 }
268 } else {
269 // Not singleton row, but need to be removed == Singleton column
270 // look for matching row ID
271 int myNum = -1;
272 for (local_ordinal_type j = 0; j < localNumSingletonCols; j++) {
273 if (ColSingletonRowLIDs[j] == i) {
274 myNum = j;
275 }
276 }
277 if (myNum == -1) {
278 Kokkos::atomic_exchange(&error_code(0), 3);
279 } else {
280 // look for matching col ID
281 LocalOrdinal targetCol = ColSingletonColLIDs[myNum];
282 for (size_t j = lclFullRowPtr(i); j < lclFullRowPtr(i + 1); j++) {
283 if (lclFullColInd[j] == targetCol) {
284 impl_scalar_type pivot = lclFullValues[j];
285 if (pivot == zero) {
286 Kokkos::atomic_exchange(&error_code(0), 2);
287 } else {
288 ColSingletonPivotLIDs[myNum] = j - lclFullRowPtr(i);
289 ColSingletonPivots[myNum] = pivot;
290 }
291 break;
292 }
293 }
294 }
295 }
296 }
297 }
298
299 // Functor used in UpdateReducedProblem with parallel-reduce
300 KOKKOS_INLINE_FUNCTION
301 void operator()(const local_ordinal_type i, local_ordinal_type& lclNumSingletonCols) const {
302 const LocalOrdinal INVALID = Tpetra::Details::OrdinalTraits<LocalOrdinal>::invalid();
303 const impl_scalar_type zero(0);
304
305 auto lclFullRowPtr = lclFullMatrix.graph.row_map;
306 auto lclFullColInd = lclFullMatrix.graph.entries;
307 auto lclFullValues = lclFullMatrix.values;
308
309 GlobalOrdinal glbID = lclFullRowMap.getGlobalElement(i);
310 LocalOrdinal lclID = lclReducedRowMap.getLocalElement(glbID);
311
312 if (lclID == INVALID) {
313 // not part of reduced-matrix, so it is singletone
314 if (lclFullRowPtr(i + 1) == lclFullRowPtr(i) + 1) {
315 // Singleton row
316 LocalOrdinal indX = lclFullColInd(lclFullRowPtr(i));
317 impl_scalar_type pivot = lclFullValues(lclFullRowPtr(i));
318 if (pivot == zero) {
319 Kokkos::atomic_exchange(&error_code(0), 1);
320 } else {
321 for (LocalOrdinal j = 0; j < NumVectors; j++) {
322 localExportX(indX, j) = localRHS(i, j) / pivot;
323 }
324 }
325 } else {
326 // Not singleton row, but need to be removed == Singleton column
327 // look for matching row ID
328 int myNum = -1;
329 for (local_ordinal_type j = 0; j < localNumSingletonCols; j++) {
330 if (ColSingletonRowLIDs[j] == i) {
331 myNum = j;
332 }
333 }
334 if (myNum == -1) {
335 Kokkos::atomic_exchange(&error_code(0), 3);
336 } else {
337 size_t j = ColSingletonPivotLIDs[myNum] + lclFullRowPtr(i);
338 impl_scalar_type pivot = lclFullValues[j];
339 if (pivot == zero) {
340 Kokkos::atomic_exchange(&error_code(0), 2);
341 } else {
342 ColSingletonPivots[myNum] = pivot;
343 }
344 lclNumSingletonCols++;
345 }
346 }
347 }
348 }
349};
350
351// Functor to compute full singleton problem
352template <class View_type_int, class View_type_scalar, class SOL_type, class RHS_type>
353struct ComputeFullSolutionFunctor {
354 using scalar_type = typename View_type_scalar::non_const_value_type;
355
356 View_type_int ColSingletonRowLIDs;
357 View_type_int ColSingletonColLIDs;
358 View_type_scalar ColSingletonPivots;
359
360 SOL_type localX;
361 RHS_type localRHS;
362 RHS_type localB;
363
364 ComputeFullSolutionFunctor(View_type_int rowLIDs, View_type_int colLIDs, View_type_scalar pivots,
365 SOL_type X, RHS_type RHS, RHS_type B)
366 : ColSingletonRowLIDs(rowLIDs)
367 , ColSingletonColLIDs(colLIDs)
368 , ColSingletonPivots(pivots)
369 , localX(X)
370 , localRHS(RHS)
371 , localB(B) {}
372
373 KOKKOS_INLINE_FUNCTION
374 void operator()(const size_t k) const {
375 int i = ColSingletonRowLIDs[k];
376 int j = ColSingletonColLIDs[k];
377 scalar_type pivot = ColSingletonPivots[k];
378
379 size_t NumVectors = localX.extent(1);
380 for (size_t jj = 0; jj < NumVectors; jj++) {
381 localX(j, jj) = (localRHS(i, jj) - localB(i, jj)) / pivot;
382 }
383 }
384};
385
386template <class Map_color_type, class LocalMap_type, class IndexList_type>
387struct GenerateReducedMapFunctor {
388 int color;
389 Map_color_type mapColors;
390 LocalMap_type lclMap;
391 IndexList_type indexList;
392
393 // .. internal view used to count match ..
394 using device_type = typename Map_color_type::device_type;
395 Kokkos::View<size_t*, device_type> numView;
396
397 // constructor for counting
398 GenerateReducedMapFunctor(int color_, Map_color_type mapColors_)
399 : color(color_)
400 , mapColors(mapColors_) {}
401
402 // constructor for inserting
403 GenerateReducedMapFunctor(int color_, Map_color_type mapColors_, LocalMap_type lclMap_, IndexList_type indexList_)
404 : color(color_)
405 , mapColors(mapColors_)
406 , lclMap(lclMap_)
407 , indexList(indexList_) {
408 Kokkos::resize(numView, 1);
409 Kokkos::deep_copy(numView, 0);
410 }
411
412 // Functor to count number of elements with matching color (parallel-reduce)
413 KOKKOS_INLINE_FUNCTION
414 void operator()(const size_t i, size_t& num) const {
415 if (mapColors(i, 0) == color) {
416 num++;
417 }
418 }
419
420 // Functor to insert elements with matching color (parallel-for)
421 KOKKOS_INLINE_FUNCTION
422 void operator()(const size_t i) const {
423 if (mapColors(i, 0) == color) {
424 auto num = Kokkos::atomic_fetch_add(&numView(0), 1); // ..atomic to count..
425 indexList(num) = lclMap.getGlobalElement(i); // to global
426 }
427 }
428};
429
430//==============================================================================
431
432template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
434 CrsSingletonFilter_LinearProblem(bool run_on_host, bool verbose)
435 : globalNumSingletonRows_(Teuchos::as<local_ordinal_type>(0))
436 , globalNumSingletonCols_(Teuchos::as<local_ordinal_type>(0))
437 , RatioOfDimensions_(0.0)
438 , RatioOfNonzeros_(0.0)
439 , HaveReducedProblem_(false)
440 , AnalysisDone_(false)
441 , SymmetricElimination_(true)
442 , localMaxNumRowEntries_(Teuchos::as<local_ordinal_type>(0))
443 , FullMatrixIsCrsMatrix_(false)
444 , run_on_host_(run_on_host)
445 , verbose_(verbose) {
446}
447
448//==============================================================================
449
450template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
451typename CrsSingletonFilter_LinearProblem<Scalar, LocalOrdinal, GlobalOrdinal, Node>::NewType
453operator()(const CrsSingletonFilter_LinearProblem::OriginalType& originalLinearProblem) {
454 analyze(originalLinearProblem);
455 return construct();
456}
457
458//==============================================================================
459
460template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
462 analyze(const CrsSingletonFilter_LinearProblem::OriginalType& originalLinearProblem) {
463 this->origObj_ = originalLinearProblem;
464
465 FullMatrix_ = originalLinearProblem->getMatrix();
466
468
469 if (verbose_ && FullMatrix()->getComm()->getRank() == 0) {
470 std::cout << "\nAnalyzed Singleton Problem:\n";
471 std::cout << "---------------------------\n";
472 }
473 if (SingletonsDetected()) {
474 if (verbose_ && FullMatrix()->getComm()->getRank() == 0) {
475 std::cout << "Singletons Detected!" << std::endl;
476 std::cout << "Num Singletons: " << NumSingletons() << std::endl;
477 }
478 } else {
479 if (verbose_ && FullMatrix()->getComm()->getRank() == 0)
480 std::cout << "No Singletons Detected!" << std::endl;
481 }
482
483 if (verbose_ && FullMatrix()->getComm()->getRank() == 0)
484 std::cout << "---------------------------\n\n";
485
486 return;
487}
488
489//==============================================================================
490
491template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
492typename CrsSingletonFilter_LinearProblem<Scalar, LocalOrdinal, GlobalOrdinal, Node>::NewType
494 construct() {
495 const char tfecfFuncName[] = "construct: ";
496
497 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(this->origObj_ == Teuchos::null, std::runtime_error,
498 "Linear problem (orgiObj_) have not been set. Call analyze() first.");
499
500 ConstructReducedProblem(this->origObj_);
501
502 this->newObj_ = ReducedProblem();
503
504 if (verbose_ && SingletonsDetected() && FullMatrix()->getComm()->getRank() == 0) {
505 std::cout << "\nConstructedSingleton Problem:\n";
506 std::cout << "---------------------------\n";
507 std::cout << "RatioOfDimensions: " << RatioOfDimensions() << std::endl;
508 std::cout << "RatioOfNonzeros: " << RatioOfNonzeros() << std::endl;
509 std::cout << "---------------------------\n\n";
510 }
511
512 return this->newObj_;
513}
514
515//==============================================================================
516
517template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
522
523//==============================================================================
524
525template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
530
531//==============================================================================
532
533template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
535 Analyze(const Teuchos::RCP<row_matrix_type>& fullMatrix) {
536 const char tfecfFuncName[] = "Analyze: ";
537
538 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(AnalysisDone_, std::runtime_error,
539 "Analyze() already done once. Cannot do it again.");
540
541 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(fullMatrix == Teuchos::null,
542 std::runtime_error, "Input matrix is Teuchos::null.");
543
544 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(fullMatrix->getGlobalNumRows() == 0,
545 std::runtime_error, "Full matrix has zero dimension.");
546
547 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(fullMatrix->getGlobalNumEntries() == 0,
548 std::runtime_error, "Full matrix has no nonzero terms.");
549
550 FullMatrix_ = fullMatrix;
551
552 // First check for columns with single entries and find columns with singleton rows
553 vector_type_int ColProfiles(FullMatrixColMap());
554 ColProfiles.putScalar(0);
555 vector_type_int ColHasRowWithSingleton(FullMatrixColMap());
556 ColHasRowWithSingleton.putScalar(0);
557
558 // localRowIDofSingletonCol[j] will contain the local row ID associated with the jth column,
559 // if the jth col has a single entry
560 vector_type_LO localRowIDofSingletonCol(FullMatrixColMap());
561 localRowIDofSingletonCol.putScalar(Teuchos::OrdinalTraits<LocalOrdinal>::invalid());
562
563 // Define MapColoring objects
564 RowMapColors_ = Teuchos::rcp(new vector_type_int(FullMatrixRowMap())); // Initial colors are all 0
565 ColMapColors_ = Teuchos::rcp(new vector_type_int(FullMatrixColMap()));
566
567 local_ordinal_type localNumRows = FullMatrix()->getLocalNumRows();
568 local_ordinal_type localNumCols = FullMatrix()->getLocalNumCols();
569
570 // Set up for accessing full matrix. Will do so row-by-row.
571 InitFullMatrixAccess();
572
573 // Scan matrix for singleton rows, build up column profiles
575 if (run_on_host_) {
576 size_t NumIndices = 1;
577 // int * localIndices;
578 // nonconst_local_inds_host_view_type localIndices;
579 Teuchos::Array<local_ordinal_type> localIndices;
580 auto ColProfilesData = ColProfiles.getLocalViewHost(Tpetra::Access::ReadWrite);
581 auto localRowIDofSingletonColData = localRowIDofSingletonCol.getLocalViewHost(Tpetra::Access::ReadWrite);
582 auto ColHasRowWithSingletonData = ColHasRowWithSingleton.getLocalViewHost(Tpetra::Access::ReadWrite);
583 auto RowMapColors_Data = RowMapColors_->getLocalViewHost(Tpetra::Access::ReadWrite);
584 auto ColMapColors_Data = ColMapColors_->getLocalViewHost(Tpetra::Access::ReadWrite);
585
586 for (int i = 0; i < localNumRows; i++) {
587 // Get ith row
588 GetRow(i, NumIndices, localIndices);
589 for (size_t j = 0; j < NumIndices; j++) {
590 local_ordinal_type ColumnIndex = localIndices[j];
591
592 // Bounds check for ColumnIndex
593 if (static_cast<size_t>(ColumnIndex) >= ColProfilesData.extent(0)) {
594 std::cout << "Error: ColumnIndex out of bounds: " << ColumnIndex << std::endl;
595 std::abort();
596 }
597
598 ColProfilesData(ColumnIndex, 0)++; // Increment column count
599
600 if (static_cast<size_t>(ColumnIndex) >= localRowIDofSingletonColData.extent(0)) {
601 std::cout << "Error: ColumnIndex out of bounds for localRowIDofSingletonColData: "
602 << ColumnIndex << std::endl;
603 std::abort();
604 }
605
606 // Record local row ID for current column
607 // will use to identify row to eliminate if column is a singleton
608 localRowIDofSingletonColData(ColumnIndex, 0) = i;
609 }
610 // If row has single entry, color it and associated column with color=1
611 if (NumIndices == 1) {
612 int j2 = localIndices[0];
613 ColHasRowWithSingletonData(j2, 0)++;
614 RowMapColors_Data(i, 0) = 1;
615 ColMapColors_Data(j2, 0) = 1;
617 }
618 }
619 } else {
620 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(!FullMatrixIsCrsMatrix_, std::runtime_error,
621 "Error: FullMatrix is not CrsMatrix");
622
623 auto ColProfilesData = ColProfiles.getLocalViewDevice(Tpetra::Access::ReadWrite);
624 auto localRowIDofSingletonColData = localRowIDofSingletonCol.getLocalViewDevice(Tpetra::Access::ReadWrite);
625 auto ColHasRowWithSingletonData = ColHasRowWithSingleton.getLocalViewDevice(Tpetra::Access::ReadWrite);
626 auto RowMapColors_Data = RowMapColors_->getLocalViewDevice(Tpetra::Access::ReadWrite);
627 auto ColMapColors_Data = ColMapColors_->getLocalViewDevice(Tpetra::Access::ReadWrite);
628
629 Kokkos::View<int*, execution_space, Kokkos::MemoryTraits<Kokkos::Unmanaged | Kokkos::Atomic>>
630 ColProfilesAtomic(ColProfilesData.data(), ColProfilesData.extent(0));
631 Kokkos::View<int*, execution_space, Kokkos::MemoryTraits<Kokkos::Unmanaged | Kokkos::Atomic>>
632 ColHasRowWithSingletonAtomic(ColHasRowWithSingletonData.data(), ColHasRowWithSingletonData.extent(0));
633 Kokkos::View<int*, execution_space, Kokkos::MemoryTraits<Kokkos::Unmanaged | Kokkos::Atomic>>
634 ColMapColors_Atomic(ColMapColors_Data.data(), ColMapColors_Data.extent(0));
635 Kokkos::View<int*, execution_space> error_code("singleton-bound-check", 2);
636 Kokkos::deep_copy(error_code, 0);
637
638 auto lclRowPtr = FullCrsMatrix_->getLocalRowPtrsDevice();
639 auto lclColInd = FullCrsMatrix_->getLocalIndicesDevice();
640
641 Kokkos::parallel_reduce(
642 "CrsSingletonFilter_LinearProblem:Analyze(find-singleton-row)", range_policy(0, localNumRows),
643 KOKKOS_LAMBDA(const size_t i, local_ordinal_type& lclNumSingletonRows) {
644 // Get ith row
645 for (size_t k = lclRowPtr(i); k < lclRowPtr(i + 1); k++) {
646 local_ordinal_type ColumnIndex = lclColInd(k);
647
648 // Bounds check for ColumnIndex
649 if (static_cast<size_t>(ColumnIndex) >= ColProfilesData.extent(0)) {
650 Kokkos::atomic_exchange(&error_code(0), ColumnIndex + 1);
651 } else {
652 ColProfilesAtomic(ColumnIndex)++; // Increment column count
653 }
654
655 // Bounds check for ColumnIndex
656 if (static_cast<size_t>(ColumnIndex) >= localRowIDofSingletonColData.extent(0)) {
657 Kokkos::atomic_exchange(&error_code(1), ColumnIndex + 1);
658 } else {
659 // Record local row ID for current column
660 // will use to identify row to eliminate if column is a singleton
661 // It will store the last local row index where this column has non-zero entry
662 // (it may not be singleton row?)
663 Kokkos::atomic_max(&localRowIDofSingletonColData(ColumnIndex, 0), i);
664 }
665 }
666 // If row has single entry, color it and associated column with color=1
667 if (lclRowPtr(i + 1) == lclRowPtr(i) + 1) {
668 local_ordinal_type ColumnIndex = lclColInd(lclRowPtr(i));
669 RowMapColors_Data(i, 0) = 1;
670
671 // this column will be eliminated by this singleton row
672 ColHasRowWithSingletonAtomic(ColumnIndex)++;
673 ColMapColors_Atomic(ColumnIndex) = 1;
674 lclNumSingletonRows++;
675 }
676 },
678
679 // Check error code for bound check
680 // NOTE: Should I broadcast the error-code?
681 auto h_error = Kokkos::create_mirror_view(error_code);
682 Kokkos::deep_copy(h_error, error_code);
683 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(h_error(0) > 0, std::runtime_error,
684 "Error: ColumnIndex out of bounds: " + std::to_string(h_error(0)));
685 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(h_error(1) > 0, std::runtime_error,
686 "Error: ColumnIndex out of bounds for localRowIDofSingletonColData " + std::to_string(h_error(1)));
687 }
688
689 // 1) The vector ColProfiles has column nonzero counts for each processor's contribution
690 // Combine these to get total column profile information and then redistribute to processors
691 // so each can determine if it is the owner of the row associated with the singleton column
692 // 2) The vector ColHasRowWithSingleton[i] contain count of singleton rows that are associated with
693 // the ith column on this processor. Must tell other processors that they should also eliminate
694 // these columns.
695
696 // Make a copy of ColProfiles for later use when detecting columns that disappear locally
697 vector_type_int NewColProfiles(ColProfiles.getMap());
698 NewColProfiles.update(1.0, ColProfiles, 0.0);
699
700 // If importer is non-trivial, we need to perform a gather/scatter to accumulate results
701 auto importer = FullCrsMatrix()->getCrsGraph()->getImporter();
702 if (importer != Teuchos::null) {
703 vector_type_int tmpVec(FullMatrixDomainMap()); // Use for gather/scatter of column vectors
704 tmpVec.putScalar(0);
705 tmpVec.doExport(ColProfiles, *importer, ADD);
706 ColProfiles.doImport(tmpVec, *importer, INSERT);
707
708 tmpVec.putScalar(0);
709 tmpVec.doExport(ColHasRowWithSingleton, *importer, ADD);
710 ColHasRowWithSingleton.doImport(tmpVec, *importer, INSERT);
711 }
712
713 // ColProfiles now contains the nonzero column entry count for all columns that have
714 // an entry on this processor.
715 // ColHasRowWithSingleton now contains a count of singleton rows associated with the corresponding
716 // local column.
717
718 // Next we check to make sure no column is associated with more than one singleton row.
719 // If multiple singleton rows have nonzero entries at the same column, the matrix is singular?
720 // [0 0 0 0 0 x 0 0]
721 // [0 0 0 0 0 x 0 0]
722 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(ColHasRowWithSingleton.normInf() > 1, std::runtime_error,
723 "At least one column is associated with two singleton rows, can't handle it.");
724
726 vector_type_int RowHasColWithSingleton(FullMatrix()->getRowMap()); // Use to check for errors
727 RowHasColWithSingleton.putScalar(0);
728 if (run_on_host_) {
729 size_t NumIndices = 1;
730 // int * localIndices;
731 // nonconst_local_inds_host_view_type localIndices;
732 Teuchos::Array<local_ordinal_type> localIndices;
733
734 auto ColProfilesData = ColProfiles.getLocalViewHost(Tpetra::Access::ReadOnly);
735 auto localRowIDofSingletonColData = localRowIDofSingletonCol.getLocalViewHost(Tpetra::Access::ReadOnly);
736 auto ColHasRowWithSingletonData = ColHasRowWithSingleton.getLocalViewHost(Tpetra::Access::ReadOnly);
737
738 auto RowMapColors_Data = RowMapColors_->getLocalViewHost(Tpetra::Access::ReadWrite);
739 auto ColMapColors_Data = ColMapColors_->getLocalViewHost(Tpetra::Access::ReadWrite);
740
741 auto NewColProfilesData = NewColProfiles.getLocalViewHost(Tpetra::Access::ReadWrite);
742 auto RowHasColWithSingletonData = RowHasColWithSingleton.getLocalViewHost(Tpetra::Access::ReadWrite);
743
744 // Count singleton columns (that were not already counted as singleton rows)
745 for (local_ordinal_type j = 0; j < localNumCols; j++) {
746 local_ordinal_type i2 = localRowIDofSingletonColData(j, 0);
747 // Check if column is a singleton
748 if (ColProfilesData(j, 0) == 1) {
749 // Check to see if this column already eliminated by the row check above
750 if (RowMapColors_Data(i2, 0) != 1) {
751 RowHasColWithSingletonData(i2, 0)++; // Increment col singleton counter for ith row
752 RowMapColors_Data(i2, 0) = 2; // Use 2 for now, to distinguish between row eliminated directly or via column singletons
753 ColMapColors_Data(j, 0) = 1;
755 // If we delete a row, we need to keep track of associated column entries that were also deleted
756 // in case all entries in a column are eventually deleted, in which case the column should
757 // also be deleted.
758 GetRow(i2, NumIndices, localIndices);
759 for (size_t jj = 0; jj < NumIndices; jj++) {
760 NewColProfilesData(localIndices[jj], 0)--;
761 }
762 }
763 }
764 // Check if some other processor eliminated this column
765 else if (ColHasRowWithSingletonData(j, 0) == 1 && RowMapColors_Data(i2, 0) != 1) {
766 ColMapColors_Data(j, 0) = 1;
767 }
768 }
769 } else {
770 auto ColProfilesData = ColProfiles.getLocalViewDevice(Tpetra::Access::ReadOnly);
771 auto localRowIDofSingletonColData = localRowIDofSingletonCol.getLocalViewDevice(Tpetra::Access::ReadOnly);
772 auto ColHasRowWithSingletonData = ColHasRowWithSingleton.getLocalViewDevice(Tpetra::Access::ReadOnly);
773
774 auto RowMapColors_Data = RowMapColors_->getLocalViewDevice(Tpetra::Access::ReadWrite);
775 auto ColMapColors_Data = ColMapColors_->getLocalViewDevice(Tpetra::Access::ReadWrite);
776
777 auto NewColProfilesData = NewColProfiles.getLocalViewDevice(Tpetra::Access::ReadWrite);
778 auto RowHasColWithSingletonData = RowHasColWithSingleton.getLocalViewDevice(Tpetra::Access::ReadWrite);
779
780 auto lclRowPtr = FullCrsMatrix_->getLocalRowPtrsDevice();
781 auto lclColInd = FullCrsMatrix_->getLocalIndicesDevice();
782
783 // Count singleton columns (that were not already counted as singleton rows)
784 Kokkos::parallel_reduce(
785 "CrsSingletonFilter_LinearProblem:Analyze(find-singleton-column)", range_policy(0, localNumCols),
786 KOKKOS_LAMBDA(const size_t j, local_ordinal_type& lclNumSingletonCols) {
787 // Check if column is a singleton
788 if (ColProfilesData(j, 0) == 1) {
789 // i = id of "last" local row with non-zero in this col
790 // since this is singletone column , "i" is the row id of the single nonzero entry in this column
791 local_ordinal_type i = localRowIDofSingletonColData(j, 0);
792
793 // Check to see if this column already eliminated by the row check above
794 // RowMapColors(i,0) : 0 = ith row was not singleton, 1 = was singleton, 2 = was not singleton, but processed
795 // Multiple singleton columns cannot have nonzero entry in the same row, i
796 // Otherwise, the matrix will be singular
797 // So, rowMapColors(i,0) should be checked by one singleton column (note 1, also see check 2)
798 // not atomic needed
799 if (RowMapColors_Data(i, 0) != 1) { // that row is not singleton, and hence this col has not been removed
800 RowMapColors_Data(i, 0) = 2;
801 ColMapColors_Data(j, 0) = 1;
802 lclNumSingletonCols++;
803
804 // Increment col singleton counter for ith row (Only one j should update (i) because of "note 1")
805 RowHasColWithSingletonData(i, 0)++;
806
807 // If we delete a row, we need to keep track of associated column entries that were also deleted
808 // in case all entries in a column are eventually deleted, in which case the column should
809 // also be deleted.
810 // Only one j should execute this loop for a specific "i" (because of "note 1")
811 for (size_t k = lclRowPtr(i); k < lclRowPtr(i + 1); k++) {
812 NewColProfilesData(lclColInd(k), 0)--;
813 }
814 }
815 }
816 },
818 Kokkos::parallel_for(
819 "CrsSingletonFilter_LinearProblem:Analyze(find-non-singleton-column)", range_policy(0, localNumCols),
820 KOKKOS_LAMBDA(const size_t j) {
821 // Check if column is *NOT* a singleton
822 if (ColProfilesData(j, 0) != 1) {
823 // Since this is not singletone col,
824 // "i" is the "last" local row with non-zero in this col and may not corresponds to the singleton row?
825 local_ordinal_type i = localRowIDofSingletonColData(j, 0);
826
827 // Check if some other processor has "previously" eliminated this column
828 // (note: multiple singleton columns cannot have nonzero in the same column, colhasrowwithsingleton(j) is 0 or 1)
829 // This column has "one" row, which is singleton
830 // that singleton row has the single nonzero entry at jth col
831 // At jth iteration, if RowMapColors_Data(i, 0) == 1, then it stays as 1
832 // otherwise it has been processed into 2 by previous column, or still 0
833 if (ColHasRowWithSingletonData(j, 0) == 1 && RowMapColors_Data(i, 0) != 1) {
834 // RowMapColors_Data(i, 0) was 0, or processed by a previous column
835 // TODO: check the second and third conditions
836 ColMapColors_Data(j, 0) = 1;
837 }
838 }
839 });
840 }
841
842 // Next we check to make sure no row is associated with more than one singleton col (check 2)
843 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(RowHasColWithSingleton.normInf() > 1, std::runtime_error,
844 "At least one row is associated with two singleton columns, can't handle it.");
845
846 // Generate arrays that keep track of column singleton row, col and pivot info needed for post-solve phase
847 CreatePostSolveArrays(localRowIDofSingletonCol, ColProfiles, NewColProfiles, ColHasRowWithSingleton);
848 if (run_on_host_) {
849 auto RowMapColors_Data = RowMapColors_->getLocalViewHost(Tpetra::Access::ReadWrite);
850 for (local_ordinal_type i = 0; i < localNumRows; i++) {
851 if (RowMapColors_Data(i, 0) == 2) RowMapColors_Data(i, 0) = 1; // Convert all eliminated rows to same color
852 }
853 } else {
854 auto RowMapColors_Data = RowMapColors_->getLocalViewDevice(Tpetra::Access::ReadWrite);
855 Kokkos::parallel_for(
856 "CrsSingletonFilter_LinearProblem:Analyze(convert-rowmap-color)", range_policy(0, localNumRows),
857 KOKKOS_LAMBDA(const size_t i) {
858 if (RowMapColors_Data(i, 0) == 2) RowMapColors_Data(i, 0) = 1; // Convert all eliminated rows to same color
859 });
860 }
861
862 const Teuchos::Ptr<local_ordinal_type> gRowsPtr(&globalNumSingletonRows_);
863 const Teuchos::Ptr<local_ordinal_type> gColsPtr(&globalNumSingletonCols_);
864 auto rowComm = FullMatrix()->getRowMap()->getComm();
865 Teuchos::reduceAll<local_ordinal_type, local_ordinal_type>(*rowComm, Teuchos::REDUCE_SUM, localNumSingletonRows_, gRowsPtr);
866 Teuchos::reduceAll<local_ordinal_type, local_ordinal_type>(*rowComm, Teuchos::REDUCE_SUM, localNumSingletonCols_, gColsPtr);
867
868 AnalysisDone_ = true;
869 return;
870}
871
872//==============================================================================
873
874template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
875Teuchos::RCP<const Tpetra::Map<LocalOrdinal, GlobalOrdinal, Node>>
877 GenerateReducedMap(const Teuchos::RCP<const map_type>& originalMap,
878 const Teuchos::RCP<vector_type_int>& mapColors, int color,
879 bool locally_sort_gids) {
880 Teuchos::RCP<const map_type> reducedMap;
881 bool canRunOnHost = std::is_same_v<typename device_type::memory_space, Kokkos::HostSpace>;
882 if (run_on_host_ && canRunOnHost) {
883 // Vector to hold the reduced global indices
884 std::vector<GlobalOrdinal> myReducedGlobalIndices;
885
886 // Get the global IDs owned by the calling process
887 auto myGlobalIndices = originalMap->getMyGlobalIndices();
888
889 // Iterate through the global indices to find the ones that match the color
890 for (size_t i = 0; i < myGlobalIndices.size(); i++) {
891 // Access the value in mapColors using the appropriate method
892 int colorValue = mapColors->getData()[i]; // Use getData() to access the vector data
893 if (colorValue == color) {
894 myReducedGlobalIndices.push_back(myGlobalIndices[i]);
895 }
896 }
897
898 // Create the reduced map using the collected indices
900 Teuchos::ArrayView<const GlobalOrdinal>(myReducedGlobalIndices.data(), myReducedGlobalIndices.size()),
901 originalMap->getComm());
902 } else {
903 // Iterate through the global indices to find the ones that match the color
904 using IndexListType = Kokkos::View<GlobalOrdinal*, device_type>;
905 using functor_type = GenerateReducedMapFunctor<local_vector_int_type, local_map_type, IndexListType>;
906 auto mapColors_Data = mapColors->getLocalViewDevice(Tpetra::Access::ReadWrite);
907
908 // count number of elements with the matching color
909 size_t lclNumReducedElements = 0;
910 size_t lclNumElements = originalMap->getLocalNumElements();
911 {
912 functor_type functor(color, mapColors_Data);
913 Kokkos::parallel_reduce(
914 "CrsSingletonFilter_LinearProblem:GenerateReduceMap(count)", range_policy(0, lclNumElements),
915 functor, lclNumReducedElements);
916 }
917 // create the list of elements with the matching color
918 IndexListType indexList;
919 {
920 auto lclMap = originalMap()->getLocalMap();
921 Kokkos::resize(indexList, lclNumReducedElements);
922 functor_type functor(color, mapColors_Data, lclMap, indexList);
923 Kokkos::parallel_for(
924 "CrsSingletonFilter_LinearProblem:GenerateReduceMap(insert)", range_policy(0, lclNumElements),
925 functor);
926 }
927 if (locally_sort_gids) {
928 // locally-sort the matched GIDs
929 Kokkos::sort(execution_space(), indexList);
930 }
931
932 // Create the reduced map using the collected indices
933 const Tpetra::global_size_t INVALID = Tpetra::Details::OrdinalTraits<Tpetra::global_size_t>::invalid();
934 reducedMap = Teuchos::rcp(new map_type(INVALID, indexList,
935 originalMap->getIndexBase(),
936 originalMap->getComm()));
937 }
938
939 return reducedMap;
940}
941
942//==============================================================================
943
944template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
946 ConstructReducedProblem(const Teuchos::RCP<linear_problem_type>& Problem) {
947 const char tfecfFuncName[] = "ConstructReducedProblem: ";
948
949 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(HaveReducedProblem_, std::runtime_error,
950 "Already have a reduced problem. Cannot do it again.");
951
952 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(Problem == Teuchos::null,
953 std::runtime_error, "Problem is Teuchos::null.");
954
955 FullProblem_ = Problem;
956 FullMatrix_ = Teuchos::rcp_dynamic_cast<row_matrix_type>(Problem->getMatrix());
957 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(FullMatrix() == Teuchos::null,
958 std::runtime_error, "Need a RowMatrix.");
959 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(Problem->getRHS() == Teuchos::null,
960 std::runtime_error, "Need a RHS.");
961 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(Problem->getLHS() == Teuchos::null,
962 std::runtime_error, "Need a LHS.");
963
964 Scalar zero = Teuchos::ScalarTraits<Scalar>::zero();
965
966 // Generate reduced row and column maps
967 if (SingletonsDetected()) {
968 ReducedMatrixRowMap_ = GenerateReducedMap(FullMatrixRowMap(), RowMapColors_, 0);
969 ReducedMatrixColMap_ = GenerateReducedMap(FullMatrixColMap(), ColMapColors_, 0);
970
971 // Create domain and range map colorings by exporting map coloring of column and row maps
972 auto importer = FullCrsMatrix()->getCrsGraph()->getImporter();
973 if (importer != Teuchos::null) {
974 Teuchos::RCP<vector_type_int> DomainMapColors = Teuchos::rcp(new vector_type_int(FullMatrixDomainMap()));
975 DomainMapColors->doExport(*ColMapColors_, *importer, Tpetra::ABSMAX);
976 OrigReducedMatrixDomainMap_ = GenerateReducedMap(FullMatrixDomainMap(), DomainMapColors, 0);
977 } else {
978 OrigReducedMatrixDomainMap_ = ReducedMatrixColMap();
979 }
980
981 auto exporter = FullCrsMatrix()->getCrsGraph()->getImporter();
982 if (FullMatrixIsCrsMatrix_) {
983 if (exporter != Teuchos::null) { // Non-trivial exporter
984 Teuchos::RCP<vector_type_int> RangeMapColors = Teuchos::rcp(new vector_type_int(FullMatrixRangeMap()));
985 RangeMapColors->doExport(*ColMapColors_, *exporter, Tpetra::ABSMAX);
986 ReducedMatrixRangeMap_ = GenerateReducedMap(FullMatrixRangeMap(), RangeMapColors, 0);
987 } else
988 ReducedMatrixRangeMap_ = ReducedMatrixRowMap();
989 } else
990 ReducedMatrixRangeMap_ = ReducedMatrixRowMap();
991
992 // Check to see if the reduced system domain and range maps are the same.
993 // If not, we need to remap entries of the LHS multivector so that they are distributed
994 // conformally with the rows of the reduced matrix and the RHS multivector
995 SymmetricElimination_ = ReducedMatrixRangeMap()->isSameAs(*OrigReducedMatrixDomainMap_);
996 if (!SymmetricElimination_) {
997 ConstructRedistributeExporter(OrigReducedMatrixDomainMap_, ReducedMatrixRangeMap_,
998 RedistributeDomainExporter_, ReducedMatrixDomainMap_);
999 } else {
1000 ReducedMatrixDomainMap_ = OrigReducedMatrixDomainMap_;
1001 OrigReducedMatrixDomainMap_ = Teuchos::null;
1002 RedistributeDomainExporter_ = Teuchos::null;
1003 }
1004
1005 // Create pointer to Full RHS, LHS
1006 Teuchos::RCP<multivector_type> FullRHS = FullProblem()->getRHS();
1007 Teuchos::RCP<multivector_type> FullLHS = FullProblem()->getLHS();
1008 local_ordinal_type NumVectors = FullLHS->getNumVectors();
1009
1010 // Create importers
1011 Full2ReducedLHSImporter_ = Teuchos::rcp(new import_type(FullMatrixDomainMap(), ReducedMatrixDomainMap()));
1012 Full2ReducedRHSImporter_ = Teuchos::rcp(new import_type(FullRHS->getMap(), ReducedMatrixRowMap()));
1013
1014 // Create storage for temporary X values due to explicit elimination of rows
1015 tempExportX_ = Teuchos::rcp(new multivector_type(FullMatrixColMap(), NumVectors));
1016
1017 size_t NumEntries = 0;
1018 Teuchos::ArrayView<const Scalar> Values;
1019 Teuchos::Array<GlobalOrdinal> Indices;
1020 LocalOrdinal localNumRows = FullMatrix()->getLocalNumRows();
1021 LocalOrdinal ColSingletonCounter = 0;
1022
1023 // Check if ColSingletonPivotLIDs_, ColSingletonPivot_ can be accessed on host
1024 bool canRunOnHost = std::is_same_v<typename device_type::memory_space, Kokkos::HostSpace>;
1025 if (run_on_host_ && canRunOnHost) {
1026 // Construct Reduced Matrix
1027 LocalOrdinal maxNumEntries = FullCrsMatrix()->getLocalMaxNumRowEntries();
1028 ReducedMatrix_ = Teuchos::rcp(new crs_matrix_type(ReducedMatrixRowMap(), ReducedMatrixColMap(), maxNumEntries));
1029
1030 for (LocalOrdinal i = 0; i < localNumRows; i++) {
1031 GlobalOrdinal curGRID = FullMatrixRowMap()->getGlobalElement(i);
1032 if (ReducedMatrixRowMap()->isNodeGlobalElement(curGRID)) { // Check if this row should go into reduced matrix
1033 GetRowGCIDs(i, NumEntries, Values, Indices); // Get current row (Indices are global)
1034
1035 // The ReducedMatrix has the row and column indices already removed so cannot "insert" them.
1036 // Need to filter them to only insert remaining.
1037 // Filter indices and values
1038 Teuchos::Array<GlobalOrdinal> filteredIndices;
1039 Teuchos::Array<Scalar> filteredValues;
1040
1041 for (typename Teuchos::Array<GlobalOrdinal>::size_type j = 0; j < Indices.size(); ++j) {
1042 if (ReducedMatrixColMap()->isNodeGlobalElement(Indices[j])) {
1043 filteredIndices.push_back(Indices[j]);
1044 filteredValues.push_back(Values[j]);
1045 }
1046 }
1047
1048 // Insert filtered values into the matrix
1049 if (!filteredIndices.empty()) {
1050 ReducedMatrix()->insertGlobalValues(curGRID, filteredIndices(), filteredValues());
1051 }
1052
1053 } else {
1054 Teuchos::ArrayView<const LocalOrdinal> localIndices;
1055 GetRow(i, NumEntries, Values, localIndices); // Get current row
1056 if (NumEntries == 1) {
1057 Scalar pivot = Values[0];
1058 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(pivot == zero, std::runtime_error,
1059 "Encountered zero row, unable to continue."); // Should improve this comparison to zero.
1060 LocalOrdinal indX = localIndices[0];
1061 for (LocalOrdinal j = 0; j < NumVectors; j++) {
1062 auto rhsData = FullRHS->getData(j); // Get the underlying data for vector j
1063 auto exportData = tempExportX_->getDataNonConst(j); // Get the underlying data for vector j (non-const)
1064 exportData[indX] = rhsData[i] / pivot;
1065 }
1066 } else { // Singleton column
1067 LocalOrdinal targetCol = ColSingletonColLIDs_[ColSingletonCounter];
1068 for (size_t j = 0; j < NumEntries; j++) {
1069 if (localIndices[j] == targetCol) {
1070 Scalar pivot = Values[j];
1071 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(pivot == zero, std::runtime_error,
1072 "Encountered zero column, unable to continue."); // Should improve this comparison to zero.
1073 ColSingletonPivotLIDs_[ColSingletonCounter] = j;
1074 ColSingletonPivots_[ColSingletonCounter] = pivot;
1075 ColSingletonCounter++;
1076 break;
1077 }
1078 }
1079 }
1080 }
1081 }
1082
1083 // Now convert to local indexing. We have constructed things so that the domain and range of the
1084 // matrix will have the same map. If the reduced matrix domain and range maps were not the same, the
1085 // differences were addressed in the ConstructRedistributeExporter() method
1087 } else {
1088 // The ReducedMatrix has the row and column indices already removed so cannot "insert" them.
1089 // Need to filter them to only insert remaining.
1090 // Filter indices and values
1091
1092 // Construct the reduced matrix
1093 // * Only the maps have been setup, and hence the rowptr, colind, nzvals need to be still allocated.
1094 {
1095 using graph_type = typename local_matrix_type::StaticCrsGraphType;
1096 using row_map_type = typename graph_type::row_map_type::non_const_type;
1097 using entries_type = typename graph_type::entries_type::non_const_type;
1098 using values_type = typename local_matrix_type::values_type;
1099 using functor_type = ConstructReducedProblemFunctor<local_ordinal_type, global_ordinal_type, local_map_type, local_matrix_type, row_map_type, entries_type, values_type>;
1100
1101 size_t nnzA = 1;
1102 auto lclFullRowMap = FullMatrixRowMap()->getLocalMap();
1103 auto lclFullColMap = FullMatrixColMap()->getLocalMap();
1104 auto lclReducedRowMap = ReducedMatrixRowMap()->getLocalMap();
1105 auto lclReducedColMap = ReducedMatrixColMap()->getLocalMap();
1106
1107 auto lclFullMatrix = FullCrsMatrix_->getLocalMatrixDevice();
1108
1109 LocalOrdinal localNumReducedRows = ReducedMatrixRowMap()->getLocalNumElements();
1110 row_map_type rowmap_view("rowmap_view", localNumReducedRows + 1);
1111 entries_type column_view("colind_view", nnzA);
1112 values_type values_view("values_view", nnzA);
1113
1114 // launch functor to count nnz per row
1115 {
1116 Kokkos::deep_copy(rowmap_view, 0);
1117
1118 functor_type functor(lclFullRowMap, lclFullColMap, lclReducedRowMap, lclReducedColMap,
1119 lclFullMatrix, rowmap_view, column_view, values_view);
1120 Kokkos::parallel_reduce(
1121 "CrsSingletonFilter_LinearProblem:CountReducedProblem", range_policy(0, localNumRows),
1122 functor, nnzA);
1123 }
1124
1125 // convert to rowptrs
1126#if KOKKOSKERNELS_VERSION >= 50299
1127 KokkosKernels::inclusive_parallel_prefix_sum(execution_space(), rowmap_view);
1128#else
1129 KokkosKernels::Impl::kk_inclusive_parallel_prefix_sum<execution_space>(1 + localNumReducedRows, rowmap_view);
1130#endif
1131
1132 // insert non-zero entries
1133 {
1134 Kokkos::resize(column_view, nnzA);
1135 Kokkos::resize(values_view, nnzA);
1136
1137 // functor with new view sizes
1138 functor_type functor(lclFullRowMap, lclFullColMap, lclReducedRowMap, lclReducedColMap,
1139 lclFullMatrix, rowmap_view, column_view, values_view);
1140 Kokkos::parallel_for(
1141 "CrsSingletonFilter_LinearProblem:InsertReducedProblem", range_policy(0, localNumRows),
1142 functor);
1143 }
1144
1145 // construct local crsmatrix
1146 graph_type static_graph(column_view, rowmap_view);
1147 local_matrix_type crsmat("CrsMatrix", localNumReducedRows, values_view, static_graph);
1148
1149 // Construct Reduced Matrix with localmatrix + rowmap, colmap, domainmap, & rangemap
1150 // We have constructed things so that the domain and range of the
1151 // matrix will have the same map. If the reduced matrix domain and range maps were not the same, the
1152 // differences were addressed in the ConstructRedistributeExporter() method
1153 ReducedMatrix_ = Teuchos::rcp(new crs_matrix_type(crsmat, ReducedMatrixRowMap(), ReducedMatrixColMap(), ReducedMatrixDomainMap(), ReducedMatrixRangeMap()));
1154 }
1155
1156 // Solve with singleton part
1157 {
1158 using error_code_type = typename Kokkos::View<int*, execution_space>;
1159 using functor_type = SolveSingletonProblemFunctor<scalar_type, local_ordinal_type, global_ordinal_type, Node, local_map_type, local_matrix_type,
1160 local_multivector_type, const_local_multivector_type,
1161 vector_view_type_int, vector_view_type_scalar, error_code_type>;
1162 auto lclFullRowMap = FullMatrixRowMap()->getLocalMap();
1163 auto lclFullColMap = FullMatrixColMap()->getLocalMap();
1164 auto lclReducedRowMap = ReducedMatrixRowMap()->getLocalMap();
1165
1166 auto lclFullMatrix = FullCrsMatrix_->getLocalMatrixDevice();
1167
1168 auto localExportX = tempExportX_->getLocalViewDevice(Tpetra::Access::ReadWrite);
1169 auto localRHS = FullRHS->getLocalViewDevice(Tpetra::Access::ReadOnly);
1170
1171 error_code_type error_code("singleton-bound-check", 2);
1172 Kokkos::deep_copy(error_code, 0);
1173
1174 // launch functor
1175 functor_type functor(NumVectors, localNumSingletonCols_, error_code,
1176 lclFullRowMap, lclFullColMap, lclReducedRowMap,
1177 lclFullMatrix, localExportX, localRHS,
1178 ColSingletonColLIDs_, ColSingletonRowLIDs_, ColSingletonPivotLIDs_, ColSingletonPivots_);
1179 Kokkos::parallel_for(
1180 "CrsSingletonFilter_LinearProblem:SolveSingletonProblem", range_policy(0, localNumRows),
1181 functor);
1182 auto h_error = Kokkos::create_mirror_view(error_code);
1183 Kokkos::deep_copy(h_error, error_code);
1184 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(h_error(0) == 1, std::runtime_error,
1185 "ConstructReducedProblem: Encountered zero row, unable to continue."); // Should improve this comparison to zero.
1186 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(h_error(0) == 2, std::runtime_error,
1187 "ConstructReducedProblem: Encountered zero column, unable to continue."); // Should improve this comparison to zero.
1188 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(h_error(0) == 3, std::runtime_error,
1189 "ConstructReducedProblem: Unable to find my column.");
1190 }
1191 }
1192
1193 // 1) The vector ColProfiles has column nonzero counts for each processor's contribution
1194 // Construct Reduced LHS (Puts any initial guess values into reduced system)
1195
1196 // Create the ReducedLHS_ MultiVector
1197 ReducedLHS_ = Teuchos::rcp(new multivector_type(ReducedMatrixDomainMap(), NumVectors));
1198 ReducedLHS_->doImport(*FullLHS, *Full2ReducedLHSImporter_, Tpetra::INSERT);
1199
1200 FullLHS->putScalar(0.0);
1201
1202 // Construct Reduced RHS
1203
1204 // First compute influence of already-known values of X on RHS
1205 tempX_ = Teuchos::rcp(new multivector_type(FullMatrixDomainMap(), NumVectors));
1206 tempB_ = Teuchos::rcp(new multivector_type(FullRHS->getMap(), NumVectors));
1207
1208 // Inject known X values into tempX for purpose of computing tempB = FullMatrix*tempX
1209 // Also inject into full X since we already know the solution
1210 if (importer != Teuchos::null) {
1211 tempX_->doExport(*tempExportX_, *importer, Tpetra::ADD);
1212 FullLHS->doExport(*tempExportX_, *importer, Tpetra::ADD);
1213 } else {
1214 tempX_->update(1.0, *tempExportX_, 0.0); // tempX_ = 1.0 * tempExportX_ + 0.0 * tempX_
1215 FullLHS->update(1.0, *tempExportX_, 0.0); // FullLHS = 1.0 * tempExportX_ + 0.0 * FullLHS
1216 }
1217
1218 FullMatrix()->apply(*tempX_, *tempB_);
1219 tempB_->update(1.0, *FullRHS, -1.0);
1220
1221 ReducedRHS_ = Teuchos::rcp(new multivector_type(ReducedMatrixRowMap(), FullRHS->getNumVectors()));
1222 ReducedRHS_->doImport(*tempB_, *Full2ReducedRHSImporter_, Tpetra::INSERT);
1223
1224 // Finally construct Reduced Linear Problem
1226 ReducedMatrix(), ReducedLHS_, ReducedRHS_));
1227 } else {
1228 // There are no singletons, so don't bother building a reduced problem.
1229 ReducedProblem_ = Problem;
1230 ReducedMatrix_ = Teuchos::rcp(dynamic_cast<crs_matrix_type*>(Problem->getMatrix().getRawPtr()), false);
1231 }
1232
1233 double fn = (double)FullMatrix()->getGlobalNumRows();
1234 double fnnz = (double)FullMatrix()->getGlobalNumEntries();
1235
1236 double rn = (double)ReducedMatrix()->getGlobalNumRows();
1237 double rnnz = (double)ReducedMatrix()->getGlobalNumEntries();
1238
1239 RatioOfDimensions_ = rn / fn;
1240 RatioOfNonzeros_ = rnnz / fnnz;
1241 HaveReducedProblem_ = true;
1242
1243 return;
1244}
1245
1246//==============================================================================
1247template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1249 UpdateReducedProblem(const Teuchos::RCP<linear_problem_type>& Problem) {
1250 const char tfecfFuncName[] = "UpdateReducedProblem: ";
1251
1252 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(!HaveReducedProblem_, std::runtime_error,
1253 "Must have a reduced problem.");
1254 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(Problem == Teuchos::null,
1255 std::runtime_error, "Problem is Teuchos::null.");
1256 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(FullMatrix() == Teuchos::null,
1257 std::runtime_error, "Need a RowMatrix.");
1258 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(Problem->getRHS() == Teuchos::null,
1259 std::runtime_error, "Need a RHS.");
1260 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(Problem->getLHS() == Teuchos::null,
1261 std::runtime_error, "Need a LHS.");
1262
1263 Scalar zero = Teuchos::ScalarTraits<Scalar>::zero();
1264
1265 if (SingletonsDetected()) {
1266 // Create pointer to Full RHS, LHS
1267 Teuchos::RCP<multivector_type> FullRHS = FullProblem()->getRHS();
1268 Teuchos::RCP<multivector_type> FullLHS = FullProblem()->getLHS();
1269
1270 local_ordinal_type NumVectors = FullLHS->getNumVectors();
1271 tempExportX_->putScalar(0.0);
1272
1273 size_t NumEntries = 0;
1274 Teuchos::ArrayView<const Scalar> Values;
1275 Teuchos::Array<GlobalOrdinal> Indices;
1276 LocalOrdinal localNumRows = FullMatrix()->getLocalNumRows();
1277 LocalOrdinal ColSingletonCounter = 0;
1278
1279 // Check if ColSingletonPivotLIDs_, ColSingletonPivot_ can be accessed on host
1280 bool canRunOnHost = std::is_same_v<typename device_type::memory_space, Kokkos::HostSpace>;
1281 if (run_on_host_ && canRunOnHost) {
1282 ReducedMatrix()->resumeFill();
1283 for (LocalOrdinal i = 0; i < localNumRows; i++) {
1284 GlobalOrdinal curGRID = FullMatrixRowMap()->getGlobalElement(i);
1285 if (ReducedMatrixRowMap()->isNodeGlobalElement(curGRID)) { // Check if this row should go into reduced matrix
1286 GetRowGCIDs(i, NumEntries, Values, Indices);
1287
1288 // Filter indices and values
1289 Teuchos::Array<GlobalOrdinal> filteredIndices;
1290 Teuchos::Array<Scalar> filteredValues;
1291
1292 for (typename Teuchos::Array<GlobalOrdinal>::size_type j = 0; j < Indices.size(); ++j) {
1293 if (ReducedMatrixColMap()->isNodeGlobalElement(Indices[j])) {
1294 filteredIndices.push_back(Indices[j]);
1295 filteredValues.push_back(Values[j]);
1296 }
1297 }
1298
1299 // Insert filtered values into the matrix
1300 if (!filteredIndices.empty()) {
1301 ReducedMatrix()->replaceGlobalValues(curGRID, filteredIndices(), filteredValues());
1302 }
1303
1304 }
1305 // Otherwise if singleton row we explicitly eliminate this row and solve for corresponding X value
1306 else {
1307 Teuchos::ArrayView<const LocalOrdinal> localIndices;
1308 GetRow(i, NumEntries, Values, localIndices); // Get current row
1309 if (NumEntries == 1) {
1310 Scalar pivot = Values[0];
1311 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(pivot == zero, std::runtime_error,
1312 "Encountered zero row, unable to continue."); // Should improve this comparison to zero.
1313 LocalOrdinal indX = localIndices[0];
1314 for (LocalOrdinal j = 0; j < NumVectors; j++) {
1315 auto rhsData = FullRHS->getData(j); // Get the underlying data for vector j
1316 auto exportData = tempExportX_->getDataNonConst(j); // Get the underlying data for vector j (non-const)
1317 exportData[indX] = rhsData[i] / pivot;
1318 }
1319 } else { // Singleton column
1320 LocalOrdinal j = ColSingletonPivotLIDs_[ColSingletonCounter];
1321
1322 Scalar pivot = Values[j];
1323 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(pivot == zero, std::runtime_error,
1324 "Encountered zero column, unable to continue."); // Should improve this comparison to zero.
1325 ColSingletonPivots_[ColSingletonCounter] = pivot;
1326 ColSingletonCounter++;
1327 }
1328 }
1329 }
1330 ReducedMatrix()->fillComplete(ReducedMatrix()->getDomainMap(), ReducedMatrix()->getRangeMap());
1331 } else {
1332 // Not part of the reduced matrix (singletones)
1333 {
1334 using error_code_type = typename Kokkos::View<int*, execution_space>;
1335 using functor_type = SolveSingletonProblemFunctor<scalar_type, local_ordinal_type, global_ordinal_type, Node, local_map_type, local_matrix_type,
1336 local_multivector_type, const_local_multivector_type,
1337 vector_view_type_int, vector_view_type_scalar, error_code_type>;
1338
1339 auto lclFullRowMap = FullMatrixRowMap()->getLocalMap();
1340 auto lclFullColMap = FullMatrixColMap()->getLocalMap();
1341 auto lclReducedRowMap = ReducedMatrixRowMap()->getLocalMap();
1342
1343 auto lclFullMatrix = FullCrsMatrix_->getLocalMatrixDevice();
1344
1345 auto localRHS = FullRHS->getLocalViewDevice(Tpetra::Access::ReadOnly);
1346 auto localExportX = tempExportX_->getLocalViewDevice(Tpetra::Access::ReadWrite);
1347
1348 error_code_type error_code("singleton-bound-check", 2);
1349 Kokkos::deep_copy(error_code, 0);
1350
1351 // launch functor
1352 functor_type functor(NumVectors, localNumSingletonCols_, error_code,
1353 lclFullRowMap, lclFullColMap, lclReducedRowMap,
1354 lclFullMatrix, localExportX, localRHS,
1355 ColSingletonColLIDs_, ColSingletonRowLIDs_, ColSingletonPivotLIDs_, ColSingletonPivots_);
1356 Kokkos::parallel_reduce(
1357 "CrsSingletonFilter_LinearProblem:SolveSingletonProblem", range_policy(0, localNumRows),
1358 functor, ColSingletonCounter);
1359
1360 // get error
1361 auto h_error = Kokkos::create_mirror_view(error_code);
1362 Kokkos::deep_copy(h_error, error_code);
1363 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(h_error(0) == 1, std::runtime_error,
1364 "UpdateReducedProblem: Encountered zero row, unable to continue."); // Should improve this comparison to zero.
1365 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(h_error(0) == 2, std::runtime_error,
1366 "UpdateReducedProblem: Encountered zero column, unable to continue."); // Should improve this comparison to zero.
1367 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(h_error(0) == 3, std::runtime_error,
1368 "UpdateReducedProblem: Unable to find local column.");
1369 }
1370 // Part of the reduced matrix
1371 {
1372 using functor_type = UpdateReducedProblemFunctor<local_ordinal_type, global_ordinal_type, local_map_type, local_matrix_type>;
1373 auto lclFullRowMap = FullMatrixRowMap()->getLocalMap();
1374 auto lclFullColMap = FullMatrixColMap()->getLocalMap();
1375 auto lclReducedRowMap = ReducedMatrixRowMap()->getLocalMap();
1376 auto lclReducedColMap = ReducedMatrixColMap()->getLocalMap();
1377
1378 auto lclReducedMatrix = ReducedMatrix_->getLocalMatrixDevice();
1379 auto lclFullMatrix = FullCrsMatrix_->getLocalMatrixDevice();
1380
1381 // launch functor
1382 functor_type functor(lclFullRowMap, lclFullColMap, lclReducedRowMap, lclReducedColMap,
1383 lclFullMatrix, lclReducedMatrix);
1384 Kokkos::parallel_for(
1385 "CrsSingletonFilter_LinearProblem:UpdateReducedProblem", range_policy(0, localNumRows),
1386 functor);
1387 }
1388 }
1389
1390 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(!(ColSingletonCounter == localNumSingletonCols_),
1391 std::runtime_error, "Sanity Check " + std::to_string(ColSingletonCounter) + " vs " + std::to_string(localNumSingletonCols_) + " (UpdateReducedProblem).");
1392
1393 // Update Reduced LHS (Puts any initial guess values into reduced system)
1394
1395 ReducedLHS_->putScalar(0.0); // zero out Reduced LHS
1396 ReducedRHS_->doImport(*FullLHS, *Full2ReducedRHSImporter_, Tpetra::INSERT);
1397
1398 FullLHS->putScalar(0.0); // zero out Full LHS since we will inject values as we get them
1399
1400 // Construct Reduced RHS
1401
1402 // Zero out temp space
1403 tempX_->putScalar(0.0);
1404 tempB_->putScalar(0.0);
1405
1406 // Inject known X values into tempX for purpose of computing tempB = FullMatrix*tempX
1407 // Also inject into full X since we already know the solution
1408
1409 auto importer = FullCrsMatrix()->getCrsGraph()->getImporter();
1410 if (importer != Teuchos::null) {
1411 tempX_->doExport(*tempExportX_, *importer, Tpetra::ADD);
1412 FullLHS->doExport(*tempExportX_, *importer, Tpetra::ADD);
1413 } else {
1414 tempX_->update(1.0, *tempExportX_, 0.0); // tempX_ = 1.0 * tempExportX_ + 0.0 * tempX_
1415 FullLHS->update(1.0, *tempExportX_, 0.0); // FullLHS = 1.0 * tempExportX_ + 0.0 * FullLHS
1416 }
1417
1418 FullMatrix()->apply(*tempX_, *tempB_);
1419 tempB_->update(1.0, *FullRHS, -1.0);
1420
1421 ReducedRHS_->putScalar(0.0);
1422 ReducedRHS_->doImport(*tempB_, *Full2ReducedRHSImporter_, Tpetra::INSERT);
1423
1424 } else {
1425 // There are no singletons, so don't bother building a reduced problem.
1426 ReducedProblem_ = Problem;
1427 ReducedMatrix_ = Teuchos::rcp(dynamic_cast<crs_matrix_type*>(Problem->getMatrix().getRawPtr()), false);
1428 }
1429
1430 return;
1431}
1432
1433//==============================================================================
1434template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1436 ConstructRedistributeExporter(Teuchos::RCP<const map_type> SourceMap, Teuchos::RCP<const map_type> TargetMap,
1437 Teuchos::RCP<export_type>& RedistributeExporter,
1438 Teuchos::RCP<const map_type>& RedistributeMap) {
1439 const char tfecfFuncName[] = "ConstructRedistributeExporter: ";
1440
1441 auto IndexBase = SourceMap->getIndexBase();
1442 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(IndexBase != TargetMap->getIndexBase(), std::runtime_error,
1443 "Base indices do not match.");
1444
1445 auto Comm = TargetMap->getComm();
1446
1447 size_t TargetNumMyElements = TargetMap->getLocalNumElements();
1448 size_t SourceNumMyElements = SourceMap->getLocalNumElements();
1449
1450 // ContiguousTargetMap has the same number of elements per PE as TargetMap, but uses contiguous indexing
1451 Teuchos::RCP<const map_type> ContiguousTargetMap =
1452 Teuchos::rcp(new const map_type(
1453 Teuchos::OrdinalTraits<Tpetra::global_size_t>::invalid(),
1454 TargetNumMyElements, IndexBase, Comm));
1455
1456 // Same for ContiguousSourceMap
1457 Teuchos::RCP<const map_type> ContiguousSourceMap =
1458 Teuchos::rcp(new const map_type(
1459 Teuchos::OrdinalTraits<Tpetra::global_size_t>::invalid(),
1460 SourceNumMyElements, IndexBase, Comm));
1461
1462 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(
1463 ContiguousSourceMap->getGlobalNumElements() != ContiguousTargetMap->getGlobalNumElements(),
1464 std::runtime_error, "Global number of elements do not match.");
1465
1466 // Retrieve the global indices owned by the calling process as a Kokkos::View
1467 auto SourceMapMyGlobalIndices = SourceMap->getMyGlobalIndices();
1468
1469 // Create a Tpetra::Vector to hold the global indices
1470 Teuchos::RCP<Tpetra::MultiVector<GlobalOrdinal, LocalOrdinal, GlobalOrdinal, Node>> SourceIndices =
1471 Teuchos::rcp(new Tpetra::MultiVector<GlobalOrdinal, LocalOrdinal, GlobalOrdinal, Node>(ContiguousSourceMap, 1));
1472
1473 // Fill the Tpetra::Vector with the global indices
1474 for (size_t i = 0; i < static_cast<size_t>(SourceMapMyGlobalIndices.size()); ++i) {
1475 SourceIndices->replaceLocalValue(i, 0, static_cast<GlobalOrdinal>(SourceMapMyGlobalIndices[i]));
1476 }
1477
1478 // Get the map associated with the vector
1479 auto map = SourceIndices->getMap();
1480
1481 Teuchos::RCP<Tpetra::Export<LocalOrdinal, GlobalOrdinal, Node>> Exporter =
1482 Teuchos::rcp(new Tpetra::Export<LocalOrdinal, GlobalOrdinal, Node>(ContiguousSourceMap, ContiguousTargetMap));
1483
1484 Teuchos::RCP<Tpetra::MultiVector<GlobalOrdinal, LocalOrdinal, GlobalOrdinal, Node>> TargetIndices =
1485 Teuchos::rcp(new Tpetra::MultiVector<GlobalOrdinal, LocalOrdinal, GlobalOrdinal, Node>(ContiguousTargetMap, 1));
1486
1487 TargetIndices->doExport(*SourceIndices, *Exporter, Tpetra::INSERT);
1488
1489 // Create a new map that describes how the Source MultiVector should be laid out so that it has
1490 // the same number of elements on each processor as the TargetMap
1492 Teuchos::ArrayView<const GlobalOrdinal>(TargetIndices->getData(0).getRawPtr(), TargetIndices->getLocalLength()),
1493 Comm);
1494
1495 RedistributeExporter =
1496 Teuchos::rcp(new Tpetra::Export<LocalOrdinal, GlobalOrdinal, Node>(SourceMap, RedistributeMap));
1497
1498 return;
1499}
1500
1501//==============================================================================
1502template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1505 if (SingletonsDetected()) {
1506 Teuchos::RCP<multivector_type> FullLHS = FullProblem()->getLHS();
1507 Teuchos::RCP<multivector_type> FullRHS = FullProblem()->getRHS();
1508
1509 tempX_->putScalar(Scalar(0.0));
1510 tempExportX_->putScalar(Scalar(0.0));
1511
1512 // Inject values that the user computed for the reduced problem into the full solution vector
1513 tempX_->doExport(*ReducedLHS_, *Full2ReducedLHSImporter_, Tpetra::ADD);
1514 FullLHS->update(Scalar(1.0), *tempX_, Scalar(1.0)); // FullLHS = 1.0 * tempX_ + 1.0 * FullLHS
1515
1516 // Next we will use our full solution vector which is populated with pre-filter solution
1517 // values and reduced system solution values to compute the sum of the row contributions
1518 // that must be subtracted to get the post-filter solution values
1519 FullMatrix()->apply(*FullLHS, *tempB_);
1520
1521 // Finally we loop through the local rows that were associated with column singletons and compute the
1522 // solution for these equations.
1523 size_t NumVectors = tempB_->getNumVectors();
1524 bool canRunOnHost = std::is_same_v<typename device_type::memory_space, Kokkos::HostSpace>;
1525 if (run_on_host_ && canRunOnHost) {
1526 for (int k = 0; k < localNumSingletonCols_; k++) {
1527 LocalOrdinal i = ColSingletonRowLIDs_[k];
1528 LocalOrdinal j = ColSingletonColLIDs_[k];
1529 Scalar pivot = ColSingletonPivots_[k];
1530 for (size_t jj = 0; jj < NumVectors; jj++) {
1531 auto tempExportXData = tempExportX_->getDataNonConst(jj);
1532 auto FullRHSData = FullRHS->getData(jj);
1533 auto tempBData = tempB_->getData(jj);
1534 tempExportXData[j] = (FullRHSData[i] - tempBData[i]) / pivot;
1535 }
1536 }
1537 } else {
1538 using functor_type = ComputeFullSolutionFunctor<vector_view_type_int, vector_view_type_scalar,
1539 local_multivector_type, const_local_multivector_type>;
1540 auto localB = tempB_->getLocalViewDevice(Tpetra::Access::ReadOnly);
1541 auto localRHS = FullRHS->getLocalViewDevice(Tpetra::Access::ReadOnly);
1542 auto localX = tempExportX_->getLocalViewDevice(Tpetra::Access::ReadWrite);
1543
1544 functor_type functor(ColSingletonRowLIDs_, ColSingletonColLIDs_, ColSingletonPivots_, localX, localRHS, localB);
1545 Kokkos::parallel_for(
1546 "CrsSingletonFilter_LinearProblem:ComputeFullSolution", range_policy(0, localNumSingletonCols_),
1547 functor);
1548 }
1549
1550 // Finally, insert values from post-solve step and we are done!!!!
1551 auto importer = FullMatrix()->getGraph()->getImporter();
1552 if (importer != Teuchos::null) {
1553 tempX_->doExport(*tempExportX_, *importer, Tpetra::ADD);
1554 } else {
1555 tempX_->update(Scalar(1.0), *tempExportX_, Scalar(0.0));
1556 }
1557
1558 FullLHS->update(Scalar(1.0), *tempX_, Scalar(1.0));
1559 }
1560
1561 return;
1562}
1563//==============================================================================
1564
1565template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1568 localMaxNumRowEntries_ = FullMatrix()->getLocalMaxNumRowEntries();
1569
1570 // Cast to CrsMatrix, if possible. Can save some work.
1571 FullCrsMatrix_ = Teuchos::rcp_dynamic_cast<crs_matrix_type>(FullMatrix());
1572 FullMatrixIsCrsMatrix_ = (FullCrsMatrix() != Teuchos::null); // Pointer is non-null if cast worked
1573
1574 return;
1575}
1576
1577//==============================================================================
1578
1579template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1580void CrsSingletonFilter_LinearProblem<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
1581 GetRow(local_ordinal_type localRow, size_t& NumIndices,
1582 Teuchos::Array<local_ordinal_type>& localIndices) {
1583 // Must get the values, but we ignore them.
1584 size_t maxNumEntries = FullCrsMatrix()->getLocalMaxNumRowEntries();
1585 nonconst_local_inds_host_view_type Indices("indices", maxNumEntries);
1586 nonconst_values_host_view_type Values("values", maxNumEntries);
1587 if (FullMatrixIsCrsMatrix_) { // View of current row
1588 FullCrsMatrix()->getLocalRowCopy(localRow, Indices, Values, NumIndices);
1589 } else { // Copy of current row (we must get the values, but we ignore them)
1590 FullMatrix()->getLocalRowCopy(localRow, Indices, Values, NumIndices);
1591 }
1592 // If LocalRow does not belong to the calling process, then the method sets
1593 // NumIndices to Teuchos::OrdinalTraits<size_t>::invalid(), and does not
1594 // modify localIndices or Values.
1595
1596 // Copy the indices to the output array
1597 localIndices.resize(NumIndices);
1598 for (size_t i = 0; i < NumIndices; ++i) {
1599 localIndices[i] = Indices(i);
1600 }
1601 return;
1602}
1603
1604//==============================================================================
1605
1606template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1608 GetRow(LocalOrdinal Row, size_t& NumIndices, Teuchos::ArrayView<const Scalar>& Values,
1609 Teuchos::ArrayView<const LocalOrdinal>& Indices) {
1610 typename Tpetra::RowMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::local_inds_host_view_type localIndices;
1611 typename Tpetra::RowMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::values_host_view_type rowValues;
1612
1613 if (FullMatrixIsCrsMatrix_) { // View of current row
1614 FullCrsMatrix()->getLocalRowView(Row, localIndices, rowValues);
1615
1616 NumIndices = localIndices.size();
1617 Values = Teuchos::ArrayView<const Scalar>(reinterpret_cast<const Scalar*>(rowValues.data()), rowValues.size());
1618 Indices = Teuchos::ArrayView<const LocalOrdinal>(localIndices.data(), localIndices.size());
1619
1620 } else { // Copy of current row
1621 typename Tpetra::RowMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::nonconst_local_inds_host_view_type localIndicesCopy("localIndicesCopy", FullMatrix()->getLocalMaxNumRowEntries());
1622 typename Tpetra::RowMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::nonconst_values_host_view_type rowValuesCopy("rowValuesCopy", FullMatrix()->getLocalMaxNumRowEntries());
1623
1624 FullMatrix()->getLocalRowCopy(Row, localIndicesCopy, rowValuesCopy, NumIndices);
1625
1626 Values = Teuchos::ArrayView<const Scalar>(reinterpret_cast<const Scalar*>(rowValuesCopy.data()), NumIndices);
1627 Indices = Teuchos::ArrayView<const LocalOrdinal>(localIndicesCopy.data(), NumIndices);
1628 }
1629 return;
1630}
1631
1632//==============================================================================
1633
1634template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1637 LocalOrdinal localRow,
1638 size_t& NumIndices,
1639 Teuchos::ArrayView<const Scalar>& Values,
1640 Teuchos::Array<GlobalOrdinal>& GlobalIndices) {
1641 // Extract the row data (local indices and values)
1642 typename Tpetra::RowMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::local_inds_host_view_type LocalIndices;
1643 typename Tpetra::RowMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::values_host_view_type RowValues;
1644
1645 FullMatrix()->getLocalRowView(localRow, LocalIndices, RowValues);
1646
1647 // Convert local indices to global indices
1648 NumIndices = LocalIndices.size();
1649 GlobalIndices.resize(NumIndices); // Resize the array to hold global indices
1650 for (size_t j = 0; j < NumIndices; ++j) {
1651 GlobalIndices[j] = FullMatrixColMap()->getGlobalElement(LocalIndices[j]);
1652 }
1653 // Copy values into the provided ArrayView
1654 Values = Teuchos::ArrayView<const Scalar>(reinterpret_cast<const Scalar*>(RowValues.data()), RowValues.size());
1655
1656 return;
1657}
1658
1659//==============================================================================
1660
1661template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1663 CreatePostSolveArrays(vector_type_LO localRowIDofSingletonCol,
1664 vector_type_LO ColProfiles,
1665 vector_type_LO NewColProfiles,
1666 vector_type_LO ColHasRowWithSingleton) {
1667 const char tfecfFuncName[] = "CreatePostSolveArrays: ";
1668
1669 if (localNumSingletonCols_ == 0) return; // Nothing to do
1670
1671 // We will need these arrays for the post-solve phase
1672 Kokkos::resize(ColSingletonRowLIDs_, localNumSingletonCols_);
1673 Kokkos::resize(ColSingletonColLIDs_, localNumSingletonCols_);
1674 Kokkos::resize(ColSingletonPivotLIDs_, localNumSingletonCols_);
1675 Kokkos::resize(ColSingletonPivots_, localNumSingletonCols_);
1676
1677 int NumMyColSingletonstmp = 0;
1678
1679 // Check if ColSingletonRowLIDs_, ColSingletonColLIDs_ can be accessed on host
1680 bool canRunOnHost = std::is_same_v<typename device_type::memory_space, Kokkos::HostSpace>;
1681 if (run_on_host_ && canRunOnHost) {
1682 // Register singleton columns (that were not already counted as singleton rows)
1683 // Check to see if any columns disappeared because all associated rows were eliminated
1684 local_ordinal_type localNumCols = FullMatrix()->getLocalNumCols();
1685 auto localRowIDofSingletonColData = localRowIDofSingletonCol.getLocalViewHost(Tpetra::Access::ReadOnly);
1686 auto ColProfilesData = ColProfiles.getLocalViewHost(Tpetra::Access::ReadOnly);
1687 auto RowMapColors_Data = RowMapColors_->getLocalViewHost(Tpetra::Access::ReadOnly);
1688 auto NewColProfilesData = NewColProfiles.getLocalViewHost(Tpetra::Access::ReadOnly);
1689 auto ColHasRowWithSingletonData = ColHasRowWithSingleton.getLocalViewHost(Tpetra::Access::ReadOnly);
1690 auto ColMapColors_Data = ColMapColors_->getLocalViewHost(Tpetra::Access::ReadWrite);
1691
1692 for (int j = 0; j < localNumCols; j++) {
1693 int i = localRowIDofSingletonColData(j, 0);
1694 if (ColProfilesData(j, 0) == 1 && RowMapColors_Data(i, 0) != 1) {
1695 ColSingletonRowLIDs_[NumMyColSingletonstmp] = i;
1696 ColSingletonColLIDs_[NumMyColSingletonstmp] = j;
1697 NumMyColSingletonstmp++;
1698 }
1699 // Also check for columns that were eliminated implicitly by
1700 // having all associated row eliminated
1701 else if (NewColProfilesData(j, 0) == 0 && ColHasRowWithSingletonData(j, 0) != 1 && RowMapColors_Data(i, 0) == 0) {
1702 ColMapColors_Data(j, 0) = 1;
1703 }
1704 }
1705
1706 } else {
1707 {
1708 // Register singleton columns (that were not already counted as singleton rows)
1709 // Check to see if any columns disappeared because all associated rows were eliminated
1710 local_ordinal_type localNumCols = FullMatrix()->getLocalNumCols();
1711 auto localRowIDofSingletonColData = localRowIDofSingletonCol.getLocalViewDevice(Tpetra::Access::ReadOnly);
1712 auto ColProfilesData = ColProfiles.getLocalViewDevice(Tpetra::Access::ReadOnly);
1713 auto RowMapColors_Data = RowMapColors_->getLocalViewDevice(Tpetra::Access::ReadOnly);
1714 auto NewColProfilesData = NewColProfiles.getLocalViewDevice(Tpetra::Access::ReadOnly);
1715 auto ColHasRowWithSingletonData = ColHasRowWithSingleton.getLocalViewDevice(Tpetra::Access::ReadOnly);
1716 auto ColMapColors_Data = ColMapColors_->getLocalViewDevice(Tpetra::Access::ReadWrite);
1717
1718 // counter to be atomic-incremented
1719 Kokkos::View<int*, execution_space> numSingletonCols("numSingletonCols", 1);
1720 Kokkos::deep_copy(numSingletonCols, 0);
1721
1722 // launch functor
1723 CreatePostSolveArraysFunctor functor(localRowIDofSingletonColData,
1724 ColSingletonRowLIDs_, ColSingletonColLIDs_,
1725 NewColProfilesData, ColProfilesData, ColHasRowWithSingletonData,
1726 ColMapColors_Data, RowMapColors_Data, numSingletonCols);
1727 Kokkos::parallel_for(
1728 "CrsSingletonFilter_LinearProblem:CreatePostSolveArrays", range_policy(0, localNumCols),
1729 functor);
1730
1731 // get the counter
1732 auto h_numSingletonCols = Kokkos::create_mirror_view(numSingletonCols);
1733 Kokkos::deep_copy(h_numSingletonCols, numSingletonCols);
1734 NumMyColSingletonstmp = h_numSingletonCols(0);
1735 }
1736 }
1737
1738 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(NumMyColSingletonstmp != localNumSingletonCols_,
1739 std::runtime_error, "Sanity Check " + std::to_string(NumMyColSingletonstmp) + " vs " + std::to_string(localNumSingletonCols_) + " (CreatePostSolveArrays).");
1740
1741 Kokkos::Experimental::sort_by_key(execution_space(), ColSingletonRowLIDs_, ColSingletonColLIDs_);
1742
1743 return;
1744}
1745
1746} // namespace Tpetra
1747
1748//
1749// Explicit instantiation macro
1750//
1751// Must be expanded from within the Tpetra namespace!
1752//
1753
1754#define TPETRA_CRSSINGLETONFILTER_INSTANT(SCALAR, LO, GO, NODE) \
1755 template class CrsSingletonFilter_LinearProblem<SCALAR, LO, GO, NODE>;
1756
1757#endif // TPETRA_CRSSINGLETONFILTER_LINEARPROBLEM_DEF_HPP
Declaration of the Tpetra::CrsSingletonFilter_LinearProblem class.
size_t getLocalNumCols() const override
The number of columns connected to the locally owned rows of this matrix.
A class for explicitly eliminating matrix rows and columns from a LinearProblem.
int NumSingletons() const
Return total number of singletons detected.
bool SingletonsDetected() const
Returns true if singletons were detected in this matrix (must be called after Analyze() to be effecti...
Teuchos::RCP< row_matrix_type > FullMatrix() const
! Returns RCP to Tpetra::RowMatrix from full problem.
double RatioOfNonzeros() const
Returns ratio of reduced system to full system nonzero count, returns -1.0 if reduced problem not con...
Teuchos::RCP< const map_type > ReducedMatrixDomainMap() const
Returns RCP to Tpetra::Map describing the domain map for the reduced system.
void ConstructReducedProblem(const Teuchos::RCP< linear_problem_type > &Problem)
Return a reduced linear problem based on results of Analyze().
Teuchos::RCP< linear_problem_type > FullProblem() const
Returns RCP to the original full (unreduced) Tpetra::LinearProblem.
Teuchos::RCP< crs_matrix_type > ReducedMatrix() const
Returns RCP to Tpetra::CrsMatrix from reduced problem.
void Analyze(const Teuchos::RCP< row_matrix_type > &FullMatrix)
Analyze the input matrix, removing row/column pairs that have singletons.
NewType construct()
Construction of new object as a result of the transform.
void UpdateReducedProblem(const Teuchos::RCP< linear_problem_type > &Problem)
Update a reduced linear problem using new values.
Teuchos::RCP< const map_type > ReducedMatrixRowMap() const
Returns RCP to Tpetra::Map describing the reduced system row distribution.
Teuchos::RCP< const map_type > ReducedMatrixColMap() const
Returns RCP to Tpetra::Map describing the reduced system column distribution.
CrsSingletonFilter_LinearProblem(bool run_on_host=false, bool verbose=false)
Constructor.
local_ordinal_type localNumSingletonCols_
Number of singleton columns not eliminated by singleton rows.
void ComputeFullSolution()
Compute a solution for the full problem using the solution of the reduced problem,...
NewType operator()(const OriginalType &originalLinearProblem)
Analysis of transform operation on original object and construction of new object.
void analyze(const OriginalType &originalLinearProblem)
Initial analysis phase of transform.
Teuchos::RCP< linear_problem_type > ReducedProblem() const
Returns RCP to the derived reduced Tpetra::LinearProblem.
Teuchos::RCP< const map_type > ReducedMatrixRangeMap() const
Returns RCP to Tpetra::Map describing the range map for the reduced system.
double RatioOfDimensions() const
Returns ratio of reduced system to full system dimensions, returns -1.0 if reduced problem not constr...
void doImport(const SrcDistObject &source, const Import< LocalOrdinal, GlobalOrdinal, Node > &importer, const CombineMode CM, const bool restrictedMode=false)
void doExport(const SrcDistObject &source, const Export< LocalOrdinal, GlobalOrdinal, Node > &exporter, const CombineMode CM, const bool restrictedMode=false)
Communication plan for data redistribution from a (possibly) multiply-owned to a uniquely-owned distr...
Class that encapulates linear problem (Ax = b).
One or more distributed dense vectors.
typename KokkosKernels::ArithTraits< Scalar >::val_type impl_scalar_type
The type used internally in place of Scalar.
dual_view_type::t_dev::const_type getLocalViewDevice(Access::ReadOnlyStruct) const
Return a read-only, up-to-date view of this MultiVector's local data on device. This requires that th...
dual_view_type::t_host::const_type getLocalViewHost(Access::ReadOnlyStruct) const
Return a read-only, up-to-date view of this MultiVector's local data on host. This requires that ther...
void putScalar(const Scalar &value)
Set all values in the multivector with the given value.
void update(const Scalar &alpha, const MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &A, const Scalar &beta)
Update: this = beta*this + alpha*A.
mag_type normInf() const
Return the infinity-norm of this Vector.
Namespace Tpetra contains the class and methods constituting the Tpetra library.
size_t global_size_t
Global size_t object.
Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > createNonContigMapWithNode(const Teuchos::ArrayView< const GlobalOrdinal > &elementList, const Teuchos::RCP< const Teuchos::Comm< int > > &comm)
Nonmember constructor for a noncontiguous Map with a user-specified, possibly nondefault Kokkos Node ...
@ ADD
Sum new values.
@ ABSMAX
Replace old value with maximum of magnitudes of old and new values.
@ INSERT
Insert new values that don't currently exist.