Expression Templates Library (ETL)
real.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/real.hpp"
11 
12 namespace etl {
13 
18 template <typename T>
19 struct real_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_complex_single_t<T> && impl::egblas::has_creal) || (is_complex_double_t<T> && impl::egblas::has_zreal);
36 
41  static constexpr int complexity() {
42  return 1;
43  }
44 
50  static constexpr typename T::value_type apply(const T& x) noexcept {
51  return get_real(x);
52  }
53 
61  template <typename X, typename Y>
62  static auto gpu_compute_hint(const X& x, Y& y) noexcept {
63  decltype(auto) t1 = smart_gpu_compute_hint(x, y);
64 
65  auto t2 = etl::force_temporary_gpu_dim_only_t<typename T::value_type>(t1);
66 
67  typename T::value_type alpha(1.0);
68  impl::egblas::real(etl::size(y), alpha, t1.gpu_memory(), 1, t2.gpu_memory(), 1);
69 
70  return t2;
71  }
72 
79  template <typename X, typename Y>
80  static Y& gpu_compute(const X& x, Y& y) noexcept {
81  // Note: Cannot use select here since x and y don't have the same type
82  decltype(auto) t1 = smart_gpu_compute_hint(x, y);
83 
84  typename T::value_type alpha(1.0);
85  impl::egblas::real(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 "real";
99  }
100 };
101 
102 } //end of namespace etl
static std::string desc() noexcept
Returns a textual representation of the operator.
Definition: real.hpp:97
Unary operation extracting the real part of a complex number.
Definition: real.hpp:19
static Y & gpu_compute(const X &x, Y &y) noexcept
Compute the result of the operation using the GPU.
Definition: real.hpp:80
EGBLAS wrappers for the real operation.
static constexpr bool linear
Indicates if the operator is linear.
Definition: real.hpp:20
value_t< sub_type > value_type
The value contained in the expression.
Definition: dyn_matrix_view.hpp:31
static constexpr bool thread_safe
Indicates if the operator is thread safe or not.
Definition: real.hpp:21
Root namespace for the ETL library.
Definition: adapter.hpp:15
static constexpr bool gpu_computable
Indicates if the operator can be computed on GPU.
Definition: real.hpp:35
static constexpr bool vectorizable
Indicates if the expression is vectorizable using the given vector mode.
Definition: real.hpp:29
static constexpr int complexity()
Estimate the complexity of operator.
Definition: real.hpp:41
T get_real(const std::complex< T > &c)
Returns the real part of the given complex number.
Definition: complex.hpp:535
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: real.hpp:62
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 T::value_type apply(const T &x) noexcept
Apply the unary operator on x.
Definition: real.hpp:50