ginkgo/core/base/perturbation.hpp Source File

ginkgo/core/base/perturbation.hpp Source File#

Reference API: ginkgo/core/base/perturbation.hpp Source File
Reference API
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>> {
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
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) {
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_
Definition polymorphic_object.hpp:767
Definition lin_op.hpp:878
Definition polymorphic_object.hpp:668
Definition lin_op.hpp:117
Definition perturbation.hpp:39
const std::shared_ptr< const LinOp > get_basis() const noexcept
Definition perturbation.hpp:51
const std::shared_ptr< const LinOp > get_projector() const noexcept
Definition perturbation.hpp:61
static std::unique_ptr< Perturbation > create(std::shared_ptr< const Executor > exec)
void validate_perturbation()
static std::unique_ptr< Perturbation > create(std::shared_ptr< const LinOp > scalar, std::shared_ptr< const LinOp > basis)
const std::shared_ptr< const LinOp > get_scalar() const noexcept
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)
static std::unique_ptr< Dense > create(std::shared_ptr< const Executor > exec, const dim< 2 > &size={}, size_type stride=0)
The Ginkgo namespace.
Definition abstract_factory.hpp:20
constexpr T one()
Definition math.hpp:630
Definition dim.hpp:26