Expression Templates Library (ETL)
dim_view.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 
22 template <typename T, size_t D>
23 struct dim_view {
24  static_assert(D == 1 || D == 2, "Invalid dimension");
25 
26  using sub_type = T;
32 
33 private:
34  T sub;
35  const size_t i;
36 
37  friend struct etl_traits<dim_view>;
38 
39 public:
45  dim_view(sub_type sub, size_t i) : sub(sub), i(i) {}
46 
52  const_return_type operator[](size_t j) const {
53  if (D == 1) {
54  return sub(i, j);
55  } else { //D == 2
56  return sub(j, i);
57  }
58  }
59 
65  return_type operator[](size_t j) {
66  if (D == 1) {
67  return sub(i, j);
68  } else { //D == 2
69  return sub(j, i);
70  }
71  }
72 
79  value_type read_flat(size_t j) const noexcept {
80  if (D == 1) {
81  return sub(i, j);
82  } else { //D == 2
83  return sub(j, i);
84  }
85  }
86 
92  const_return_type operator()(size_t j) const {
93  if (D == 1) {
94  return sub(i, j);
95  } else { //D == 2
96  return sub(j, i);
97  }
98  }
99 
106  if (D == 1) {
107  return sub(i, j);
108  } else { //D == 2
109  return sub(j, i);
110  }
111  }
112 
118  template <typename E>
119  bool alias(const E& rhs) const noexcept {
120  return sub.alias(rhs);
121  }
122 
127  memory_type memory_start() noexcept requires(etl_dma<T> && D == 1) {
128  return sub.memory_start() + i * subsize(sub);
129  }
130 
135  const_memory_type memory_start() const noexcept requires(etl_dma<T> && D == 1){
136  return sub.memory_start() + i * subsize(sub);
137  }
138 
143  memory_type memory_end() noexcept requires(etl_dma<T> && D == 1){
144  return sub.memory_start() + (i + 1) * subsize(sub);
145  }
146 
151  const_memory_type memory_end() const noexcept requires(etl_dma<T> && D == 1){
152  return sub.memory_start() + (i + 1) * subsize(sub);
153  }
154 
155  // Assignment functions
156 
161  template <typename L>
162  void assign_to(L&& lhs) const {
163  std_assign_evaluate(*this, std::forward<L>(lhs));
164  }
165 
170  template <typename L>
171  void assign_add_to(L&& lhs) const {
172  std_add_evaluate(*this, std::forward<L>(lhs));
173  }
174 
179  template <typename L>
180  void assign_sub_to(L&& lhs) const {
181  std_sub_evaluate(*this, std::forward<L>(lhs));
182  }
183 
188  template <typename L>
189  void assign_mul_to(L&& lhs) const {
190  std_mul_evaluate(*this, std::forward<L>(lhs));
191  }
192 
197  template <typename L>
198  void assign_div_to(L&& lhs) const {
199  std_div_evaluate(*this, std::forward<L>(lhs));
200  }
201 
206  template <typename L>
207  void assign_mod_to(L&& lhs) const {
208  std_mod_evaluate(*this, std::forward<L>(lhs));
209  }
210 
211  // Internals
212 
217  void visit(detail::evaluator_visitor& visitor) const {
218  sub.visit(visitor);
219  }
220 
225  void ensure_cpu_up_to_date() const {
226  // Need to ensure sub value
227  sub.ensure_cpu_up_to_date();
228  }
229 
234  void ensure_gpu_up_to_date() const {
235  // Need to ensure both LHS and RHS
236  sub.ensure_gpu_up_to_date();
237  }
238 
245  friend std::ostream& operator<<(std::ostream& os, const dim_view& v) {
246  return os << "dim[" << D << "](" << v.sub << ", " << v.i << ")";
247  }
248 };
249 
253 template <typename T, size_t D>
254 struct etl_traits<etl::dim_view<T, D>> {
256  using sub_expr_t = std::decay_t<T>;
258 
259  static constexpr bool is_etl = true;
260  static constexpr bool is_transformer = false;
261  static constexpr bool is_view = true;
262  static constexpr bool is_magic_view = false;
263  static constexpr bool is_fast = etl_traits<sub_expr_t>::is_fast;
264  static constexpr bool is_linear = false;
266  static constexpr bool is_value = false;
267  static constexpr bool is_direct = etl_traits<sub_expr_t>::is_direct && D == 1;
268  static constexpr bool is_generator = false;
269  static constexpr bool is_padded = false;
270  static constexpr bool is_aligned = false;
271  static constexpr bool is_temporary = etl_traits<sub_expr_t>::is_temporary;
272  static constexpr bool gpu_computable = false;
273  static constexpr order storage_order = etl_traits<sub_expr_t>::storage_order;
274 
280  template <vector_mode_t V>
281  static constexpr bool vectorizable = false;
282 
288  static size_t size(const expr_t& v) {
289  if (D == 1) {
290  return etl_traits<sub_expr_t>::dim(v.sub, 1);
291  } else {
292  return etl_traits<sub_expr_t>::dim(v.sub, 0);
293  }
294  }
295 
302  static size_t dim(const expr_t& v, [[maybe_unused]] size_t d) {
303  cpp_assert(d == 0, "Invalid dimension");
304 
305  return size(v);
306  }
307 
312  static constexpr size_t size() {
313  return D == 1 ? etl_traits<sub_expr_t>::template dim<1>() : etl_traits<sub_expr_t>::template dim<0>();
314  }
315 
321  template <size_t D2>
322  static constexpr size_t dim() requires(D2 == 0) {
323  return size();
324  }
325 
330  static constexpr size_t dimensions() {
331  return 1;
332  }
333 
338  static constexpr int complexity() noexcept {
339  return -1;
340  }
341 };
342 
343 } //end of namespace etl
void assign_add_to(L &&lhs) const
Add to the given left-hand-side expression.
Definition: dyn_matrix_view.hpp:217
constexpr int complexity([[maybe_unused]] const E &expr) noexcept
Return the complexity of the expression.
Definition: helpers.hpp:38
static size_t dim(const expr_t &v, [[maybe_unused]] size_t d)
Returns the dth dimension of the given expression.
Definition: dim_view.hpp:302
T sub_type
The sub type.
Definition: dim_view.hpp:26
void std_assign_evaluate(Expr &&expr, Result &&result)
Evaluation of the expr into result.
Definition: evaluator.hpp:1176
void assign_div_to(L &&lhs) const
Divide the given left-hand-side expression.
Definition: dyn_matrix_view.hpp:244
const_return_type operator[](size_t j) const
Returns the element at the given index.
Definition: dim_view.hpp:52
void assign_mul_to(L &&lhs) const
Multiply the given left-hand-side expression.
Definition: dyn_matrix_view.hpp:235
constexpr bool is_magic_view
Traits indicating if the given ETL type is a magic view expression.
Definition: traits.hpp:311
memory_t< sub_type > memory_type
The memory acess type.
Definition: dim_view.hpp:28
static constexpr size_t size()
Returns the size of an expression of this fast type.
Definition: dim_view.hpp:312
std::decay_t< T > sub_expr_t
The sub expression type.
Definition: dim_view.hpp:256
void assign_to(L &&lhs) const
Assign to the given left-hand-side expression.
Definition: dyn_matrix_view.hpp:208
bool alias(const E &rhs) const noexcept
Test if this expression aliases with the given expression.
Definition: dim_view.hpp:119
D D
The number of dimensions.
Definition: dyn_matrix_view.hpp:24
order
Storage order of a matrix.
Definition: order.hpp:15
std::conditional_t< std::is_lvalue_reference_v< S >, const value_t< T > &, value_t< T > > const_return_helper
Definition: traits.hpp:872
return_helper< sub_type, decltype(std::declval< sub_type >()(0, 0))> return_type
The type returned by the view.
Definition: dim_view.hpp:30
const_return_helper< sub_type, decltype(std::declval< sub_type >()(0, 0))> const_return_type
The const type return by the view.
Definition: dim_view.hpp:31
constexpr bool is_fast
Traits to test if the given ETL expresion type is fast (sizes known at compile-time) ...
Definition: traits.hpp:588
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
const_memory_t< sub_type > const_memory_type
The const memory access type.
Definition: dim_view.hpp:29
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
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
std::ostream & operator<<(std::ostream &os, const etl::complex< T > &c)
Outputs a textual representation of the complex number in the given stream.
Definition: complex.hpp:576
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
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: dyn_matrix_view.hpp:271
Visitor to perform local evaluation when necessary.
Definition: eval_visitors.hpp:23
typename etl_traits< sub_expr_t >::value_type value_type
The value type.
Definition: dim_view.hpp:257
void std_mod_evaluate(Expr &&expr, Result &&result)
Compound modulo evaluation of the expr into result.
Definition: evaluator.hpp:1271
value_t< sub_type > value_type
The value contained in the expression.
Definition: dim_view.hpp:27
void std_mul_evaluate(Expr &&expr, Result &&result)
Compound multiply evaluation of the expr into result.
Definition: evaluator.hpp:1233
constexpr bool is_transformer
Traits indicating if the given ETL type is a transformer expression.
Definition: traits.hpp:297
void visit(detail::evaluator_visitor &visitor) const
Apply the given visitor to this expression and its descendants.
Definition: dyn_matrix_view.hpp:263
dim_view(sub_type sub, size_t i)
Construct a new dim_view over the given sub expression.
Definition: dim_view.hpp:45
const_return_type operator()(size_t j) const
Returns the element at the given index.
Definition: dim_view.hpp:92
View that shows one dimension of a matrix.
Definition: expr_fwd.hpp:56
requires(D > 0) struct dyn_base
Matrix with run-time fixed dimensions.
Definition: dyn_base.hpp:113
constexpr bool is_view
Traits indicating if the given ETL type is a view expression.
Definition: traits.hpp:304
void ensure_gpu_up_to_date() const
Copy back from the GPU to the expression memory if necessary.
Definition: dyn_matrix_view.hpp:280
static size_t size(const expr_t &v)
Returns the size of the given expression.
Definition: dim_view.hpp:288
value_type read_flat(size_t j) const noexcept
Returns the value at the given index This function never has side effects.
Definition: dim_view.hpp:79
void std_sub_evaluate(Expr &&expr, Result &&result)
Compound subtract evaluation of the expr into result.
Definition: evaluator.hpp:1214
void assign_mod_to(L &&lhs) const
Modulo the given left-hand-side expression.
Definition: dyn_matrix_view.hpp:253
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 j)
Returns the element at the given index.
Definition: dim_view.hpp:65
return_type operator()(size_t j)
Returns the element at the given index.
Definition: dim_view.hpp:105
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
size_t subsize(const E &expr)
Returns the sub-size of the given ETL expression, i.e. the size not considering the first dimension...
Definition: helpers.hpp:118
void std_add_evaluate(Expr &&expr, Result &&result)
Compound add evaluation of the expr into result.
Definition: evaluator.hpp:1195
void assign_sub_to(L &&lhs) const
Sub from the given left-hand-side expression.
Definition: dyn_matrix_view.hpp:226