Expression Templates Library (ETL)
cos.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/cos.hpp"
11 
12 namespace etl {
13 
18 template <typename T>
19 struct cos_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 = (V == vector_mode_t::SSE3 || V == vector_mode_t::AVX) && is_single_precision_t<T>;
30 
34  template <typename E>
35  static constexpr bool gpu_computable = (is_single_precision_t<T> && impl::egblas::has_scos) || (is_double_precision_t<T> && impl::egblas::has_dcos)
36  || (is_complex_single_t<T> && impl::egblas::has_ccos) || (is_complex_double_t<T> && impl::egblas::has_zcos);
37 
42  static constexpr int complexity() {
43  return 8;
44  }
45 
49  template <typename V = default_vec>
50  using vec_type = typename V::template vec_type<T>;
51 
57  static constexpr T apply(const T& x) noexcept {
58  return std::cos(x);
59  }
60 
67  template <typename V = default_vec>
68  static vec_type<V> load(const vec_type<V>& x) noexcept {
69  return V::cos(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::cos(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::cos(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 "cos";
116  }
117 };
118 
122 template <typename TT>
123 struct cos_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_scos) || (is_double_precision_t<T> && impl::egblas::has_dcos)
142  || (is_complex_single_t<T> && impl::egblas::has_ccos) || (is_complex_double_t<T> && impl::egblas::has_zcos);
143 
148  static constexpr int complexity() {
149  return 8;
150  }
151 
157  static constexpr T apply(const T& x) noexcept {
158  return etl::cos(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::cos(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::cos(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 "cos";
205  }
206 };
207 
208 } //end of namespace etl
static Y & gpu_compute(const X &x, Y &y) noexcept
Compute the result of the operation using the GPU.
Definition: cos.hpp:187
Complex number implementation.
Definition: complex.hpp:31
static constexpr bool linear
Indicates if the operator is linear.
Definition: cos.hpp:20
static std::string desc() noexcept
Returns a textual representation of the operator.
Definition: cos.hpp:114
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 vec_type< V > load(const vec_type< V > &x) noexcept
Compute several applications of the operator at a time.
Definition: cos.hpp:68
SSE3 is the max vectorization available.
static constexpr T apply(const T &x) noexcept
Apply the unary operator on x.
Definition: cos.hpp:57
static constexpr bool thread_safe
Indicates if the operator is thread safe or not.
Definition: cos.hpp:21
typename V::template vec_type< T > vec_type
Definition: cos.hpp:50
static constexpr bool gpu_computable
Indicates if the operator can be computed on GPU.
Definition: cos.hpp:35
static constexpr int complexity()
Estimate the complexity of operator.
Definition: cos.hpp:148
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 vectorizable
Indicates if the expression is vectorizable using the given vector mode.
Definition: cos.hpp:29
static constexpr T apply(const T &x) noexcept
Apply the unary operator on x.
Definition: cos.hpp:157
decltype(auto) force_temporary_gpu_dim_only(E &&expr)
Force a temporary out of the expression, without copying its content.
Definition: temporary.hpp:223
static std::string desc() noexcept
Returns a textual representation of the operator.
Definition: cos.hpp:203
constexpr size_t size(const E &expr) noexcept
Returns the size of the given ETL expression.
Definition: helpers.hpp:108
static auto gpu_compute_hint(const X &x, Y &y) noexcept
Compute the result of the operation using the GPU.
Definition: cos.hpp:169
AVX is the max vectorization available.
static constexpr int complexity()
Estimate the complexity of operator.
Definition: cos.hpp:42
static auto gpu_compute_hint(const X &x, Y &y) noexcept
Compute the result of the operation using the GPU.
Definition: cos.hpp:80
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 Y & gpu_compute(const X &x, Y &y) noexcept
Compute the result of the operation using the GPU.
Definition: cos.hpp:98
Unary operation computing the cosinus.
Definition: cos.hpp:19
EGBLAS wrappers for the cos operation.