Expression Templates Library (ETL)
minus.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>
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 = !is_complex_t<T>;
36 
40  template <typename E>
41  static constexpr bool gpu_computable = (is_single_precision_t<T> && impl::egblas::has_sminus) || (is_double_precision_t<T> && impl::egblas::has_dminus)
42  || (is_complex_single_t<T> && impl::egblas::has_cminus) || (is_complex_double_t<T> && impl::egblas::has_zminus);
43 
48  static constexpr int complexity() {
49  return 1;
50  }
51 
57  static constexpr T apply(const T& x) noexcept {
58  return -x;
59  }
60 
67  template <typename V = default_vec>
68  static vec_type<V> load(const vec_type<V>& x) noexcept {
69  return V::minus(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::minus(etl::size(y), alpha, t1.gpu_memory(), 1, t2.gpu_memory(), 1);
87 
88  return t2;
89  }
96  template <typename X, typename Y>
97  static Y& gpu_compute(const X& x, Y& y) noexcept {
98  decltype(auto) t1 = select_smart_gpu_compute(x, y);
99 
100  T alpha(1.0);
101  impl::egblas::minus(etl::size(y), alpha, t1.gpu_memory(), 1, y.gpu_memory(), 1);
102 
103  y.validate_gpu();
104  y.invalidate_cpu();
105 
106  return y;
107  }
108 
113  static std::string desc() noexcept {
114  return "-";
115  }
116 };
117 
118 } //end of namespace etl
static vec_type< V > load(const vec_type< V > &x) noexcept
Compute several applications of the operator at a time.
Definition: minus.hpp:68
static std::string desc() noexcept
Returns a textual representation of the operator.
Definition: minus.hpp:113
static constexpr bool gpu_computable
Indicates if the operator can be computed on GPU.
Definition: minus.hpp:41
typename V::template vec_type< T > vec_type
Definition: minus.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 auto gpu_compute_hint(const X &x, Y &y) noexcept
Compute the result of the operation using the GPU.
Definition: minus.hpp:80
static constexpr bool linear
Indicates if the operator is linear.
Definition: minus.hpp:26
static constexpr T apply(const T &x) noexcept
Apply the unary operator on x.
Definition: minus.hpp:57
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: minus.hpp:97
Unary operation computing the minus operation.
Definition: minus.hpp:19
EGBLAS wrappers for the minus operation.
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: minus.hpp:48
static constexpr bool thread_safe
Indicates if the operator is thread safe or not.
Definition: minus.hpp:27
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 vectorizable
Indicates if the expression is vectorizable using the given vector mode.
Definition: minus.hpp:35