Tpetra parallel linear algebra Version of the Day
Loading...
Searching...
No Matches
Tpetra_Details_leftScaleLocalCrsMatrix.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_DETAILS_LEFTSCALELOCALCRSMATRIX_HPP
11#define TPETRA_DETAILS_LEFTSCALELOCALCRSMATRIX_HPP
12
19
20#include "TpetraCore_config.h"
21#include "Kokkos_Core.hpp"
22#include "KokkosKernels_ArithTraits.hpp"
23#include <type_traits>
24
25namespace Tpetra {
26namespace Details {
27
36template <class LocalSparseMatrixType,
37 class ScalingFactorsViewType,
38 const bool divide>
40 public:
41 using val_type =
42 typename std::remove_const<typename LocalSparseMatrixType::value_type>::type;
43 using mag_type = typename ScalingFactorsViewType::non_const_value_type;
44 static_assert(ScalingFactorsViewType::rank == 1,
45 "scalingFactors must be a rank-1 Kokkos::View.");
46 using device_type = typename LocalSparseMatrixType::device_type;
47 using LO = typename LocalSparseMatrixType::ordinal_type;
48 using policy_type = Kokkos::TeamPolicy<typename device_type::execution_space, LO>;
49
60 const bool assumeSymmetric)
61 : A_lcl_(A_lcl)
62 , scalingFactors_(scalingFactors)
63 , assumeSymmetric_(assumeSymmetric) {}
64
66 operator()(const typename policy_type::member_type& team) const {
67 using KAM = KokkosKernels::ArithTraits<mag_type>;
68
69 const LO lclRow = team.league_rank();
70 const mag_type curRowNorm = scalingFactors_(lclRow);
71 // Users are responsible for any divisions or multiplications by
72 // zero.
73 const mag_type scalingFactor = assumeSymmetric_ ? KAM::sqrt(curRowNorm) : curRowNorm;
74 auto curRow = A_lcl_.row(lclRow);
75 const LO numEnt = curRow.length;
76 Kokkos::parallel_for(Kokkos::TeamThreadRange(team, numEnt), [&](const LO k) {
77 if (divide) { // constexpr, so should get compiled out
78 curRow.value(k) = curRow.value(k) / scalingFactor;
79 } else {
80 curRow.value(k) = curRow.value(k) * scalingFactor;
81 }
82 });
83 }
84
85 private:
86 LocalSparseMatrixType A_lcl_;
87 typename ScalingFactorsViewType::const_type scalingFactors_;
88 bool assumeSymmetric_;
89};
90
104template <class LocalSparseMatrixType, class ScalingFactorsViewType>
107 const bool assumeSymmetric,
108 const bool divide = true) {
109 using device_type = typename LocalSparseMatrixType::device_type;
110 using execution_space = typename device_type::execution_space;
111 using LO = typename LocalSparseMatrixType::ordinal_type;
112 using policy_type = Kokkos::TeamPolicy<execution_space, LO>;
113
114 const LO lclNumRows = A_lcl.numRows();
115 if (divide) {
116 using functor_type =
118 typename ScalingFactorsViewType::const_type, true>;
119 functor_type functor(A_lcl, scalingFactors, assumeSymmetric);
120 Kokkos::parallel_for("leftScaleLocalCrsMatrix",
121 policy_type(lclNumRows, Kokkos::AUTO), functor);
122 } else {
123 using functor_type =
125 typename ScalingFactorsViewType::const_type, false>;
126 functor_type functor(A_lcl, scalingFactors, assumeSymmetric);
127 Kokkos::parallel_for("leftScaleLocalCrsMatrix",
128 policy_type(lclNumRows, Kokkos::AUTO), functor);
129 }
130}
131
132} // namespace Details
133} // namespace Tpetra
134
135#endif // TPETRA_DETAILS_LEFTSCALELOCALCRSMATRIX_HPP
Struct that holds views of the contents of a CrsMatrix.
Kokkos::parallel_for functor that left-scales a KokkosSparse::CrsMatrix.
LeftScaleLocalCrsMatrix(const LocalSparseMatrixType &A_lcl, const ScalingFactorsViewType &scalingFactors, const bool assumeSymmetric)
Implementation details of Tpetra.
void leftScaleLocalCrsMatrix(const LocalSparseMatrixType &A_lcl, const ScalingFactorsViewType &scalingFactors, const bool assumeSymmetric, const bool divide=true)
Left-scale a KokkosSparse::CrsMatrix.
Namespace Tpetra contains the class and methods constituting the Tpetra library.