Expression Templates Library (ETL)
log2.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/log2.hpp"
11 
12 namespace etl {
13 
18 template <typename T>
19 struct log2_unary_op {
20  static constexpr bool linear = true;
21  static constexpr bool thread_safe = true;
22 
28  template <vector_mode_t V>
29  static constexpr bool vectorizable =
30  (V == vector_mode_t::SSE3 && is_single_precision_t<T>) || (V == vector_mode_t::AVX && is_single_precision_t<T>) || (intel_compiler && !is_complex_t<T>);
31 
35  template <typename E>
36  static constexpr bool gpu_computable = (is_single_precision_t<T> && impl::egblas::has_slog2) || (is_double_precision_t<T> && impl::egblas::has_dlog2)
37  || (is_complex_single_t<T> && impl::egblas::has_clog2) || (is_complex_double_t<T> && impl::egblas::has_zlog2);
38 
43  static constexpr int complexity() {
44  return 12;
45  }
46 
50  template <typename V = default_vec>
51  using vec_type = typename V::template vec_type<T>;
52 
58  static constexpr T apply(const T& x) {
59  return std::log2(x);
60  }
61 
68  template <typename V = default_vec>
69  static vec_type<V> load(const vec_type<V>& x) noexcept {
70  return V::div(V::log(x), V::log(V::set(T(2))));
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::log2(etl::size(y), alpha, t1.gpu_memory(), 1, t2.gpu_memory(), 1);
88 
89  return t2;
90  }
91 
98  template <typename X, typename Y>
99  static Y& gpu_compute(const X& x, Y& y) noexcept {
100  decltype(auto) t1 = select_smart_gpu_compute(x, y);
101 
102  T alpha(1.0);
103  impl::egblas::log2(etl::size(y), alpha, t1.gpu_memory(), 1, y.gpu_memory(), 1);
104 
105  y.validate_gpu();
106  y.invalidate_cpu();
107 
108  return y;
109  }
110 
115  static std::string desc() noexcept {
116  return "log2";
117  }
118 };
119 
123 template <typename TT>
124 struct log2_unary_op<std::complex<TT>> {
125  using T = std::complex<TT>;
126 
127  static constexpr bool linear = true;
128  static constexpr bool thread_safe = true;
129 
135  template <vector_mode_t V>
136  static constexpr bool vectorizable = false;
137 
141  template <typename E>
142  static constexpr bool gpu_computable = (is_single_precision_t<T> && impl::egblas::has_slog2) || (is_double_precision_t<T> && impl::egblas::has_dlog2)
143  || (is_complex_single_t<T> && impl::egblas::has_clog2) || (is_complex_double_t<T> && impl::egblas::has_zlog2);
144 
149  static constexpr int complexity() {
150  return 12;
151  }
152 
158  static constexpr T apply(const T& x) {
159  return std::log(x) / std::log(T{TT(2)});
160  }
161 
169  template <typename X, typename Y>
170  static auto gpu_compute_hint(const X& x, Y& y) noexcept {
171  decltype(auto) t1 = smart_gpu_compute_hint(x, y);
172 
173  auto t2 = force_temporary_gpu_dim_only(t1);
174 
175  T alpha(1.0);
176  impl::egblas::log2(etl::size(y), alpha, t1.gpu_memory(), 1, t2.gpu_memory(), 1);
177 
178  return t2;
179  }
180 
187  template <typename X, typename Y>
188  static Y& gpu_compute(const X& x, Y& y) noexcept {
189  decltype(auto) t1 = select_smart_gpu_compute(x, y);
190 
191  T alpha(1.0);
192  impl::egblas::log2(etl::size(y), alpha, t1.gpu_memory(), 1, y.gpu_memory(), 1);
193 
194  y.validate_gpu();
195  y.invalidate_cpu();
196 
197  return y;
198  }
199 
204  static std::string desc() noexcept {
205  return "log2";
206  }
207 };
208 
212 template <typename TT>
213 struct log2_unary_op<etl::complex<TT>> {
214  using T = etl::complex<TT>;
215 
216  static constexpr bool linear = true;
217  static constexpr bool thread_safe = true;
218 
224  template <vector_mode_t V>
225  static constexpr bool vectorizable = false;
226 
230  template <typename E>
231  static constexpr bool gpu_computable = (is_single_precision_t<T> && impl::egblas::has_slog2) || (is_double_precision_t<T> && impl::egblas::has_dlog2)
232  || (is_complex_single_t<T> && impl::egblas::has_clog2) || (is_complex_double_t<T> && impl::egblas::has_zlog2);
233 
238  static constexpr int complexity() {
239  return 12;
240  }
241 
247  static constexpr T apply(const T& x) {
248  return etl::log2(x);
249  }
250 
258  template <typename X, typename Y>
259  static auto gpu_compute_hint(const X& x, Y& y) noexcept {
260  decltype(auto) t1 = smart_gpu_compute_hint(x, y);
261 
262  auto t2 = force_temporary_gpu_dim_only(t1);
263 
264  T alpha(1.0);
265  impl::egblas::log2(etl::size(y), alpha, t1.gpu_memory(), 1, t2.gpu_memory(), 1);
266 
267  return t2;
268  }
269 
276  template <typename X, typename Y>
277  static Y& gpu_compute(const X& x, Y& y) noexcept {
278  decltype(auto) t1 = select_smart_gpu_compute(x, y);
279 
280  T alpha(1.0);
281  impl::egblas::log2(etl::size(y), alpha, t1.gpu_memory(), 1, y.gpu_memory(), 1);
282 
283  y.validate_gpu();
284  y.invalidate_cpu();
285 
286  return y;
287  }
288 
293  static std::string desc() noexcept {
294  return "log2";
295  }
296 };
297 
298 } //end of namespace etl
static constexpr T apply(const T &x)
Apply the unary operator on x.
Definition: log2.hpp:158
static vec_type< V > load(const vec_type< V > &x) noexcept
Compute several applications of the operator at a time.
Definition: log2.hpp:69
Complex number implementation.
Definition: complex.hpp:31
auto log2(E &&value) -> detail::unary_helper< E, log2_unary_op >
Apply logarithm (base 2) on each value of the given expression.
Definition: function_expression_builder.hpp:74
static auto gpu_compute_hint(const X &x, Y &y) noexcept
Compute the result of the operation using the GPU.
Definition: log2.hpp:170
static Y & gpu_compute(const X &x, Y &y) noexcept
Compute the result of the operation using the GPU.
Definition: log2.hpp:277
static constexpr T apply(const T &x)
Apply the unary operator on x.
Definition: log2.hpp:247
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 bool vectorizable
Indicates if the expression is vectorizable using the given vector mode.
Definition: log2.hpp:29
SSE3 is the max vectorization available.
constexpr bool intel_compiler
Indicates if the projectis compiled with intel compiler.
Definition: config.hpp:225
static constexpr int complexity()
Estimate the complexity of operator.
Definition: log2.hpp:238
Root namespace for the ETL library.
Definition: adapter.hpp:15
static std::string desc() noexcept
Returns a textual representation of the operator.
Definition: log2.hpp:115
Unary operation taking the logarithmic value (base 2)
Definition: log2.hpp:19
static auto gpu_compute_hint(const X &x, Y &y) noexcept
Compute the result of the operation using the GPU.
Definition: log2.hpp:259
static constexpr int complexity()
Estimate the complexity of operator.
Definition: log2.hpp:43
EGBLAS wrappers for the log operation.
static std::string desc() noexcept
Returns a textual representation of the operator.
Definition: log2.hpp:293
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 thread_safe
Indicates if the operator is thread safe or not.
Definition: log2.hpp:21
static constexpr T apply(const T &x)
Apply the unary operator on x.
Definition: log2.hpp:58
static constexpr bool gpu_computable
Indicates if the operator can be computed on GPU.
Definition: log2.hpp:36
AVX is the max vectorization available.
static auto gpu_compute_hint(const X &x, Y &y) noexcept
Compute the result of the operation using the GPU.
Definition: log2.hpp:81
static Y & gpu_compute(const X &x, Y &y) noexcept
Compute the result of the operation using the GPU.
Definition: log2.hpp:99
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: log2.hpp:51
auto log(E &&value) -> detail::unary_helper< E, log_unary_op >
Apply logarithm (base e) on each value of the given expression.
Definition: function_expression_builder.hpp:64
std::complex< TT > T
The real type.
Definition: log2.hpp:125
static constexpr bool linear
Indicates if the operator is linear.
Definition: log2.hpp:20
static std::string desc() noexcept
Returns a textual representation of the operator.
Definition: log2.hpp:204
static Y & gpu_compute(const X &x, Y &y) noexcept
Compute the result of the operation using the GPU.
Definition: log2.hpp:188
static constexpr int complexity()
Estimate the complexity of operator.
Definition: log2.hpp:149