Expression Templates Library (ETL)
sign.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/sign.hpp"
11 
12 namespace etl {
13 
18 template <typename T>
19 struct sign_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_ssign) || (is_double_precision_t<T> && impl::egblas::has_dsign)
36  || (is_complex_single_t<T> && impl::egblas::has_csign) || (is_complex_double_t<T> && impl::egblas::has_zsign);
37 
42  static constexpr int complexity() {
43  return 1;
44  }
45 
51  static constexpr T apply(const T& x) noexcept {
52  return math::sign(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::sign(etl::size(y), alpha, t1.gpu_memory(), 1, t2.gpu_memory(), 1);
70 
71  return t2;
72  }
73 
80  template <typename X, typename Y>
81  static Y& gpu_compute(const X& x, Y& y) noexcept {
82  decltype(auto) t1 = select_smart_gpu_compute(x, y);
83 
84  T alpha(1.0);
85  impl::egblas::sign(etl::size(y), alpha, t1.gpu_memory(), 1, y.gpu_memory(), 1);
86 
87  y.validate_gpu();
88  y.invalidate_cpu();
89 
90  return y;
91  }
92 
97  static std::string desc() noexcept {
98  return "sign";
99  }
100 };
101 
102 } //end of namespace etl
Unary operation computing the sign.
Definition: sign.hpp:19
static std::string desc() noexcept
Returns a textual representation of the operator.
Definition: sign.hpp:97
static Y & gpu_compute(const X &x, Y &y) noexcept
Compute the result of the operation using the GPU.
Definition: sign.hpp:81
EGBLAS wrappers for the sign operation.
static constexpr T apply(const T &x) noexcept
Apply the unary operator on x.
Definition: sign.hpp:51
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: sign.hpp:29
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: sign.hpp:63
static constexpr int complexity()
Estimate the complexity of operator.
Definition: sign.hpp:42
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: sign.hpp:21
static constexpr bool linear
Indicates if the operator is linear.
Definition: sign.hpp:20
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 constexpr bool gpu_computable
Indicates if the operator can be computed on GPU.
Definition: sign.hpp:35