Expression Templates Library (ETL)
exp.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/impl/egblas/exp.hpp"
11 
12 namespace etl {
13 
18 template <typename T>
19 struct exp_unary_op {
23  template <typename V = default_vec>
24  using vec_type = typename V::template vec_type<T>;
25 
26  static constexpr bool linear = true;
27  static constexpr bool thread_safe = true;
28 
34  template <vector_mode_t V>
35  static constexpr bool vectorizable =
36  (V == vector_mode_t::SSE3 && !is_complex_t<T>) || (V == vector_mode_t::AVX && !is_complex_t<T>) || (intel_compiler && !is_complex_t<T>);
37 
41  template <typename E>
42  static constexpr bool gpu_computable = (is_single_precision_t<T> && impl::egblas::has_sexp) || (is_double_precision_t<T> && impl::egblas::has_dexp)
43  || (is_complex_single_t<T> && impl::egblas::has_cexp) || (is_complex_double_t<T> && impl::egblas::has_zexp);
44 
49  static constexpr int complexity() {
50  return 12;
51  }
52 
58  static constexpr T apply(const T& x) {
59  return std::exp(x);
60  }
61 
68  template <typename V = default_vec>
69  static vec_type<V> load(const vec_type<V>& x) noexcept {
70  return V::exp(x);
71  }
72 
80  template <typename X, typename Y>
81  static auto gpu_compute_hint(const X& x, Y& y) noexcept {
82  decltype(auto) t1 = smart_gpu_compute_hint(x, y);
83 
84  auto t2 = force_temporary_gpu_dim_only(t1);
85 
86  T alpha(1.0);
87  impl::egblas::exp(etl::size(y), alpha, t1.gpu_memory(), 1, t2.gpu_memory(), 1);
88 
89  return t2;
90  }
97  template <typename X, typename Y>
98  static Y& gpu_compute(const X& x, Y& y) noexcept {
99  decltype(auto) t1 = select_smart_gpu_compute(x, y);
100 
101  T alpha(1.0);
102  impl::egblas::exp(etl::size(y), alpha, t1.gpu_memory(), 1, y.gpu_memory(), 1);
103 
104  y.validate_gpu();
105  y.invalidate_cpu();
106 
107  return y;
108  }
109 
114  static std::string desc() noexcept {
115  return "exp";
116  }
117 };
118 
119 } //end of namespace etl
static constexpr bool linear
Indicates if the operator is linear.
Definition: exp.hpp:26
static constexpr int complexity()
Estimate the complexity of operator.
Definition: exp.hpp:49
static constexpr bool thread_safe
Indicates if the operator is thread safe or not.
Definition: exp.hpp:27
decltype(auto) select_smart_gpu_compute(X &x, Y &y)
Compute the expression into a representation that is GPU up to date and possibly store this represent...
Definition: helpers.hpp:434
static constexpr T apply(const T &x)
Apply the unary operator on x.
Definition: exp.hpp:58
SSE3 is the max vectorization available.
static std::string desc() noexcept
Returns a textual representation of the operator.
Definition: exp.hpp:114
EGBLAS wrappers for the exp operation.
static constexpr bool vectorizable
Indicates if the expression is vectorizable using the given vector mode.
Definition: exp.hpp:35
constexpr bool intel_compiler
Indicates if the projectis compiled with intel compiler.
Definition: config.hpp:225
Root namespace for the ETL library.
Definition: adapter.hpp:15
static auto gpu_compute_hint(const X &x, Y &y) noexcept
Compute the result of the operation using the GPU.
Definition: exp.hpp:81
static vec_type< V > load(const vec_type< V > &x) noexcept
Compute several applications of the operator at a time.
Definition: exp.hpp:69
static Y & gpu_compute(const X &x, Y &y) noexcept
Compute the result of the operation using the GPU.
Definition: exp.hpp:98
decltype(auto) force_temporary_gpu_dim_only(E &&expr)
Force a temporary out of the expression, without copying its content.
Definition: temporary.hpp:223
constexpr size_t size(const E &expr) noexcept
Returns the size of the given ETL expression.
Definition: helpers.hpp:108
static constexpr bool gpu_computable
Indicates if the operator can be computed on GPU.
Definition: exp.hpp:42
AVX is the max vectorization available.
Unary operation computing the exponential.
Definition: exp.hpp:19
decltype(auto) smart_gpu_compute_hint(E &expr, Y &y)
Compute the expression into a representation that is GPU up to date.
Definition: helpers.hpp:368
typename V::template vec_type< T > vec_type
Definition: exp.hpp:24