Ginkgo  Generated from pipelines/3136675824 branch based on develop. Ginkgo version 2.0.0
A numerical linear algebra library targeting many-core architectures
composition.hpp
1 // SPDX-FileCopyrightText: 2017 - 2026 The Ginkgo authors
2 //
3 // SPDX-License-Identifier: BSD-3-Clause
4 
5 #ifndef GKO_PUBLIC_CORE_BASE_COMPOSITION_HPP_
6 #define GKO_PUBLIC_CORE_BASE_COMPOSITION_HPP_
7 
8 
9 #include <type_traits>
10 #include <vector>
11 
12 #include <ginkgo/core/base/executor.hpp>
13 #include <ginkgo/core/base/lin_op.hpp>
14 
15 
16 namespace gko {
17 
18 
38 template <typename ValueType = default_precision>
39 class Composition : public LinOp,
40  public EnableCreateMethod<Composition<ValueType>>,
41  public Transposable {
42  friend class EnableCreateMethod<Composition>;
43  GKO_ASSERT_SUPPORTED_VALUE_TYPE;
44 
45 public:
46  using value_type = ValueType;
48 
54  const std::vector<std::shared_ptr<const LinOp>>& get_operators()
55  const noexcept
56  {
57  return operators_;
58  }
59 
60  std::unique_ptr<LinOp> transpose() const override;
61 
62  std::unique_ptr<LinOp> conj_transpose() const override;
63 
69 
77 
82  Composition(const Composition&);
83 
90 
91 protected:
92  void add_operators() {}
93 
94  template <typename... Rest>
95  void add_operators(std::shared_ptr<const LinOp> oper, Rest&&... rest)
96  {
97  if (!operators_.empty()) {
98  GKO_ASSERT_CONFORMANT(this, oper);
99  }
100  auto exec = this->get_executor();
101  operators_.push_back(std::move(oper));
102  if (operators_.back()->get_executor() != exec) {
103  operators_.back() = gko::clone(exec, operators_.back());
104  }
105  this->set_size(dim<2>{operators_.front()->get_size()[0],
106  operators_.back()->get_size()[1]});
107  add_operators(std::forward<Rest>(rest)...);
108  }
109 
115  explicit Composition(std::shared_ptr<const Executor> exec)
116  : LinOp(exec), storage_{exec}
117  {}
118 
128  template <typename Iterator,
129  typename = std::void_t<
130  typename std::iterator_traits<Iterator>::iterator_category>>
131  explicit Composition(Iterator begin, Iterator end)
132  : LinOp([&] {
133  if (begin == end) {
134  throw OutOfBoundsError(__FILE__, __LINE__, 1, 0);
135  }
136  return (*begin)->get_executor();
137  }()),
138  storage_{this->get_executor()}
139  {
140  for (auto it = begin; it != end; ++it) {
141  add_operators(*it);
142  }
143  }
144 
153  template <typename... Rest>
154  explicit Composition(std::shared_ptr<const LinOp> oper, Rest&&... rest)
155  : Composition(oper->get_executor())
156  {
157  add_operators(std::move(oper), std::forward<Rest>(rest)...);
158  }
159 
160  void apply_impl(const AbstractMultiVector* b,
161  AbstractMultiVector* x) const override;
162 
163  void apply_impl(const AbstractMultiVector* alpha,
164  const AbstractMultiVector* b,
165  const AbstractMultiVector* beta,
166  AbstractMultiVector* x) const override;
167 
168 private:
169  std::vector<std::shared_ptr<const LinOp>> operators_;
170  mutable array<ValueType> storage_;
171 };
172 
173 
180 template <typename ValueType = default_precision>
182 public:
183  using value_type = ValueType;
189  std::shared_ptr<Composition<ValueType>> get_composition() const
190  {
191  return composition_;
192  }
193 
206  std::shared_ptr<const LinOp> get_operator_at(size_type index) const
207  {
208  if (composition_ == nullptr) {
209  return nullptr;
210  } else {
211  return composition_->get_operators().at(index);
212  }
213  }
214 
215 protected:
219  template <typename... LinOp>
220  void set_composition(LinOp&&... linop)
221  {
222  composition_ =
223  Composition<ValueType>::create(std::forward<LinOp>(linop)...);
224  }
225 
226 private:
227  std::shared_ptr<Composition<ValueType>> composition_;
228 };
229 
230 
231 } // namespace gko
232 
233 
234 #endif // GKO_PUBLIC_CORE_BASE_COMPOSITION_HPP_
gko::Composition::operator=
Composition & operator=(const Composition &)
Copy-assigns a Composition.
gko::UseComposition
The UseComposition class can be used to store the composition information in LinOp.
Definition: composition.hpp:181
gko::LinOp
Definition: lin_op.hpp:163
gko::EnableCreateMethod
This mixin implements a static create() method on ConcreteType that dynamically allocates the memory,...
Definition: polymorphic_object.hpp:497
gko::Transposable
Linear operators which support transposition should implement the Transposable interface.
Definition: lin_op.hpp:596
gko::size_type
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:102
gko::UseComposition::get_composition
std::shared_ptr< Composition< ValueType > > get_composition() const
Returns the composition operators.
Definition: composition.hpp:189
gko::clone
detail::cloned_type< Pointer > clone(const Pointer &p)
Creates a unique clone of the object pointed to by p.
Definition: utils_helper.hpp:425
gko
The Ginkgo namespace.
Definition: abstract_factory.hpp:19
gko::Composition
The Composition class can be used to compose linear operators op1, op2, ..., opn and obtain the opera...
Definition: composition.hpp:39
gko::Composition::Composition
Composition(const Composition &)
Copy-constructs a Composition.
gko::Composition::get_operators
const std::vector< std::shared_ptr< const LinOp > > & get_operators() const noexcept
Returns a list of operators of the composition.
Definition: composition.hpp:54
gko::UseComposition::get_operator_at
std::shared_ptr< const LinOp > get_operator_at(size_type index) const
Returns the operator at index-th position of composition.
Definition: composition.hpp:206
gko::Composition::conj_transpose
std::unique_ptr< LinOp > conj_transpose() const override
Returns a LinOp representing the conjugate transpose of the Transposable object.
gko::PolymorphicObject::get_executor
std::shared_ptr< const Executor > get_executor() const noexcept
Returns the Executor of the object.
Definition: polymorphic_object.hpp:69
gko::Composition::transpose
std::unique_ptr< LinOp > transpose() const override
Returns a LinOp representing the transpose of the Transposable object.
gko::LinOp::LinOp
LinOp(const LinOp &)=default
Copy-constructs a LinOp.