Ginkgo Generated from branch based on main. Ginkgo version 1.10.0
A numerical linear algebra library targeting many-core architectures
Loading...
Searching...
No Matches
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
15namespace gko {
16
17
37template <typename ValueType = default_precision>
38class Perturbation : public EnableLinOp<Perturbation<ValueType>>,
39 public EnableCreateMethod<Perturbation<ValueType>> {
40 friend class EnablePolymorphicObject<Perturbation, LinOp>;
41 friend class EnableCreateMethod<Perturbation>;
42
43public:
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
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
121protected:
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
143private:
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 {
161 if (one == nullptr) {
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_
This mixin implements a static create() method on ConcreteType that dynamically allocates the memory,...
Definition polymorphic_object.hpp:767
The EnableLinOp mixin can be used to provide sensible default implementations of the majority of the ...
Definition lin_op.hpp:879
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition polymorphic_object.hpp:668
Definition lin_op.hpp:117
The Perturbation class can be used to construct a LinOp to represent the operation (identity + scalar...
Definition perturbation.hpp:39
const std::shared_ptr< const LinOp > get_basis() const noexcept
Returns the basis of the perturbation.
Definition perturbation.hpp:51
const std::shared_ptr< const LinOp > get_projector() const noexcept
Returns the projector of the perturbation.
Definition perturbation.hpp:61
static std::unique_ptr< Perturbation > create(std::shared_ptr< const Executor > exec)
Creates an empty perturbation operator (0x0 operator).
static std::unique_ptr< Perturbation > create(std::shared_ptr< const LinOp > scalar, std::shared_ptr< const LinOp > basis)
Creates a perturbation with scalar and basis by setting projector to the conjugate transpose of basis...
const std::shared_ptr< const LinOp > get_scalar() const noexcept
Returns the scalar of the perturbation.
Definition perturbation.hpp:71
static std::unique_ptr< Perturbation > create(std::shared_ptr< const LinOp > scalar, std::shared_ptr< const LinOp > basis, std::shared_ptr< const LinOp > projector)
Creates a perturbation of scalar, basis and projector.
Dense is a matrix format which explicitly stores all values of the matrix.
Definition dense.hpp:120
std::unique_ptr< Matrix > initialize(size_type stride, std::initializer_list< typename Matrix::value_type > vals, std::shared_ptr< const Executor > exec, TArgs &&... create_args)
Creates and initializes a column-vector.
Definition dense.hpp:1574
The Ginkgo namespace.
Definition abstract_factory.hpp:20
constexpr T one()
Returns the multiplicative identity for T.
Definition math.hpp:654