Tpetra parallel linear algebra Version of the Day
Loading...
Searching...
No Matches
Tpetra_leftAndOrRightScaleCrsMatrix_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_LEFTANDORRIGHTSCALECRSMATRIX_DEF_HPP
11#define TPETRA_LEFTANDORRIGHTSCALECRSMATRIX_DEF_HPP
12
19
20#include "Tpetra_CrsMatrix.hpp"
21#include "Tpetra_Vector.hpp"
25#include "Teuchos_TestForException.hpp"
26
27namespace Tpetra {
28
29template <class SC, class LO, class GO, class NT>
31 const Kokkos::View<
32 const typename KokkosKernels::ArithTraits<SC>::mag_type*,
33 typename NT::device_type>& rowScalingFactors,
34 const Kokkos::View<
35 const typename KokkosKernels::ArithTraits<SC>::mag_type*,
36 typename NT::device_type>& colScalingFactors,
37 const bool leftScale,
38 const bool rightScale,
39 const bool assumeSymmetric,
40 const EScaling scaling) {
41 if (!leftScale && !rightScale) {
42 return;
43 }
44
45 const bool A_fillComplete_on_input = A.isFillComplete();
46 if (!A_fillComplete_on_input) {
47 // Make sure that A has a valid local matrix. It might not if it
48 // was not created with a local matrix, and if fillComplete has
49 // never been called on it before. A never-initialized (and thus
50 // invalid) local matrix has zero rows, because it was default
51 // constructed.
52 auto A_lcl = A.getLocalMatrixDevice();
53 const LO lclNumRows =
54 static_cast<LO>(A.getRowMap()->getLocalNumElements());
55 TEUCHOS_TEST_FOR_EXCEPTION(A_lcl.numRows() != lclNumRows, std::invalid_argument,
56 "leftAndOrRightScaleCrsMatrix: Local matrix is not valid. "
57 "This means that A was not created with a local matrix, "
58 "and that fillComplete has never yet been called on A before. "
59 "Please call fillComplete on A at least once first "
60 "before calling this method.");
61 } else {
62 A.resumeFill();
63 }
64
65 const bool divide = scaling == SCALING_DIVIDE;
66 if (leftScale) {
68 rowScalingFactors,
69 assumeSymmetric,
70 divide);
71 }
72 if (rightScale) {
74 colScalingFactors,
75 assumeSymmetric,
76 divide);
77 }
78
79 if (A_fillComplete_on_input) { // put A back how we found it
80 Teuchos::RCP<Teuchos::ParameterList> params = Teuchos::parameterList();
81 params->set("No Nonlocal Changes", true);
82 A.fillComplete(A.getDomainMap(), A.getRangeMap(), params);
83 }
84}
85
86template <class SC, class LO, class GO, class NT>
88 const Tpetra::Vector<
89 typename KokkosKernels::ArithTraits<SC>::mag_type,
90 LO, GO, NT>& rowScalingFactors,
91 const Tpetra::Vector<
92 typename KokkosKernels::ArithTraits<SC>::mag_type,
93 LO, GO, NT>& colScalingFactors,
94 const bool leftScale,
95 const bool rightScale,
96 const bool assumeSymmetric,
97 const EScaling scaling) {
98 using device_type = typename NT::device_type;
99 using dev_memory_space = typename device_type::memory_space;
100 using mag_type = typename KokkosKernels::ArithTraits<SC>::mag_type;
101 const char prefix[] = "leftAndOrRightScaleCrsMatrix: ";
102 const bool debug = ::Tpetra::Details::Behavior::debug();
103
104 Kokkos::View<const mag_type*, device_type> row_lcl;
105 Kokkos::View<const mag_type*, device_type> col_lcl;
106 if (leftScale) {
107 if (debug) {
108 const bool same = rowScalingFactors.getMap()->isSameAs(*(A.getRowMap()));
109 TEUCHOS_TEST_FOR_EXCEPTION(!same, std::invalid_argument, prefix << "rowScalingFactors's Map "
110 "must be the same as the CrsMatrix's row Map. If you see this "
111 "message, it's likely that you are using a range Map Vector and that "
112 "the CrsMatrix's row Map is overlapping.");
113 }
114 // if (rowScalingFactors.template need_sync<dev_memory_space> ()) {
115 // const_cast<vec_type&> (rowScalingFactors).template sync<dev_memory_space> ();
116 // }
117 auto row_lcl_2d = rowScalingFactors.template getLocalView<dev_memory_space>(Access::ReadOnly);
118 row_lcl = Kokkos::subview(row_lcl_2d, Kokkos::ALL(), 0);
119 }
120 if (rightScale) {
121 if (debug) {
122 const bool same = colScalingFactors.getMap()->isSameAs(*(A.getColMap()));
123 TEUCHOS_TEST_FOR_EXCEPTION(!same, std::invalid_argument, prefix << "colScalingFactors's Map "
124 "must be the same as the CrsMatrix's column Map. If you see this "
125 "message, it's likely that you are using a domain Map Vector.");
126 }
127 // if (colScalingFactors.template need_sync<dev_memory_space> ()) {
128 // const_cast<vec_type&> (colScalingFactors).template sync<dev_memory_space> ();
129 // }
130 auto col_lcl_2d = colScalingFactors.template getLocalView<dev_memory_space>(Access::ReadOnly);
131 col_lcl = Kokkos::subview(col_lcl_2d, Kokkos::ALL(), 0);
132 }
133
134 leftAndOrRightScaleCrsMatrix(A, row_lcl, col_lcl, leftScale, rightScale,
135 assumeSymmetric, scaling);
136}
137
138} // namespace Tpetra
139
140//
141// Explicit instantiation macro
142//
143// Must be expanded from within the Tpetra namespace!
144//
145
146#define TPETRA_LEFTANDORRIGHTSCALECRSMATRIX_INSTANT(SC, LO, GO, NT) \
147 template void \
148 leftAndOrRightScaleCrsMatrix( \
149 Tpetra::CrsMatrix<SC, LO, GO, NT>& A, \
150 const Kokkos::View< \
151 const KokkosKernels::ArithTraits<SC>::mag_type*, \
152 NT::device_type>& rowScalingFactors, \
153 const Kokkos::View< \
154 const KokkosKernels::ArithTraits<SC>::mag_type*, \
155 NT::device_type>& colScalingFactors, \
156 const bool leftScale, \
157 const bool rightScale, \
158 const bool assumeSymmetric, \
159 const EScaling scaling); \
160 \
161 template void \
162 leftAndOrRightScaleCrsMatrix( \
163 Tpetra::CrsMatrix<SC, LO, GO, NT>& A, \
164 const Tpetra::Vector<KokkosKernels::ArithTraits<SC>::mag_type, LO, GO, NT>& rowScalingFactors, \
165 const Tpetra::Vector<KokkosKernels::ArithTraits<SC>::mag_type, LO, GO, NT>& colScalingFactors, \
166 const bool leftScale, \
167 const bool rightScale, \
168 const bool assumeSymmetric, \
169 const EScaling scaling);
170
171#endif // TPETRA_LEFTANDORRIGHTSCALECRSMATRIX_DEF_HPP
Declaration of Tpetra::Details::Behavior, a class that describes Tpetra's behavior.
Declaration and definition of Tpetra::Details::leftScaleLocalCrsMatrix.
Declaration and definition of Tpetra::Details::rightScaleLocalCrsMatrix.
Sparse matrix that presents a row-oriented interface that lets users read or modify entries.
Teuchos::RCP< const map_type > getDomainMap() const override
The domain Map of this matrix.
Teuchos::RCP< const map_type > getRangeMap() const override
The range Map of this matrix.
TPETRA_DETAILS_ALWAYS_INLINE local_matrix_device_type getLocalMatrixDevice() const
The local sparse matrix.
Teuchos::RCP< const map_type > getColMap() const override
The Map that describes the column distribution in this matrix.
void fillComplete(const Teuchos::RCP< const map_type > &domainMap, const Teuchos::RCP< const map_type > &rangeMap, const Teuchos::RCP< Teuchos::ParameterList > &params=Teuchos::null)
Tell the matrix that you are done changing its structure or values, and that you are ready to do comp...
bool isFillComplete() const override
Whether the matrix is fill complete.
Teuchos::RCP< const map_type > getRowMap() const override
The Map that describes the row distribution in this matrix.
void resumeFill(const Teuchos::RCP< Teuchos::ParameterList > &params=Teuchos::null)
Resume operations that may change the values or structure of the matrix.
static bool debug()
Whether Tpetra is in debug mode.
A distributed dense vector.
void leftScaleLocalCrsMatrix(const LocalSparseMatrixType &A_lcl, const ScalingFactorsViewType &scalingFactors, const bool assumeSymmetric, const bool divide=true)
Left-scale a KokkosSparse::CrsMatrix.
void rightScaleLocalCrsMatrix(const LocalSparseMatrixType &A_lcl, const ScalingFactorsViewType &scalingFactors, const bool assumeSymmetric, const bool divide=true)
Right-scale a KokkosSparse::CrsMatrix.
Namespace Tpetra contains the class and methods constituting the Tpetra library.
EScaling
Whether "scaling" a matrix means multiplying or dividing its entries.
void leftAndOrRightScaleCrsMatrix(Tpetra::CrsMatrix< SC, LO, GO, NT > &A, const Kokkos::View< const typename KokkosKernels::ArithTraits< SC >::mag_type *, typename NT::device_type > &rowScalingFactors, const Kokkos::View< const typename KokkosKernels::ArithTraits< SC >::mag_type *, typename NT::device_type > &colScalingFactors, const bool leftScale, const bool rightScale, const bool assumeSymmetric, const EScaling scaling)
Left-scale and/or right-scale (in that order) the entries of the input Tpetra::CrsMatrix A.