-
Notifications
You must be signed in to change notification settings - Fork 9
/
CUDA.h
405 lines (375 loc) · 11.1 KB
/
CUDA.h
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
#ifndef RANDOM_RUNTIME_H
#define RANDOM_RUNTIME_H
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include "sm_35_atomic_functions.h"
#include "cuda.h"
#include <stdio.h>
typedef unsigned char uchar;
#define N 1024
//#include <inttype.h>
#define int64_t long
#define uint64_t long
#define int_least64_t long
#define uint_least64_t long
#define int_fast64_t long
#define uint_fast64_t long
#define intmax_t long
#define uintmax_t long
#define int32_t int
#define uint32_t int
#define int16_t short
#define uint16_t short
#define int8_t char
#define uint8_t char
#define uint int
//typedef ushort unsigned short;
//typedef uint unsigned int;
//typedef ulong unsigned long;
/*
#define ulong unsigned long
#define uint unsigned int
#define ushort unsigned short
#define uchar unsigned char
*/
#define INT64_MIN LONG_MIN
#define INT64_MAX LONG_MAX
#define INT32_MIN INT_MIN
#define INT32_MAX INT_MAX
#define INT16_MIN SHRT_MIN
#define INT16_MAX SHRT_MAX
#define INT8_MIN CHAR_MIN
#define INT8_MAX CHAR_MAX
#define UINT64_MIN ULONG_MIN
#define UINT64_MAX ULONG_MAX
#define UINT32_MIN UINT_MIN
#define UINT32_MAX UINT_MAX
#define UINT16_MIN USHRT_MIN
#define UINT16_MAX USHRT_MAX
#define UINT8_MIN UCHAR_MIN
#define UINT8_MAX UCHAR_MAX
#define transparent_crc(X, Y, Z) transparent_crc_(&crc64_context, X, Y, Z)
#define VECTOR(X , Y) VECTOR_(X, Y)
#define VECTOR_(X, Y) X##Y
#define VECTOR_MAKE(X , Y) VECTOR_MAKE_(X, Y)
#define VECTOR_MAKE_(X, Y) make_##X##Y
#include "cl_safe_math_macros.h"
#include "safe_math_macros.h"
#ifdef NO_ATOMICS
#define atomic_inc(x) -1
#define atomic_add(x,y) (1+1)
#define atomic_sub(x,y) (1+1)
#define atomic_min(x,y) (1+1)
#define atomic_max(x,y) (1+1)
#define atomic_and(x,y) (1+1)
#define atomic_or(x,y) (1+1)
#define atomic_xor(x,y) (1+1)
#define atomic_noop() /* for sanity checking */
#endif
__device__ unsigned int myAtomicInc(volatile unsigned int *address)
{
return atomicAdd((unsigned int*)address,1);
}
__device__ int myAtomicInc(volatile int *address)
{
return atomicAdd((int*)address,1);
}
__device__ unsigned int myAtomicDec(volatile unsigned int *address)
{
return atomicSub((unsigned int*)address,1);
}
__device__ int myAtomicDec(volatile int *address)
{
return atomicSub((int*)address,1);
}
__device__ int myAtomicMin(volatile int *address,int val)
{
return atomicMin((int*)address,val);
}
__device__ unsigned int myAtomicMin(volatile unsigned int *address,unsigned int val)
{
return atomicMin((unsigned int*)address,val);
}
__device__ int myAtomicMax(volatile int *address,int val)
{
return atomicMax((int*)address,val);
}
__device__ unsigned int myAtomicMax(volatile unsigned int *address,unsigned int val)
{
return atomicMax((unsigned int*)address,val);
}
__device__ int myAtomicAdd(volatile int *address,int val)
{
return atomicAdd((int*)address,val);
}
__device__ unsigned int myAtomicAdd(volatile unsigned int *address,unsigned int val)
{
return atomicAdd((unsigned int *)address,val);
}
__device__ int myAtomicSub(volatile int *address,int val)
{
return atomicSub((int*)address,val);
}
__device__ unsigned int myAtomicSub(volatile unsigned int *address,unsigned int val)
{
return atomicSub((unsigned int*)address,val);
}
__device__ int myAtomicExch(volatile int *address,int val)
{
return atomicExch((unsigned int*)address,val);
}
__device__ int myAtomicAnd(volatile int *address,int val)
{
return atomicAnd((int *)address,val);
}
__device__ unsigned int myAtomicAnd(volatile unsigned int *address,unsigned int val)
{
return atomicAnd((unsigned int*)address,val);
}
__device__ int myAtomicOr(volatile int *address,int val)
{
return atomicOr((int*)address,val);
}
__device__ unsigned int myAtomicOr(volatile unsigned int *address,unsigned int val)
{
return atomicOr((unsigned int*)address,val);
}
__device__ int myAtomicXor(volatile int *address,int val)
{
return atomicXor((int *)address,val);
}
__device__ unsigned int myAtomicXor(volatile unsigned int *address,unsigned int val)
{
return atomicXor((unsigned int*)address,val);
}
/*
__device__ unsigned int myAtomicInc(volatile unsigned int *address)
{
unsigned int* address_as_ull = (unsigned int *)address;
unsigned int old = *address_as_ull,assumed;
do{
assumed = old;
old = atomicCAS(address_as_ull,assumed,old+1);
}while(assumed != old);
return old;
}
__device__ int myAtomicInc(volatile int *address)
{
int* address_as_ull = (int *)address;
int old = *address_as_ull,assumed;
do{
assumed = old;
old = atomicCAS(address_as_ull,assumed,old+1);
}while(assumed != old);
return old;
}
__device__ unsigned int myAtomicDec(volatile unsigned int *address)
{
unsigned int* address_as_ull = (unsigned int *)address;
unsigned int old = *address_as_ull,assumed;
do{
assumed = old;
old = atomicCAS(address_as_ull,assumed,old-1);
}while(assumed != old);
return old;
}
__device__ int myAtomicDec(volatile int *address)
{
int* address_as_ull = (int *)address;
int old = *address_as_ull,assumed;
do{
assumed = old;
old = atomicCAS(address_as_ull,assumed,old-1);
}while(assumed != old);
return old;
}
__device__ int myAtomicMin(volatile int *address,int val)
{
int* address_ = (int *)address;
return atomicMin(address_,val);
}
__device__ unsigned int myAtomicMin(volatile unsigned int *address,unsigned int val)
{
unsigned int* address_ = (unsigned int *)address;
return atomicMin(address_,val);
}
__device__ int myAtomicMax(volatile int *address,int val)
{
int* address_ = (int *)address;
return atomicMax(address_,val);
}
__device__ unsigned int myAtomicMax(volatile unsigned int *address,unsigned int val)
{
unsigned int* address_ = (unsigned int *)address;
return atomicMax(address_,val);
}
__device__ int myAtomicAdd(volatile int *address,int val)
{
int* address_ = (int *)address;
return atomicAdd(address_,val);
}
__device__ unsigned int myAtomicAdd(volatile unsigned int *address,unsigned int val)
{
unsigned int* address_ = (unsigned int *)address;
return atomicAdd(address_,val);
}
__device__ int myAtomicSub(volatile int *address,int val)
{
int* address_ = (int *)address;
return atomicSub(address_,val);
}
__device__ unsigned int myAtomicSub(volatile unsigned int *address,unsigned int val)
{
unsigned int* address_ = (unsigned int *)address;
return atomicSub(address_,val);
}
__device__ int myAtomicExch(volatile int *address,int val)
{
int* address_ = (int *)address;
return atomicExch(address_,val);
}
__device__ unsigned int myAtomicExch(volatile unsigned int *address,unsigned int val)
{
unsigned int* address_ = (unsigned int *)address;
return atomicExch(address_,val);
}
__device__ int myAtomicAnd(volatile int *address,int val)
{
int* address_ = (int *)address;
return atomicAnd(address_,val);
}
__device__ unsigned int myAtomicAnd(volatile unsigned int *address,unsigned int val)
{
unsigned int* address_ = (unsigned int *)address;
return atomicAnd(address_,val);
}
__device__ int myAtomicOr(volatile int *address,int val)
{
int* address_ = (int *)address;
return atomicOr(address_,val);
}
__device__ unsigned int myAtomicOr(volatile unsigned int *address,unsigned int val)
{
unsigned int* address_ = (unsigned int *)address;
return atomicOr(address_,val);
}
__device__ int myAtomicXor(volatile int *address,int val)
{
int* address_ = (int *)address;
return atomicXor(address_,val);
}
__device__ unsigned int myAtomicXor(volatile unsigned int *address,unsigned int val)
{
unsigned int* address_ = (unsigned int *)address;
return atomicXor(address_,val);
}
*/
__device__ int get_block_id(int a)
{
switch (a)
{
case (0) : return blockIdx.x; break;
case (1) : return blockIdx.y; break;
case (2) : return blockIdx.z; break;
}
}
__device__ inline void transparent_crc_no_string (uint64_t *crc64_context, uint64_t val)
{
*crc64_context += val;
}
#define transparent_crc_(A, B, C, D) transparent_crc_no_string(A, B)
__device__ inline uint32_t
get_global_size(int index){
if (index==0)
return gridDim.x*blockDim.x;
else if (index==1)
return gridDim.y*blockDim.y;
else if (index==2)
return gridDim.z*blockDim.z;
}
__device__ inline uint32_t
get_global_id(int index){
if (index==0)
return blockIdx.x*blockDim.x+threadIdx.x;
else if (index==1)
return blockIdx.y*blockDim.y+threadIdx.y;
else if (index==2)
return blockIdx.z*blockDim.z+threadIdx.z;
}
__device__ inline uint32_t
get_local_size(int index){
if (index==0)
return blockDim.x;
else if (index==1)
return blockDim.y;
else if (index==2)
return blockDim.z;
}
__device__ inline uint32_t
get_local_id (int index)
{
if (index==0)
return threadIdx.x;
else if (index==1)
return threadIdx.y;
else if (index==2)
return threadIdx.z;
}
__device__ inline uint32_t
get_num_groups(int index){
if (index==0)
return gridDim.x;
else if (index==1)
return gridDim.y;
else if (index==2)
return gridDim.z;
}
__device__ inline uint32_t
get_group_id(int index){
if (index==0)
return blockIdx.x;
else if (index==1)
return blockIdx.y;
else if (index==2)
return blockIdx.z;
}
__device__ inline uint32_t
get_linear_group_id (void)
{
return (get_group_id(2) * get_num_groups(1) + get_group_id(1)) *
get_num_groups(0) + get_group_id(0);
}
__device__ inline uint32_t
get_linear_global_id (void)
{
return (get_global_id(2) * get_global_size(1) + get_global_id(1)) *
get_global_size(0) + get_global_id(0);
}
__device__ inline uint32_t
get_linear_local_id (void)
{
return (get_local_id(2) * get_local_size(1) + get_local_id(1)) *
get_local_size(0) + get_local_id(0);
}
/*
__device__ inline uint32_t
get_linear_group_id (void)
{
return ((blockIdx.z * gridDim.y + blockIdx.y) *
gridDim.x + blockIdx.x);
}
__device__ inline uint32_t
get_linear_global_id (void)
{
return ((blockIdx.z*gridDim.y+blockIdx.y)*gridDim.x+blockIdx.x)*(blockDim.x*blockDim.y*blockDim.z) + ((threadIdx.z*blockDim.y+threadIdx.y)*blockDim.x+threadIdx.x);
// return ((blockIdx.z*blockDim.z + threadIdx.z) * gridDim.y*blockDim.y + (blockIdx.y*blockDim.y + threadIdx.y) *
// gridDim.x*blockDim.x + (blockIdx.x*blockDim.x + threadIdx.x));
}
__device__ inline uint32_t
get_linear_local_id (void)
{
return ((threadIdx.z * blockDim.y + threadIdx.y) *
blockDim.x + threadIdx.x);
}
*/
#endif /* RANDOM_RUNTIME_H */