Expression Templates Library (ETL)
generator_expr.hpp
Go to the documentation of this file.
1 //=======================================================================
2 // Copyright (c) 2014-2023 Baptiste Wicht
3 // Distributed under the terms of the MIT License.
4 // (See accompanying file LICENSE or copy at
5 // http://opensource.org/licenses/MIT)
6 //=======================================================================
7 
17 #pragma once
18 
19 namespace etl {
20 
26 template <typename Generator>
27 class generator_expr final {
28 private:
29  mutable Generator generator;
30 
31 public:
32  using value_type = typename Generator::value_type;
33 
38  template <typename... Args>
39  explicit generator_expr(Args&&... args) : generator(std::forward<Args>(args)...) {}
40 
41  generator_expr(const generator_expr& e) = default;
42  generator_expr(generator_expr&& e) noexcept = default;
43 
49  // Nothing else to init
50  }
51 
52  //Expression are invariant
53  generator_expr& operator=(const generator_expr& e) = delete;
54  generator_expr& operator=(generator_expr&& e) = delete;
55 
56  //Apply the expression
57 
63  value_type operator[]([[maybe_unused]] size_t i) const {
64  return generator();
65  }
66 
73  value_type read_flat([[maybe_unused]] size_t i) const {
74  return generator();
75  }
76 
81  template <typename Y>
82  decltype(auto) gpu_compute_hint(Y& y) const {
83  return generator.gpu_compute_hint(y);
84  }
85 
90  template <typename Y>
91  decltype(auto) gpu_compute(Y& y) const {
92  return generator.gpu_compute(y);
93  }
94 
100  template <typename E>
101  constexpr bool alias(const E& rhs) const noexcept {
102  return (void)rhs, false;
103  }
104 
110  return generator();
111  }
112 
117  const Generator& get_generator() const {
118  return generator;
119  }
120 
121  // Assignment functions
122 
127  template <typename L>
128  void assign_to(L&& lhs) const {
129  std_assign_evaluate(*this, std::forward<L>(lhs));
130  }
131 
136  template <typename L>
137  void assign_add_to(L&& lhs) const {
138  std_add_evaluate(*this, std::forward<L>(lhs));
139  }
140 
145  template <typename L>
146  void assign_sub_to(L&& lhs) const {
147  std_sub_evaluate(*this, std::forward<L>(lhs));
148  }
149 
154  template <typename L>
155  void assign_mul_to(L&& lhs) const {
156  std_mul_evaluate(*this, std::forward<L>(lhs));
157  }
158 
163  template <typename L>
164  void assign_div_to(L&& lhs) const {
165  std_div_evaluate(*this, std::forward<L>(lhs));
166  }
167 
172  template <typename L>
173  void assign_mod_to(L&& lhs) const {
174  std_mod_evaluate(*this, std::forward<L>(lhs));
175  }
176 
177  // Internals
178 
183  template <typename V>
184  void visit([[maybe_unused]] V&& visitor) const {}
185 
190  void ensure_cpu_up_to_date() const {
191  // Nothing to ensure
192  }
193 
198  void ensure_gpu_up_to_date() const {
199  // Nothing to ensure
200  }
201 
208  friend std::ostream& operator<<(std::ostream& os, const generator_expr& expr) {
209  return os << expr.get_generator();
210  }
211 };
212 
216 template <typename Generator>
217 struct etl_traits<etl::generator_expr<Generator>> {
219 
220  static constexpr bool is_etl = true;
221  static constexpr bool is_transformer = false;
222  static constexpr bool is_view = false;
223  static constexpr bool is_magic_view = false;
224  static constexpr bool is_linear = true;
225  static constexpr bool is_thread_safe = false;
226  static constexpr bool is_fast = true;
227  static constexpr bool is_value = false;
228  static constexpr bool is_direct = false;
229  static constexpr bool is_generator = true;
230  static constexpr bool is_temporary = false;
231  static constexpr bool is_padded = false;
232  static constexpr bool is_aligned = false;
233  static constexpr bool gpu_computable = Generator::gpu_computable;
234  static constexpr order storage_order = order::RowMajor;
235 
241  template <vector_mode_t V>
242  static constexpr bool vectorizable = false;
243 
247  static constexpr size_t size() {
248  return 0;
249  }
250 
254  static constexpr size_t dimensions() {
255  return 0;
256  }
257 
262  static constexpr int complexity() noexcept {
263  return 4;
264  }
265 };
266 
267 } //end of namespace etl
void ensure_gpu_up_to_date() const
Copy back from the GPU to the expression memory if necessary.
Definition: generator_expr.hpp:198
void assign_mul_to(L &&lhs) const
Multiply the given left-hand-side expression.
Definition: generator_expr.hpp:155
static constexpr int complexity() noexcept
Estimate the complexity of computation.
Definition: generator_expr.hpp:262
void std_assign_evaluate(Expr &&expr, Result &&result)
Evaluation of the expr into result.
Definition: evaluator.hpp:1176
void assign_mod_to(L &&lhs) const
Modulo the given left-hand-side expression.
Definition: generator_expr.hpp:173
void assign_add_to(L &&lhs) const
Add to the given left-hand-side expression.
Definition: generator_expr.hpp:137
static constexpr size_t size()
Return the size of the expression.
Definition: generator_expr.hpp:247
value_t< sub_type > value_type
The value contained in the expression.
Definition: dyn_matrix_view.hpp:31
void assign_to(L &&lhs) const
Assign to the given left-hand-side expression.
Definition: generator_expr.hpp:128
constexpr bool is_magic_view
Traits indicating if the given ETL type is a magic view expression.
Definition: traits.hpp:311
const Generator & get_generator() const
Returns a reference to the generator op.
Definition: generator_expr.hpp:117
friend std::ostream & operator<<(std::ostream &os, const generator_expr &expr)
Outputs the expression to the given stream.
Definition: generator_expr.hpp:208
order
Storage order of a matrix.
Definition: order.hpp:15
decltype(auto) gpu_compute(Y &y) const
Return a GPU computed version of this expression.
Definition: generator_expr.hpp:91
constexpr bool alias(const E &rhs) const noexcept
Test if this expression aliases with the given expression.
Definition: generator_expr.hpp:101
value_type operator()() const
Apply the functor.
Definition: generator_expr.hpp:109
decltype(auto) gpu_compute_hint(Y &y) const
Return a GPU computed version of this expression.
Definition: generator_expr.hpp:82
constexpr bool is_fast
Traits to test if the given ETL expresion type is fast (sizes known at compile-time) ...
Definition: traits.hpp:588
void visit([[maybe_unused]] V &&visitor) const
Apply the given visitor to this expression and its descendants.
Definition: generator_expr.hpp:184
Traits to get information about ETL types.
Definition: tmp.hpp:68
value_type read_flat([[maybe_unused]] size_t i) const
Returns the value at the given index This function never alters the state of the container.
Definition: generator_expr.hpp:73
Root namespace for the ETL library.
Definition: adapter.hpp:15
value_type operator[]([[maybe_unused]] size_t i) const
Returns the element at the given index.
Definition: generator_expr.hpp:63
void assign_div_to(L &&lhs) const
Divide the given left-hand-side expression.
Definition: generator_expr.hpp:164
typename Generator::value_type value_type
The type of value generated.
Definition: generator_expr.hpp:32
void ensure_cpu_up_to_date() const
Ensures that the GPU memory is allocated and that the GPU memory is up to date (to undefined value)...
Definition: generator_expr.hpp:190
void std_mod_evaluate(Expr &&expr, Result &&result)
Compound modulo evaluation of the expr into result.
Definition: evaluator.hpp:1271
static constexpr size_t dimensions()
Return the number of dimensions of the expression.
Definition: generator_expr.hpp:254
void std_mul_evaluate(Expr &&expr, Result &&result)
Compound multiply evaluation of the expr into result.
Definition: evaluator.hpp:1233
void assign_sub_to(L &&lhs) const
Sub from the given left-hand-side expression.
Definition: generator_expr.hpp:146
constexpr bool is_transformer
Traits indicating if the given ETL type is a transformer expression.
Definition: traits.hpp:297
constexpr bool is_view
Traits indicating if the given ETL type is a view expression.
Definition: traits.hpp:304
void std_sub_evaluate(Expr &&expr, Result &&result)
Compound subtract evaluation of the expr into result.
Definition: evaluator.hpp:1214
constexpr bool is_thread_safe
Traits to test if the given ETL expresion type is thread safe.
Definition: traits.hpp:687
void std_div_evaluate(Expr &&expr, Result &&result)
Compound divide evaluation of the expr into result.
Definition: evaluator.hpp:1252
generator_expr(Args &&... args)
Construct a generator expression and forward the arguments to the generator.
Definition: generator_expr.hpp:39
Row-Major storage.
generator_expr(generator_expr &e)
Copy constructor for non-const argument. This simply forwards to the regular copy constructor to avoi...
Definition: generator_expr.hpp:48
typename Generator::value_type value_type
The value type.
Definition: generator_expr.hpp:218
A generator expression.
Definition: generator_expr.hpp:27
void std_add_evaluate(Expr &&expr, Result &&result)
Compound add evaluation of the expr into result.
Definition: evaluator.hpp:1195