Ginkgo  Generated from pipelines/3133041809 branch based on develop. Ginkgo version 2.0.0
A numerical linear algebra library targeting many-core architectures
perturbation.hpp
1 // SPDX-FileCopyrightText: 2017 - 2026 The Ginkgo authors
2 //
3 // SPDX-License-Identifier: BSD-3-Clause
4 
5 #ifndef GKO_PUBLIC_CORE_BASE_PERTURBATION_HPP_
6 #define GKO_PUBLIC_CORE_BASE_PERTURBATION_HPP_
7 
8 
9 #include <memory>
10 
11 #include <ginkgo/core/base/lin_op.hpp>
12 #include <ginkgo/core/matrix/dense.hpp>
13 #include <ginkgo/core/matrix/multivector.hpp>
14 
15 
16 namespace gko {
17 
18 
38 template <typename ValueType = default_precision>
39 class Perturbation : public LinOp,
40  public EnableCloneable<Perturbation<ValueType>>,
41  public EnableCreateMethod<Perturbation<ValueType>> {
42  friend class EnableCloneable<Perturbation>;
43  friend class EnableCreateMethod<Perturbation>;
44  GKO_ASSERT_SUPPORTED_VALUE_TYPE;
45 
46 public:
47  using value_type = ValueType;
48 
54  const std::shared_ptr<const LinOp> get_basis() const noexcept
55  {
56  return basis_;
57  }
58 
64  const std::shared_ptr<const LinOp> get_projector() const noexcept
65  {
66  return projector_;
67  }
68 
74  const std::shared_ptr<const matrix::MultiVector<ValueType>> get_scalar()
75  const noexcept
76  {
77  return scalar_;
78  }
79 
80  Perturbation& operator=(const Perturbation& other);
81 
82  Perturbation& operator=(Perturbation&& other);
83 
84  Perturbation(const Perturbation& other);
85 
86  Perturbation(Perturbation&& other);
87 
95  static std::unique_ptr<Perturbation> create(
96  std::shared_ptr<const Executor> exec);
97 
108  static std::unique_ptr<Perturbation> create(
109  std::shared_ptr<const matrix::MultiVector<ValueType>> scalar,
110  std::shared_ptr<const LinOp> basis);
111 
121  static std::unique_ptr<Perturbation> create(
122  std::shared_ptr<const matrix::MultiVector<ValueType>> scalar,
123  std::shared_ptr<const LinOp> basis,
124  std::shared_ptr<const LinOp> projector);
125 
126 protected:
127  explicit Perturbation(std::shared_ptr<const Executor> exec);
128 
129  explicit Perturbation(
130  std::shared_ptr<const matrix::MultiVector<ValueType>> scalar,
131  std::shared_ptr<const LinOp> basis);
132 
133  explicit Perturbation(
134  std::shared_ptr<const matrix::MultiVector<ValueType>> scalar,
135  std::shared_ptr<const LinOp> basis,
136  std::shared_ptr<const LinOp> projector);
137 
138  void apply_impl(const AbstractMultiVector* b,
139  AbstractMultiVector* x) const override;
140 
141  void apply_impl(const AbstractMultiVector* alpha,
142  const AbstractMultiVector* b,
143  const AbstractMultiVector* beta,
144  AbstractMultiVector* x) const override;
145 
151  void validate_perturbation();
152 
153 private:
154  std::shared_ptr<const LinOp> basis_;
155  std::shared_ptr<const LinOp> projector_;
156  std::shared_ptr<const matrix::MultiVector<ValueType>> scalar_;
157 
158  // TODO: solve race conditions when multithreading
159  mutable struct cache_struct {
160  cache_struct() = default;
161  ~cache_struct() = default;
162  cache_struct(const cache_struct& other) {}
163  cache_struct& operator=(const cache_struct& other) { return *this; }
164 
165  // allocate linops of cache. The dimension of `intermediate` is
166  // (the number of rows of projector, the number of columns of b). Others
167  // are 1x1 scalar.
168  void allocate(std::shared_ptr<const Executor> exec, dim<2> size)
169  {
170  using vec = matrix::MultiVector<ValueType>;
171  if (one == nullptr) {
172  one = initialize<vec>({gko::one<ValueType>()}, exec);
173  }
174  if (alpha_scalar == nullptr) {
175  alpha_scalar = vec::create(exec, gko::dim<2>(1));
176  }
177  if (intermediate == nullptr || intermediate->get_size() != size) {
178  intermediate = vec::create(exec, size);
179  }
180  }
181 
182  std::unique_ptr<AbstractMultiVector> intermediate;
183  std::unique_ptr<matrix::MultiVector<ValueType>> one;
184  std::unique_ptr<matrix::MultiVector<ValueType>> alpha_scalar;
185  } cache_;
186 };
187 
188 
189 } // namespace gko
190 
191 
192 #endif // GKO_PUBLIC_CORE_BASE_PERTURBATION_HPP_
gko::Perturbation
The Perturbation class can be used to construct a LinOp to represent the operation (identity + scalar...
Definition: perturbation.hpp:39
gko::LinOp
Definition: lin_op.hpp:163
gko::Perturbation::get_scalar
const std::shared_ptr< const matrix::MultiVector< ValueType > > get_scalar() const noexcept
Returns the scalar of the perturbation.
Definition: perturbation.hpp:74
gko::EnableCreateMethod
This mixin implements a static create() method on ConcreteType that dynamically allocates the memory,...
Definition: polymorphic_object.hpp:497
gko
The Ginkgo namespace.
Definition: abstract_factory.hpp:19
gko::dim< 2 >
gko::Perturbation::get_basis
const std::shared_ptr< const LinOp > get_basis() const noexcept
Returns the basis of the perturbation.
Definition: perturbation.hpp:54
gko::Perturbation::get_projector
const std::shared_ptr< const LinOp > get_projector() const noexcept
Returns the projector of the perturbation.
Definition: perturbation.hpp:64
gko::matrix::MultiVector
MultiVector is a multivector format which explicitly stores all values of the vector.
Definition: dense_cache.hpp:28
gko::EnableCloneable
This mixin is used to enable a default Cloneable::clone() implementation and similar for objects that...
Definition: polymorphic_object.hpp:372
gko::AbstractMultiVector
Abstract interface class for (multi-)vector types.
Definition: multivector.hpp:81
gko::one
constexpr T one()
Returns the multiplicative identity for T.
Definition: math.hpp:653
gko::Perturbation::create
static std::unique_ptr< Perturbation > create(std::shared_ptr< const Executor > exec)
Creates an empty perturbation operator (0x0 operator).