Expression Templates Library (ETL)
clip.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 
19 template <typename T, typename S>
24  template <typename V = default_vec>
25  using vec_type = typename V::template vec_type<T>;
26 
27  static constexpr bool linear = true;
28  static constexpr bool thread_safe = true;
29 
35  template <vector_mode_t V>
36  static constexpr bool vectorizable = intel_compiler && !is_complex_t<T>;
37 
41  template <typename E>
42  static constexpr bool gpu_computable = (is_single_precision_t<T> && impl::egblas::has_sclip_value)
43  || (is_double_precision_t<T> && impl::egblas::has_dclip_value)
44  || (is_complex_single_t<T> && impl::egblas::has_cclip_value)
45  || (is_complex_double_t<T> && impl::egblas::has_zclip_value);
46 
51  static constexpr int complexity() {
52  return 1;
53  }
54 
55  S min;
56  S max;
57 
63  clip_scalar_op(S min, S max) : min(min), max(max) {}
64 
70  constexpr T apply(const T& x) const noexcept {
71  return std::min(std::max(x, min), max);
72  }
73 
74 #ifdef __INTEL_COMPILER
75 
82  template <typename V = default_vec>
83  vec_type<V> load(const vec_type<V>& lhs) const noexcept {
84  return V::min(V::max(lhs, V::set(min)), V::set(max));
85  }
86 
87 #endif
88 
96  template <typename X, typename Y>
97  auto gpu_compute_hint(const X& x, Y& y) const noexcept {
98  decltype(auto) t1 = smart_gpu_compute_hint(x, y);
99 
100  auto t2 = force_temporary_gpu(t1);
101  impl::egblas::clip_value(etl::size(y), T(1), min, max, t2.gpu_memory(), 1);
102  return t2;
103  }
104 
111  template <typename X, typename Y>
112  Y& gpu_compute(const X& x, Y& y) const noexcept {
113  smart_gpu_compute(x, y);
114 
115  impl::egblas::clip_value(etl::size(y), T(1), min, max, y.gpu_memory(), 1);
116 
117  y.validate_gpu();
118  y.invalidate_cpu();
119 
120  return y;
121  }
122 
127  static std::string desc() noexcept {
128  return "clip";
129  }
130 };
131 
132 } //end of namespace etl
S min
The minimum for clipping.
Definition: clip.hpp:55
auto max(L &&lhs, R &&rhs)
Create an expression with the max value of lhs or rhs.
Definition: expression_builder.hpp:65
static constexpr bool vectorizable
Indicates if the expression is vectorizable using the given vector mode.
Definition: clip.hpp:36
Unary operation that clips all values between two scalars.
Definition: clip.hpp:20
Y & gpu_compute(const X &x, Y &y) const noexcept
Compute the result of the operation using the GPU.
Definition: clip.hpp:112
typename V::template vec_type< T > vec_type
Definition: clip.hpp:25
static constexpr int complexity()
Estimate the complexity of operator.
Definition: clip.hpp:51
constexpr bool intel_compiler
Indicates if the projectis compiled with intel compiler.
Definition: config.hpp:225
auto load(size_t x) const noexcept
Load several elements of the expression at once.
Definition: dyn_matrix_view.hpp:143
Root namespace for the ETL library.
Definition: adapter.hpp:15
static std::string desc() noexcept
Returns a textual representation of the operator.
Definition: clip.hpp:127
clip_scalar_op(S min, S max)
Builds a new operator.
Definition: clip.hpp:63
auto min(L &&lhs, R &&rhs)
Create an expression with the min value of lhs or rhs.
Definition: expression_builder.hpp:77
static constexpr bool linear
Indicates if the operator is linear or not.
Definition: clip.hpp:27
constexpr T apply(const T &x) const noexcept
Apply the unary operator on x.
Definition: clip.hpp:70
auto gpu_compute_hint(const X &x, Y &y) const noexcept
Compute the result of the operation using the GPU.
Definition: clip.hpp:97
constexpr size_t size(const E &expr) noexcept
Returns the size of the given ETL expression.
Definition: helpers.hpp:108
EGBLAS wrappers for the clip_value operation.
static constexpr bool thread_safe
Indicates if the operator is thread safe or not.
Definition: clip.hpp:28
decltype(auto) force_temporary_gpu(E &&expr)
Force a temporary out of the expression.
Definition: temporary.hpp:196
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
decltype(auto) smart_gpu_compute(X &x, Y &y)
Compute the expression into a representation that is GPU up to date and store this representation in ...
Definition: helpers.hpp:397
static constexpr bool gpu_computable
Indicates if the operator can be computed on GPU.
Definition: clip.hpp:42
S max
The maximum for clipping.
Definition: clip.hpp:56