Expression Templates Library (ETL)
adapter.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 
13 #pragma once
14 
15 namespace etl {
16 
20 template <typename Matrix>
21 struct adapter {
22  using matrix_t = Matrix;
23  using expr_t = matrix_t;
24 
27  using const_memory_type = const value_type*;
28 
32  template <typename V = default_vec>
33  using vec_type = typename V::template vec_type<value_type>;
34 
35 protected:
37 
38 public:
44  adapter() noexcept : value(value_type()) {
45  //Nothing else to init
46  }
47 
55  explicit adapter(value_type value) noexcept : value(value) {
56  //Nothing else to init
57  }
58 
63  explicit adapter(size_t dim) noexcept : value(dim, dim, value_type()) {
64  //Nothing else to init
65  }
66 
73  adapter(size_t dim, value_type value) noexcept : value(dim, dim, value) {
74  //Nothing else to init
75  }
76 
81  adapter(const adapter& rhs) = default;
82 
88  adapter& operator=(const adapter& rhs) = default;
89 
94  adapter(adapter&& rhs) noexcept = default;
95 
101  adapter& operator=(adapter&& rhs) noexcept = default;
102 
111  const value_type& operator()(size_t i, size_t j) const noexcept {
112  return value(i, j);
113  }
114 
120  const value_type& operator[](size_t i) const noexcept {
121  return value[i];
122  }
123 
129  value_type& operator[](size_t i) noexcept {
130  return value[i];
131  }
132 
139  value_type read_flat(size_t i) const noexcept {
140  return value.read_flat(i);
141  }
142 
151  return value.memory_start();
152  }
153 
161  const_memory_type memory_start() const noexcept {
162  return value.memory_start();
163  }
164 
172  memory_type memory_end() noexcept {
173  return value.memory_end();
174  }
175 
183  const_memory_type memory_end() const noexcept {
184  return value.memory_end();
185  }
186 
193  template <typename V = default_vec>
194  vec_type<V> load(size_t i) const noexcept {
195  return value.template load<V>(i);
196  }
197 
204  template <typename V = default_vec>
205  vec_type<V> loadu(size_t i) const noexcept {
206  return value.template loadu<V>(i);
207  }
208 
215  template <typename V = default_vec>
216  void stream(vec_type<V> in, size_t i) noexcept {
217  value.template stream<V>(in, i);
218  }
219 
226  template <typename V = default_vec>
227  void store(vec_type<V> in, size_t i) noexcept {
228  value.template store<V>(in, i);
229  }
230 
237  template <typename V = default_vec>
238  void storeu(vec_type<V> in, size_t i) noexcept {
239  value.template storeu<V>(in, i);
240  }
241 
247  template <typename E>
248  bool alias(const E& rhs) const noexcept {
249  return value.alias(rhs);
250  }
251 
256  template <typename Y>
257  auto& gpu_compute_hint([[maybe_unused]] Y& y) {
258  value.ensure_gpu_up_to_date();
259  return value;
260  }
261 
266  template <typename Y>
267  const auto& gpu_compute_hint([[maybe_unused]] Y& y) const {
268  value.ensure_gpu_up_to_date();
269  return value;
270  }
271 
272  // Assignment functions
273 
278  template <typename L>
279  void assign_to(L&& lhs) const {
280  std_assign_evaluate(value, std::forward<L>(lhs));
281  }
282 
287  template <typename L>
288  void assign_add_to(L&& lhs) const {
289  std_add_evaluate(value, std::forward<L>(lhs));
290  }
291 
296  template <typename L>
297  void assign_sub_to(L&& lhs) const {
298  std_sub_evaluate(value, std::forward<L>(lhs));
299  }
300 
305  template <typename L>
306  void assign_mul_to(L&& lhs) const {
307  std_mul_evaluate(value, std::forward<L>(lhs));
308  }
309 
314  template <typename L>
315  void assign_div_to(L&& lhs) const {
316  std_div_evaluate(value, std::forward<L>(lhs));
317  }
318 
323  template <typename L>
324  void assign_mod_to(L&& lhs) const {
325  std_mod_evaluate(value, std::forward<L>(lhs));
326  }
327 
328  // Internals
329 
334  void visit([[maybe_unused]] const detail::evaluator_visitor& visitor) const {}
335 
340  value_type* gpu_memory() const noexcept {
341  return value.gpu_memory();
342  }
343 
347  void gpu_evict() const noexcept {
348  value.gpu_evict();
349  }
350 
354  void invalidate_cpu() const noexcept {
355  value.invalidate_cpu();
356  }
357 
361  void invalidate_gpu() const noexcept {
362  value.invalidate_gpu();
363  }
364 
368  void validate_cpu() const noexcept {
369  value.validate_cpu();
370  }
371 
375  void validate_gpu() const noexcept {
376  value.validate_gpu();
377  }
378 
383  void ensure_gpu_allocated() const {
384  value.ensure_gpu_allocated();
385  }
386 
390  void ensure_gpu_up_to_date() const {
391  value.ensure_gpu_up_to_date();
392  }
393 
398  void ensure_cpu_up_to_date() const {
399  value.ensure_cpu_up_to_date();
400  }
401 
406  void gpu_copy_from(const value_type* gpu_memory) const {
407  value.gpu_copy_from(gpu_memory);
408  }
409 
414  bool is_cpu_up_to_date() const noexcept {
415  return value.is_cpu_up_to_date();
416  }
417 
422  bool is_gpu_up_to_date() const noexcept {
423  return value.is_gpu_up_to_date();
424  }
425 
430  static constexpr size_t dimensions() noexcept {
431  return 2;
432  }
433 };
434 
435 } //end of namespace etl
void validate_gpu() const noexcept
Validates the GPU memory.
Definition: adapter.hpp:375
bool is_gpu_up_to_date() const noexcept
Indicates if the GPU memory is up to date.
Definition: adapter.hpp:422
void visit([[maybe_unused]] const detail::evaluator_visitor &visitor) const
Apply the given visitor to this expression and its descendants.
Definition: adapter.hpp:334
const value_type & operator()(size_t i, size_t j) const noexcept
Access the (i, j) element of the 2D matrix.
Definition: adapter.hpp:111
void ensure_gpu_allocated() const
Ensures that the GPU memory is allocated and that the GPU memory is up to date (to undefined value)...
Definition: adapter.hpp:383
void assign_to(L &&lhs) const
Assign to the given left-hand-side expression.
Definition: adapter.hpp:279
const value_type & operator[](size_t i) const noexcept
Returns the element at the given index.
Definition: adapter.hpp:120
auto & gpu_compute_hint([[maybe_unused]] Y &y)
Return a GPU computed version of this expression.
Definition: adapter.hpp:257
value_type & operator[](size_t i) noexcept
Returns the element at the given index.
Definition: adapter.hpp:129
value_type * gpu_memory() const noexcept
Return GPU memory of this expression, if any.
Definition: adapter.hpp:340
void std_assign_evaluate(Expr &&expr, Result &&result)
Evaluation of the expr into result.
Definition: evaluator.hpp:1176
memory_type memory_start() noexcept
Returns a pointer to the first element in memory.
Definition: adapter.hpp:150
typename V::template vec_type< value_type > vec_type
The vectorization type for V.
Definition: adapter.hpp:33
void store(vec_type< V > in, size_t i) noexcept
Store several elements in the matrix at once.
Definition: adapter.hpp:227
adapter() noexcept
Construct a new matrix and fill it with zeros.
Definition: adapter.hpp:44
void storeu(vec_type< V > in, size_t i) noexcept
Store several elements in the matrix at once.
Definition: adapter.hpp:238
void assign_mod_to(L &&lhs) const
Modulo the given left-hand-side expression.
Definition: adapter.hpp:324
const_memory_type memory_end() const noexcept
Returns a pointer to the past-the-end element in memory.
Definition: adapter.hpp:183
void assign_div_to(L &&lhs) const
Divide the given left-hand-side expression.
Definition: adapter.hpp:315
static constexpr size_t dimensions() noexcept
Returns the number of dimensions of the matrix.
Definition: adapter.hpp:430
void ensure_gpu_up_to_date() const
Allocate memory on the GPU for the expression and copy the values into the GPU.
Definition: adapter.hpp:390
const value_type * const_memory_type
The const memory type.
Definition: adapter.hpp:27
value_t< matrix_t > value_type
The value type.
Definition: adapter.hpp:25
vec_type< V > load(size_t i) const noexcept
Load several elements of the matrix at once.
Definition: adapter.hpp:194
void assign_sub_to(L &&lhs) const
Sub from the given left-hand-side expression.
Definition: adapter.hpp:297
const_memory_type memory_start() const noexcept
Returns a pointer to the first element in memory.
Definition: adapter.hpp:161
adapter(size_t dim, value_type value) noexcept
Construct a new adapter matrix and fill it witht the given value.
Definition: adapter.hpp:73
void assign_add_to(L &&lhs) const
Add to the given left-hand-side expression.
Definition: adapter.hpp:288
Root namespace for the ETL library.
Definition: adapter.hpp:15
adapter & operator=(const adapter &rhs)=default
Assign to the matrix by copy.
void ensure_cpu_up_to_date() const
Copy back from the GPU to the expression memory if necessary.
Definition: adapter.hpp:398
auto dim(E &&value, size_t i) -> detail::identity_helper< E, dim_view< detail::build_identity_type< E >, D >>
Return a view representing the ith Dth dimension.
Definition: view_expression_builder.hpp:25
value_type * memory_type
The memory type.
Definition: adapter.hpp:26
A base class for adapters.
Definition: adapter.hpp:21
Visitor to perform local evaluation when necessary.
Definition: eval_visitors.hpp:23
matrix_t value
The adapted matrix.
Definition: adapter.hpp:36
void std_mod_evaluate(Expr &&expr, Result &&result)
Compound modulo evaluation of the expr into result.
Definition: evaluator.hpp:1271
void invalidate_cpu() const noexcept
Invalidates the CPU memory.
Definition: adapter.hpp:354
const auto & gpu_compute_hint([[maybe_unused]] Y &y) const
Return a GPU computed version of this expression.
Definition: adapter.hpp:267
void std_mul_evaluate(Expr &&expr, Result &&result)
Compound multiply evaluation of the expr into result.
Definition: evaluator.hpp:1233
memory_type memory_end() noexcept
Returns a pointer to the past-the-end element in memory.
Definition: adapter.hpp:172
void gpu_evict() const noexcept
Evict the expression from GPU.
Definition: adapter.hpp:347
matrix_t expr_t
The wrapped expression type.
Definition: adapter.hpp:23
bool alias(const E &rhs) const noexcept
Test if this expression aliases with the given expression.
Definition: adapter.hpp:248
void validate_cpu() const noexcept
Validates the CPU memory.
Definition: adapter.hpp:368
void assign_mul_to(L &&lhs) const
Multiply the given left-hand-side expression.
Definition: adapter.hpp:306
void gpu_copy_from(const value_type *gpu_memory) const
Copy from GPU to GPU.
Definition: adapter.hpp:406
void std_sub_evaluate(Expr &&expr, Result &&result)
Compound subtract evaluation of the expr into result.
Definition: evaluator.hpp:1214
adapter(value_type value) noexcept
Construct a new adapter matrix and fill it witht the given value.
Definition: adapter.hpp:55
adapter(size_t dim) noexcept
Construct a new adapter matrix and fill it with zeros.
Definition: adapter.hpp:63
typename decay_traits< E >::value_type value_t
Traits to extract the value type out of an ETL type.
Definition: tmp.hpp:81
void std_div_evaluate(Expr &&expr, Result &&result)
Compound divide evaluation of the expr into result.
Definition: evaluator.hpp:1252
bool is_cpu_up_to_date() const noexcept
Indicates if the CPU memory is up to date.
Definition: adapter.hpp:414
vec_type< V > loadu(size_t i) const noexcept
Load several elements of the matrix at once.
Definition: adapter.hpp:205
void invalidate_gpu() const noexcept
Invalidates the GPU memory.
Definition: adapter.hpp:361
value_type read_flat(size_t i) const noexcept
Definition: adapter.hpp:139
void stream(vec_type< V > in, size_t i) noexcept
Store several elements in the matrix at once, using non-temporal stores.
Definition: adapter.hpp:216
Matrix matrix_t
The adapted matrix type.
Definition: adapter.hpp:22
void std_add_evaluate(Expr &&expr, Result &&result)
Compound add evaluation of the expr into result.
Definition: evaluator.hpp:1195