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
composition.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_COMPOSITION_HPP_
34#define GKO_PUBLIC_CORE_BASE_COMPOSITION_HPP_
35
36
37#include <vector>
38
39
40#include <ginkgo/core/base/executor.hpp>
41#include <ginkgo/core/base/lin_op.hpp>
42
43
44namespace gko {
45
46
66template <typename ValueType = default_precision>
67class Composition : public EnableLinOp<Composition<ValueType>>,
68 public EnableCreateMethod<Composition<ValueType>>,
69 public Transposable {
71 friend class EnableCreateMethod<Composition>;
72
73public:
74 using value_type = ValueType;
76
82 const std::vector<std::shared_ptr<const LinOp>>& get_operators()
84 {
85 return operators_;
86 }
87
88 std::unique_ptr<LinOp> transpose() const override;
89
90 std::unique_ptr<LinOp> conj_transpose() const override;
91
97
105
111
118
119protected:
120 void add_operators() {}
121
122 template <typename... Rest>
123 void add_operators(std::shared_ptr<const LinOp> oper, Rest&&... rest)
124 {
125 if (!operators_.empty()) {
126 GKO_ASSERT_CONFORMANT(this, oper);
127 }
128 auto exec = this->get_executor();
129 operators_.push_back(std::move(oper));
130 if (operators_.back()->get_executor() != exec) {
131 operators_.back() = gko::clone(exec, operators_.back());
132 }
133 this->set_size(dim<2>{operators_.front()->get_size()[0],
134 operators_.back()->get_size()[1]});
135 add_operators(std::forward<Rest>(rest)...);
136 }
137
143 explicit Composition(std::shared_ptr<const Executor> exec)
144 : EnableLinOp<Composition>(exec), storage_{exec}
145 {}
146
156 template <typename Iterator,
157 typename = xstd::void_t<
158 typename std::iterator_traits<Iterator>::iterator_category>>
159 explicit Composition(Iterator begin, Iterator end)
160 : EnableLinOp<Composition>([&] {
161 if (begin == end) {
162 throw OutOfBoundsError(__FILE__, __LINE__, 1, 0);
163 }
164 return (*begin)->get_executor();
165 }()),
166 storage_{this->get_executor()}
167 {
168 for (auto it = begin; it != end; ++it) {
169 add_operators(*it);
170 }
171 }
172
181 template <typename... Rest>
182 explicit Composition(std::shared_ptr<const LinOp> oper, Rest&&... rest)
184 {
185 add_operators(std::move(oper), std::forward<Rest>(rest)...);
186 }
187
188 void apply_impl(const LinOp* b, LinOp* x) const override;
189
190 void apply_impl(const LinOp* alpha, const LinOp* b, const LinOp* beta,
191 LinOp* x) const override;
192
193private:
194 std::vector<std::shared_ptr<const LinOp>> operators_;
195 mutable array<ValueType> storage_;
196};
197
198
205template <typename ValueType = default_precision>
207public:
208 using value_type = ValueType;
214 std::shared_ptr<Composition<ValueType>> get_composition() const
215 {
216 return composition_;
217 }
218
231 std::shared_ptr<const LinOp> get_operator_at(size_type index) const
232 {
233 if (composition_ == nullptr) {
234 return nullptr;
235 } else {
236 return composition_->get_operators().at(index);
237 }
238 }
239
240protected:
244 template <typename... LinOp>
245 void set_composition(LinOp&&... linop)
246 {
247 composition_ =
248 Composition<ValueType>::create(std::forward<LinOp>(linop)...);
249 }
250
251private:
252 std::shared_ptr<Composition<ValueType>> composition_;
253};
254
255
256} // namespace gko
257
258
259#endif // GKO_PUBLIC_CORE_BASE_COMPOSITION_HPP_
The Composition class can be used to compose linear operators op1, op2, ..., opn and obtain the opera...
Definition composition.hpp:69
Composition & operator=(const Composition &)
Copy-assigns a Composition.
Composition(Composition &&)
Move-constructs a Composition.
std::unique_ptr< LinOp > transpose() const override
Returns a LinOp representing the transpose of the Transposable object.
Composition(const Composition &)
Copy-constructs a Composition.
Composition & operator=(Composition &&)
Move-assigns a Composition.
std::unique_ptr< LinOp > conj_transpose() const override
Returns a LinOp representing the conjugate transpose of the Transposable object.
const std::vector< std::shared_ptr< const LinOp > > & get_operators() const noexcept
Returns a list of operators of the composition.
Definition composition.hpp:82
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 UseComposition class can be used to store the composition information in LinOp.
Definition composition.hpp:206
std::shared_ptr< const LinOp > get_operator_at(size_type index) const
Returns the operator at index-th position of composition.
Definition composition.hpp:231
std::shared_ptr< Composition< ValueType > > get_composition() const
Returns the composition operators.
Definition composition.hpp:214
The Ginkgo namespace.
Definition abstract_factory.hpp:48
constexpr T one()
Returns the multiplicative identity for T.
Definition math.hpp:803
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:120
detail::cloned_type< Pointer > clone(const Pointer &p)
Creates a unique clone of the object pointed to by p.
Definition utils_helper.hpp:203