Expression Templates Library (ETL)
softplus.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_ssoftplus)
36  || (is_double_precision_t<T> && impl::egblas::has_dsoftplus)
37  || (is_complex_single_t<T> && impl::egblas::has_csoftplus)
38  || (is_complex_double_t<T> && impl::egblas::has_zsoftplus);
39 
44  static constexpr int complexity() {
45  return 8;
46  }
47 
53  static constexpr T apply(const T& x) {
54  return math::softplus(x);
55  }
56 
64  template <typename X, typename Y>
65  static auto gpu_compute_hint(const X& x, Y& y) noexcept {
66  decltype(auto) t1 = smart_gpu_compute_hint(x, y);
67 
68  auto t2 = force_temporary_gpu_dim_only(t1);
69 
70  T alpha(1.0);
71  impl::egblas::softplus(etl::size(y), alpha, t1.gpu_memory(), 1, t2.gpu_memory(), 1);
72 
73  return t2;
74  }
75 
82  template <typename X, typename Y>
83  static Y& gpu_compute(const X& x, Y& y) noexcept {
84  decltype(auto) t1 = select_smart_gpu_compute(x, y);
85 
86  T alpha(1.0);
87  impl::egblas::softplus(etl::size(y), alpha, t1.gpu_memory(), 1, y.gpu_memory(), 1);
88 
89  y.validate_gpu();
90  y.invalidate_cpu();
91 
92  return y;
93  }
94 
99  static std::string desc() noexcept {
100  return "softplus";
101  }
102 };
103 
104 } //end of namespace etl
static auto gpu_compute_hint(const X &x, Y &y) noexcept
Compute the result of the operation using the GPU.
Definition: softplus.hpp:65
static std::string desc() noexcept
Returns a textual representation of the operator.
Definition: softplus.hpp:99
EGBLAS wrappers for the softplus operation.
Unary operation computing the softplus.
Definition: softplus.hpp:19
static constexpr bool thread_safe
Indicates if the operator is thread safe or not.
Definition: softplus.hpp:21
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 gpu_computable
Indicates if the operator can be computed on GPU.
Definition: softplus.hpp:35
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: softplus.hpp:83
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 constexpr bool linear
Indicates if the operator is linear.
Definition: softplus.hpp:20
constexpr size_t size(const E &expr) noexcept
Returns the size of the given ETL expression.
Definition: helpers.hpp:108
static constexpr T apply(const T &x)
Apply the unary operator on x.
Definition: softplus.hpp:53
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: softplus.hpp:29
static constexpr int complexity()
Estimate the complexity of operator.
Definition: softplus.hpp:44