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
combination.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_COMBINATION_HPP_
34#define GKO_PUBLIC_CORE_BASE_COMBINATION_HPP_
35
36
37#include <vector>
38
39
40#include <ginkgo/core/base/lin_op.hpp>
41
42
43namespace gko {
44
45
58template <typename ValueType = default_precision>
59class Combination : public EnableLinOp<Combination<ValueType>>,
60 public EnableCreateMethod<Combination<ValueType>>,
61 public Transposable {
63 friend class EnableCreateMethod<Combination>;
64
65public:
66 using value_type = ValueType;
68
74 const std::vector<std::shared_ptr<const LinOp>>& get_coefficients()
76 {
77 return coefficients_;
78 }
79
85 const std::vector<std::shared_ptr<const LinOp>>& get_operators()
87 {
88 return operators_;
89 }
90
91 std::unique_ptr<LinOp> transpose() const override;
92
93 std::unique_ptr<LinOp> conj_transpose() const override;
94
100
108
114
121
122protected:
123 void add_operators() {}
124
125 template <typename... Rest>
126 void add_operators(std::shared_ptr<const LinOp> coef,
127 std::shared_ptr<const LinOp> oper, Rest&&... rest)
128 {
129 GKO_ASSERT_EQUAL_DIMENSIONS(coef, dim<2>(1, 1));
130 GKO_ASSERT_EQUAL_DIMENSIONS(oper, this->get_size());
131 auto exec = this->get_executor();
132 coefficients_.push_back(std::move(coef));
133 operators_.push_back(std::move(oper));
134 if (coefficients_.back()->get_executor() != exec) {
135 coefficients_.back() = gko::clone(exec, coefficients_.back());
136 }
137 if (operators_.back()->get_executor() != exec) {
138 operators_.back() = gko::clone(exec, operators_.back());
139 }
140 add_operators(std::forward<Rest>(rest)...);
141 }
142
148 explicit Combination(std::shared_ptr<const Executor> exec)
149 : EnableLinOp<Combination>(exec)
150 {}
151
166 template <
167 typename CoefficientIterator, typename OperatorIterator,
168 typename = xstd::void_t<
169 typename std::iterator_traits<
171 typename std::iterator_traits<OperatorIterator>::iterator_category>>
176 : EnableLinOp<Combination>([&] {
178 throw OutOfBoundsError(__FILE__, __LINE__, 1, 0);
179 }
180 return (*operator_begin)->get_executor();
181 }())
182 {
183 GKO_ASSERT_EQ(std::distance(coefficient_begin, coefficient_end),
184 std::distance(operator_begin, operator_end));
185 this->set_size((*operator_begin)->get_size());
188 ++operator_it) {
189 add_operators(*coefficient_it, *operator_it);
191 }
192 }
193
204 template <typename... Rest>
205 explicit Combination(std::shared_ptr<const LinOp> coef,
206 std::shared_ptr<const LinOp> oper, Rest&&... rest)
208 {
209 this->set_size(oper->get_size());
210 add_operators(std::move(coef), std::move(oper),
211 std::forward<Rest>(rest)...);
212 }
213
214 void apply_impl(const LinOp* b, LinOp* x) const override;
215
216 void apply_impl(const LinOp* alpha, const LinOp* b, const LinOp* beta,
217 LinOp* x) const override;
218
219private:
220 std::vector<std::shared_ptr<const LinOp>> coefficients_;
221 std::vector<std::shared_ptr<const LinOp>> operators_;
222
223 // TODO: solve race conditions when multithreading
224 mutable struct cache_struct {
225 cache_struct() = default;
226 ~cache_struct() = default;
227 cache_struct(const cache_struct& other) {}
228 cache_struct& operator=(const cache_struct& other) { return *this; }
229
230 std::unique_ptr<LinOp> zero;
231 std::unique_ptr<LinOp> one;
232 std::unique_ptr<LinOp> intermediate_x;
233 } cache_;
234};
235
236
237} // namespace gko
238
239
240#endif // GKO_PUBLIC_CORE_BASE_COMBINATION_HPP_
The Combination class can be used to construct a linear combination of multiple linear operators c1 *...
Definition combination.hpp:61
Combination & operator=(const Combination &)
Copy-assigns a Combination.
std::unique_ptr< LinOp > conj_transpose() const override
Returns a LinOp representing the conjugate transpose of the Transposable object.
std::unique_ptr< LinOp > transpose() const override
Returns a LinOp representing the transpose of the Transposable object.
Combination(Combination &&)
Move-constructs a Combination.
Combination & operator=(Combination &&)
Move-assigns a Combination.
const std::vector< std::shared_ptr< const LinOp > > & get_operators() const noexcept
Returns a list of operators of the combination.
Definition combination.hpp:85
const std::vector< std::shared_ptr< const LinOp > > & get_coefficients() const noexcept
Returns a list of coefficients of the combination.
Definition combination.hpp:74
Combination(const Combination &)
Copy-constructs a Combination.
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
std::shared_ptr< const Executor > get_executor() const noexcept
Returns the Executor of the object.
Definition polymorphic_object.hpp:263
Linear operators which support transposition should implement the Transposable interface.
Definition lin_op.hpp:462
The Ginkgo namespace.
Definition abstract_factory.hpp:48
constexpr T one()
Returns the multiplicative identity for T.
Definition math.hpp:803
detail::cloned_type< Pointer > clone(const Pointer &p)
Creates a unique clone of the object pointed to by p.
Definition utils_helper.hpp:203
A type representing the dimensions of a multidimensional object.
Definition dim.hpp:55