forked from kokkos/simd-math
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cuda_warp.hpp
272 lines (247 loc) · 9.12 KB
/
cuda_warp.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
//@HEADER
// ************************************************************************
//
// Kokkos v. 2.0
// Copyright (2014) Sandia Corporation
//
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// the U.S. Government retains certain rights in this software.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the Corporation nor the names of the
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Questions? Contact Christian R. Trott ([email protected])
//
// ************************************************************************
//@HEADER
*/
#pragma once
#include "simd_common.hpp"
#ifdef __CUDACC__
#define SIMD_CUDA_ALWAYS_INLINE __forceinline__
#endif
#ifdef __CUDACC__
#define SIMD_HOST_DEVICE __host__ __device__
#else
#define SIMD_HOST_DEVICE
#endif
#ifdef __CUDACC__
#define SIMD_DEVICE __device__
#else
#define SIMD_DEVICE
#endif
#ifdef __CUDACC__
namespace SIMD_NAMESPACE {
namespace simd_abi {
template <int N>
class cuda_warp {
static_assert(N <= 32, "CUDA warps can't be more than 32 threads");
public:
SIMD_HOST_DEVICE static unsigned mask() {
return (unsigned(1) << N) - unsigned(1);
}
};
}
template <class T, int N>
class simd_storage<T, simd_abi::cuda_warp<N>> {
T m_value[simd<T, simd_abi::cuda_warp<N>>::size()];
public:
using value_type = T;
using abi_type = simd_abi::cuda_warp<N>;
using simd_type = simd<T, abi_type>;
SIMD_ALWAYS_INLINE inline simd_storage() = default;
SIMD_ALWAYS_INLINE SIMD_HOST_DEVICE inline static constexpr
int size() { return simd<T, abi_type>::size(); }
SIMD_ALWAYS_INLINE SIMD_HOST_DEVICE inline
simd_storage(simd<T, abi_type> const& value) {
value.copy_to(m_value, element_aligned_tag());
}
SIMD_ALWAYS_INLINE SIMD_HOST_DEVICE explicit inline
simd_storage(T value)
:simd_storage(simd<T, abi_type>(value))
{}
SIMD_ALWAYS_INLINE SIMD_HOST_DEVICE inline
simd_storage& operator=(simd<T, abi_type> const& value) {
value.copy_to(m_value, element_aligned_tag());
return *this;
}
SIMD_ALWAYS_INLINE SIMD_HOST_DEVICE
T const* data() const { return m_value; }
SIMD_ALWAYS_INLINE SIMD_HOST_DEVICE
T* data() { return m_value; }
SIMD_ALWAYS_INLINE SIMD_HOST_DEVICE
T const& operator[](int i) const { return m_value[i]; }
SIMD_ALWAYS_INLINE SIMD_HOST_DEVICE
T& operator[](int i) { return m_value[i]; }
};
template <class T, int N>
class simd_mask<T, simd_abi::cuda_warp<N>> {
bool m_value;
public:
using value_type = bool;
using abi_type = simd_abi::cuda_warp<N>;
using simd_type = simd<T, abi_type>;
SIMD_CUDA_ALWAYS_INLINE simd_mask() = default;
SIMD_ALWAYS_INLINE SIMD_HOST_DEVICE static constexpr
int size() { return N; }
SIMD_CUDA_ALWAYS_INLINE SIMD_HOST_DEVICE
simd_mask(bool value)
:m_value(value)
{}
SIMD_CUDA_ALWAYS_INLINE SIMD_HOST_DEVICE constexpr
bool get() const {
return m_value;
}
SIMD_CUDA_ALWAYS_INLINE SIMD_HOST_DEVICE
simd_mask operator||(simd_mask const& other) const {
return m_value || other.m_value;
}
SIMD_CUDA_ALWAYS_INLINE SIMD_HOST_DEVICE
simd_mask operator&&(simd_mask const& other) const {
return m_value && other.m_value;
}
SIMD_CUDA_ALWAYS_INLINE SIMD_HOST_DEVICE
simd_mask operator!() const {
return !m_value;
}
};
template <class T, int N>
SIMD_CUDA_ALWAYS_INLINE SIMD_HOST_DEVICE
bool all_of(simd_mask<T, simd_abi::cuda_warp<N>> const& a) {
return bool(__all_sync(simd_abi::cuda_warp<N>::mask(), int(a.get())));
}
template <class T, int N>
SIMD_CUDA_ALWAYS_INLINE SIMD_HOST_DEVICE
bool any_of(simd_mask<T, simd_abi::cuda_warp<N>> const& a) {
return bool(__any_sync(simd_abi::cuda_warp<N>::mask(), int(a.get())));
}
template <class T, int N>
class simd<T, simd_abi::cuda_warp<N>> {
T m_value;
public:
using value_type = T;
using abi_type = simd_abi::cuda_warp<N>;
using mask_type = simd_mask<T, abi_type>;
using storage_type = simd_storage<T, abi_type>;
SIMD_CUDA_ALWAYS_INLINE simd() = default;
SIMD_CUDA_ALWAYS_INLINE SIMD_HOST_DEVICE static constexpr int size() { return N; }
SIMD_CUDA_ALWAYS_INLINE SIMD_HOST_DEVICE simd(T value)
:m_value(value)
{}
SIMD_CUDA_ALWAYS_INLINE SIMD_HOST_DEVICE
simd(storage_type const& value) {
copy_from(value.data(), element_aligned_tag());
}
SIMD_CUDA_ALWAYS_INLINE SIMD_HOST_DEVICE
simd& operator=(storage_type const& value) {
copy_from(value.data(), element_aligned_tag());
return *this;
}
template <class Flags>
SIMD_CUDA_ALWAYS_INLINE SIMD_HOST_DEVICE simd(T const* ptr, Flags flags) {
copy_from(ptr, flags);
}
SIMD_CUDA_ALWAYS_INLINE SIMD_HOST_DEVICE simd operator*(simd const& other) const {
return simd(m_value * other.m_value);
}
SIMD_CUDA_ALWAYS_INLINE SIMD_HOST_DEVICE simd operator/(simd const& other) const {
return simd(m_value / other.m_value);
}
SIMD_CUDA_ALWAYS_INLINE SIMD_HOST_DEVICE simd operator+(simd const& other) const {
return simd(m_value + other.m_value);
}
SIMD_CUDA_ALWAYS_INLINE SIMD_HOST_DEVICE simd operator-(simd const& other) const {
return simd(m_value - other.m_value);
}
SIMD_CUDA_ALWAYS_INLINE SIMD_HOST_DEVICE simd operator-() const {
return simd(-m_value);
}
SIMD_CUDA_ALWAYS_INLINE SIMD_HOST_DEVICE void copy_from(T const* ptr, element_aligned_tag) {
#ifdef __CUDA_ARCH__
m_value = ptr[threadIdx.x];
#endif
}
SIMD_CUDA_ALWAYS_INLINE SIMD_HOST_DEVICE void copy_to(T* ptr, element_aligned_tag) const {
#ifdef __CUDA_ARCH__
ptr[threadIdx.x] = m_value;
#endif
}
SIMD_CUDA_ALWAYS_INLINE SIMD_HOST_DEVICE T get() const {
return m_value;
}
SIMD_CUDA_ALWAYS_INLINE SIMD_HOST_DEVICE
mask_type operator<(simd const& other) const {
return mask_type(m_value < other.m_value);
}
SIMD_CUDA_ALWAYS_INLINE SIMD_HOST_DEVICE
mask_type operator==(simd const& other) const {
return mask_type(m_value == other.m_value);
}
};
template <class T, int N>
SIMD_CUDA_ALWAYS_INLINE SIMD_HOST_DEVICE simd<T, simd_abi::cuda_warp<N>> abs(simd<T, simd_abi::cuda_warp<N>> const& a) {
return simd<T, simd_abi::cuda_warp<N>>(std::abs(a.get()));
}
template <class T, int N>
SIMD_CUDA_ALWAYS_INLINE SIMD_HOST_DEVICE simd<T, simd_abi::cuda_warp<N>> sqrt(simd<T, simd_abi::cuda_warp<N>> const& a) {
return simd<T, simd_abi::cuda_warp<N>>(std::sqrt(a.get()));
}
template <class T, int N>
SIMD_CUDA_ALWAYS_INLINE SIMD_HOST_DEVICE simd<T, simd_abi::cuda_warp<N>> cbrt(simd<T, simd_abi::cuda_warp<N>> const& a) {
return simd<T, simd_abi::cuda_warp<N>>(std::cbrt(a.get()));
}
template <class T, int N>
SIMD_CUDA_ALWAYS_INLINE SIMD_HOST_DEVICE simd<T, simd_abi::cuda_warp<N>> exp(simd<T, simd_abi::cuda_warp<N>> const& a) {
return simd<T, simd_abi::cuda_warp<N>>(std::exp(a.get()));
}
template <class T, int N>
SIMD_CUDA_ALWAYS_INLINE SIMD_HOST_DEVICE simd<T, simd_abi::cuda_warp<N>> fma(
simd<T, simd_abi::cuda_warp<N>> const& a,
simd<T, simd_abi::cuda_warp<N>> const& b,
simd<T, simd_abi::cuda_warp<N>> const& c) {
return simd<T, simd_abi::cuda_warp<N>>((a.get() * b.get()) + c.get());
}
template <class T, int N>
SIMD_CUDA_ALWAYS_INLINE SIMD_HOST_DEVICE simd<T, simd_abi::cuda_warp<N>> max(
simd<T, simd_abi::cuda_warp<N>> const& a, simd<T, simd_abi::cuda_warp<N>> const& b) {
return simd<T, simd_abi::cuda_warp<N>>((a.get() < b.get()) ? b.get() : a.get());
}
template <class T, int N>
SIMD_CUDA_ALWAYS_INLINE SIMD_HOST_DEVICE simd<T, simd_abi::cuda_warp<N>> min(
simd<T, simd_abi::cuda_warp<N>> const& a, simd<T, simd_abi::cuda_warp<N>> const& b) {
return simd<T, simd_abi::cuda_warp<N>>((b.get() < a.get()) ? b.get() : a.get());
}
template <class T, int N>
SIMD_CUDA_ALWAYS_INLINE SIMD_HOST_DEVICE simd<T, simd_abi::cuda_warp<N>> choose(
simd_mask<T, simd_abi::cuda_warp<N>> const& a,
simd<T, simd_abi::cuda_warp<N>> const& b,
simd<T, simd_abi::cuda_warp<N>> const& c) {
return simd<T, simd_abi::cuda_warp<N>>(a.get() ? b.get() : c.get());
}
}
#endif