Ginkgo Generated from branch based on master. Ginkgo version 1.7.0
A numerical linear algebra library targeting many-core architectures
Loading...
Searching...
No Matches
perturbation.hpp
1/*******************************<GINKGO LICENSE>******************************
2Copyright (c) 2017-2023, the Ginkgo authors
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions
7are met:
8
91. Redistributions of source code must retain the above copyright
10notice, this list of conditions and the following disclaimer.
11
122. Redistributions in binary form must reproduce the above copyright
13notice, this list of conditions and the following disclaimer in the
14documentation and/or other materials provided with the distribution.
15
163. Neither the name of the copyright holder nor the names of its
17contributors may be used to endorse or promote products derived from
18this software without specific prior written permission.
19
20THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31******************************<GINKGO LICENSE>*******************************/
32
33#ifndef GKO_PUBLIC_CORE_BASE_PERTURBATION_HPP_
34#define GKO_PUBLIC_CORE_BASE_PERTURBATION_HPP_
35
36
37#include <memory>
38
39
40#include <ginkgo/core/base/lin_op.hpp>
41#include <ginkgo/core/matrix/dense.hpp>
42
43
44namespace gko {
45
46
66template <typename ValueType = default_precision>
67class Perturbation : public EnableLinOp<Perturbation<ValueType>>,
68 public EnableCreateMethod<Perturbation<ValueType>> {
70 friend class EnableCreateMethod<Perturbation>;
71
72public:
73 using value_type = ValueType;
74
80 const std::shared_ptr<const LinOp> get_basis() const noexcept
81 {
82 return basis_;
83 }
84
90 const std::shared_ptr<const LinOp> get_projector() const noexcept
91 {
92 return projector_;
93 }
94
100 const std::shared_ptr<const LinOp> get_scalar() const noexcept
101 {
102 return scalar_;
103 }
104
105 Perturbation& operator=(const Perturbation& other);
106
107 Perturbation& operator=(Perturbation&& other);
108
110
112
113protected:
119 explicit Perturbation(std::shared_ptr<const Executor> exec)
120 : EnableLinOp<Perturbation>(std::move(exec))
121 {}
122
131 explicit Perturbation(std::shared_ptr<const LinOp> scalar,
132 std::shared_ptr<const LinOp> basis)
133 : Perturbation(
134 std::move(scalar),
135 // basis can not be std::move(basis). Otherwise, Program deletes
136 // basis before applying conjugate transpose
137 basis,
138 std::move((as<gko::Transposable>(basis))->conj_transpose()))
139 {}
140
148 explicit Perturbation(std::shared_ptr<const LinOp> scalar,
149 std::shared_ptr<const LinOp> basis,
150 std::shared_ptr<const LinOp> projector)
151 : EnableLinOp<Perturbation>(basis->get_executor(),
152 gko::dim<2>{basis->get_size()[0]}),
153 scalar_{std::move(scalar)},
154 basis_{std::move(basis)},
155 projector_{std::move(projector)}
156 {
157 this->validate_perturbation();
158 }
159
160 void apply_impl(const LinOp* b, LinOp* x) const override;
161
162 void apply_impl(const LinOp* alpha, const LinOp* b, const LinOp* beta,
163 LinOp* x) const override;
164
170 void validate_perturbation()
171 {
172 GKO_ASSERT_CONFORMANT(basis_, projector_);
173 GKO_ASSERT_CONFORMANT(projector_, basis_);
174 GKO_ASSERT_EQUAL_DIMENSIONS(scalar_, dim<2>(1, 1));
175 }
176
177private:
178 std::shared_ptr<const LinOp> basis_;
179 std::shared_ptr<const LinOp> projector_;
180 std::shared_ptr<const LinOp> scalar_;
181
182 // TODO: solve race conditions when multithreading
183 mutable struct cache_struct {
184 cache_struct() = default;
185 ~cache_struct() = default;
186 cache_struct(const cache_struct& other) {}
187 cache_struct& operator=(const cache_struct& other) { return *this; }
188
189 // allocate linops of cache. The dimension of `intermediate` is
190 // (the number of rows of projector, the number of columns of b). Others
191 // are 1x1 scalar.
192 void allocate(std::shared_ptr<const Executor> exec, dim<2> size)
193 {
195 if (one == nullptr) {
196 one = initialize<vec>({gko::one<ValueType>()}, exec);
197 }
198 if (alpha_scalar == nullptr) {
199 alpha_scalar = vec::create(exec, gko::dim<2>(1));
200 }
201 if (intermediate == nullptr || intermediate->get_size() != size) {
202 intermediate = vec::create(exec, size);
203 }
204 }
205
206 std::unique_ptr<LinOp> intermediate;
207 std::unique_ptr<LinOp> one;
208 std::unique_ptr<LinOp> alpha_scalar;
209 } cache_;
210};
211
212
213} // namespace gko
214
215
216#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:776
The EnableLinOp mixin can be used to provide sensible default implementations of the majority of the ...
Definition lin_op.hpp:908
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition polymorphic_object.hpp:691
Definition lin_op.hpp:146
The Perturbation class can be used to construct a LinOp to represent the operation (identity + scalar...
Definition perturbation.hpp:68
const std::shared_ptr< const LinOp > get_basis() const noexcept
Returns the basis of the perturbation.
Definition perturbation.hpp:80
const std::shared_ptr< const LinOp > get_projector() const noexcept
Returns the projector of the perturbation.
Definition perturbation.hpp:90
const std::shared_ptr< const LinOp > get_scalar() const noexcept
Returns the scalar of the perturbation.
Definition perturbation.hpp:100
std::shared_ptr< const Executor > get_executor() const noexcept
Returns the Executor of the object.
Definition polymorphic_object.hpp:263
Dense is a matrix format which explicitly stores all values of the matrix.
Definition dense.hpp:136
The Ginkgo namespace.
Definition abstract_factory.hpp:48
constexpr T one()
Returns the multiplicative identity for T.
Definition math.hpp:803
std::decay_t< T > * as(U *obj)
Performs polymorphic type conversion.
Definition utils_helper.hpp:337
A type representing the dimensions of a multidimensional object.
Definition dim.hpp:55