Uni20
Asynchronous tensor and runtime library.
Loading...
Searching...
No Matches
elementwise_functions.hpp
Go to the documentation of this file.
1#pragma once
2
10
11#include <type_traits>
12#include <utility>
13
14namespace uni20::linalg
15{
16
19template <class Value> struct constant
20{
21 [[no_unique_address]] Value value;
22
23 [[nodiscard]] UNI20_HOST_DEVICE constexpr auto operator()() const -> Value { return value; }
24};
25
26template <class Value> constant(Value) -> constant<std::decay_t<Value>>;
27
31struct negate
32{
33 template <class Value>
34 [[nodiscard]] UNI20_HOST_DEVICE constexpr auto operator()(Value value) const noexcept(noexcept(-value))
35 -> decltype(-value)
36 {
37 return -value;
38 }
39};
40
42struct square
43{
44 template <class Value>
45 [[nodiscard]] UNI20_HOST_DEVICE constexpr auto operator()(Value value) const noexcept(noexcept(value * value))
46 -> decltype(value * value)
47 {
48 return value * value;
49 }
50};
51
54{
55 template <class Value>
56 [[nodiscard]] UNI20_HOST_DEVICE constexpr auto operator()(Value value) const noexcept(noexcept(Value{1} / value))
57 -> decltype(Value{1} / value)
58 {
59 return Value{1} / value;
60 }
61};
62
67template <class Factor> struct scale
68{
69 Factor factor;
70
71 template <class Value>
72 [[nodiscard]] UNI20_HOST_DEVICE constexpr auto operator()(Value value) const noexcept(noexcept(factor * value))
73 -> decltype(factor * value)
74 {
75 return factor * value;
76 }
77};
78
80template <class Factor> scale(Factor) -> scale<std::decay_t<Factor>>;
81
85template <class Factor> struct add_scaled
86{
87 Factor factor;
88
89 template <class Output, class Input>
90 [[nodiscard]] UNI20_HOST_DEVICE constexpr auto operator()(Output output, Input input) const
91 noexcept(noexcept(output + factor * input)) -> decltype(output + factor * input)
92 {
93 return output + factor * input;
94 }
95};
96
98template <class Factor> add_scaled(Factor) -> add_scaled<std::decay_t<Factor>>;
99
101struct add
102{
103 template <class Left, class Right>
104 [[nodiscard]] UNI20_HOST_DEVICE constexpr auto operator()(Left left, Right right) const
105 noexcept(noexcept(left + right)) -> decltype(left + right)
106 {
107 return left + right;
108 }
109};
110
113{
114 template <class Left, class Right>
115 [[nodiscard]] UNI20_HOST_DEVICE constexpr auto operator()(Left left, Right right) const
116 noexcept(noexcept(left - right)) -> decltype(left - right)
117 {
118 return left - right;
119 }
120};
121
124{
125 template <class Left, class Right>
126 [[nodiscard]] UNI20_HOST_DEVICE constexpr auto operator()(Left left, Right right) const
127 noexcept(noexcept(left * right)) -> decltype(left * right)
128 {
129 return left * right;
130 }
131};
132
134struct divide
135{
136 template <class Left, class Right>
137 [[nodiscard]] UNI20_HOST_DEVICE constexpr auto operator()(Left left, Right right) const
138 noexcept(noexcept(left / right)) -> decltype(left / right)
139 {
140 return left / right;
141 }
142};
143
144} // namespace uni20::linalg
Project compiler annotations shared by host and device code.
#define UNI20_HOST_DEVICE
Definition compiler_attributes.hpp:12
locale_t value
Definition data_table.cpp:49
Definition linalg/async.hpp:28
Add one retained scalar multiple to an existing element value.
Definition elementwise_functions.hpp:86
Factor factor
Definition elementwise_functions.hpp:87
UNI20_HOST_DEVICE constexpr auto operator()(Output output, Input input) const noexcept(noexcept(output+factor *input)) -> decltype(output+factor *input)
Definition elementwise_functions.hpp:90
Return the sum of two element values.
Definition elementwise_functions.hpp:102
UNI20_HOST_DEVICE constexpr auto operator()(Left left, Right right) const noexcept(noexcept(left+right)) -> decltype(left+right)
Definition elementwise_functions.hpp:104
Nullary elementwise function that returns one retained value.
Definition elementwise_functions.hpp:20
UNI20_HOST_DEVICE constexpr auto operator()() const -> Value
Definition elementwise_functions.hpp:23
Value value
Definition elementwise_functions.hpp:21
Return the quotient of two element values.
Definition elementwise_functions.hpp:135
UNI20_HOST_DEVICE constexpr auto operator()(Left left, Right right) const noexcept(noexcept(left/right)) -> decltype(left/right)
Definition elementwise_functions.hpp:137
Return the product of two element values.
Definition elementwise_functions.hpp:124
UNI20_HOST_DEVICE constexpr auto operator()(Left left, Right right) const noexcept(noexcept(left *right)) -> decltype(left *right)
Definition elementwise_functions.hpp:126
Return the additive inverse of one element value.
Definition elementwise_functions.hpp:32
UNI20_HOST_DEVICE constexpr auto operator()(Value value) const noexcept(noexcept(-value)) -> decltype(-value)
Definition elementwise_functions.hpp:34
Return the multiplicative inverse of one element value.
Definition elementwise_functions.hpp:54
UNI20_HOST_DEVICE constexpr auto operator()(Value value) const noexcept(noexcept(Value{1}/value)) -> decltype(Value{1}/value)
Definition elementwise_functions.hpp:56
Multiply one element value by a retained scalar factor.
Definition elementwise_functions.hpp:68
Factor factor
Definition elementwise_functions.hpp:69
UNI20_HOST_DEVICE constexpr auto operator()(Value value) const noexcept(noexcept(factor *value)) -> decltype(factor *value)
Definition elementwise_functions.hpp:72
Return the product of one element value with itself.
Definition elementwise_functions.hpp:43
UNI20_HOST_DEVICE constexpr auto operator()(Value value) const noexcept(noexcept(value *value)) -> decltype(value *value)
Definition elementwise_functions.hpp:45
Return the difference of two element values.
Definition elementwise_functions.hpp:113
UNI20_HOST_DEVICE constexpr auto operator()(Left left, Right right) const noexcept(noexcept(left - right)) -> decltype(left - right)
Definition elementwise_functions.hpp:115