10 #ifndef IFPACK2_DETAILS_SCALEDDAMPEDRESIDUAL_DEF_HPP
11 #define IFPACK2_DETAILS_SCALEDDAMPEDRESIDUAL_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 #include "Kokkos_ArithTraits.hpp"
20 #include "Teuchos_Assert.hpp"
21 #include <type_traits>
22 #include "KokkosSparse_spmv_impl.hpp"
32 template<
class WVector,
40 static_assert (static_cast<int> (WVector::rank) == 1,
41 "WVector must be a rank 1 View.");
42 static_assert (static_cast<int> (DVector::rank) == 1,
43 "DVector must be a rank 1 View.");
44 static_assert (static_cast<int> (BVector::rank) == 1,
45 "BVector must be a rank 1 View.");
46 static_assert (static_cast<int> (XVector::rank) == 1,
47 "XVector must be a rank 1 View.");
49 using execution_space =
typename AMatrix::execution_space;
50 using LO =
typename AMatrix::non_const_ordinal_type;
51 using value_type =
typename AMatrix::non_const_value_type;
52 using team_policy =
typename Kokkos::TeamPolicy<execution_space>;
53 using team_member =
typename team_policy::member_type;
54 using ATV = Kokkos::ArithTraits<value_type>;
64 const LO rows_per_team;
73 const int rows_per_team_) :
81 rows_per_team (rows_per_team_)
83 const size_t numRows = m_A.numRows ();
84 const size_t numCols = m_A.numCols ();
92 KOKKOS_INLINE_FUNCTION
93 void operator() (
const team_member& dev)
const
95 using residual_value_type =
typename BVector::non_const_value_type;
96 using KAT = Kokkos::ArithTraits<residual_value_type>;
99 (Kokkos::TeamThreadRange (dev, 0, rows_per_team),
100 [&] (
const LO& loop) {
102 static_cast<LO
> (dev.league_rank ()) * rows_per_team + loop;
103 if (lclRow >= m_A.numRows ()) {
106 const auto A_row = m_A.rowConst(lclRow);
107 const LO row_length =
static_cast<LO
> (A_row.length);
108 residual_value_type A_x = KAT::zero ();
110 Kokkos::parallel_reduce
111 (Kokkos::ThreadVectorRange (dev, row_length),
112 [&] (
const LO iEntry, residual_value_type& lsum) {
113 const auto A_val = A_row.value(iEntry);
114 lsum += A_val * m_x(A_row.colidx(iEntry));
118 (Kokkos::PerThread(dev),
120 const auto alpha_D_res =
121 alpha * m_d(lclRow) * (m_b(lclRow) - A_x);
123 m_w(lclRow) = beta * m_w(lclRow) + alpha_D_res;
126 m_w(lclRow) = alpha_D_res;
136 template<
class WVector,
143 scaled_damped_residual_vector
144 (
const Scalar& alpha,
152 using execution_space =
typename AMatrix::execution_space;
154 if (A.numRows () == 0) {
159 int vector_length = -1;
160 int64_t rows_per_thread = -1;
162 const int64_t rows_per_team = KokkosSparse::Impl::spmv_launch_parameters<execution_space>(A.numRows(), A.nnz(), rows_per_thread, team_size, vector_length);
163 int64_t worksets = (b.extent (0) + rows_per_team - 1) / rows_per_team;
165 using Kokkos::Dynamic;
166 using Kokkos::Static;
167 using Kokkos::Schedule;
168 using Kokkos::TeamPolicy;
169 using policy_type_dynamic = TeamPolicy<execution_space, Schedule<Dynamic> >;
170 using policy_type_static = TeamPolicy<execution_space, Schedule<Static> >;
171 const char kernel_label[] =
"scaled_damped_residual_vector";
172 policy_type_dynamic policyDynamic (1, 1);
173 policy_type_static policyStatic (1, 1);
175 policyDynamic = policy_type_dynamic (worksets, Kokkos::AUTO, vector_length);
176 policyStatic = policy_type_static (worksets, Kokkos::AUTO, vector_length);
179 policyDynamic = policy_type_dynamic (worksets, team_size, vector_length);
180 policyStatic = policy_type_static (worksets, team_size, vector_length);
184 using w_vec_type =
typename WVector::non_const_type;
185 using d_vec_type =
typename DVector::const_type;
186 using b_vec_type =
typename BVector::const_type;
187 using matrix_type = AMatrix;
188 using x_vec_type =
typename XVector::const_type;
189 using scalar_type =
typename Kokkos::ArithTraits<Scalar>::val_type;
191 if (beta == Kokkos::ArithTraits<Scalar>::zero ()) {
192 constexpr
bool use_beta =
false;
194 ScaledDampedResidualVectorFunctor<w_vec_type, d_vec_type,
195 b_vec_type, matrix_type,
196 x_vec_type, scalar_type,
198 functor_type func (alpha, w, d, b, A, x, beta, rows_per_team);
200 Kokkos::parallel_for (kernel_label, policyDynamic, func);
202 Kokkos::parallel_for (kernel_label, policyStatic, func);
205 constexpr
bool use_beta =
true;
207 ScaledDampedResidualVectorFunctor<w_vec_type, d_vec_type,
208 b_vec_type, matrix_type,
209 x_vec_type, scalar_type,
211 functor_type func (alpha, w, d, b, A, x, beta, rows_per_team);
213 Kokkos::parallel_for (kernel_label, policyDynamic, func);
215 Kokkos::parallel_for (kernel_label, policyStatic, func);
221 template<
class TpetraOperatorType>
222 ScaledDampedResidual<TpetraOperatorType>::
228 template<
class TpetraOperatorType>
230 ScaledDampedResidual<TpetraOperatorType>::
233 if (A_op_.get () != A.
get ()) {
237 X_colMap_ = std::unique_ptr<vector_type> (
nullptr);
238 V1_ = std::unique_ptr<multivector_type> (
nullptr);
240 using Teuchos::rcp_dynamic_cast;
242 rcp_dynamic_cast<
const crs_matrix_type> (A);
244 A_crs_ = Teuchos::null;
245 imp_ = Teuchos::null;
246 exp_ = Teuchos::null;
251 auto G = A_crs->getCrsGraph ();
252 imp_ = G->getImporter ();
253 exp_ = G->getExporter ();
258 template<
class TpetraOperatorType>
260 ScaledDampedResidual<TpetraOperatorType>::
261 compute (multivector_type& W,
273 W_vec_ = W.getVectorNonConst (0);
274 B_vec_ = B.getVectorNonConst (0);
275 X_vec_ = X.getVectorNonConst (0);
277 fusedCase (*W_vec_, alpha, D_inv, *B_vec_, *A_crs_, *X_vec_, beta);
281 unfusedCase (W, alpha, D_inv, B, *A_op_, X, beta);
285 template<
class TpetraOperatorType>
286 typename ScaledDampedResidual<TpetraOperatorType>::vector_type&
287 ScaledDampedResidual<TpetraOperatorType>::
288 importVector (vector_type& X_domMap)
290 if (imp_.is_null ()) {
294 if (X_colMap_.get () ==
nullptr) {
295 using V = vector_type;
296 X_colMap_ = std::unique_ptr<V> (
new V (imp_->getTargetMap ()));
298 X_colMap_->doImport (X_domMap, *imp_, Tpetra::REPLACE);
303 template<
class TpetraOperatorType>
305 ScaledDampedResidual<TpetraOperatorType>::
306 canFuse (
const multivector_type& B)
const
308 return B.getNumVectors () == size_t (1) &&
309 ! A_crs_.is_null () &&
313 template<
class TpetraOperatorType>
315 ScaledDampedResidual<TpetraOperatorType>::
316 unfusedCase (multivector_type& W,
320 const operator_type& A,
324 if (V1_.get () ==
nullptr) {
325 using MV = multivector_type;
326 const size_t numVecs = B.getNumVectors ();
327 V1_ = std::unique_ptr<MV> (
new MV (B.getMap (), numVecs));
332 Tpetra::deep_copy (*V1_, B);
336 W.elementWiseMultiply (alpha, D_inv, *V1_, beta);
339 template<
class TpetraOperatorType>
341 ScaledDampedResidual<TpetraOperatorType>::
342 fusedCase (vector_type& W,
346 const crs_matrix_type& A,
350 vector_type& X_colMap = importVector (X);
352 using Impl::scaled_damped_residual_vector;
355 auto A_lcl = A.getLocalMatrixDevice ();
356 auto Dinv_lcl = Kokkos::subview(D_inv.getLocalViewDevice(Tpetra::Access::ReadOnly), Kokkos::ALL(), 0);
357 auto B_lcl = Kokkos::subview(B.getLocalViewDevice(Tpetra::Access::ReadOnly), Kokkos::ALL(), 0);
358 auto X_lcl = Kokkos::subview(X_colMap.getLocalViewDevice(Tpetra::Access::ReadOnly), Kokkos::ALL(), 0);
359 if (beta == STS::zero ()) {
360 auto W_lcl = Kokkos::subview(W.getLocalViewDevice(Tpetra::Access::OverwriteAll), Kokkos::ALL(), 0);
361 scaled_damped_residual_vector (alpha, W_lcl, Dinv_lcl,
362 B_lcl, A_lcl, X_lcl, beta);
365 auto W_lcl = Kokkos::subview(W.getLocalViewDevice(Tpetra::Access::ReadWrite), Kokkos::ALL(), 0);
366 scaled_damped_residual_vector (alpha, W_lcl, Dinv_lcl,
367 B_lcl, A_lcl, X_lcl, beta);
374 #define IFPACK2_DETAILS_SCALEDDAMPEDRESIDUAL_INSTANT(SC,LO,GO,NT) \
375 template class Ifpack2::Details::ScaledDampedResidual<Tpetra::Operator<SC, LO, GO, NT> >;
377 #endif // IFPACK2_DETAILS_SCALEDDAMPEDRESIDUAL_DEF_HPP
Functor for computing W := alpha * D * (B - A*X) + beta * W.
Definition: Ifpack2_Details_ScaledDampedResidual_def.hpp:39
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
#define TEUCHOS_ASSERT(assertion_test)