Ginkgo  Generated from pipelines/2171896597 branch based on develop. Ginkgo version 1.11.0
A numerical linear algebra library targeting many-core architectures
composition.hpp
1 // SPDX-FileCopyrightText: 2017 - 2025 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 EnableLinOp<Composition<ValueType>>,
40  public EnableCreateMethod<Composition<ValueType>>,
41  public Transposable {
43  friend class EnableCreateMethod<Composition>;
44  GKO_ASSERT_SUPPORTED_VALUE_TYPE;
45 
46 public:
47  using value_type = ValueType;
49 
55  const std::vector<std::shared_ptr<const LinOp>>& get_operators()
56  const noexcept
57  {
58  return operators_;
59  }
60 
61  std::unique_ptr<LinOp> transpose() const override;
62 
63  std::unique_ptr<LinOp> conj_transpose() const override;
64 
70 
78 
83  Composition(const Composition&);
84 
91 
92 protected:
93  void add_operators() {}
94 
95  template <typename... Rest>
96  void add_operators(std::shared_ptr<const LinOp> oper, Rest&&... rest)
97  {
98  if (!operators_.empty()) {
99  GKO_ASSERT_CONFORMANT(this, oper);
100  }
101  auto exec = this->get_executor();
102  operators_.push_back(std::move(oper));
103  if (operators_.back()->get_executor() != exec) {
104  operators_.back() = gko::clone(exec, operators_.back());
105  }
106  this->set_size(dim<2>{operators_.front()->get_size()[0],
107  operators_.back()->get_size()[1]});
108  add_operators(std::forward<Rest>(rest)...);
109  }
110 
116  explicit Composition(std::shared_ptr<const Executor> exec)
117  : EnableLinOp<Composition>(exec), storage_{exec}
118  {}
119 
129  template <typename Iterator,
130  typename = std::void_t<
131  typename std::iterator_traits<Iterator>::iterator_category>>
132  explicit Composition(Iterator begin, Iterator end)
133  : EnableLinOp<Composition>([&] {
134  if (begin == end) {
135  throw OutOfBoundsError(__FILE__, __LINE__, 1, 0);
136  }
137  return (*begin)->get_executor();
138  }()),
139  storage_{this->get_executor()}
140  {
141  for (auto it = begin; it != end; ++it) {
142  add_operators(*it);
143  }
144  }
145 
154  template <typename... Rest>
155  explicit Composition(std::shared_ptr<const LinOp> oper, Rest&&... rest)
156  : Composition(oper->get_executor())
157  {
158  add_operators(std::move(oper), std::forward<Rest>(rest)...);
159  }
160 
161  void apply_impl(const LinOp* b, LinOp* x) const override;
162 
163  void apply_impl(const LinOp* alpha, const LinOp* b, const LinOp* beta,
164  LinOp* x) const override;
165 
166 private:
167  std::vector<std::shared_ptr<const LinOp>> operators_;
168  mutable array<ValueType> storage_;
169 };
170 
171 
178 template <typename ValueType = default_precision>
180 public:
181  using value_type = ValueType;
187  std::shared_ptr<Composition<ValueType>> get_composition() const
188  {
189  return composition_;
190  }
191 
204  std::shared_ptr<const LinOp> get_operator_at(size_type index) const
205  {
206  if (composition_ == nullptr) {
207  return nullptr;
208  } else {
209  return composition_->get_operators().at(index);
210  }
211  }
212 
213 protected:
217  template <typename... LinOp>
218  void set_composition(LinOp&&... linop)
219  {
220  composition_ =
221  Composition<ValueType>::create(std::forward<LinOp>(linop)...);
222  }
223 
224 private:
225  std::shared_ptr<Composition<ValueType>> composition_;
226 };
227 
228 
229 } // namespace gko
230 
231 
232 #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:179
gko::LinOp
Definition: lin_op.hpp:117
gko::EnableCreateMethod
This mixin implements a static create() method on ConcreteType that dynamically allocates the memory,...
Definition: polymorphic_object.hpp:767
gko::Transposable
Linear operators which support transposition should implement the Transposable interface.
Definition: lin_op.hpp:433
gko::size_type
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:90
gko::UseComposition::get_composition
std::shared_ptr< Composition< ValueType > > get_composition() const
Returns the composition operators.
Definition: composition.hpp:187
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:173
gko
The Ginkgo namespace.
Definition: abstract_factory.hpp:20
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:55
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:204
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:243
gko::EnableLinOp
The EnableLinOp mixin can be used to provide sensible default implementations of the majority of the ...
Definition: lin_op.hpp:877
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.
gko::EnablePolymorphicObject
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition: polymorphic_object.hpp:667