Expression Templates Library (ETL)
cbrt.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/cbrt.hpp"
11 
12 namespace etl {
13 
18 template <typename T>
19 struct cbrt_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 = false;
30 
34  template <typename E>
35  static constexpr bool gpu_computable = (is_single_precision_t<T> && impl::egblas::has_scbrt) || (is_double_precision_t<T> && impl::egblas::has_dcbrt)
36  || (is_complex_single_t<T> && impl::egblas::has_ccbrt) || (is_complex_double_t<T> && impl::egblas::has_zcbrt);
37 
42  static constexpr int complexity() {
43  return 8;
44  }
45 
51  static constexpr T apply(const T& x) {
52  return std::cbrt(x);
53  }
54 
62  template <typename X, typename Y>
63  static auto gpu_compute_hint(const X& x, Y& y) noexcept {
64  decltype(auto) t1 = smart_gpu_compute_hint(x, y);
65 
66  auto t2 = force_temporary_gpu_dim_only(t1);
67 
68  T alpha(1.0);
69  impl::egblas::cbrt(etl::size(y), alpha, t1.gpu_memory(), 1, t2.gpu_memory(), 1);
70 
71  return t2;
72  }
79  template <typename X, typename Y>
80  static Y& gpu_compute(const X& x, Y& y) noexcept {
81  decltype(auto) t1 = select_smart_gpu_compute(x, y);
82 
83  T alpha(1.0);
84  impl::egblas::cbrt(etl::size(y), alpha, t1.gpu_memory(), 1, y.gpu_memory(), 1);
85 
86  y.validate_gpu();
87  y.invalidate_cpu();
88 
89  return y;
90  }
91 
96  static std::string desc() noexcept {
97  return "cbrt";
98  }
99 };
100 
104 template <typename TT>
105 struct cbrt_unary_op<std::complex<TT>> {
106  using T = std::complex<TT>;
107 
108  static constexpr bool linear = true;
109  static constexpr bool thread_safe = true;
110 
116  template <vector_mode_t V>
117  static constexpr bool vectorizable = false;
118 
122  template <typename E>
123  static constexpr bool gpu_computable = (is_single_precision_t<T> && impl::egblas::has_scbrt) || (is_double_precision_t<T> && impl::egblas::has_dcbrt)
124  || (is_complex_single_t<T> && impl::egblas::has_ccbrt) || (is_complex_double_t<T> && impl::egblas::has_zcbrt);
125 
130  static constexpr int complexity() {
131  return 8;
132  }
133 
139  static constexpr T apply(const T& z) {
140  auto z_abs = std::abs(z);
141  auto z_arg = std::arg(z);
142 
143  auto new_abs = std::cbrt(z_abs);
144  auto new_arg = z_arg / 3.0f;
145 
146  return {new_abs * std::cos(new_arg), new_abs * std::sin(new_arg)};
147  }
148 
156  template <typename X, typename Y>
157  static auto gpu_compute_hint(const X& x, Y& y) noexcept {
158  decltype(auto) t1 = smart_gpu_compute_hint(x, y);
159 
160  auto t2 = force_temporary_gpu_dim_only(t1);
161 
162  T alpha(1.0);
163  impl::egblas::cbrt(etl::size(y), alpha, t1.gpu_memory(), 1, t2.gpu_memory(), 1);
164 
165  return t2;
166  }
173  template <typename X, typename Y>
174  static Y& gpu_compute(const X& x, Y& y) noexcept {
175  decltype(auto) t1 = select_smart_gpu_compute(x, y);
176 
177  T alpha(1.0);
178  impl::egblas::cbrt(etl::size(y), alpha, t1.gpu_memory(), 1, y.gpu_memory(), 1);
179 
180  y.validate_gpu();
181  y.invalidate_cpu();
182 
183  return y;
184  }
185 
190  static std::string desc() noexcept {
191  return "cbrt";
192  }
193 };
194 
198 template <typename TT>
199 struct cbrt_unary_op<etl::complex<TT>> {
200  using T = etl::complex<TT>;
201 
202  static constexpr bool linear = true;
203  static constexpr bool thread_safe = true;
204 
210  template <vector_mode_t V>
211  static constexpr bool vectorizable = false;
212 
216  template <typename E>
217  static constexpr bool gpu_computable = (is_single_precision_t<T> && impl::egblas::has_scbrt) || (is_double_precision_t<T> && impl::egblas::has_dcbrt)
218  || (is_complex_single_t<T> && impl::egblas::has_ccbrt) || (is_complex_double_t<T> && impl::egblas::has_zcbrt);
219 
224  static constexpr int complexity() {
225  return 8;
226  }
227 
233  static constexpr T apply(const T& x) {
234  return etl::cbrt(x);
235  }
236 
244  template <typename X, typename Y>
245  static auto gpu_compute_hint(const X& x, Y& y) noexcept {
246  decltype(auto) t1 = smart_gpu_compute_hint(x, y);
247 
248  auto t2 = force_temporary_gpu_dim_only(t1);
249 
250  T alpha(1.0);
251  impl::egblas::cbrt(etl::size(y), alpha, t1.gpu_memory(), 1, t2.gpu_memory(), 1);
252 
253  return t2;
254  }
261  template <typename X, typename Y>
262  static Y& gpu_compute(const X& x, Y& y) noexcept {
263  decltype(auto) t1 = select_smart_gpu_compute(x, y);
264 
265  T alpha(1.0);
266  impl::egblas::cbrt(etl::size(y), alpha, t1.gpu_memory(), 1, y.gpu_memory(), 1);
267 
268  y.validate_gpu();
269  y.invalidate_cpu();
270 
271  return y;
272  }
273 
278  static std::string desc() noexcept {
279  return "cbrt";
280  }
281 };
282 
283 } //end of namespace etl
static constexpr int complexity()
Estimate the complexity of operator.
Definition: cbrt.hpp:130
auto sin(E &&value) -> detail::unary_helper< E, sin_unary_op >
Apply sinus on each value of the given expression.
Definition: function_expression_builder.hpp:114
static std::string desc() noexcept
Returns a textual representation of the operator.
Definition: cbrt.hpp:278
Complex number implementation.
Definition: complex.hpp:31
static Y & gpu_compute(const X &x, Y &y) noexcept
Compute the result of the operation using the GPU.
Definition: cbrt.hpp:80
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
auto cbrt(E &&value) -> detail::unary_helper< E, cbrt_unary_op >
Apply cubic root on each value of the given expression.
Definition: function_expression_builder.hpp:44
static Y & gpu_compute(const X &x, Y &y) noexcept
Compute the result of the operation using the GPU.
Definition: cbrt.hpp:174
Unary operation taking the cubic root value.
Definition: cbrt.hpp:19
static constexpr T apply(const T &x)
Apply the unary operator on x.
Definition: cbrt.hpp:233
static constexpr T apply(const T &x)
Apply the unary operator on x.
Definition: cbrt.hpp:51
auto abs(E &&value)
Apply absolute on each value of the given expression.
Definition: expression_builder.hpp:54
static constexpr int complexity()
Estimate the complexity of operator.
Definition: cbrt.hpp:224
static auto gpu_compute_hint(const X &x, Y &y) noexcept
Compute the result of the operation using the GPU.
Definition: cbrt.hpp:245
auto cos(E &&value) -> detail::unary_helper< E, cos_unary_op >
Apply cosinus on each value of the given expression.
Definition: function_expression_builder.hpp:104
Root namespace for the ETL library.
Definition: adapter.hpp:15
static constexpr bool thread_safe
Indicates if the operator is thread safe or not.
Definition: cbrt.hpp:21
static std::string desc() noexcept
Returns a textual representation of the operator.
Definition: cbrt.hpp:96
std::complex< TT > T
The real type.
Definition: cbrt.hpp:106
static constexpr bool vectorizable
Indicates if the expression is vectorizable using the given vector mode.
Definition: cbrt.hpp:29
static constexpr bool linear
Indicates if the operator is linear.
Definition: cbrt.hpp:20
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 std::string desc() noexcept
Returns a textual representation of the operator.
Definition: cbrt.hpp:190
static constexpr bool gpu_computable
Indicates if the operator can be computed on GPU.
Definition: cbrt.hpp:35
EGBLAS wrappers for the cbrt operation.
static Y & gpu_compute(const X &x, Y &y) noexcept
Compute the result of the operation using the GPU.
Definition: cbrt.hpp:262
static constexpr int complexity()
Estimate the complexity of operator.
Definition: cbrt.hpp:42
static constexpr T apply(const T &z)
Apply the unary operator on x.
Definition: cbrt.hpp:139
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
static auto gpu_compute_hint(const X &x, Y &y) noexcept
Compute the result of the operation using the GPU.
Definition: cbrt.hpp:63
static auto gpu_compute_hint(const X &x, Y &y) noexcept
Compute the result of the operation using the GPU.
Definition: cbrt.hpp:157
T arg(complex< T > z)
Computes the phase angle of the given complex number.
Definition: complex.hpp:311