Tpetra parallel linear algebra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Tpetra_replaceDiagonalCrsMatrix_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_REPLACEDIAGONALCRSMATRIX_DEF_HPP
11 #define TPETRA_REPLACEDIAGONALCRSMATRIX_DEF_HPP
12 
15 
17 #include "Tpetra_CrsMatrix.hpp"
18 #include "Tpetra_Vector.hpp"
19 
20 namespace Tpetra {
21 
22 template<class SC, class LO, class GO, class NT>
23 LO
24 replaceDiagonalCrsMatrix (CrsMatrix<SC, LO, GO, NT>& matrix,
25  const Vector<SC, LO, GO, NT>& newDiag) {
26 
27  using map_type = Map<LO, GO, NT>;
28  using crs_matrix_type = CrsMatrix<SC, LO, GO, NT>;
29  // using vec_type = ::Tpetra::Vector<SC, LO, GO, NT>;
30 
31  const LO oneLO = Teuchos::OrdinalTraits<LO>::one();
32 
33  // Count number of successfully replaced diagonal entries
34  LO numReplacedDiagEntries = 0;
35 
36  // Extract some useful data
37  auto rowMapPtr = matrix.getRowMap();
38  if (rowMapPtr.is_null() || rowMapPtr->getComm().is_null()) {
39  // Processes on which the row Map or its communicator is null
40  // don't participate. Users shouldn't even call this method on
41  // those processes.
42  return numReplacedDiagEntries;
43  }
44  auto colMapPtr = matrix.getColMap();
45 
46  TEUCHOS_TEST_FOR_EXCEPTION
47  (colMapPtr.get () == nullptr, std::invalid_argument,
48  "replaceDiagonalCrsMatrix: "
49  "Input matrix must have a nonnull column Map.");
50 
51  const map_type& rowMap = *rowMapPtr;
52  const map_type& colMap = *colMapPtr;
53  const LO myNumRows = static_cast<LO>(matrix.getLocalNumRows());
54  const bool isFillCompleteOnInput = matrix.isFillComplete();
55 
57  TEUCHOS_TEST_FOR_EXCEPTION(!rowMap.isSameAs(*newDiag.getMap()), std::runtime_error,
58  "Row map of matrix and map of input vector do not match.");
59  }
60 
61  // KJ: This fence is necessary for UVM. Views used in the row map and colmap
62  // can use UVM and they are accessed in the following routine. So, we need to
63  // make sure that the values are available for touching in host.
64  typename crs_matrix_type::execution_space().fence();
65 
66  if (isFillCompleteOnInput)
67  matrix.resumeFill();
68 
69  Teuchos::ArrayRCP<const SC> newDiagData = newDiag.getVector(0)->getData();
70  LO numReplacedEntriesPerRow = 0;
71 
72  auto invalid = Teuchos::OrdinalTraits<LO>::invalid();
73 
74  // Loop over all local rows to replace the diagonal entry row by row
75  for (LO lclRowInd = 0; lclRowInd < myNumRows; ++lclRowInd) {
76 
77  // Get row and column indices of the diagonal entry
78  const GO gblInd = rowMap.getGlobalElement(lclRowInd);
79  const LO lclColInd = colMap.getLocalElement(gblInd);
80 
81  // If the row map is not one-to-one, the diagonal may not be on this proc.
82  // Skip this row; some processor will have the diagonal for this row.
83  if (lclColInd == invalid) continue;
84 
85  const SC vals[] = {static_cast<SC>(newDiagData[lclRowInd])};
86  const LO cols[] = {lclColInd};
87 
88  // Do the actual replacement of the diagonal element, if on this proc
89  numReplacedEntriesPerRow = matrix.replaceLocalValues(lclRowInd, oneLO,
90  vals, cols);
91 
92  // Check for success of replacement.
93  // numReplacedEntriesPerRow is one if the diagonal was replaced.
94  // numReplacedEntriesPerRow is zero if the diagonal is not on
95  // this processor. For example, in a 2D matrix distribution, gblInd may
96  // be in both the row and column map, but the diagonal may not be on
97  // this processor.
98  if (numReplacedEntriesPerRow == oneLO) {
99  ++numReplacedDiagEntries;
100  }
101  }
102 
103  if (isFillCompleteOnInput)
104  matrix.fillComplete();
105 
106  return numReplacedDiagEntries;
107 }
108 
109 } // namespace Tpetra
110 //
111 // Explicit instantiation macro
112 //
113 // Must be expanded from within the Tpetra namespace!
114 //
115 
116 #define TPETRA_REPLACEDIAGONALCRSMATRIX_INSTANT(SCALAR,LO,GO,NODE) \
117  \
118  template \
119  LO replaceDiagonalCrsMatrix( \
120  CrsMatrix<SCALAR, LO, GO, NODE>& matrix, \
121  const Vector<SCALAR, LO, GO, NODE>& newDiag); \
122  \
123 
124 #endif // #ifndef TPETRA_REPLACEDIAGONALCRSMATRIX_DEF_HPP
static bool debug()
Whether Tpetra is in debug mode.
LO replaceDiagonalCrsMatrix(::Tpetra::CrsMatrix< SC, LO, GO, NT > &matrix, const ::Tpetra::Vector< SC, LO, GO, NT > &newDiag)
Replace diagonal entries of an input Tpetra::CrsMatrix matrix with values given in newDiag...
Declaration of Tpetra::Details::Behavior, a class that describes Tpetra&#39;s behavior.