MueLu Version of the Day
Loading...
Searching...
No Matches
MueLu_MatrixFreeTentativeP_def.hpp
Go to the documentation of this file.
1// @HEADER
2// *****************************************************************************
3// MueLu: A package for multigrid based preconditioning
4//
5// Copyright 2012 NTESS and the MueLu contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9
10#ifndef MUELU_MATRIXFREETENTATIVEP_DEF_HPP
11#define MUELU_MATRIXFREETENTATIVEP_DEF_HPP
12
14
15#include "MueLu_Aggregates.hpp"
16
17#include <Tpetra_KokkosCompat_ClassicNodeAPI_Wrapper.hpp>
18
19#include "Teuchos_ScalarTraits.hpp"
20
21#include "Xpetra_Map.hpp"
22#include "Xpetra_Vector.hpp"
23#include "Xpetra_MultiVector.hpp"
24#include "Xpetra_Operator.hpp"
25
26namespace MueLu {
27
28// compute Y = alpha*R*X + beta*Y
29template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
31 MultiVector &Y,
32 Teuchos::ETransp mode,
33 Scalar alpha,
34 Scalar beta) const {
35#if KOKKOS_VERSION >= 40799
36 using impl_scalar_type = typename KokkosKernels::ArithTraits<Scalar>::val_type;
37#else
38 using impl_scalar_type = typename Kokkos::ArithTraits<Scalar>::val_type;
39#endif
40 impl_scalar_type implAlpha = alpha;
41
42 // Step 1: Y*=beta*Y, setup necessary structures
43 Y.scale(beta);
44
45 // TODO: probably smarter to sqrt the whole aggSizes once, but may be slower if it's done in a separate kernel launch?
46 typename Aggregates::aggregates_sizes_type::const_type aggSizes = aggregates_->ComputeAggregateSizes();
47
48 auto kokkos_view_X = X.getLocalViewDevice(Tpetra::Access::ReadOnly);
49 auto kokkos_view_Y = Y.getLocalViewDevice(Tpetra::Access::ReadWrite);
50 LO numCols = kokkos_view_X.extent(1);
51
52 if (mode == Teuchos::TRANS) { // if we're in restrictor mode
53 auto vertex2AggId = aggregates_->GetVertex2AggId();
54 auto vertex2AggIdView = vertex2AggId->getLocalViewDevice(Tpetra::Access::ReadOnly);
55 LO numNodes = kokkos_view_X.extent(0);
56
57 // Step 2: Compute Y=Y+alpha*R*X
58 // recall R*X is an average of X over aggregates
59 Kokkos::parallel_for(
60 "MueLu:MatrixFreeTentativeR_kokkos:apply", md_range_type({0, 0}, {numCols, numNodes}),
61 KOKKOS_LAMBDA(const int colIdx, const int NodeIdx) {
62 LO aggIdx = vertex2AggIdView(NodeIdx, 0);
63 if (aggIdx != -1) { // depending on maps, vertex2agg
64 Kokkos::atomic_add(&kokkos_view_Y(aggIdx, colIdx), implAlpha * kokkos_view_X(NodeIdx, colIdx) / Kokkos::sqrt(aggSizes(aggIdx)));
65 }
66 });
67 } else { // if we're in prolongator mode
68 const auto vertex2Agg = aggregates_->GetVertex2AggId();
69 auto vertex2AggView = vertex2Agg->getLocalViewDevice(Tpetra::Access::ReadOnly);
70 LO numNodes = kokkos_view_Y.extent(0);
71
72 // Step 2: Compute Y=Y+alpha*P*X
73 // recall P*X is essentially injection of X, but sum if a node belongs to multiple aggregates
74 Kokkos::parallel_for(
75 "MueLu:MatrixFreeTentativeP:apply", md_range_type({0, 0}, {numCols, numNodes}),
76 KOKKOS_LAMBDA(const int colIdx, const int fineIdx) {
77 LO aggIdx = vertex2AggView(fineIdx, 0);
78 kokkos_view_Y(fineIdx, colIdx) += implAlpha * kokkos_view_X(aggIdx, colIdx) / Kokkos::sqrt(aggSizes(aggIdx));
79 });
80 }
81}
82
83// I don't care
84template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
85void MatrixFreeTentativeP<Scalar, LocalOrdinal, GlobalOrdinal, Node>::residual(const MultiVector &X, const MultiVector &B, MultiVector &R) const {
86 TEUCHOS_TEST_FOR_EXCEPTION(true, Exceptions::RuntimeError, "MatrixFreeTentativeP residual would make no sense as the operator is not square!");
87}
88
89} // namespace MueLu
90
91#define MUELU_MATRIXFREETENTATIVEP_SHORT
92#endif // MUELU_MATRIXFREETENTATIVEP_DEF_HPP
MueLu::DefaultScalar Scalar
Exception throws to report errors in the internal logical of the program.
void residual(const MultiVector &X, const MultiVector &B, MultiVector &R) const override
Kokkos::MDRangePolicy< local_ordinal_type, execution_space, Kokkos::Rank< 2 > > md_range_type
void apply(const MultiVector &X, MultiVector &Y, Teuchos::ETransp mode=Teuchos::NO_TRANS, Scalar alpha=Teuchos::ScalarTraits< Scalar >::one(), Scalar beta=Teuchos::ScalarTraits< Scalar >::zero()) const override
Namespace for MueLu classes and methods.