Ginkgo  Generated from pipelines/3136675824 branch based on develop. Ginkgo version 2.0.0
A numerical linear algebra library targeting many-core architectures
combination.hpp
1 // SPDX-FileCopyrightText: 2017 - 2026 The Ginkgo authors
2 //
3 // SPDX-License-Identifier: BSD-3-Clause
4 
5 #ifndef GKO_PUBLIC_CORE_BASE_COMBINATION_HPP_
6 #define GKO_PUBLIC_CORE_BASE_COMBINATION_HPP_
7 
8 
9 #include <type_traits>
10 #include <vector>
11 
12 #include <ginkgo/core/base/lin_op.hpp>
13 #include <ginkgo/core/matrix/multivector.hpp>
14 
15 
16 namespace gko {
17 
18 
31 template <typename ValueType = default_precision>
32 class Combination : public LinOp,
33  public EnableCreateMethod<Combination<ValueType>>,
34  public Transposable {
35  friend class EnableCreateMethod<Combination>;
36  GKO_ASSERT_SUPPORTED_VALUE_TYPE;
37 
38 public:
39  using value_type = ValueType;
41 
47  const std::vector<std::shared_ptr<const matrix::MultiVector<ValueType>>>&
48  get_coefficients() const noexcept
49  {
50  return coefficients_;
51  }
52 
58  const std::vector<std::shared_ptr<const LinOp>>& get_operators()
59  const noexcept
60  {
61  return operators_;
62  }
63 
64  std::unique_ptr<LinOp> transpose() const override;
65 
66  std::unique_ptr<LinOp> conj_transpose() const override;
67 
73 
81 
86  Combination(const Combination&);
87 
94 
95 protected:
96  void add_operators() {}
97 
98  template <typename... Rest>
99  void add_operators(
100  std::shared_ptr<const matrix::MultiVector<ValueType>> coef,
101  std::shared_ptr<const LinOp> oper, Rest&&... rest)
102  {
103  GKO_ASSERT_EQUAL_DIMENSIONS(coef, dim<2>(1, 1));
104  GKO_ASSERT_EQUAL_DIMENSIONS(oper, this->get_size());
105  auto exec = this->get_executor();
106  coefficients_.push_back(std::move(coef));
107  operators_.push_back(std::move(oper));
108  if (coefficients_.back()->get_executor() != exec) {
109  coefficients_.back() = gko::clone(exec, coefficients_.back());
110  }
111  if (operators_.back()->get_executor() != exec) {
112  operators_.back() = gko::clone(exec, operators_.back());
113  }
114  add_operators(std::forward<Rest>(rest)...);
115  }
116 
122  explicit Combination(std::shared_ptr<const Executor> exec) : LinOp(exec) {}
123 
138  template <
139  typename CoefficientIterator, typename OperatorIterator,
140  typename = std::void_t<
141  typename std::iterator_traits<
142  CoefficientIterator>::iterator_category,
143  typename std::iterator_traits<OperatorIterator>::iterator_category>>
144  explicit Combination(CoefficientIterator coefficient_begin,
145  CoefficientIterator coefficient_end,
146  OperatorIterator operator_begin,
147  OperatorIterator operator_end)
148  : LinOp([&] {
149  if (operator_begin == operator_end) {
150  throw OutOfBoundsError(__FILE__, __LINE__, 1, 0);
151  }
152  return (*operator_begin)->get_executor();
153  }())
154  {
155  GKO_ASSERT_EQ(std::distance(coefficient_begin, coefficient_end),
156  std::distance(operator_begin, operator_end));
157  this->set_size((*operator_begin)->get_size());
158  auto coefficient_it = coefficient_begin;
159  for (auto operator_it = operator_begin; operator_it != operator_end;
160  ++operator_it) {
161  add_operators(*coefficient_it, *operator_it);
162  ++coefficient_it;
163  }
164  }
165 
176  template <typename... Rest>
177  explicit Combination(
178  std::shared_ptr<const matrix::MultiVector<ValueType>> coef,
179  std::shared_ptr<const LinOp> oper, Rest&&... rest)
180  : Combination(oper->get_executor())
181  {
182  this->set_size(oper->get_size());
183  add_operators(std::move(coef), std::move(oper),
184  std::forward<Rest>(rest)...);
185  }
186 
187  void apply_impl(const AbstractMultiVector* b,
188  AbstractMultiVector* x) const override;
189 
190  void apply_impl(const AbstractMultiVector* alpha,
191  const AbstractMultiVector* b,
192  const AbstractMultiVector* beta,
193  AbstractMultiVector* x) const override;
194 
195 private:
196  std::vector<std::shared_ptr<const matrix::MultiVector<ValueType>>>
197  coefficients_;
198  std::vector<std::shared_ptr<const LinOp>> operators_;
199 
200  // TODO: solve race conditions when multithreading
201  mutable struct cache_struct {
202  cache_struct() = default;
203  ~cache_struct() = default;
204  cache_struct(const cache_struct& other) {}
205  cache_struct& operator=(const cache_struct& other) { return *this; }
206 
207  std::unique_ptr<matrix::MultiVector<ValueType>> zero;
208  std::unique_ptr<matrix::MultiVector<ValueType>> one;
209  std::unique_ptr<AbstractMultiVector> intermediate_x;
210  } cache_;
211 };
212 
213 
214 } // namespace gko
215 
216 
217 #endif // GKO_PUBLIC_CORE_BASE_COMBINATION_HPP_
gko::Combination
The Combination class can be used to construct a linear combination of multiple linear operators c1 *...
Definition: combination.hpp:32
gko::LinOp
Definition: lin_op.hpp:163
gko::Combination::get_coefficients
const std::vector< std::shared_ptr< const matrix::MultiVector< ValueType > > > & get_coefficients() const noexcept
Returns a list of coefficients of the combination.
Definition: combination.hpp:48
gko::EnableCreateMethod
This mixin implements a static create() method on ConcreteType that dynamically allocates the memory,...
Definition: polymorphic_object.hpp:497
gko::Combination::transpose
std::unique_ptr< LinOp > transpose() const override
Returns a LinOp representing the transpose of the Transposable object.
gko::Transposable
Linear operators which support transposition should implement the Transposable interface.
Definition: lin_op.hpp:596
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::Combination::Combination
Combination(const Combination &)
Copy-constructs a Combination.
gko::Combination::operator=
Combination & operator=(const Combination &)
Copy-assigns a Combination.
gko::Combination::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::LinOp::get_size
const dim< 2 > & get_size() const noexcept
Returns the size of the operator.
Definition: lin_op.hpp:259
gko::Combination::get_operators
const std::vector< std::shared_ptr< const LinOp > > & get_operators() const noexcept
Returns a list of operators of the combination.
Definition: combination.hpp:58
gko::LinOp::LinOp
LinOp(const LinOp &)=default
Copy-constructs a LinOp.
gko::zero
constexpr T zero()
Returns the additive identity for T.
Definition: math.hpp:625
gko::one
constexpr T one()
Returns the multiplicative identity for T.
Definition: math.hpp:653