Ginkgo  Generated from tags/v1.0.0^0 branch based on master. Ginkgo version 1.0.0
A numerical linear algebra library targeting many-core architectures
combination.hpp
1 /*******************************<GINKGO LICENSE>******************************
2 Copyright (c) 2017-2019, the Ginkgo authors
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8 
9 1. Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 
12 2. Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
15 
16 3. Neither the name of the copyright holder nor the names of its
17 contributors may be used to endorse or promote products derived from
18 this software without specific prior written permission.
19 
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 ******************************<GINKGO LICENSE>*******************************/
32 
33 #ifndef GKO_CORE_BASE_COMBINATION_HPP_
34 #define GKO_CORE_BASE_COMBINATION_HPP_
35 
36 
37 #include <vector>
38 
39 
40 #include <ginkgo/core/base/lin_op.hpp>
41 
42 
43 namespace gko {
44 
45 
54 template <typename ValueType = default_precision>
55 class Combination : public EnableLinOp<Combination<ValueType>>,
56  public EnableCreateMethod<Combination<ValueType>> {
57  friend class EnablePolymorphicObject<Combination, LinOp>;
58  friend class EnableCreateMethod<Combination>;
59 
60 public:
61  using value_type = ValueType;
62 
68  const std::vector<std::shared_ptr<const LinOp>> &get_coefficients() const
69  noexcept
70  {
71  return coefficients_;
72  }
73 
79  const std::vector<std::shared_ptr<const LinOp>> &get_operators() const
80  noexcept
81  {
82  return operators_;
83  }
84 
85 protected:
91  explicit Combination(std::shared_ptr<const Executor> exec)
93  {}
94 
109  template <
110  typename CoefficientIterator, typename OperatorIterator,
111  typename = xstd::void_t<
112  typename std::iterator_traits<
113  CoefficientIterator>::iterator_category,
114  typename std::iterator_traits<OperatorIterator>::iterator_category>>
115  explicit Combination(CoefficientIterator coefficient_begin,
116  CoefficientIterator coefficient_end,
117  OperatorIterator operator_begin,
118  OperatorIterator operator_end)
120  if (operator_begin == operator_end) {
121  throw OutOfBoundsError(__FILE__, __LINE__, 1, 0);
122  }
123  return (*operator_begin)->get_executor();
124  }()),
125  coefficients_(coefficient_begin, coefficient_end),
126  operators_(operator_begin, operator_end)
127  {
128  for (const auto &c : coefficients_) {
129  GKO_ASSERT_EQUAL_DIMENSIONS(c, dim<2>(1, 1));
130  }
131  this->set_size(operators_[0]->get_size());
132  for (const auto &o : operators_) {
133  GKO_ASSERT_EQUAL_DIMENSIONS(o, this->get_size());
134  }
135  }
136 
147  template <typename... Rest>
148  explicit Combination(std::shared_ptr<const LinOp> coef,
149  std::shared_ptr<const LinOp> oper, Rest &&... rest)
150  : Combination(std::forward<Rest>(rest)...)
151  {
152  GKO_ASSERT_EQUAL_DIMENSIONS(coef, dim<2>(1, 1));
153  GKO_ASSERT_EQUAL_DIMENSIONS(oper, this->get_size());
154  coefficients_.insert(begin(coefficients_), coef);
155  operators_.insert(begin(operators_), oper);
156  }
157 
171  explicit Combination(std::shared_ptr<const LinOp> coef,
172  std::shared_ptr<const LinOp> oper)
173  : EnableLinOp<Combination>(oper->get_executor(), oper->get_size()),
174  coefficients_{coef},
175  operators_{oper}
176  {}
177 
178  void apply_impl(const LinOp *b, LinOp *x) const override;
179 
180  void apply_impl(const LinOp *alpha, const LinOp *b, const LinOp *beta,
181  LinOp *x) const override;
182 
183 private:
184  std::vector<std::shared_ptr<const LinOp>> coefficients_;
185  std::vector<std::shared_ptr<const LinOp>> operators_;
186 
187  // TODO: solve race conditions when multithreading
188  mutable struct cache_struct {
189  cache_struct() = default;
190  cache_struct(const cache_struct &other) {}
191  cache_struct &operator=(const cache_struct &other) { return *this; }
192 
193  std::unique_ptr<LinOp> zero;
194  std::unique_ptr<LinOp> one;
195  std::unique_ptr<LinOp> intermediate_x;
196  } cache_;
197 };
198 
199 
200 } // namespace gko
201 
202 
203 #endif // GKO_CORE_BASE_COMBINATION_HPP_
This mixin implements a static create() method on ConcreteType that dynamically allocates the memory...
Definition: polymorphic_object.hpp:576
constexpr T zero()
Returns the additive identity for T.
Definition: math.hpp:292
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition: polymorphic_object.hpp:505
The Ginkgo namespace.
Definition: abstract_factory.hpp:45
const std::vector< std::shared_ptr< const LinOp > > & get_coefficients() const noexcept
Returns a list of coefficients of the combination.
Definition: combination.hpp:68
OutOfBoundsError is thrown if a memory access is detected to be out-of-bounds.
Definition: exception.hpp:297
The EnableLinOp mixin can be used to provide sensible default implementations of the majority of the ...
Definition: lin_op.hpp:509
const dim< 2 > & get_size() const noexcept
Returns the size of the operator.
Definition: lin_op.hpp:221
const std::vector< std::shared_ptr< const LinOp > > & get_operators() const noexcept
Returns a list of operators of the combination.
Definition: combination.hpp:79
The Combination class can be used to construct a linear combination of multiple linear operators `c1 ...
Definition: combination.hpp:55
constexpr T one()
Returns the multiplicative identity for T.
Definition: math.hpp:319