Ginkgo  Generated from pipelines/2662685947 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 
14 
15 namespace gko {
16 
17 
37 template <typename ValueType = default_precision>
38 class Perturbation : public LinOp,
39  public EnableCloneable<Perturbation<ValueType>>,
40  public EnableCreateMethod<Perturbation<ValueType>> {
41  friend class EnableCloneable<Perturbation>;
42  friend class EnableCreateMethod<Perturbation>;
43  GKO_ASSERT_SUPPORTED_VALUE_TYPE;
44 
45 public:
46  using value_type = ValueType;
47 
53  const std::shared_ptr<const LinOp> get_basis() const noexcept
54  {
55  return basis_;
56  }
57 
63  const std::shared_ptr<const LinOp> get_projector() const noexcept
64  {
65  return projector_;
66  }
67 
73  const std::shared_ptr<const LinOp> get_scalar() const noexcept
74  {
75  return scalar_;
76  }
77 
78  Perturbation& operator=(const Perturbation& other);
79 
80  Perturbation& operator=(Perturbation&& other);
81 
82  Perturbation(const Perturbation& other);
83 
84  Perturbation(Perturbation&& other);
85 
93  static std::unique_ptr<Perturbation> create(
94  std::shared_ptr<const Executor> exec);
95 
106  static std::unique_ptr<Perturbation> create(
107  std::shared_ptr<const LinOp> scalar,
108  std::shared_ptr<const LinOp> basis);
109 
119  static std::unique_ptr<Perturbation> create(
120  std::shared_ptr<const LinOp> scalar, std::shared_ptr<const LinOp> basis,
121  std::shared_ptr<const LinOp> projector);
122 
123 protected:
124  explicit Perturbation(std::shared_ptr<const Executor> exec);
125 
126  explicit Perturbation(std::shared_ptr<const LinOp> scalar,
127  std::shared_ptr<const LinOp> basis);
128 
129  explicit Perturbation(std::shared_ptr<const LinOp> scalar,
130  std::shared_ptr<const LinOp> basis,
131  std::shared_ptr<const LinOp> projector);
132 
133  void apply_impl(const LinOp* b, LinOp* x) const override;
134 
135  void apply_impl(const LinOp* alpha, const LinOp* b, const LinOp* beta,
136  LinOp* x) const override;
137 
143  void validate_perturbation();
144 
145 private:
146  std::shared_ptr<const LinOp> basis_;
147  std::shared_ptr<const LinOp> projector_;
148  std::shared_ptr<const LinOp> scalar_;
149 
150  // TODO: solve race conditions when multithreading
151  mutable struct cache_struct {
152  cache_struct() = default;
153  ~cache_struct() = default;
154  cache_struct(const cache_struct& other) {}
155  cache_struct& operator=(const cache_struct& other) { return *this; }
156 
157  // allocate linops of cache. The dimension of `intermediate` is
158  // (the number of rows of projector, the number of columns of b). Others
159  // are 1x1 scalar.
160  void allocate(std::shared_ptr<const Executor> exec, dim<2> size)
161  {
162  using vec = gko::matrix::Dense<ValueType>;
163  if (one == nullptr) {
164  one = initialize<vec>({gko::one<ValueType>()}, exec);
165  }
166  if (alpha_scalar == nullptr) {
167  alpha_scalar = vec::create(exec, gko::dim<2>(1));
168  }
169  if (intermediate == nullptr || intermediate->get_size() != size) {
170  intermediate = vec::create(exec, size);
171  }
172  }
173 
174  std::unique_ptr<LinOp> intermediate;
175  std::unique_ptr<LinOp> one;
176  std::unique_ptr<LinOp> alpha_scalar;
177  } cache_;
178 };
179 
180 
181 } // namespace gko
182 
183 
184 #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:38
gko::LinOp
Definition: lin_op.hpp:117
gko::matrix::Dense
Dense is a matrix format which explicitly stores all values of the matrix.
Definition: dense_cache.hpp:28
gko::EnableCreateMethod
This mixin implements a static create() method on ConcreteType that dynamically allocates the memory,...
Definition: polymorphic_object.hpp:522
gko
The Ginkgo namespace.
Definition: abstract_factory.hpp:20
gko::dim< 2 >
gko::Perturbation::get_scalar
const std::shared_ptr< const LinOp > get_scalar() const noexcept
Returns the scalar of the perturbation.
Definition: perturbation.hpp:73
gko::Perturbation::get_basis
const std::shared_ptr< const LinOp > get_basis() const noexcept
Returns the basis of the perturbation.
Definition: perturbation.hpp:53
gko::Perturbation::get_projector
const std::shared_ptr< const LinOp > get_projector() const noexcept
Returns the projector of the perturbation.
Definition: perturbation.hpp:63
gko::EnableCloneable
This mixin is used to enable a default Cloneable::clone() implementation and similar for objects that...
Definition: polymorphic_object.hpp:369
gko::one
constexpr T one()
Returns the multiplicative identity for T.
Definition: math.hpp:654
gko::Perturbation::create
static std::unique_ptr< Perturbation > create(std::shared_ptr< const Executor > exec)
Creates an empty perturbation operator (0x0 operator).