42 #ifndef IFPACK2_DETAILS_INVERSEDIAGONALKERNEL_DEF_HPP
43 #define IFPACK2_DETAILS_INVERSEDIAGONALKERNEL_DEF_HPP
45 #include "Tpetra_CrsMatrix.hpp"
46 #include "Tpetra_MultiVector.hpp"
47 #include "Tpetra_Operator.hpp"
48 #include "Tpetra_Vector.hpp"
49 #include "Tpetra_Export_decl.hpp"
50 #include "Tpetra_Import_decl.hpp"
51 #include "Kokkos_ArithTraits.hpp"
52 #include "Teuchos_Assert.hpp"
53 #include <type_traits>
54 #include "KokkosSparse_spmv_impl.hpp"
63 template<
class DVector,
70 using execution_space =
typename AMatrix::execution_space;
71 using LO =
typename AMatrix::non_const_ordinal_type;
72 using value_type =
typename AMatrix::non_const_value_type;
73 using team_policy =
typename Kokkos::TeamPolicy<execution_space>;
74 using team_member =
typename team_policy::member_type;
75 using ATV = Kokkos::ArithTraits<value_type>;
77 using magnitude_type =
typename ATV::mag_type;
78 using MATV = Kokkos::ArithTraits<magnitude_type>;
82 DiagOffsetType m_offsets;
83 magnitude_type m_L1Eta;
84 magnitude_type m_MinDiagonalValue;
88 const DiagOffsetType& m_offsets_,
89 const magnitude_type m_L1Eta_,
90 const magnitude_type m_MinDiagonalValue_) :
93 m_offsets (m_offsets_),
95 m_MinDiagonalValue (m_MinDiagonalValue_)
97 const size_t numRows = m_A.numRows ();
103 KOKKOS_INLINE_FUNCTION
104 void operator() (
const LO lclRow)
const
106 const size_t INV = Tpetra::Details::OrdinalTraits<size_t>::invalid ();
107 const value_type one = ATV::one();
110 m_d(lclRow,0) = ATV::zero();
112 if (m_offsets(lclRow) != INV) {
113 auto curRow = m_A.rowConst (lclRow);
114 value_type d = curRow.value(m_offsets(lclRow));
125 const magnitude_type half = MATV::one () / (MATV::one () + MATV::one ());
126 const LO numRows =
static_cast<LO
> (m_A.numRows ());
127 const LO row_length =
static_cast<LO
> (curRow.length);
128 magnitude_type diagonal_boost = MATV::zero();
129 for (LO iEntry = 0; iEntry < row_length; iEntry++) {
130 if (curRow.colidx(iEntry) >= numRows)
131 diagonal_boost += ATV::magnitude(curRow.value(iEntry));
133 diagonal_boost *= half;
134 if (ATV::magnitude(d) < m_L1Eta * diagonal_boost)
141 if (ATV::magnitude(d) <= m_MinDiagonalValue)
142 d = m_MinDiagonalValue;
146 m_d(lclRow,0) = one / d;
155 template<
class TpetraOperatorType>
162 template<
class TpetraOperatorType>
164 InverseDiagonalKernel<TpetraOperatorType>::
167 if (A_op_.get () != A.
get ()) {
170 using Teuchos::rcp_dynamic_cast;
171 A_crs_ = rcp_dynamic_cast<
const crs_matrix_type> (A);
174 (A_crs_.is_null(), std::logic_error,
175 "Ifpack2::Details::InverseDiagonalKernel: operator A must be a Tpetra::CrsMatrix.");
177 const size_t lclNumRows = A_crs_->getRowMap ()->getLocalNumElements ();
179 if (offsets_.extent (0) < lclNumRows) {
180 using Kokkos::view_alloc;
181 using Kokkos::WithoutInitializing;
182 using offsets_view_type = Kokkos::View<size_t*, device_type>;
184 offsets_ = offsets_view_type ();
185 auto howAlloc = view_alloc (
"offsets", WithoutInitializing);
186 offsets_ = offsets_view_type (howAlloc, lclNumRows);
189 A_crs_->getCrsGraph ()->getLocalDiagOffsets (offsets_);
193 template<
class TpetraOperatorType>
195 InverseDiagonalKernel<TpetraOperatorType>::
196 compute (vector_type& D_inv,
197 bool do_l1, magnitude_type L1Eta,
198 bool fixTinyDiagEntries, magnitude_type MinDiagonalValue)
203 using d_type =
typename vector_type::dual_view_type::t_dev;
205 using d_matrix_type =
typename crs_matrix_type::local_matrix_device_type;
207 const char kernel_label[] =
"inverse_diagonal_kernel";
208 using execution_space =
typename NT::execution_space;
209 using range_type = Kokkos::RangePolicy<execution_space, LO>;
210 const size_t lclNumRows = A_crs_->getRowMap ()->getLocalNumElements ();
211 auto policy = range_type(0, lclNumRows);
213 d_type d = D_inv.getLocalViewDevice(Tpetra::Access::OverwriteAll);
214 d_matrix_type a = A_crs_->getLocalMatrixDevice();
217 constexpr
bool do_l1_template =
true;
218 if (fixTinyDiagEntries) {
219 constexpr
bool fix_tiny_template =
true;
221 Impl::InverseDiagonalWithExtraction<d_type,
226 functor_type func (d, a, offsets_, L1Eta, MinDiagonalValue);
227 Kokkos::parallel_for (kernel_label, policy, func);
229 constexpr
bool fix_tiny_template =
false;
231 Impl::InverseDiagonalWithExtraction<d_type,
236 functor_type func (d, a, offsets_, L1Eta, MinDiagonalValue);
237 Kokkos::parallel_for (kernel_label, policy, func);
240 constexpr
bool do_l1_template =
false;
241 if (fixTinyDiagEntries) {
242 constexpr
bool fix_tiny_template =
true;
244 Impl::InverseDiagonalWithExtraction<d_type,
249 functor_type func (d, a, offsets_, L1Eta, MinDiagonalValue);
250 Kokkos::parallel_for (kernel_label, policy, func);
252 constexpr
bool fix_tiny_template =
false;
254 Impl::InverseDiagonalWithExtraction<d_type,
259 functor_type func (d, a, offsets_, L1Eta, MinDiagonalValue);
260 Kokkos::parallel_for (kernel_label, policy, func);
268 #define IFPACK2_DETAILS_INVERSEDIAGONALKERNEL_INSTANT(SC,LO,GO,NT) \
269 template class Ifpack2::Details::InverseDiagonalKernel<Tpetra::Operator<SC, LO, GO, NT> >;
271 #endif // IFPACK2_DETAILS_INVERSEDIAGONALKERNEL_DEF_HPP
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Compute scaled damped residual for Chebyshev.
Definition: Ifpack2_Details_InverseDiagonalKernel_decl.hpp:77
#define TEUCHOS_ASSERT(assertion_test)