Ginkgo: ginkgo/core/base/perturbation.hpp Source File

| Ginkgo Generated from branch based on main. Ginkgo version 1.11.0 A numerical linear algebra library targeting many-core architectures

Loading...
Searching...
No Matches
perturbation.hpp
1// SPDX-FileCopyrightText: 2017 - 2025 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>
38 class Perturbation : public EnableLinOp<Perturbation<ValueType>>,
39 public EnableCreateMethod<Perturbation<ValueType>> {
40 friend class EnablePolymorphicObject<Perturbation, LinOp>;
41 friend class EnableCreateMethod<Perturbation>;
42 GKO_ASSERT_SUPPORTED_VALUE_TYPE;
43
44public:
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
122protected:
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
144private:
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::EnableCreateMethod
This mixin implements a static create() method on ConcreteType that dynamically allocates the memory,...
Definition polymorphic_object.hpp:767
gko::EnableLinOp
The EnableLinOp mixin can be used to provide sensible default implementations of the majority of the ...
Definition lin_op.hpp:879
gko::EnablePolymorphicObject
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition polymorphic_object.hpp:668
gko::LinOp
Definition lin_op.hpp:117
gko::Perturbation
The Perturbation class can be used to construct a LinOp to represent the operation (identity + scalar...
Definition perturbation.hpp:39
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::Perturbation::create
static std::unique_ptr< Perturbation > create(std::shared_ptr< const Executor > exec)
Creates an empty perturbation operator (0x0 operator).
gko::Perturbation::create
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...
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::create
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.
gko::matrix::Dense
Dense is a matrix format which explicitly stores all values of the matrix.
Definition dense.hpp:120
gko::initialize
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:1593
gko
The Ginkgo namespace.
Definition abstract_factory.hpp:20
gko::one
constexpr T one()
Returns the multiplicative identity for T.
Definition math.hpp:654