Expression Templates Library (ETL)
invsqrt.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 
11 
12 namespace etl {
13 
18 template <typename T>
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_sinvsqrt) || (is_double_precision_t<T> && impl::egblas::has_dinvsqrt)
36  || (is_complex_single_t<T> && impl::egblas::has_cinvsqrt) || (is_complex_double_t<T> && impl::egblas::has_zinvsqrt);
37 
42  static constexpr int complexity() {
43  return 8;
44  }
45 
51  static constexpr T apply(const T& x) {
52  return T(1) / std::sqrt(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::invsqrt(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::invsqrt(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 "invsqrt";
98  }
99 };
100 
104 template <typename TT>
106  using T = etl::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_sinvsqrt) || (is_double_precision_t<T> && impl::egblas::has_dinvsqrt)
124  || (is_complex_single_t<T> && impl::egblas::has_cinvsqrt) || (is_complex_double_t<T> && impl::egblas::has_zinvsqrt);
125 
130  static constexpr int complexity() {
131  return 8;
132  }
133 
139  static constexpr T apply(const T& x) {
140  return etl::invsqrt(x);
141  }
142 
150  template <typename X, typename Y>
151  static auto gpu_compute_hint(const X& x, Y& y) noexcept {
152  decltype(auto) t1 = smart_gpu_compute_hint(x, y);
153 
154  auto t2 = force_temporary_gpu_dim_only(t1);
155 
156  T alpha(1.0);
157  impl::egblas::invsqrt(etl::size(y), alpha, t1.gpu_memory(), 1, t2.gpu_memory(), 1);
158 
159  return t2;
160  }
167  template <typename X, typename Y>
168  static Y& gpu_compute(const X& x, Y& y) noexcept {
169  decltype(auto) t1 = select_smart_gpu_compute(x, y);
170 
171  T alpha(1.0);
172  impl::egblas::invsqrt(etl::size(y), alpha, t1.gpu_memory(), 1, y.gpu_memory(), 1);
173 
174  y.validate_gpu();
175  y.invalidate_cpu();
176 
177  return y;
178  }
179 
184  static std::string desc() noexcept {
185  return "invsqrt";
186  }
187 };
188 
189 } //end of namespace etl
static constexpr T apply(const T &x)
Apply the unary operator on x.
Definition: invsqrt.hpp:51
static constexpr int complexity()
Estimate the complexity of operator.
Definition: invsqrt.hpp:130
Complex number implementation.
Definition: complex.hpp:31
static auto gpu_compute_hint(const X &x, Y &y) noexcept
Compute the result of the operation using the GPU.
Definition: invsqrt.hpp:63
auto sqrt(E &&value) -> detail::unary_helper< E, sqrt_unary_op >
Apply square root on each value of the given expression.
Definition: function_expression_builder.hpp:24
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: invsqrt.hpp:21
static auto gpu_compute_hint(const X &x, Y &y) noexcept
Compute the result of the operation using the GPU.
Definition: invsqrt.hpp:151
static constexpr T apply(const T &x)
Apply the unary operator on x.
Definition: invsqrt.hpp:139
static constexpr bool linear
Indicates if the operator is linear.
Definition: invsqrt.hpp:20
Root namespace for the ETL library.
Definition: adapter.hpp:15
static Y & gpu_compute(const X &x, Y &y) noexcept
Compute the result of the operation using the GPU.
Definition: invsqrt.hpp:80
EGBLAS wrappers for the invsqrt operation.
static std::string desc() noexcept
Returns a textual representation of the operator.
Definition: invsqrt.hpp:96
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 vectorizable
Indicates if the expression is vectorizable using the given vector mode.
Definition: invsqrt.hpp:29
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
Unary operation taking the inverse square root value.
Definition: invsqrt.hpp:19
auto invsqrt(E &&value) -> detail::unary_helper< E, invsqrt_unary_op >
Apply inverse square root on each value of the given expression.
Definition: function_expression_builder.hpp:34
static Y & gpu_compute(const X &x, Y &y) noexcept
Compute the result of the operation using the GPU.
Definition: invsqrt.hpp:168
static constexpr int complexity()
Estimate the complexity of operator.
Definition: invsqrt.hpp:42
static std::string desc() noexcept
Returns a textual representation of the operator.
Definition: invsqrt.hpp:184
static constexpr bool gpu_computable
Indicates if the operator can be computed on GPU.
Definition: invsqrt.hpp:35