Ginkgo  Generated from pipelines/1330831941 branch based on master. Ginkgo version 1.8.0
A numerical linear algebra library targeting many-core architectures
perturbation.hpp
1 // SPDX-FileCopyrightText: 2017 - 2024 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 
12 #include <ginkgo/core/base/lin_op.hpp>
13 #include <ginkgo/core/matrix/dense.hpp>
14 
15 
16 namespace gko {
17 
18 
38 template <typename ValueType = default_precision>
39 class Perturbation : public EnableLinOp<Perturbation<ValueType>>,
40  public EnableCreateMethod<Perturbation<ValueType>> {
42  friend class EnableCreateMethod<Perturbation>;
43 
44 public:
45  using value_type = ValueType;
46 
52  const std::shared_ptr<const LinOp> get_basis() const noexcept
53  {
54  return basis_;
55  }
56 
62  const std::shared_ptr<const LinOp> get_projector() const noexcept
63  {
64  return projector_;
65  }
66 
72  const std::shared_ptr<const LinOp> get_scalar() const noexcept
73  {
74  return scalar_;
75  }
76 
77  Perturbation& operator=(const Perturbation& other);
78 
79  Perturbation& operator=(Perturbation&& other);
80 
81  Perturbation(const Perturbation& other);
82 
83  Perturbation(Perturbation&& other);
84 
92  static std::unique_ptr<Perturbation> create(
93  std::shared_ptr<const Executor> exec);
94 
105  static std::unique_ptr<Perturbation> create(
106  std::shared_ptr<const LinOp> scalar,
107  std::shared_ptr<const LinOp> basis);
108 
118  static std::unique_ptr<Perturbation> create(
119  std::shared_ptr<const LinOp> scalar, std::shared_ptr<const LinOp> basis,
120  std::shared_ptr<const LinOp> projector);
121 
122 protected:
123  explicit Perturbation(std::shared_ptr<const Executor> exec);
124 
125  explicit Perturbation(std::shared_ptr<const LinOp> scalar,
126  std::shared_ptr<const LinOp> basis);
127 
128  explicit Perturbation(std::shared_ptr<const LinOp> scalar,
129  std::shared_ptr<const LinOp> basis,
130  std::shared_ptr<const LinOp> projector);
131 
132  void apply_impl(const LinOp* b, LinOp* x) const override;
133 
134  void apply_impl(const LinOp* alpha, const LinOp* b, const LinOp* beta,
135  LinOp* x) const override;
136 
142  void validate_perturbation();
143 
144 private:
145  std::shared_ptr<const LinOp> basis_;
146  std::shared_ptr<const LinOp> projector_;
147  std::shared_ptr<const LinOp> scalar_;
148 
149  // TODO: solve race conditions when multithreading
150  mutable struct cache_struct {
151  cache_struct() = default;
152  ~cache_struct() = default;
153  cache_struct(const cache_struct& other) {}
154  cache_struct& operator=(const cache_struct& other) { return *this; }
155 
156  // allocate linops of cache. The dimension of `intermediate` is
157  // (the number of rows of projector, the number of columns of b). Others
158  // are 1x1 scalar.
159  void allocate(std::shared_ptr<const Executor> exec, dim<2> size)
160  {
161  using vec = gko::matrix::Dense<ValueType>;
162  if (one == nullptr) {
163  one = initialize<vec>({gko::one<ValueType>()}, exec);
164  }
165  if (alpha_scalar == nullptr) {
166  alpha_scalar = vec::create(exec, gko::dim<2>(1));
167  }
168  if (intermediate == nullptr || intermediate->get_size() != size) {
169  intermediate = vec::create(exec, size);
170  }
171  }
172 
173  std::unique_ptr<LinOp> intermediate;
174  std::unique_ptr<LinOp> one;
175  std::unique_ptr<LinOp> alpha_scalar;
176  } cache_;
177 };
178 
179 
180 } // namespace gko
181 
182 
183 #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:118
gko::matrix::Dense
Dense is a matrix format which explicitly stores all values of the matrix.
Definition: dense_cache.hpp:20
gko::EnableCreateMethod
This mixin implements a static create() method on ConcreteType that dynamically allocates the memory,...
Definition: polymorphic_object.hpp:748
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:72
gko::Perturbation::get_basis
const std::shared_ptr< const LinOp > get_basis() const noexcept
Returns the basis of the perturbation.
Definition: perturbation.hpp:52
gko::Perturbation::get_projector
const std::shared_ptr< const LinOp > get_projector() const noexcept
Returns the projector of the perturbation.
Definition: perturbation.hpp:62
gko::EnableLinOp
The EnableLinOp mixin can be used to provide sensible default implementations of the majority of the ...
Definition: lin_op.hpp:878
gko::one
constexpr T one()
Returns the multiplicative identity for T.
Definition: math.hpp:775
gko::EnablePolymorphicObject
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition: polymorphic_object.hpp:662
gko::Perturbation::create
static std::unique_ptr< Perturbation > create(std::shared_ptr< const Executor > exec)
Creates an empty perturbation operator (0x0 operator).