Expression Templates Library (ETL)
sin.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/sin.hpp"
11 
12 namespace etl {
13 
18 template <typename T>
19 struct sin_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 = (V == vector_mode_t::SSE3 || V == vector_mode_t::AVX) && is_single_precision_t<T>;
36 
40  template <typename E>
41  static constexpr bool gpu_computable = (is_single_precision_t<T> && impl::egblas::has_ssin) || (is_double_precision_t<T> && impl::egblas::has_dsin)
42  || (is_complex_single_t<T> && impl::egblas::has_csin) || (is_complex_double_t<T> && impl::egblas::has_zsin);
43 
48  static constexpr int complexity() {
49  return 8;
50  }
51 
57  static constexpr T apply(const T& x) noexcept {
58  return std::sin(x);
59  }
60 
67  template <typename V = default_vec>
68  static vec_type<V> load(const vec_type<V>& x) noexcept {
69  return V::sin(x);
70  }
71 
79  template <typename X, typename Y>
80  static auto gpu_compute_hint(const X& x, Y& y) noexcept {
81  decltype(auto) t1 = smart_gpu_compute_hint(x, y);
82 
83  auto t2 = force_temporary_gpu_dim_only(t1);
84 
85  T alpha(1.0);
86  impl::egblas::sin(etl::size(y), alpha, t1.gpu_memory(), 1, t2.gpu_memory(), 1);
87 
88  return t2;
89  }
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::sin(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 "sin";
116  }
117 };
118 
122 template <typename TT>
123 struct sin_unary_op<etl::complex<TT>> {
124  using T = etl::complex<TT>;
125 
126  static constexpr bool linear = true;
127  static constexpr bool thread_safe = true;
128 
134  template <vector_mode_t V>
135  static constexpr bool vectorizable = false;
136 
140  template <typename E>
141  static constexpr bool gpu_computable = (is_single_precision_t<T> && impl::egblas::has_ssin) || (is_double_precision_t<T> && impl::egblas::has_dsin)
142  || (is_complex_single_t<T> && impl::egblas::has_csin) || (is_complex_double_t<T> && impl::egblas::has_zsin);
143 
148  static constexpr int complexity() {
149  return 8;
150  }
151 
157  static constexpr T apply(const T& x) noexcept {
158  return etl::sin(x);
159  }
160 
168  template <typename X, typename Y>
169  static auto gpu_compute_hint(const X& x, Y& y) noexcept {
170  decltype(auto) t1 = smart_gpu_compute_hint(x, y);
171 
172  auto t2 = force_temporary_gpu_dim_only(t1);
173 
174  T alpha(1.0);
175  impl::egblas::sin(etl::size(y), alpha, t1.gpu_memory(), 1, t2.gpu_memory(), 1);
176 
177  return t2;
178  }
179 
186  template <typename X, typename Y>
187  static Y& gpu_compute(const X& x, Y& y) noexcept {
188  decltype(auto) t1 = select_smart_gpu_compute(x, y);
189 
190  T alpha(1.0);
191  impl::egblas::sin(etl::size(y), alpha, t1.gpu_memory(), 1, y.gpu_memory(), 1);
192 
193  y.validate_gpu();
194  y.invalidate_cpu();
195 
196  return y;
197  }
198 
203  static std::string desc() noexcept {
204  return "sin";
205  }
206 };
207 
208 } //end of namespace etl
static constexpr bool gpu_computable
Indicates if the operator can be computed on GPU.
Definition: sin.hpp:41
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 constexpr T apply(const T &x) noexcept
Apply the unary operator on x.
Definition: sin.hpp:57
static constexpr T apply(const T &x) noexcept
Apply the unary operator on x.
Definition: sin.hpp:157
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: sin.hpp:98
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 thread_safe
Indicates if the operator is thread safe or not.
Definition: sin.hpp:27
static Y & gpu_compute(const X &x, Y &y) noexcept
Compute the result of the operation using the GPU.
Definition: sin.hpp:187
SSE3 is the max vectorization available.
typename V::template vec_type< T > vec_type
Definition: sin.hpp:24
static vec_type< V > load(const vec_type< V > &x) noexcept
Compute several applications of the operator at a time.
Definition: sin.hpp:68
static auto gpu_compute_hint(const X &x, Y &y) noexcept
Compute the result of the operation using the GPU.
Definition: sin.hpp:80
Root namespace for the ETL library.
Definition: adapter.hpp:15
static constexpr bool linear
Indicates if the operator is linear.
Definition: sin.hpp:26
EGBLAS wrappers for the sin operation.
static constexpr int complexity()
Estimate the complexity of operator.
Definition: sin.hpp:148
static std::string desc() noexcept
Returns a textual representation of the operator.
Definition: sin.hpp:203
static constexpr bool vectorizable
Indicates if the expression is vectorizable using the given vector mode.
Definition: sin.hpp:35
static std::string desc() noexcept
Returns a textual representation of the operator.
Definition: sin.hpp:114
Unary operation computing the sinus.
Definition: sin.hpp:19
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 int complexity()
Estimate the complexity of operator.
Definition: sin.hpp:48
static auto gpu_compute_hint(const X &x, Y &y) noexcept
Compute the result of the operation using the GPU.
Definition: sin.hpp:169
AVX is the max vectorization available.
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