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
composition.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_COMPOSITION_HPP_
34 #define GKO_CORE_BASE_COMPOSITION_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 Composition : public EnableLinOp<Composition<ValueType>>,
56  public EnableCreateMethod<Composition<ValueType>> {
57  friend class EnablePolymorphicObject<Composition, LinOp>;
58  friend class EnableCreateMethod<Composition>;
59 
60 public:
61  using value_type = ValueType;
62 
68  const std::vector<std::shared_ptr<const LinOp>> &get_operators() const
69  noexcept
70  {
71  return operators_;
72  }
73 
74 protected:
80  explicit Composition(std::shared_ptr<const Executor> exec)
82  {}
83 
93  template <typename Iterator,
94  typename = xstd::void_t<
95  typename std::iterator_traits<Iterator>::iterator_category>>
96  explicit Composition(Iterator begin, Iterator end)
98  if (begin == end) {
99  throw OutOfBoundsError(__FILE__, __LINE__, 1, 0);
100  }
101  return (*begin)->get_executor();
102  }()),
103  operators_(begin, end)
104  {
105  this->set_size(gko::dim<2>{operators_.front()->get_size()[0],
106  operators_.back()->get_size()[1]});
107  for (size_type i = 1; i < operators_.size(); ++i) {
108  GKO_ASSERT_CONFORMANT(operators_[i - 1], operators_[i]);
109  }
110  }
111 
120  template <typename... Rest>
121  explicit Composition(std::shared_ptr<const LinOp> oper, Rest &&... rest)
122  : Composition(std::forward<Rest>(rest)...)
123  {
124  GKO_ASSERT_CONFORMANT(oper, operators_[0]);
125  operators_.insert(begin(operators_), oper);
126  this->set_size(gko::dim<2>{operators_.front()->get_size()[0],
127  operators_.back()->get_size()[1]});
128  }
129 
138  explicit Composition(std::shared_ptr<const LinOp> oper)
139  : EnableLinOp<Composition>(oper->get_executor(), oper->get_size()),
140  operators_{oper}
141  {}
142 
143  void apply_impl(const LinOp *b, LinOp *x) const override;
144 
145  void apply_impl(const LinOp *alpha, const LinOp *b, const LinOp *beta,
146  LinOp *x) const override;
147 
148 private:
149  std::vector<std::shared_ptr<const LinOp>> operators_;
150 
151  // TODO: solve race conditions when multithreading
152  mutable struct cache_struct {
153  cache_struct() = default;
154  cache_struct(const cache_struct &other) {}
155  cache_struct &operator=(const cache_struct &other) { return *this; }
156 
157  // TODO: reduce the amount of intermediate vectors we need (careful --
158  // not all of them are of the same size)
159  std::vector<std::unique_ptr<LinOp>> intermediate;
160  } cache_;
161 };
162 
163 
164 } // namespace gko
165 
166 
167 #endif // GKO_CORE_BASE_COMPOSITION_HPP_
This mixin implements a static create() method on ConcreteType that dynamically allocates the memory...
Definition: polymorphic_object.hpp:576
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition: polymorphic_object.hpp:505
The Composition class can be used to compose linear operators op1, op2, ..., opn and obtain the opera...
Definition: composition.hpp:55
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:94
The Ginkgo namespace.
Definition: abstract_factory.hpp:45
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 std::vector< std::shared_ptr< const LinOp > > & get_operators() const noexcept
Returns a list of operators of the composition.
Definition: composition.hpp:68