-
Notifications
You must be signed in to change notification settings - Fork 0
/
sm_utils.inl
executable file
·414 lines (343 loc) · 12.6 KB
/
sm_utils.inl
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
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#if (defined(_MSC_VER) && defined(_WIN64)) || defined(__LP64__)
#define __PTR "l"
#else
#define __PTR "r"
#endif
#if __CUDA_ARCH__ >= 700
#define FULL_MASK 0xffffffff
#endif
namespace utils {
// ====================================================================================================================
// Atomics.
// ====================================================================================================================
static __device__ __forceinline__ void atomic_add( float *address, float value )
{
atomicAdd( address, value );
}
static __device__ __forceinline__ void atomic_add( double *address, double value )
{
unsigned long long *address_as_ull = (unsigned long long *) address;
unsigned long long old = __double_as_longlong( address[0] ), assumed;
do {
assumed = old;
old = atomicCAS( address_as_ull, assumed, __double_as_longlong( value + __longlong_as_double( assumed ) ) );
}
while( assumed != old );
}
// ====================================================================================================================
// Bit tools.
// ====================================================================================================================
static __device__ __forceinline__ int bfe( int src, int num_bits )
{
unsigned mask;
asm( "bfe.u32 %0, %1, 0, %2;" : "=r"(mask) : "r"(src), "r"(num_bits) );
return mask;
}
static __device__ __forceinline__ int bfind( int src )
{
int msb;
asm( "bfind.u32 %0, %1;" : "=r"(msb) : "r"(src) );
return msb;
}
static __device__ __forceinline__ int bfind( unsigned long long src )
{
int msb;
asm( "bfind.u64 %0, %1;" : "=r"(msb) : "l"(src) );
return msb;
}
static __device__ __forceinline__ unsigned long long brev( unsigned long long src )
{
unsigned long long rev;
asm( "brev.b64 %0, %1;" : "=l"(rev) : "l"(src) );
return rev;
}
// ====================================================================================================================
// Warp tools.
// ====================================================================================================================
static __device__ __forceinline__ int lane_id()
{
int id;
asm( "mov.u32 %0, %%laneid;" : "=r"(id) );
return id;
}
static __device__ __forceinline__ int lane_mask_lt()
{
int mask;
asm( "mov.u32 %0, %%lanemask_lt;" : "=r"(mask) );
return mask;
}
static __device__ __forceinline__ int warp_id()
{
return threadIdx.x >> 5;
}
// ====================================================================================================================
// Loads.
// ====================================================================================================================
enum Ld_mode { LD_AUTO = 0, LD_CA, LD_CG, LD_TEX, LD_NC };
template< Ld_mode Mode >
struct Ld {};
template<>
struct Ld<LD_AUTO>
{
template< typename T >
static __device__ __forceinline__ T load( const T *ptr ) { return *ptr; }
};
template<>
struct Ld<LD_CG>
{
static __device__ __forceinline__ int load( const int *ptr )
{
int ret;
asm volatile ( "ld.global.cg.s32 %0, [%1];" : "=r"(ret) : __PTR(ptr) );
return ret;
}
static __device__ __forceinline__ float load( const float *ptr )
{
float ret;
asm volatile ( "ld.global.cg.f32 %0, [%1];" : "=f"(ret) : __PTR(ptr) );
return ret;
}
static __device__ __forceinline__ double load( const double *ptr )
{
double ret;
asm volatile ( "ld.global.cg.f64 %0, [%1];" : "=d"(ret) : __PTR(ptr) );
return ret;
}
};
template<>
struct Ld<LD_CA>
{
static __device__ __forceinline__ int load( const int *ptr )
{
int ret;
asm volatile ( "ld.global.ca.s32 %0, [%1];" : "=r"(ret) : __PTR(ptr) );
return ret;
}
static __device__ __forceinline__ float load( const float *ptr )
{
float ret;
asm volatile ( "ld.global.ca.f32 %0, [%1];" : "=f"(ret) : __PTR(ptr) );
return ret;
}
static __device__ __forceinline__ double load( const double *ptr )
{
double ret;
asm volatile ( "ld.global.ca.f64 %0, [%1];" : "=d"(ret) : __PTR(ptr) );
return ret;
}
};
template<>
struct Ld<LD_NC>
{
template< typename T >
static __device__ __forceinline__ T load( const T *ptr ) { return __ldg( ptr ); }
};
// ====================================================================================================================
// Vector loads.
// ====================================================================================================================
static __device__ __forceinline__ void load_vec2( float (&u)[2], const float *ptr )
{
asm( "ld.global.cg.f32.v2 {%0, %1}, [%2];" : "=f"(u[0]), "=f"(u[1]) : __PTR(ptr) );
}
static __device__ __forceinline__ void load_vec2( double (&u)[2], const double *ptr )
{
asm( "ld.global.cg.f64.v2 {%0, %1}, [%2];" : "=d"(u[0]), "=d"(u[1]) : __PTR(ptr) );
}
static __device__ __forceinline__ void load_vec4( float (&u)[4], const float *ptr )
{
asm( "ld.global.cg.f32.v4 {%0, %1, %2, %3}, [%4];" : "=f"(u[0]), "=f"(u[1]), "=f"(u[2]), "=f"(u[3]) : __PTR(ptr) );
}
static __device__ __forceinline__ void load_vec4( double (&u)[4], const double *ptr )
{
asm( "ld.global.cg.f64.v2 {%0, %1}, [%2];" : "=d"(u[0]), "=d"(u[1]) : __PTR(ptr + 0) );
asm( "ld.global.cg.f64.v2 {%0, %1}, [%2];" : "=d"(u[2]), "=d"(u[3]) : __PTR(ptr + 2) );
}
// ====================================================================================================================
// Shuffle.
// ====================================================================================================================
static __device__ __forceinline__ float shfl( float r, int lane, int warp_size )
{
#if __CUDA_ARCH__ >= 700
return __shfl_sync(FULL_MASK, r, lane , warp_size);
#elif __CUDA_ARCH__ >= 300
return __shfl( r, lane , warp_size);
#else
return 0.0f;
#endif
}
static __device__ __forceinline__ double shfl( double r, int lane, int warp_size )
{
#if __CUDA_ARCH__ >= 700
int hi = __shfl_sync(FULL_MASK, __double2hiint(r), lane , warp_size);
int lo = __shfl_sync(FULL_MASK, __double2loint(r), lane , warp_size);
return __hiloint2double( hi, lo );
#elif __CUDA_ARCH__ >= 300
int hi = __shfl( __double2hiint(r), lane , warp_size);
int lo = __shfl( __double2loint(r), lane , warp_size);
return __hiloint2double( hi, lo );
#else
return 0.0;
#endif
}
static __device__ __forceinline__ float shfl_xor( float r, int mask, int warp_size )
{
#if __CUDA_ARCH__ >= 700
return __shfl_xor_sync(FULL_MASK, r, mask, warp_size );
#elif __CUDA_ARCH__ >= 300
return __shfl_xor( r, mask, warp_size );
#else
return 0.0f;
#endif
}
static __device__ __forceinline__ double shfl_xor( double r, int mask, int warp_size )
{
#if __CUDA_ARCH__ >= 700
int hi = __shfl_xor_sync( __double2hiint(r), mask, warp_size );
int lo = __shfl_xor_sync( __double2loint(r), mask, warp_size );
return __hiloint2double( hi, lo );
#elif __CUDA_ARCH__ >= 300
int hi = __shfl_xor( __double2hiint(r), mask, warp_size );
int lo = __shfl_xor( __double2loint(r), mask, warp_size );
return __hiloint2double( hi, lo );
#else
return 0.0;
#endif
}
static __device__ __forceinline__ float shfl_down( float r, int offset )
{
#if __CUDA_ARCH__ >= 700
return __shfl_down_sync(FULL_MASK, r, offset );
#elif __CUDA_ARCH__ >= 300
return __shfl_down( r, offset );
#else
return 0.0f;
#endif
}
static __device__ __forceinline__ double shfl_down( double r, int offset )
{
#if __CUDA_ARCH__ >= 700
int hi = __shfl_down_sync(FULL_MASK, __double2hiint(r), offset );
int lo = __shfl_down_sync(FULL_MASK, __double2loint(r), offset );
return __hiloint2double( hi, lo );
#elif __CUDA_ARCH__ >= 300
int hi = __shfl_down( __double2hiint(r), offset );
int lo = __shfl_down( __double2loint(r), offset );
return __hiloint2double( hi, lo );
#else
return 0.0;
#endif
}
// ====================================================================================================================
// Warp-level reductions.
// ====================================================================================================================
struct Add
{
template< typename Value_type >
static __device__ __forceinline__ Value_type eval( Value_type x, Value_type y ) { return x+y; }
};
#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 300
template< int NUM_THREADS_PER_ITEM, int WARP_SIZE >
struct Warp_reduce_pow2
{
template< typename Operator, typename Value_type >
static __device__ __inline__ Value_type execute( Value_type x )
{
#pragma unroll
for( int mask = WARP_SIZE / 2 ; mask >= NUM_THREADS_PER_ITEM ; mask >>= 1 )
x = Operator::eval( x, shfl_xor(x, mask) );
return x;
}
};
template< int NUM_THREADS_PER_ITEM, int WARP_SIZE >
struct Warp_reduce_linear
{
template< typename Operator, typename Value_type >
static __device__ __inline__ Value_type execute( Value_type x )
{
const int NUM_STEPS = WARP_SIZE / NUM_THREADS_PER_ITEM;
int my_lane_id = utils::lane_id();
#pragma unroll
for( int i = 1 ; i < NUM_STEPS ; ++i )
{
Value_type y = shfl_down( x, i*NUM_THREADS_PER_ITEM );
if( my_lane_id < NUM_THREADS_PER_ITEM )
x = Operator::eval( x, y );
}
return x;
}
};
#else
template< int NUM_THREADS_PER_ITEM, int WARP_SIZE >
struct Warp_reduce_pow2
{
template< typename Operator, typename Value_type >
static __device__ __inline__ Value_type execute( volatile Value_type *smem, Value_type x )
{
int my_lane_id = utils::lane_id();
#pragma unroll
for( int offset = WARP_SIZE / 2 ; offset >= NUM_THREADS_PER_ITEM ; offset >>= 1 )
if( my_lane_id < offset )
smem[threadIdx.x] = x = Operator::eval( x, smem[threadIdx.x+offset] );
return x;
}
};
template< int NUM_THREADS_PER_ITEM, int WARP_SIZE >
struct Warp_reduce_linear
{
template< typename Operator, typename Value_type >
static __device__ __inline__ Value_type execute( volatile Value_type *smem, Value_type x )
{
const int NUM_STEPS = WARP_SIZE / NUM_THREADS_PER_ITEM;
int my_lane_id = utils::lane_id();
#pragma unroll
for( int i = 1 ; i < NUM_STEPS ; ++i )
if( my_lane_id < NUM_THREADS_PER_ITEM )
smem[threadIdx.x] = x = Operator::eval( x, smem[threadIdx.x+i*NUM_THREADS_PER_ITEM] );
return x;
}
};
#endif
// ====================================================================================================================
template< int NUM_THREADS_PER_ITEM, int WARP_SIZE = 32 >
struct Warp_reduce : public Warp_reduce_pow2<NUM_THREADS_PER_ITEM, WARP_SIZE> {};
template< int WARP_SIZE >
struct Warp_reduce< 3, WARP_SIZE> : public Warp_reduce_linear< 3, WARP_SIZE> {};
template< int WARP_SIZE >
struct Warp_reduce< 4, WARP_SIZE> : public Warp_reduce_linear< 4, WARP_SIZE> {};
template< int WARP_SIZE >
struct Warp_reduce< 5, WARP_SIZE> : public Warp_reduce_linear< 5, WARP_SIZE> {};
template< int WARP_SIZE >
struct Warp_reduce< 6, WARP_SIZE> : public Warp_reduce_linear< 6, WARP_SIZE> {};
template< int WARP_SIZE >
struct Warp_reduce< 7, WARP_SIZE> : public Warp_reduce_linear< 7, WARP_SIZE> {};
template< int WARP_SIZE >
struct Warp_reduce< 9, WARP_SIZE> : public Warp_reduce_linear< 9, WARP_SIZE> {};
template< int WARP_SIZE >
struct Warp_reduce<10, WARP_SIZE> : public Warp_reduce_linear<10, WARP_SIZE> {};
template< int WARP_SIZE >
struct Warp_reduce<11, WARP_SIZE> : public Warp_reduce_linear<11, WARP_SIZE> {};
template< int WARP_SIZE >
struct Warp_reduce<12, WARP_SIZE> : public Warp_reduce_linear<12, WARP_SIZE> {};
template< int WARP_SIZE >
struct Warp_reduce<13, WARP_SIZE> : public Warp_reduce_linear<13, WARP_SIZE> {};
template< int WARP_SIZE >
struct Warp_reduce<14, WARP_SIZE> : public Warp_reduce_linear<14, WARP_SIZE> {};
template< int WARP_SIZE >
struct Warp_reduce<15, WARP_SIZE> : public Warp_reduce_linear<15, WARP_SIZE> {};
// ====================================================================================================================
#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 300
template< int NUM_THREADS_PER_ITEM, typename Operator, typename Value_type >
static __device__ __forceinline__ Value_type warp_reduce( Value_type x )
{
return Warp_reduce<NUM_THREADS_PER_ITEM>::template execute<Operator>( x );
}
#else
template< int NUM_THREADS_PER_ITEM, typename Operator, typename Value_type >
static __device__ __forceinline__ Value_type warp_reduce( volatile Value_type *smem, Value_type x )
{
return Warp_reduce<NUM_THREADS_PER_ITEM>::template execute<Operator>( smem, x );
}
#endif
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
} // namespace utils