Expression Templates Library (ETL)
outer_product_expr.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 #include "etl/expr/base_temporary_expr.hpp"
11 
12 //Include the implementations
13 #include "etl/impl/std/outer.hpp"
14 #include "etl/impl/blas/outer.hpp"
16 #include "etl/impl/vec/outer.hpp"
17 
18 namespace etl {
19 
24 template <etl_expr A, etl_expr B>
25 struct outer_product_expr : base_temporary_expr_bin<outer_product_expr<A, B>, A, B> {
30 
31  static constexpr auto storage_order = left_traits::storage_order;
32 
37  static constexpr bool gpu_computable = false;
38 
43  explicit outer_product_expr(A a, B b) : base_type(a, b) {
44  //Nothing else to init
45  }
46 
47  // Assignment functions
48 
57  template <etl_expr C>
59  if (cblas_enabled) {
60  return etl::outer_impl::BLAS;
61  } else {
62  return etl::outer_impl::STD;
63  }
64  }
65 
66 #ifdef ETL_MANUAL_SELECT
67 
73  template <etl_expr C>
75  if (local_context().outer_selector.forced) {
76  auto forced = local_context().outer_selector.impl;
77 
78  switch (forced) {
79  //AVX cannot always be used
80  case outer_impl::BLAS:
81  if (!cblas_enabled) {
82  std::cerr << "Forced selection to BLAS outer implementation, but not possible for this expression" << std::endl;
83  return select_default_outer_impl<C>();
84  }
85 
86  return forced;
87 
88  //In other cases, simply use the forced impl
89  default:
90  return forced;
91  }
92  }
93 
94  return select_default_outer_impl<C>();
95  }
96 
97 #else
98 
105  template <etl_expr C>
106  static constexpr etl::outer_impl select_outer_impl() {
107  return select_default_outer_impl<C>();
108  }
109 
110 #endif
111 
116  template <etl_expr C>
117  void assign_to(C&& c) const {
118  inc_counter("temp:assign");
119 
120  auto& a = this->a();
121  auto& b = this->b();
122 
123  constexpr_select auto impl = select_outer_impl<C>();
124 
125  if
126  constexpr_select(impl == etl::outer_impl::BLAS) {
127  inc_counter("impl:blas");
128  etl::impl::blas::outer(smart_forward(a), smart_forward(b), c);
129  }
130  else {
131  inc_counter("impl:std");
132  etl::impl::standard::outer(smart_forward(a), smart_forward(b), c);
133  }
134  }
135 
140  template <etl_expr L>
141  void assign_add_to(L&& lhs) const {
142  std_add_evaluate(*this, lhs);
143  }
144 
149  template <etl_expr L>
150  void assign_sub_to(L&& lhs) const {
151  std_sub_evaluate(*this, lhs);
152  }
153 
158  template <etl_expr L>
159  void assign_mul_to(L&& lhs) const {
160  std_mul_evaluate(*this, lhs);
161  }
162 
167  template <etl_expr L>
168  void assign_div_to(L&& lhs) const {
169  std_div_evaluate(*this, lhs);
170  }
171 
176  template <etl_expr L>
177  void assign_mod_to(L&& lhs) const {
178  std_mod_evaluate(*this, lhs);
179  }
180 
187  friend std::ostream& operator<<(std::ostream& os, const outer_product_expr& expr) {
188  return os << "out(" << expr._a << ", " << expr._b << ")";
189  }
190 };
191 
196 template <typename A, typename B>
199  using left_expr_t = std::decay_t<A>;
200  using right_expr_t = std::decay_t<B>;
204 
205  static constexpr bool is_etl = true;
206  static constexpr bool is_transformer = false;
207  static constexpr bool is_view = false;
208  static constexpr bool is_magic_view = false;
209  static constexpr bool is_fast = left_traits::is_fast && right_traits::is_fast;
210  static constexpr bool is_linear = false;
211  static constexpr bool is_thread_safe = true;
212  static constexpr bool is_value = false;
213  static constexpr bool is_direct = true;
214  static constexpr bool is_generator = false;
215  static constexpr bool is_padded = false;
216  static constexpr bool is_aligned = true;
217  static constexpr bool is_temporary = true;
218  static constexpr bool gpu_computable = is_gpu_t<value_type> && cuda_enabled;
219  static constexpr order storage_order = left_traits::storage_order;
220 
226  template <vector_mode_t V>
227  static constexpr bool vectorizable = true;
228 
233  template <size_t DD>
234  static constexpr size_t dim() {
235  return DD == 0 ? decay_traits<A>::template dim<0>() : decay_traits<B>::template dim<0>();
236  }
237 
244  static size_t dim(const expr_t& e, size_t d) {
245  if (d == 0) {
246  return etl::dim(e._a, 0);
247  } else {
248  return etl::dim(e._b, 0);
249  }
250  }
251 
257  static size_t size(const expr_t& e) {
258  return etl::dim(e._a, 0) * etl::dim(e._b, 0);
259  }
260 
265  static constexpr size_t size() {
266  return decay_traits<A>::template dim<0>() * decay_traits<B>::template dim<0>();
267  }
268 
273  static constexpr size_t dimensions() {
274  return 2;
275  }
276 
281  static constexpr int complexity() noexcept {
282  return 4;
283  }
284 };
285 
292 template <etl_expr A, etl_expr B>
295 }
296 
304 template <etl_expr A, etl_expr B, etl_expr C>
305 auto outer(A&& a, B&& b, C&& c) {
306  c = outer(a, b);
307  return c;
308 }
309 
310 } //end of namespace etl
BLAS implementation of the outer product.
std::decay_t< B > right_expr_t
The right sub expression type.
Definition: outer_product_expr.hpp:200
static constexpr etl::outer_impl select_outer_impl()
Select the outer product implementation for an expression of type A and B.
Definition: outer_product_expr.hpp:106
void assign_mod_to(L &&lhs) const
Modulo the given left-hand-side expression.
Definition: outer_product_expr.hpp:177
A transposition expression.
Definition: outer_product_expr.hpp:25
std::decay_t< A > left_expr_t
The left sub expression type.
Definition: outer_product_expr.hpp:199
B _b
The sub expression reference.
Definition: base_temporary_expr.hpp:534
Standard implementation.
outer_impl
Enumeration describing the different implementations of outer product.
Definition: outer_impl.hpp:21
value_t< A > value_type
The type of value of the expression.
Definition: outer_product_expr.hpp:26
constexpr bool is_magic_view
Traits indicating if the given ETL type is a magic view expression.
Definition: traits.hpp:311
void assign_div_to(L &&lhs) const
Divide the given left-hand-side expression.
Definition: outer_product_expr.hpp:168
A _a
The sub expression reference.
Definition: base_temporary_expr.hpp:533
static constexpr size_t dim()
Returns the DDth dimension of the expression.
Definition: outer_product_expr.hpp:234
order
Storage order of a matrix.
Definition: order.hpp:15
constexpr bool cuda_enabled
Indicates if CUDA is available.
Definition: config.hpp:94
BLAS implementation of the outer product.
static size_t dim(const expr_t &e, size_t d)
Returns the dth dimension of the expression.
Definition: outer_product_expr.hpp:244
Abstract base class for temporary binary expression.
Definition: base_temporary_expr.hpp:529
BLAS implementation.
std::add_lvalue_reference_t< B > b()
Returns the sub expression.
Definition: base_temporary_expr.hpp:593
static constexpr size_t size()
Returns the size of the expression.
Definition: outer_product_expr.hpp:265
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< A > value_type
The value type of the expression.
Definition: outer_product_expr.hpp:203
Standard implementation of the outer product.
static constexpr auto storage_order
The sub storage order.
Definition: outer_product_expr.hpp:31
Traits to get information about ETL types.
Definition: tmp.hpp:68
Root namespace for the ETL library.
Definition: adapter.hpp:15
context & local_context()
Return the configuration context of the current thread.
Definition: context.hpp:50
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
static constexpr size_t dimensions()
Returns the number of dimensions of the expression.
Definition: outer_product_expr.hpp:273
std::conditional_t< is_etl_value< T >, const std::decay_t< T > &, std::decay_t< T > > build_type
Helper to build the type for a sub expression.
Definition: expression_helpers.hpp:24
Standard implementation of the outer product.
void assign_mul_to(L &&lhs) const
Multiply the given left-hand-side expression.
Definition: outer_product_expr.hpp:159
outer_product_expr(A a, B b)
Construct a new expression.
Definition: outer_product_expr.hpp:43
void std_mod_evaluate(Expr &&expr, Result &&result)
Compound modulo evaluation of the expr into result.
Definition: evaluator.hpp:1271
void assign_to(C &&c) const
Assign to a matrix of the same storage order.
Definition: outer_product_expr.hpp:117
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 assign_sub_to(L &&lhs) const
Sub from the given left-hand-side expression.
Definition: outer_product_expr.hpp:150
static size_t size(const expr_t &e)
Returns the size of the expression.
Definition: outer_product_expr.hpp:257
constexpr bool is_view
Traits indicating if the given ETL type is a view expression.
Definition: traits.hpp:304
static constexpr bool is_fast
Indicates if T is a fast structure.
Definition: traits_base.hpp:25
void std_sub_evaluate(Expr &&expr, Result &&result)
Compound subtract evaluation of the expr into result.
Definition: evaluator.hpp:1214
decltype(auto) smart_forward(E &expr)
Smart forwarding for a temporary expression.
Definition: helpers.hpp:323
constexpr bool cblas_enabled
Indicates if a BLAS library is available for ETL.
Definition: config.hpp:76
static constexpr bool gpu_computable
Indicates if the temporary expression can be directly evaluated using only GPU.
Definition: outer_product_expr.hpp:37
constexpr bool is_thread_safe
Traits to test if the given ETL expresion type is thread safe.
Definition: traits.hpp:687
static constexpr etl::outer_impl select_default_outer_impl()
Select the outer product implementation for an expression of type A and B.
Definition: outer_product_expr.hpp:58
outer_product_expr< detail::build_type< A >, detail::build_type< B > > outer(A &&a, B &&b)
Outer product multiplication of two matrices.
Definition: outer_product_expr.hpp:293
typename decay_traits< E >::value_type value_t
Traits to extract the value type out of an ETL type.
Definition: tmp.hpp:81
void assign_add_to(L &&lhs) const
Add to the given left-hand-side expression.
Definition: outer_product_expr.hpp:141
void std_div_evaluate(Expr &&expr, Result &&result)
Compound divide evaluation of the expr into result.
Definition: evaluator.hpp:1252
static constexpr int complexity() noexcept
Estimate the complexity of computation.
Definition: outer_product_expr.hpp:281
void inc_counter([[maybe_unused]] const char *name)
Increase the given counter.
Definition: counters.hpp:25
friend std::ostream & operator<<(std::ostream &os, const outer_product_expr &expr)
Print a representation of the expression on the given stream.
Definition: outer_product_expr.hpp:187
std::add_lvalue_reference_t< A > a()
Returns the sub expression.
Definition: base_temporary_expr.hpp:577
void std_add_evaluate(Expr &&expr, Result &&result)
Compound add evaluation of the expr into result.
Definition: evaluator.hpp:1195