Ginkgo  Generated from pipelines/1478841010 branch based on develop. Ginkgo version 1.9.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 #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 EnableLinOp<Perturbation<ValueType>>,
39  public EnableCreateMethod<Perturbation<ValueType>> {
41  friend class EnableCreateMethod<Perturbation>;
42 
43 public:
44  using value_type = ValueType;
45 
51  const std::shared_ptr<const LinOp> get_basis() const noexcept
52  {
53  return basis_;
54  }
55 
61  const std::shared_ptr<const LinOp> get_projector() const noexcept
62  {
63  return projector_;
64  }
65 
71  const std::shared_ptr<const LinOp> get_scalar() const noexcept
72  {
73  return scalar_;
74  }
75 
76  Perturbation& operator=(const Perturbation& other);
77 
78  Perturbation& operator=(Perturbation&& other);
79 
80  Perturbation(const Perturbation& other);
81 
82  Perturbation(Perturbation&& other);
83 
91  static std::unique_ptr<Perturbation> create(
92  std::shared_ptr<const Executor> exec);
93 
104  static std::unique_ptr<Perturbation> create(
105  std::shared_ptr<const LinOp> scalar,
106  std::shared_ptr<const LinOp> basis);
107 
117  static std::unique_ptr<Perturbation> create(
118  std::shared_ptr<const LinOp> scalar, std::shared_ptr<const LinOp> basis,
119  std::shared_ptr<const LinOp> projector);
120 
121 protected:
122  explicit Perturbation(std::shared_ptr<const Executor> exec);
123 
124  explicit Perturbation(std::shared_ptr<const LinOp> scalar,
125  std::shared_ptr<const LinOp> basis);
126 
127  explicit Perturbation(std::shared_ptr<const LinOp> scalar,
128  std::shared_ptr<const LinOp> basis,
129  std::shared_ptr<const LinOp> projector);
130 
131  void apply_impl(const LinOp* b, LinOp* x) const override;
132 
133  void apply_impl(const LinOp* alpha, const LinOp* b, const LinOp* beta,
134  LinOp* x) const override;
135 
141  void validate_perturbation();
142 
143 private:
144  std::shared_ptr<const LinOp> basis_;
145  std::shared_ptr<const LinOp> projector_;
146  std::shared_ptr<const LinOp> scalar_;
147 
148  // TODO: solve race conditions when multithreading
149  mutable struct cache_struct {
150  cache_struct() = default;
151  ~cache_struct() = default;
152  cache_struct(const cache_struct& other) {}
153  cache_struct& operator=(const cache_struct& other) { return *this; }
154 
155  // allocate linops of cache. The dimension of `intermediate` is
156  // (the number of rows of projector, the number of columns of b). Others
157  // are 1x1 scalar.
158  void allocate(std::shared_ptr<const Executor> exec, dim<2> size)
159  {
160  using vec = gko::matrix::Dense<ValueType>;
161  if (one == nullptr) {
162  one = initialize<vec>({gko::one<ValueType>()}, exec);
163  }
164  if (alpha_scalar == nullptr) {
165  alpha_scalar = vec::create(exec, gko::dim<2>(1));
166  }
167  if (intermediate == nullptr || intermediate->get_size() != size) {
168  intermediate = vec::create(exec, size);
169  }
170  }
171 
172  std::unique_ptr<LinOp> intermediate;
173  std::unique_ptr<LinOp> one;
174  std::unique_ptr<LinOp> alpha_scalar;
175  } cache_;
176 };
177 
178 
179 } // namespace gko
180 
181 
182 #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:19
gko::EnableCreateMethod
This mixin implements a static create() method on ConcreteType that dynamically allocates the memory,...
Definition: polymorphic_object.hpp:747
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:71
gko::Perturbation::get_basis
const std::shared_ptr< const LinOp > get_basis() const noexcept
Returns the basis of the perturbation.
Definition: perturbation.hpp:51
gko::Perturbation::get_projector
const std::shared_ptr< const LinOp > get_projector() const noexcept
Returns the projector of the perturbation.
Definition: perturbation.hpp:61
gko::EnableLinOp
The EnableLinOp mixin can be used to provide sensible default implementations of the majority of the ...
Definition: lin_op.hpp:877
gko::one
constexpr T one()
Returns the multiplicative identity for T.
Definition: math.hpp:774
gko::EnablePolymorphicObject
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition: polymorphic_object.hpp:661
gko::Perturbation::create
static std::unique_ptr< Perturbation > create(std::shared_ptr< const Executor > exec)
Creates an empty perturbation operator (0x0 operator).