Ginkgo Generated from branch based on master. Ginkgo version 1.8.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
12#include <ginkgo/core/base/lin_op.hpp>
13#include <ginkgo/core/matrix/dense.hpp>
14
15
16namespace gko {
17
18
38template <typename ValueType = default_precision>
39class Perturbation : public EnableLinOp<Perturbation<ValueType>>,
40 public EnableCreateMethod<Perturbation<ValueType>> {
42 friend class EnableCreateMethod<Perturbation>;
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
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 {
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_
This mixin implements a static create() method on ConcreteType that dynamically allocates the memory,...
Definition polymorphic_object.hpp:748
The EnableLinOp mixin can be used to provide sensible default implementations of the majority of the ...
Definition lin_op.hpp:880
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition polymorphic_object.hpp:663
Definition lin_op.hpp:118
The Perturbation class can be used to construct a LinOp to represent the operation (identity + scalar...
Definition perturbation.hpp:40
const std::shared_ptr< const LinOp > get_basis() const noexcept
Returns the basis of the perturbation.
Definition perturbation.hpp:52
const std::shared_ptr< const LinOp > get_projector() const noexcept
Returns the projector of the perturbation.
Definition perturbation.hpp:62
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:72
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 sparsity_csr.hpp:26
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:1540
The Ginkgo namespace.
Definition abstract_factory.hpp:20
constexpr T one()
Returns the multiplicative identity for T.
Definition math.hpp:775
A type representing the dimensions of a multidimensional object.
Definition dim.hpp:27