Expression Templates Library (ETL)
virtual_views.hpp
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 
8 #pragma once
9 
10 namespace etl {
11 
12 namespace detail {
13 
21 template <typename V>
22 V compute(size_t n, size_t i, size_t j) {
23  if (n == 1) {
24  return 1;
25  } else if (n == 2) {
26  return i == 0 && j == 0 ? 1 : i == 0 && j == 1 ? 3 : i == 1 && j == 0 ? 4 : 2;
27  } else {
28  //Siamese method
29  return n * (((i + 1) + (j + 1) - 1 + n / 2) % n) + (((i + 1) + 2 * (j + 1) - 2) % n) + 1;
30  }
31 }
32 
33 } //end of namespace detail
34 
35 //Note: Matrix of even order > 2 are only pseudo-magic
36 //TODO Add algorithm for even order
37 
41 template <typename V>
42 struct magic_view {
43  using value_type = V;
44 
45  const size_t n;
46 
47  static constexpr bool gpu_computable = false;
48 
52  explicit magic_view(size_t n) : n(n) {}
53 
59  value_type operator[](size_t i) {
60  return detail::compute<value_type>(n, i / n, i % n);
61  }
62 
68  value_type operator[](size_t i) const {
69  return detail::compute<value_type>(n, i / n, i % n);
70  }
71 
78  value_type read_flat(size_t i) const {
79  return detail::compute<value_type>(n, i / n, i % n);
80  }
81 
88  value_type operator()(size_t i, size_t j) {
89  return detail::compute<value_type>(n, i, j);
90  }
91 
98  value_type operator()(size_t i, size_t j) const {
99  return detail::compute<value_type>(n, i, j);
100  }
101 
107  template <typename E>
108  constexpr bool alias(const E& rhs) const noexcept {
109  return (void)rhs, false;
110  }
111 
112  // Internals
113 
118  template <typename Visitor>
119  void visit([[maybe_unused]] Visitor&& visitor) const {}
120 
125  void ensure_cpu_up_to_date() const {
126  // Nothing to ensure
127  }
128 
133  void ensure_gpu_up_to_date() const {
134  // Nothing to ensure
135  }
136 };
137 
141 template <typename V, size_t N>
143  using value_type = V;
144 
145  static constexpr bool gpu_computable = false;
146 
152  value_type operator[](size_t i) {
153  return detail::compute<value_type>(N, i / N, i % N);
154  }
155 
161  value_type operator[](size_t i) const {
162  return detail::compute<value_type>(N, i / N, i % N);
163  }
164 
171  value_type read_flat(size_t i) const {
172  return detail::compute<value_type>(N, i / N, i % N);
173  }
174 
181  value_type operator()(size_t i, size_t j) {
182  return detail::compute<value_type>(N, i, j);
183  }
184 
191  value_type operator()(size_t i, size_t j) const {
192  return detail::compute<value_type>(N, i, j);
193  }
194 
200  template <typename E>
201  constexpr bool alias(const E& rhs) const noexcept {
202  return (void)rhs, false;
203  }
204 
205  // Internals
206 
211  template <typename Visitor>
212  void visit([[maybe_unused]] Visitor&& visitor) const {}
213 
218  void ensure_cpu_up_to_date() const {
219  // Nothing to ensure
220  }
221 
226  void ensure_gpu_up_to_date() const {
227  // Nothing to ensure
228  }
229 };
230 
234 template <typename V>
235 struct etl_traits<etl::magic_view<V>> {
237  using value_type = V;
238 
239  static constexpr bool is_etl = true;
240  static constexpr bool is_transformer = false;
241  static constexpr bool is_view = false;
242  static constexpr bool is_magic_view = true;
243  static constexpr bool is_fast = false;
244  static constexpr bool is_linear = false;
245  static constexpr bool is_thread_safe = true;
246  static constexpr bool is_value = false;
247  static constexpr bool is_direct = false;
248  static constexpr bool is_generator = false;
249  static constexpr bool is_temporary = false;
250  static constexpr bool is_padded = false;
251  static constexpr bool is_aligned = false;
252  static constexpr bool gpu_computable = false;
253  static constexpr order storage_order = order::RowMajor;
254 
260  template <vector_mode_t VV>
261  static constexpr bool vectorizable = false;
262 
268  static size_t size(const expr_t& v) {
269  return v.n * v.n;
270  }
271 
278  static size_t dim(const expr_t& v, [[maybe_unused]] size_t d) {
279  return v.n;
280  }
281 
286  static constexpr size_t dimensions() {
287  return 2;
288  }
289 
294  static constexpr int complexity() noexcept {
295  return 16;
296  }
297 };
298 
302 template <size_t N, typename V>
305  using value_type = V;
306 
307  static constexpr bool is_etl = true;
308  static constexpr bool is_transformer = false;
309  static constexpr bool is_view = false;
310  static constexpr bool is_magic_view = true;
311  static constexpr bool is_fast = true;
312  static constexpr bool is_linear = false;
313  static constexpr bool is_thread_safe = true;
314  static constexpr bool is_value = false;
315  static constexpr bool is_direct = false;
316  static constexpr bool is_generator = false;
317  static constexpr bool is_temporary = false;
318  static constexpr bool is_padded = false;
319  static constexpr bool is_aligned = false;
320  static constexpr bool gpu_computable = false;
321  static constexpr order storage_order = order::RowMajor;
322 
328  template <vector_mode_t VV>
329  static constexpr bool vectorizable = false;
330 
335  static constexpr size_t size() {
336  return N * N;
337  }
338 
344  static constexpr size_t size(const expr_t& v) {
345  return (void)v, N * N;
346  }
347 
353  template <size_t D>
354  static constexpr size_t dim() {
355  return N;
356  }
357 
364  static constexpr size_t dim(const expr_t& e, size_t d) {
365  return (void)e, (void)d, N;
366  }
367 
372  static constexpr size_t dimensions() {
373  return 2;
374  }
375 
380  static constexpr int complexity() noexcept {
381  return 16;
382  }
383 };
384 
385 } //end of namespace etl
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: virtual_views.hpp:125
value_type operator[](size_t i)
Returns the element at the given index.
Definition: virtual_views.hpp:59
V value_type
The value type in the matrix.
Definition: virtual_views.hpp:43
value_type operator[](size_t i) const
Returns the element at the given index.
Definition: virtual_views.hpp:161
static constexpr size_t dim(const expr_t &e, size_t d)
Returns the dth dimension of the given expression.
Definition: virtual_views.hpp:364
constexpr bool alias(const E &rhs) const noexcept
Test if this expression aliases with the given expression.
Definition: virtual_views.hpp:108
constexpr bool is_magic_view
Traits indicating if the given ETL type is a magic view expression.
Definition: traits.hpp:311
static size_t dim(const expr_t &v, [[maybe_unused]] size_t d)
Returns the dth dimension of the given expression.
Definition: virtual_views.hpp:278
A view (virtual) of a dynamic magic matrix.
Definition: virtual_views.hpp:42
value_type operator()(size_t i, size_t j)
Access to the value at the given (i, j) position.
Definition: virtual_views.hpp:181
void visit([[maybe_unused]] Visitor &&visitor) const
Apply the given visitor to this expression and its descendants.
Definition: virtual_views.hpp:212
V value_type
The value type of the expression.
Definition: virtual_views.hpp:305
order
Storage order of a matrix.
Definition: order.hpp:15
static constexpr int complexity() noexcept
Estimate the complexity of computation.
Definition: virtual_views.hpp:294
V value_type
The value type.
Definition: virtual_views.hpp:143
static constexpr size_t dimensions()
Returns the number of expressions for this type.
Definition: virtual_views.hpp:372
value_type operator()(size_t i, size_t j) const
Access to the value at the given (i, j) position.
Definition: virtual_views.hpp:191
void ensure_gpu_up_to_date() const
Copy back from the GPU to the expression memory if necessary.
Definition: virtual_views.hpp:133
constexpr bool is_fast
Traits to test if the given ETL expresion type is fast (sizes known at compile-time) ...
Definition: traits.hpp:588
value_type operator[](size_t i)
Returns the element at the given index.
Definition: virtual_views.hpp:152
static constexpr size_t dim()
Returns the D2th dimension of an expression of this type.
Definition: virtual_views.hpp:354
Traits to get information about ETL types.
Definition: tmp.hpp:68
Root namespace for the ETL library.
Definition: adapter.hpp:15
const size_t n
The dimensions of the magic matrix.
Definition: virtual_views.hpp:45
V value_type
The value type of the expression.
Definition: virtual_views.hpp:237
constexpr bool alias(const E &rhs) const noexcept
Test if this expression aliases with the given expression.
Definition: virtual_views.hpp:201
static constexpr size_t dimensions()
Returns the number of expressions for this type.
Definition: virtual_views.hpp:286
value_type operator[](size_t i) const
Returns the element at the given index.
Definition: virtual_views.hpp:68
void visit([[maybe_unused]] Visitor &&visitor) const
Apply the given visitor to this expression and its descendants.
Definition: virtual_views.hpp:119
static size_t size(const expr_t &v)
Returns the size of the given expression.
Definition: virtual_views.hpp:268
A view (virtual) of a static magic matrix.
Definition: virtual_views.hpp:142
static constexpr size_t size()
Returns the size of an expression of this fast type.
Definition: virtual_views.hpp:335
constexpr bool is_transformer
Traits indicating if the given ETL type is a transformer expression.
Definition: traits.hpp:297
magic_view(size_t n)
Construct a new magic_view with the given dimension.
Definition: virtual_views.hpp:52
constexpr bool is_view
Traits indicating if the given ETL type is a view expression.
Definition: traits.hpp:304
static constexpr size_t size(const expr_t &v)
Returns the size of the given expression.
Definition: virtual_views.hpp:344
value_type operator()(size_t i, size_t j) const
Access to the value at the given (i, j) position.
Definition: virtual_views.hpp:98
value_type read_flat(size_t i) const
Returns the value at the given index This function never has side effects.
Definition: virtual_views.hpp:78
value_type operator()(size_t i, size_t j)
Access to the value at the given (i, j) position.
Definition: virtual_views.hpp:88
constexpr bool is_thread_safe
Traits to test if the given ETL expresion type is thread safe.
Definition: traits.hpp:687
Row-Major storage.
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: virtual_views.hpp:218
void ensure_gpu_up_to_date() const
Copy back from the GPU to the expression memory if necessary.
Definition: virtual_views.hpp:226
static constexpr int complexity() noexcept
Estimate the complexity of computation.
Definition: virtual_views.hpp:380
value_type read_flat(size_t i) const
Returns the value at the given index This function never has side effects.
Definition: virtual_views.hpp:171