-
Notifications
You must be signed in to change notification settings - Fork 3
/
cuda_common.cuh
467 lines (369 loc) · 11.7 KB
/
cuda_common.cuh
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
#pragma once
#include <string>
#include <vector>
#include <utility>
#include <algorithm>
#include <memory>
#include <cstddef>
#include "common.hpp"
#include <iostream>
#define CUDA_KERNEL __global__
#define CUDA_HOST_DEVICE __host__ __device__
#define CUDA_DEVICE __device__
#define CUDA_WARP_SIZE (32)
namespace hygraph {
using namespace std;
class cuda_exception: public exception {
string msg;
cudaError_t err;
public:
cuda_exception(const char *msg) :
msg(msg),
err(cudaSuccess) {
//
}
cuda_exception(cudaError_t code) :
msg(cudaGetErrorString(code)),
err(code) {
//
}
cuda_exception(const char *msg, cudaError_t code) :
msg(string(msg) + ": " + string(cudaGetErrorString(code))),
err(code) {
//
}
const char* what() const noexcept {
return msg.c_str();
}
cudaError_t code() {
return err;
}
};
#define CUDA_CHECK(fun, ...) (cuda_check(#fun, fun(__VA_ARGS__)))
void cuda_check(const char *msg, cudaError_t err) {
if (err != cudaSuccess) {
throw cuda_exception(msg, err);
}
}
void cuda_check_last(const char *msg) {
cuda_check(msg, cudaGetLastError());
}
template <typename T, bool OnHost>
class cuda_mem {
private:
T *ptr;
size_t capacity;
public:
cuda_mem() {
ptr = NULL;
capacity = 0;
}
cuda_mem(size_t size) {
ptr = NULL;
capacity = 0;
allocate(size);
}
cuda_mem(const cuda_mem &rhs) {
ptr = NULL;
capacity = 0;
*this = rhs;
}
cuda_mem(cuda_mem &&rhs) {
ptr = NULL;
capacity = 0;
*this = move(rhs);
}
~cuda_mem() {
free();
}
void allocate(size_t new_capacity) {
if (new_capacity == capacity) {
return; // noop
}
free();
if (new_capacity > 0) {
if (OnHost) {
CUDA_CHECK(cudaMallocHost, &ptr, new_capacity * sizeof(T));
} else {
CUDA_CHECK(cudaMalloc, &ptr, new_capacity * sizeof(T));
}
} else {
ptr = NULL;
}
capacity = new_capacity;
}
void allocate(const T *data, size_t count) {
allocate(count);
from_host(data);
}
void allocate(const vector<T> &vec) {
allocate(vec.data(), vec.size());
}
void allocate(const cuda_mem &m) {
allocate(m.size());
copy_from(m.get());
}
void free() {
if (ptr) {
if (OnHost) {
CUDA_CHECK(cudaFreeHost, ptr);
} else {
CUDA_CHECK(cudaFree, ptr);
}
}
ptr = NULL;
capacity = 0;
}
void reallocate(size_t new_capacity) {
if (new_capacity != capacity) {
cuda_mem clone(new_capacity);
clone.copy_from(*this, 0, 0, min(capacity, new_capacity));
swap(clone);
}
}
void fill(const T &val) {
if (ptr) {
if (OnHost) {
std::fill(ptr, ptr + capacity, val);
} else {
char *val_ptr = (char*) &val;
bool all_match = true;
for (size_t index = 0; index < sizeof(T); index++) {
all_match |= val_ptr[index] == val_ptr[0];
}
if (all_match) {
CUDA_CHECK(cudaMemset, ptr, val_ptr[0], sizeof(T) * capacity);
} else {
throw cuda_exception("Fill unsupported for general values");
}
}
}
}
void clear() {
fill(T());
}
private:
template <bool IsAsync, bool IsSource, bool IsPeerHost>
void transfer(cudaStream_t stream, void *data, size_t offset, size_t count) const {
if (count == 0) {
return;
}
if (offset >= capacity || offset + count > capacity) {
throw cuda_exception("copy out of bounds");
}
cudaMemcpyKind kind;
const void *src;
void *dst;
if (IsPeerHost && OnHost) {
kind = cudaMemcpyHostToHost;
} else if (!IsPeerHost && !OnHost) {
kind = cudaMemcpyDeviceToDevice;
} else if (IsSource ? OnHost : IsPeerHost) {
kind = cudaMemcpyHostToDevice;
} else {
kind = cudaMemcpyDeviceToHost;
}
if (IsSource) {
src = const_cast<T*>(ptr) + offset;
dst = data;
} else {
src = data;
dst = ptr + offset;
}
if (IsAsync) {
CUDA_CHECK(cudaMemcpyAsync, dst, src, count * sizeof(T), kind, stream);
} else {
CUDA_CHECK(cudaMemcpy, dst, src, count * sizeof(T), kind);
}
}
public:
void to_host(T *data, size_t offset, size_t count) const {
transfer<false, true, true>(0, (void*) data, offset, count);
}
void from_host(const T *data, size_t offset, size_t count) {
transfer<false, false, true>(0, const_cast<T*>(data), offset, count);
}
void to_host_async(cudaStream_t stream, T *data, size_t offset, size_t count) const {
transfer<true, true, true>(stream, (void*) data, offset, count);
}
void from_host_async(cudaStream_t stream, const T *data, size_t offset, size_t count) {
transfer<true, false, true>(stream, const_cast<T*>(data), offset, count);
}
void to_device(T *data, size_t offset, size_t count) const {
transfer<false, true, false>(0, (void*) data, offset, count);
}
void from_device(const T *data, size_t offset, size_t count) {
transfer<false, false, false>(0, const_cast<T*>(data), offset, count);
}
void to_device_async(cudaStream_t stream, T *data, size_t offset, size_t count) const {
transfer<true, true, false>(stream, (void*) data, offset, count);
}
void from_device_async(cudaStream_t stream, const T *data, size_t offset, size_t count) {
transfer<true, false, false>(stream, const_cast<T*>(data), offset, count);
}
void to_host(T *data) const {
to_host(data, 0, capacity);
}
void from_host(const T *data) {
from_host(data, 0, capacity);
}
void to_host_async(cudaStream_t stream, T *data) const {
to_host_async(stream, data, 0, capacity);
}
void from_host_async(cudaStream_t stream, const T *data) {
from_host_async(stream, data, 0, capacity);
}
void to_device(T *data) const {
to_device(data, 0, capacity);
}
void from_device(const T *data) {
from_device(data, 0, capacity);
}
void to_device_async(cudaStream_t stream, T *data) const {
to_device_async(stream, data, 0, capacity);
}
void from_device_async(cudaStream_t stream, const T *data) {
from_device_async(stream, data, 0, capacity);
}
void to_host(vector<T> &vec) const {
vec.resize(size());
to_host(vec.data());
}
void from_host(const vector<T> &vec) {
from_host(vec.data(), 0, vec.size());
}
template <bool PeerOnHost>
void copy_to(cuda_mem<T, PeerOnHost> &rhs, size_t lhs_offset, size_t rhs_offset, size_t count) const {
if (rhs_offset + count > rhs.size()) {
throw cuda_exception("Copy out of bounds");
}
if (PeerOnHost) {
to_host(rhs.get() + rhs_offset, lhs_offset, count);
} else {
to_device(rhs.get() + rhs_offset, lhs_offset, count);
}
}
template <bool PeerOnHost>
void copy_to(cuda_mem<T, PeerOnHost> &rhs) const {
copy_to(rhs, 0, 0, capacity);
}
template <bool PeerOnHost>
void copy_to_async(cudaStream_t stream, cuda_mem<T, PeerOnHost> &rhs, size_t lhs_offset, size_t rhs_offset, size_t count) const {
if (rhs_offset + count > rhs.size()) {
throw cuda_exception("Copy out of bounds");
}
if (PeerOnHost) {
to_host_async(stream, rhs.get() + rhs_offset, lhs_offset, count);
} else {
to_device_async(stream, rhs.get() + rhs_offset, lhs_offset, count);
}
}
template <bool PeerOnHost>
void copy_to_async(cudaStream_t stream, cuda_mem<T, PeerOnHost> &rhs) const {
copy_to_async(stream, rhs, 0, 0, capacity);
}
template <bool PeerOnHost>
void copy_from(cuda_mem<T, PeerOnHost> &rhs, size_t lhs_offset, size_t rhs_offset, size_t count) {
rhs.copy_to(*this, rhs_offset, lhs_offset, count);
}
template <bool PeerOnHost>
void copy_from(cuda_mem<T, PeerOnHost> &rhs) {
rhs.copy_to(*this);
}
template <bool PeerOnHost>
void copy_from_async(cudaStream_t stream, cuda_mem<T, PeerOnHost> &rhs, size_t lhs_offset, size_t rhs_offset, size_t count) {
rhs.copy_to_async(stream, *this, rhs_offset, lhs_offset, count);
}
template <bool PeerOnHost>
void copy_from_async(cudaStream_t stream, cuda_mem<T, PeerOnHost> &rhs) {
rhs.copy_to_async(stream, *this);
}
T at(size_t index) {
if (OnHost) {
return ptr[index];
} else {
T val;
to_host(&val, index, 1);
return val;
}
}
void swap(cuda_mem &rhs) {
std::swap(ptr, rhs.ptr);
std::swap(capacity, rhs.capacity);
}
void operator=(cuda_mem &&rhs) {
swap(rhs);
}
void operator=(const cuda_mem &rhs) {
allocate(rhs.size());
copy_from(rhs);
}
cuda_mem clone() const {
return cuda_mem(*this);
}
INLINE T *get() {
return ptr;
}
INLINE const T *get() const {
return ptr;
}
INLINE size_t size() const {
return capacity;
}
INLINE size_t size_in_bytes() const {
return size() * sizeof(T);
}
};
template <typename T>
using cuda_pinned_mem = cuda_mem<T, true>;
template <typename T>
using cuda_device_mem = cuda_mem<T, false>;
CUDA_DEVICE bool cuda_get_bit(bitvec_t *v, size_t index) {
uint32_t word_index = index / 32;
uint32_t bit_index = index % 32;
uint32_t val = ((uint32_t*)v)[word_index];
int ret;
asm("bfe.u32 %0, %1, %2, 1;" : "=r"(ret) : "r"(val), "r"(bit_index));
return ret;
}
CUDA_DEVICE void cuda_atomic_set_bit(bitvec_t *v, size_t index) {
uint32_t word_index = index / 32;
uint32_t bit_index = index % 32;
bitvec_t mask = ((bitvec_t) 1) << bit_index;
if ((v[word_index] & mask) == 0) {
atomicOr(((uint32_t*) v) + word_index, mask);
}
}
template <typename T>
CUDA_DEVICE bool cuda_atomic_cas(T *ptr, T *oldVal, T newVal) {
bool success = false;
if (sizeof(T) == sizeof(unsigned int)) {
unsigned int a = * (unsigned int*) oldVal;
unsigned int b = * (unsigned int*) &newVal;
unsigned int c = atomicCAS((unsigned int*) ptr, a, b);
success = a == c;
*((unsigned int*) oldVal) = c;
}
else if (sizeof(T) == sizeof(unsigned long long)) {
unsigned long long a = * (unsigned long long*) oldVal;
unsigned long long b = * (unsigned long long*) &newVal;
unsigned long long c = atomicCAS((unsigned long long*) ptr, a, b);
success = a == c;
*((unsigned long long*) oldVal) = c;
}
else {
printf("ERROR: unsupported message size\n");
asm("trap;");
}
return success;
}
template <typename F, typename ...Args>
static CUDA_KERNEL void cuda_launch_kernel_wrapper(Args ...args) {
F()(args...);
}
template <typename F, typename N, typename ...Args>
static void cuda_launch_kernel(cudaStream_t stream, size_t cta_size, F fun, N size, Args ...args) {
cuda_launch_kernel_wrapper<F, N, Args...><<<ceil_div(size, (N) cta_size), cta_size, 0, stream>>>(size, args...);
cuda_check_last(typeid(F).name());
}
};