10#ifndef IFPACK2_DETAILS_INVERSEDIAGONALKERNEL_DEF_HPP
11#define IFPACK2_DETAILS_INVERSEDIAGONALKERNEL_DEF_HPP
13#include "Tpetra_CrsMatrix.hpp"
14#include "Tpetra_MultiVector.hpp"
15#include "Tpetra_Operator.hpp"
16#include "Tpetra_Vector.hpp"
17#include "Tpetra_Export_decl.hpp"
18#include "Tpetra_Import_decl.hpp"
19#if KOKKOS_VERSION >= 40799
20#include "KokkosKernels_ArithTraits.hpp"
22#include "Kokkos_ArithTraits.hpp"
24#include "Teuchos_Assert.hpp"
26#include "KokkosSparse_spmv_impl.hpp"
35template <
class DVector,
41 using execution_space =
typename AMatrix::execution_space;
42 using LO =
typename AMatrix::non_const_ordinal_type;
43 using value_type =
typename AMatrix::non_const_value_type;
44 using team_policy =
typename Kokkos::TeamPolicy<execution_space>;
45 using team_member =
typename team_policy::member_type;
46#if KOKKOS_VERSION >= 40799
47 using ATV = KokkosKernels::ArithTraits<value_type>;
49 using ATV = Kokkos::ArithTraits<value_type>;
52 using magnitude_type =
typename ATV::mag_type;
53#if KOKKOS_VERSION >= 40799
54 using MATV = KokkosKernels::ArithTraits<magnitude_type>;
56 using MATV = Kokkos::ArithTraits<magnitude_type>;
62 magnitude_type m_L1Eta;
63 magnitude_type m_MinDiagonalValue;
75 const size_t numRows = m_A.numRows();
82 void operator()(
const LO
lclRow)
const {
83 const size_t INV = Tpetra::Details::OrdinalTraits<size_t>::invalid();
84 const value_type
one = ATV::one();
87 m_d(
lclRow, 0) = ATV::zero();
102 const magnitude_type
half = MATV::one() / (MATV::one() + MATV::one());
103 const LO numRows =
static_cast<LO
>(m_A.numRows());
118 if (ATV::magnitude(
d) <= m_MinDiagonalValue)
119 d = m_MinDiagonalValue;
130template <
class TpetraOperatorType>
136template <
class TpetraOperatorType>
137void InverseDiagonalKernel<TpetraOperatorType>::
138 setMatrix(
const Teuchos::RCP<const operator_type>& A) {
139 if (A_op_.get() != A.get()) {
142 using Teuchos::rcp_dynamic_cast;
143 A_crs_ = rcp_dynamic_cast<const crs_matrix_type>(A);
145 TEUCHOS_TEST_FOR_EXCEPTION(A_crs_.is_null(), std::logic_error,
146 "Ifpack2::Details::InverseDiagonalKernel: operator A must be a Tpetra::CrsMatrix.");
148 const size_t lclNumRows = A_crs_->getRowMap()->getLocalNumElements();
150 if (offsets_.extent(0) < lclNumRows) {
151 using Kokkos::view_alloc;
152 using Kokkos::WithoutInitializing;
153 using offsets_view_type = Kokkos::View<size_t*, device_type>;
155 offsets_ = offsets_view_type();
156 auto howAlloc = view_alloc(
"offsets", WithoutInitializing);
157 offsets_ = offsets_view_type(howAlloc, lclNumRows);
160 A_crs_->getCrsGraph()->getLocalDiagOffsets(offsets_);
164template <
class TpetraOperatorType>
165void InverseDiagonalKernel<TpetraOperatorType>::
166 compute(vector_type& D_inv,
167 bool do_l1, magnitude_type L1Eta,
168 bool fixTinyDiagEntries, magnitude_type MinDiagonalValue) {
170 using d_type =
typename vector_type::dual_view_type::t_dev;
172 using d_matrix_type =
typename crs_matrix_type::local_matrix_device_type;
174 const char kernel_label[] =
"inverse_diagonal_kernel";
175 using execution_space =
typename NT::execution_space;
176 using range_type = Kokkos::RangePolicy<execution_space, LO>;
177 const size_t lclNumRows = A_crs_->getRowMap()->getLocalNumElements();
178 auto policy = range_type(0, lclNumRows);
180 d_type d = D_inv.getLocalViewDevice(Tpetra::Access::OverwriteAll);
181 d_matrix_type a = A_crs_->getLocalMatrixDevice();
184 constexpr bool do_l1_template =
true;
185 if (fixTinyDiagEntries) {
186 constexpr bool fix_tiny_template =
true;
188 Impl::InverseDiagonalWithExtraction<d_type,
193 functor_type func(d, a, offsets_, L1Eta, MinDiagonalValue);
194 Kokkos::parallel_for(kernel_label, policy, func);
196 constexpr bool fix_tiny_template =
false;
198 Impl::InverseDiagonalWithExtraction<d_type,
203 functor_type func(d, a, offsets_, L1Eta, MinDiagonalValue);
204 Kokkos::parallel_for(kernel_label, policy, func);
207 constexpr bool do_l1_template =
false;
208 if (fixTinyDiagEntries) {
209 constexpr bool fix_tiny_template =
true;
211 Impl::InverseDiagonalWithExtraction<d_type,
216 functor_type func(d, a, offsets_, L1Eta, MinDiagonalValue);
217 Kokkos::parallel_for(kernel_label, policy, func);
219 constexpr bool fix_tiny_template =
false;
221 Impl::InverseDiagonalWithExtraction<d_type,
226 functor_type func(d, a, offsets_, L1Eta, MinDiagonalValue);
227 Kokkos::parallel_for(kernel_label, policy, func);
235#define IFPACK2_DETAILS_INVERSEDIAGONALKERNEL_INSTANT(SC, LO, GO, NT) \
236 template class Ifpack2::Details::InverseDiagonalKernel<Tpetra::Operator<SC, LO, GO, NT> >;
Compute scaled damped residual for Chebyshev.
Definition Ifpack2_Details_InverseDiagonalKernel_decl.hpp:45
Ifpack2's implementation of Trilinos::Details::LinearSolver interface.
Definition Ifpack2_Details_LinearSolver_decl.hpp:75
void setMatrix(const Teuchos::RCP< const OP > &A)
Set the solver's matrix.
Definition Ifpack2_Details_LinearSolver_def.hpp:53
Ifpack2 implementation details.
Preconditioners and smoothers for Tpetra sparse matrices.
Definition Ifpack2_AdditiveSchwarz_decl.hpp:40