Tpetra parallel linear algebra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Tpetra_replaceDiagonalCrsMatrix_def.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Tpetra: Templated Linear Algebra Services Package
5 // Copyright (2008) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ************************************************************************
40 // @HEADER
41 
42 #ifndef TPETRA_REPLACEDIAGONALCRSMATRIX_DEF_HPP
43 #define TPETRA_REPLACEDIAGONALCRSMATRIX_DEF_HPP
44 
47 
49 #include "Tpetra_CrsMatrix.hpp"
50 #include "Tpetra_Vector.hpp"
51 
52 namespace Tpetra {
53 
54 template<class SC, class LO, class GO, class NT>
55 LO
56 replaceDiagonalCrsMatrix (CrsMatrix<SC, LO, GO, NT>& matrix,
57  const Vector<SC, LO, GO, NT>& newDiag) {
58 
59  using map_type = Map<LO, GO, NT>;
60  using crs_matrix_type = CrsMatrix<SC, LO, GO, NT>;
61  // using vec_type = ::Tpetra::Vector<SC, LO, GO, NT>;
62 
63  const LO oneLO = Teuchos::OrdinalTraits<LO>::one();
64 
65  // Count number of successfully replaced diagonal entries
66  LO numReplacedDiagEntries = 0;
67 
68  // Extract some useful data
69  auto rowMapPtr = matrix.getRowMap();
70  if (rowMapPtr.is_null() || rowMapPtr->getComm().is_null()) {
71  // Processes on which the row Map or its communicator is null
72  // don't participate. Users shouldn't even call this method on
73  // those processes.
74  return numReplacedDiagEntries;
75  }
76  auto colMapPtr = matrix.getColMap();
77 
78  TEUCHOS_TEST_FOR_EXCEPTION
79  (colMapPtr.get () == nullptr, std::invalid_argument,
80  "replaceDiagonalCrsMatrix: "
81  "Input matrix A must have a nonnull column Map.");
82 
83  const map_type& rowMap = *rowMapPtr;
84  const map_type& colMap = *colMapPtr;
85  const LO myNumRows = static_cast<LO>(matrix.getNodeNumRows());
86  const bool isFillCompleteOnInput = matrix.isFillComplete();
87 
89  TEUCHOS_TEST_FOR_EXCEPTION(!rowMap.isSameAs(*newDiag.getMap()), std::runtime_error,
90  "Row map of matrix and map of input vector do not match.");
91  }
92 
93  crs_matrix_type::execution_space::fence(); // for UVM's sake
94 
95  if (isFillCompleteOnInput)
96  matrix.resumeFill();
97 
98  Teuchos::ArrayRCP<const SC> newDiagData = newDiag.getVector(0)->getData();
99  LO numReplacedEntriesPerRow = 0;
100 
101  // Loop over all local rows to replace the diagonal entry row by row
102  for (LO lclRowInd = 0; lclRowInd < myNumRows; ++lclRowInd) {
103 
104  // Get row and column indices of the diagonal entry
105  const GO gblInd = rowMap.getGlobalElement(lclRowInd);
106  const LO lclColInd = colMap.getLocalElement(gblInd);
107 
108  const SC vals[] = {static_cast<SC>(newDiagData[lclRowInd])};
109  const LO cols[] = {lclColInd};
110 
111  // Do the actual replacement of the diagonal element
112  numReplacedEntriesPerRow = matrix.replaceLocalValues(lclRowInd, oneLO, vals, cols);
113 
114  // Check for success of replacement
115  if (numReplacedEntriesPerRow == oneLO) {
116  ++numReplacedDiagEntries;
117  } else {
118  TEUCHOS_TEST_FOR_EXCEPTION(true, std::runtime_error,
119  "Number of replaced entries in this row is not equal to one. "
120  "It has to be exactly one, since we want to replace the diagonal element.");
121  }
122  }
123 
124  if (isFillCompleteOnInput)
125  matrix.fillComplete();
126 
127  return numReplacedDiagEntries;
128 }
129 
130 } // namespace Tpetra
131 //
132 // Explicit instantiation macro
133 //
134 // Must be expanded from within the Tpetra namespace!
135 //
136 
137 #define TPETRA_REPLACEDIAGONALCRSMATRIX_INSTANT(SCALAR,LO,GO,NODE) \
138  \
139  template \
140  LO replaceDiagonalCrsMatrix( \
141  CrsMatrix<SCALAR, LO, GO, NODE>& matrix, \
142  const Vector<SCALAR, LO, GO, NODE>& newDiag); \
143  \
144 
145 #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.