Expression Templates Library (ETL)
sub_matrix_3d.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 #ifdef ETL_CUDA
16 #include "etl/impl/cublas/cuda.hpp"
17 #endif
18 
19 namespace etl {
20 
25 template <etl_expr T, bool Aligned>
26 struct sub_matrix_3d final : iterable<sub_matrix_3d<T, Aligned>, false>,
27  assignable<sub_matrix_3d<T, Aligned>, value_t<T>>,
28  value_testable<sub_matrix_3d<T, Aligned>>,
29  inplace_assignable<sub_matrix_3d<T, Aligned>> {
33  using sub_type = T;
41 
45  template <typename V = default_vec>
46  using vec_type = typename V::template vec_type<value_type>;
47 
48  using assignable_base_type::operator=;
51 
52 private:
53  sub_type sub_expr;
54  const size_t base_i;
55  const size_t base_j;
56  const size_t base_k;
57  const size_t m;
58  const size_t n;
59  const size_t o;
60 
61  friend struct etl_traits<sub_matrix_3d>;
62 
63  static constexpr order storage_order = decay_traits<sub_type>::storage_order;
64 
65 public:
71  sub_matrix_3d(sub_type sub_expr, size_t i, size_t j, size_t k, size_t m, size_t n, size_t o)
72  : sub_expr(sub_expr), base_i(i), base_j(j), base_k(k), m(m), n(n), o(o) {}
73 
79  const_return_type operator[](size_t f) const {
80  cpp_assert(f < m * n * o, "Invalid index inside sub_matrix_3d");
81 
82  if constexpr (storage_order == order::RowMajor) {
83  // Extract 3D indices from flat inside the view
84  auto my_i = f / (n * o);
85  auto t = f % (n * o);
86  auto my_j = t / o;
87  auto my_k = t % o;
88 
89  // Let the sub view compute the index
90  return sub_expr(base_i + my_i, base_j + my_j, base_k + my_k);
91  } else {
92  // Extract 3D indices from flat inside the view
93  auto my_k = f / (m * n);
94  auto t = f % (m * n);
95  auto my_j = t / m;
96  auto my_i = t % m;
97 
98  // Let the sub view compute the index
99  return sub_expr(base_i + my_i, base_j + my_j, base_k + my_k);
100  }
101  }
102 
109  cpp_assert(f < m * n * o, "Invalid index inside sub_matrix_3d");
110 
111  if constexpr (storage_order == order::RowMajor) {
112  // Extract 3D indices from flat inside the view
113  auto my_i = f / (n * o);
114  auto t = f % (n * o);
115  auto my_j = t / o;
116  auto my_k = t % o;
117 
118  // Let the sub view compute the index
119  return sub_expr(base_i + my_i, base_j + my_j, base_k + my_k);
120  } else {
121  // Extract 3D indices from flat inside the view
122  auto my_k = f / (m * n);
123  auto t = f % (m * n);
124  auto my_j = t / m;
125  auto my_i = t % m;
126 
127  // Let the sub view compute the index
128  return sub_expr(base_i + my_i, base_j + my_j, base_k + my_k);
129  }
130  }
131 
138  value_type read_flat(size_t f) const noexcept(assert_nothrow) {
139  cpp_assert(f < m * n * o, "Invalid index inside sub_matrix_3d");
140 
141  if constexpr (storage_order == order::RowMajor) {
142  // Extract 3D indices from flat inside the view
143  auto my_i = f / (n * o);
144  auto t = f % (n * o);
145  auto my_j = t / o;
146  auto my_k = t % o;
147 
148  // Let the sub view compute the index
149  return sub_expr(base_i + my_i, base_j + my_j, base_k + my_k);
150  } else {
151  // Extract 3D indices from flat inside the view
152  auto my_k = f / (m * n);
153  auto t = f % (m * n);
154  auto my_j = t / m;
155  auto my_i = t % m;
156 
157  // Let the sub view compute the index
158  return sub_expr(base_i + my_i, base_j + my_j, base_k + my_k);
159  }
160  }
161 
168  ETL_STRONG_INLINE(const_return_type) operator()(size_t i, size_t j, size_t k) const {
169  cpp_assert(i < m, "Invalid 3d index inside sub_matrix_3d");
170  cpp_assert(j < n, "Invalid 3d index inside sub_matrix_3d");
171  cpp_assert(k < o, "Invalid 3d index inside sub_matrix_3d");
172 
173  return sub_expr(base_i + i, base_j + j, base_k + k);
174  }
175 
182  ETL_STRONG_INLINE(return_type) operator()(size_t i, size_t j, size_t k) {
183  cpp_assert(i < m, "Invalid 3d index inside sub_matrix_3d");
184  cpp_assert(j < n, "Invalid 3d index inside sub_matrix_3d");
185  cpp_assert(k < o, "Invalid 3d index inside sub_matrix_3d");
186 
187  return sub_expr(base_i + i, base_j + j, base_k + k);
188  }
189 
195  auto operator()(size_t x) const {
196  return sub(*this, x);
197  }
198 
204  template <typename E>
205  bool alias(const E& rhs) const noexcept {
206  return sub_expr.alias(rhs);
207  }
208 
216  size_t& unsafe_dimension_access(size_t x) {
217  return sub_expr.unsafe_dimension_access(x);
218  }
219 
220  // Assignment functions
221 
226  template <typename L>
227  void assign_to(L&& lhs) const {
228  std_assign_evaluate(*this, std::forward<L>(lhs));
229  }
230 
235  template <typename L>
236  void assign_add_to(L&& lhs) const {
237  std_add_evaluate(*this, std::forward<L>(lhs));
238  }
239 
244  template <typename L>
245  void assign_sub_to(L&& lhs) const {
246  std_sub_evaluate(*this, std::forward<L>(lhs));
247  }
248 
253  template <typename L>
254  void assign_mul_to(L&& lhs) const {
255  std_mul_evaluate(*this, std::forward<L>(lhs));
256  }
257 
262  template <typename L>
263  void assign_div_to(L&& lhs) const {
264  std_div_evaluate(*this, std::forward<L>(lhs));
265  }
266 
271  template <typename L>
272  void assign_mod_to(L&& lhs) const {
273  std_mod_evaluate(*this, std::forward<L>(lhs));
274  }
275 
276  // Internals
277 
282  void ensure_cpu_up_to_date() const {
283  // The sub value must be ensured
284  sub_expr.ensure_cpu_up_to_date();
285  }
286 
291  void ensure_gpu_up_to_date() const {
292  // The sub value must be ensured
293  sub_expr.ensure_gpu_up_to_date();
294  }
295 
300  void visit(detail::evaluator_visitor& visitor) const {
301  sub_expr.visit(visitor);
302  }
303 
310  friend std::ostream& operator<<(std::ostream& os, const sub_matrix_3d& v) {
311  return os << "sub(" << v.sub_expr << ", " << v.base_i << ", " << v.base_j << ", " << v.base_k << ", " << v.m << ", " << v.n << ", " << v.o << ")";
312  }
313 };
314 
318 template <typename T, bool Aligned>
319 struct etl_traits<etl::sub_matrix_3d<T, Aligned>> {
321  using sub_expr_t = std::decay_t<T>;
324 
325  static constexpr bool is_etl = true;
326  static constexpr bool is_transformer = false;
327  static constexpr bool is_view = true;
328  static constexpr bool is_magic_view = false;
329  static constexpr bool is_fast = false;
330  static constexpr bool is_linear = sub_traits::is_linear;
331  static constexpr bool is_thread_safe = sub_traits::is_thread_safe;
332  static constexpr bool is_value = false;
333  static constexpr bool is_direct = false;
334  static constexpr bool is_generator = false;
335  static constexpr bool is_padded = false;
336  static constexpr bool is_aligned = false;
337  static constexpr bool is_temporary = sub_traits::is_temporary;
338  static constexpr bool gpu_computable = false;
339  static constexpr order storage_order = sub_traits::storage_order;
340 
346  template <vector_mode_t V>
347  static constexpr bool vectorizable = false;
348 
354  static size_t size(const expr_t& v) noexcept {
355  return v.m * v.n * v.o;
356  }
357 
364  static size_t dim(const expr_t& v, size_t d) noexcept {
365  if (d == 0) {
366  return v.m;
367  } else if (d == 1) {
368  return v.n;
369  } else {
370  return v.o;
371  }
372  }
373 
378  static constexpr size_t dimensions() noexcept {
379  return 3;
380  }
381 
386  static constexpr int complexity() noexcept {
387  return -1;
388  }
389 };
390 
391 } //end of namespace etl
std::decay_t< T > sub_expr_t
The sub expression type.
Definition: sub_matrix_3d.hpp:321
CRTP class to inject iterators functions.
Definition: iterable.hpp:23
auto operator()(size_t x) const
Creates a sub view of the matrix, effectively removing the first dimension and fixing it to the given...
Definition: sub_matrix_3d.hpp:195
void assign_div_to(L &&lhs) const
Divide the given left-hand-side expression.
Definition: sub_matrix_3d.hpp:263
friend std::ostream & operator<<(std::ostream &os, const sub_matrix_3d &v)
Print a representation of the view on the given stream.
Definition: sub_matrix_3d.hpp:310
View that shows a 3d sub matrix of an expression.
Definition: expr_fwd.hpp:65
void ensure_gpu_up_to_date() const
Copy back from the GPU to the expression memory if necessary.
Definition: sub_matrix_3d.hpp:291
void std_assign_evaluate(Expr &&expr, Result &&result)
Evaluation of the expr into result.
Definition: evaluator.hpp:1176
void assign_sub_to(L &&lhs) const
Sub from the given left-hand-side expression.
Definition: sub_matrix_3d.hpp:245
void assign_mul_to(L &&lhs) const
Multiply the given left-hand-side expression.
Definition: sub_matrix_3d.hpp:254
void assign_to(L &&lhs) const
Assign to the given left-hand-side expression.
Definition: sub_matrix_3d.hpp:227
static size_t dim(const expr_t &v, size_t d) noexcept
Returns the dth dimension of the given expression.
Definition: sub_matrix_3d.hpp:364
value_t< sub_type > value_type
The value contained in the expression.
Definition: dyn_matrix_view.hpp:31
constexpr bool is_magic_view
Traits indicating if the given ETL type is a magic view expression.
Definition: traits.hpp:311
order
Storage order of a matrix.
Definition: order.hpp:15
void visit(detail::evaluator_visitor &visitor) const
Apply the given visitor to this expression and its descendants.
Definition: sub_matrix_3d.hpp:300
const_memory_t< sub_type > const_memory_type
The const memory access type.
Definition: sub_matrix_3d.hpp:36
std::conditional_t< std::is_lvalue_reference_v< S >, const value_t< T > &, value_t< T > > const_return_helper
Definition: traits.hpp:872
bool alias(const E &rhs) const noexcept
Test if this expression aliases with the given expression.
Definition: sub_matrix_3d.hpp:205
const_return_helper< sub_type, decltype(std::declval< sub_type >()[0])> const_return_type
The const type return by the view.
Definition: sub_matrix_3d.hpp:38
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_t< sub_type > value_type
The value contained in the expression.
Definition: sub_matrix_3d.hpp:34
auto end() noexcept
Return an iterator to the past-the-end element of the matrix.
Definition: iterable.hpp:59
sub_matrix_3d(sub_type sub_expr, size_t i, size_t j, size_t k, size_t m, size_t n, size_t o)
Construct a new sub_matrix_3d over the given sub expression.
Definition: sub_matrix_3d.hpp:71
Traits to get information about ETL types.
Definition: tmp.hpp:68
Root namespace for the ETL library.
Definition: adapter.hpp:15
std::conditional_t< std::is_const_v< std::remove_reference_t< S > >, const value_t< T > &, std::conditional_t< std::is_lvalue_reference_v< S > &&!std::is_const_v< T >, value_t< T > &, value_t< T > >> return_helper
Definition: traits.hpp:866
std::conditional_t< std::is_const_v< std::remove_reference_t< S > >, typename std::decay_t< S >::const_memory_type, typename std::decay_t< S >::memory_type > memory_t
Traits to extract the direct memory type out of an ETL type.
Definition: tmp.hpp:88
typename std::decay_t< S >::const_memory_type const_memory_t
Traits to extract the direct const memory type out of an ETL type.
Definition: tmp.hpp:94
typename V::template vec_type< value_type > vec_type
The vectorization type for V.
Definition: sub_matrix_3d.hpp:46
Visitor to perform local evaluation when necessary.
Definition: eval_visitors.hpp:23
static size_t size(const expr_t &v) noexcept
Returns the size of the given expression.
Definition: sub_matrix_3d.hpp:354
Configurable iterator for ETL expressions.
Definition: iterator.hpp:24
void std_mod_evaluate(Expr &&expr, Result &&result)
Compound modulo evaluation of the expr into result.
Definition: evaluator.hpp:1271
auto begin() noexcept
Return an iterator to the first element of the matrix.
Definition: iterable.hpp:46
void std_mul_evaluate(Expr &&expr, Result &&result)
Compound multiply evaluation of the expr into result.
Definition: evaluator.hpp:1233
static constexpr size_t dimensions() noexcept
Returns the number of expressions for this type.
Definition: sub_matrix_3d.hpp:378
constexpr bool is_transformer
Traits indicating if the given ETL type is a transformer expression.
Definition: traits.hpp:297
return_helper< sub_type, decltype(std::declval< sub_type >()[0])> return_type
The type returned by the view.
Definition: sub_matrix_3d.hpp:37
value_type read_flat(size_t f) const noexcept(assert_nothrow)
Returns the value at the given index This function never has side effects.
Definition: sub_matrix_3d.hpp:138
memory_t< sub_type > memory_type
The memory acess type.
Definition: sub_matrix_3d.hpp:35
constexpr bool is_view
Traits indicating if the given ETL type is a view expression.
Definition: traits.hpp:304
CRTP class to inject assign operations to matrix and vector structures.
Definition: assignable.hpp:25
ETL_STRONG_INLINE(const_return_type) operator()(size_t i
Access to the element at the given (args...) position.
void std_sub_evaluate(Expr &&expr, Result &&result)
Compound subtract evaluation of the expr into result.
Definition: evaluator.hpp:1214
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: sub_matrix_3d.hpp:282
static constexpr int complexity() noexcept
Estimate the complexity of computation.
Definition: sub_matrix_3d.hpp:386
size_t & unsafe_dimension_access(size_t x)
Returns a reference to the ith dimension value.
Definition: sub_matrix_3d.hpp:216
T sub_type
The sub type.
Definition: sub_matrix_3d.hpp:33
constexpr bool is_thread_safe
Traits to test if the given ETL expresion type is thread safe.
Definition: traits.hpp:687
return_type operator[](size_t f)
Returns the element at the given index.
Definition: sub_matrix_3d.hpp:108
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
Row-Major storage.
const_return_type operator[](size_t f) const
Returns the element at the given index.
Definition: sub_matrix_3d.hpp:79
void assign_add_to(L &&lhs) const
Add to the given left-hand-side expression.
Definition: sub_matrix_3d.hpp:236
void std_add_evaluate(Expr &&expr, Result &&result)
Compound add evaluation of the expr into result.
Definition: evaluator.hpp:1195
typename sub_traits::value_type value_type
The value type of the expression.
Definition: sub_matrix_3d.hpp:323
void assign_mod_to(L &&lhs) const
Modulo the given left-hand-side expression.
Definition: sub_matrix_3d.hpp:272