Repository navigation
Expand file tree
/
Copy pathcuidx.cuh
More file actions
370 lines (327 loc) · 15.2 KB
/
Copy pathcuidx.cuh
File metadata and controls
370 lines (327 loc) · 15.2 KB
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
/*! @file cuidx.cuh */
// MIT License
//
// Copyright (c) 2019 George Karlos
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#ifndef __CUIDX_CUH__
#define __CUIDX_CUH__
#include <cuda_runtime.h>
#include <cstdint>
#if !defined(__NVCC__) && !defined(__CUDACC__)
#error "cuIdx must be compiled with NVCC"
#endif
#if !defined(CUDART_VERSION)
#error "cuIdx requires CUDART_VERSION which is Undefined!"
#elif CUDART_VERSION < 2010
#error "cuIdx minimum supported Cuda version: 2.1"
#endif
#if (CUDART_VERSION >= 7050) && defined(__cplusplus) && (__cplusplus >= 201103)
#define __CUIDX_C11_ENABLED 1
#else
#define __CUIDX_C11_ENABLED 0
#endif
#define WARPSIZE 32
#define HALFWARPSIZE 16
#ifndef DOXYGEN_SHOULD_SKIP_THIS
#define __bsz1 ( blockDim.x )
#define __bsz2 ( blockDim.y * blockDim.x )
#define __bsz3 ( blockDim.z * blockDim.y * blockDim.x )
#define __bid1 ( blockIdx.x )
#define __bid2 ( blockIdx.y * gridDim.x \
+ blockIdx.x )
#define __bid3 ( blockIdx.z * gridDim.y * gridDim.x \
+ blockIdx.y * gridDim.x \
+ blockIdx.x )
#define __tid1 ( threadIdx.x )
#define __tid2 ( threadIdx.y * blockDim.x \
+ threadIdx.x )
#define __tid3 ( threadIdx.z * blockDim.y * blockDim.x \
+ threadIdx.y * blockDim.x \
+ threadIdx.x )
#define __gtid11 ( __bid(1) * __bsz(1) + __tid(1))
#define __gtid12 ( __bid(1) * __bsz(2) + __tid(2))
#define __gtid13 ( __bid(1) * __bsz(3) + __tid(3))
#define __gtid21 ( __bid(2) * __bsz(1) + __tid(1))
#define __gtid22 ( __bid(2) * __bsz(2) + __tid(2))
#define __gtid23 ( __bid(2) * __bsz(3) + __tid(3))
#define __gtid31 ( __bid(3) * __bsz(1) + __tid(1))
#define __gtid32 ( __bid(3) * __bsz(2) + __tid(2))
#define __gtid33 ( __bid(3) * __bsz(3) + __tid(3))
#define __wid1 ( __tid(1) / WARPSIZE )
#define __wid2 ( __tid(2) / WARPSIZE )
#define __wid3 ( __tid(3) / WARPSIZE )
#define __gwid11 ( __gtid(1,1) / WARPSIZE)
#define __gwid12 ( __gtid(1,2) / WARPSIZE)
#define __gwid13 ( __gtid(1,3) / WARPSIZE)
#define __gwid21 ( __gtid(2,1) / WARPSIZE)
#define __gwid22 ( __gtid(2,2) / WARPSIZE)
#define __gwid23 ( __gtid(2,3) / WARPSIZE)
#define __gwid31 ( __gtid(3,1) / WARPSIZE)
#define __gwid32 ( __gtid(3,2) / WARPSIZE)
#define __gwid33 ( __gtid(3,3) / WARPSIZE)
#define __bsz( blockdims) __bsz##blockdims
#define __bid( griddims ) __bid##griddims
#define __tid( blockdims) __tid##blockdims
#define __gtid(griddims, blockdims) __gtid##griddims ##blockdims
#define __wid( blockdims) __wid##blockdims
#define __gwid(griddims, blockdims) __gwid##griddims ##blockdims
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
namespace cuidx
{
/*! @brief Retrieve the number of threads in the the thread-block the calling
* thread belongs to
* @tparam blockdims The number of threadblock dimensions specified on kernel
* launch
*
* <b>Example</b>(<b>2D</b>-Blocks)<br/>
* @code
* uint32_t block_size = cuidx::bsz<2>();
* @endcode
*
* @details <code>cuidx::bsz()</code> (without template arguments) works
* for all cases. However, when <code>blockdims < 3</code>, passing
* a template argument results in a slightly faster computation.
*/
#if __CUIDX_C11_ENABLED
template<int blockdims> __device__ __forceinline__ uint32_t bsz(void) = delete;
#else
template<int blockdims> __device__ __forceinline__ uint32_t bsz(void);
#endif
/*! @brief Retrieve the linear index of the thread block, w.r.t to the grid,
* the calling thread belongs to
* @tparam griddims The number of grid dimensions specified on kernel launch
*
* <b>Example</b>(<b>2D</b>-Grid)<br/>
* @code
* uint32_t block_id = cuidx::bid<2>();
* @endcode
*
* @details <code>cuidx::bid()</code> (without template arguments) works
* for all cases. However, when <code>griddims < 3</code>, passing
* a template argument results in a slightly faster computation.
*/
#if __CUIDX_C11_ENABLED
template<int griddims> __device__ __forceinline__ uint32_t bid(void) = delete;
#else
template<int griddims> __device__ __forceinline__ uint32_t bid(void);
#endif
/*!
* @brief Retrieve the linear index of the calling thread w.r.t its thread block.
* Unique among threads in the same block.
*
* @tparam blockdims The number of block dimensions specified on kernel launch
*
* <b>Example</b>(<b>2D</b>-Block)<br/>
* @code
* uint32_t thread_id = cuidx::tid<2>();
* @endcode
*
* @details <code>cuidx::tid()</code> (without template arguments) works
* for all cases. However, when <code>blockdims < 3</code>, passing
* a template argument results in a slightly faster computation.
*/
#if __CUIDX_C11_ENABLED
template<int blockdims> __device__ __forceinline__ uint32_t tid(void) = delete;
#else
template<int blockdims> __device__ __forceinline__ uint32_t tid(void);
#endif
/*!
* @brief Retrieve the global, linear index of the calling thread w.r.t the grid.
* Unique among all threads in the grid.
*
* @tparam griddims The number of grid dimensions specified on kernel launch
* @tparam blockdims The number of block dimensions specified on kernel launch
*
* <b>Example</b>(<b>2D</b>-Grid, <b>3</b>-Blocks)<br/>
* @code
* uint32_t global_id = cuidx::gid<2,3>();
* @endcode
* @details <code>cuidx::gtid()</code> (without template arguments) works
* for all cases. However, when <code>blockdims < 3 && griddims < 3</code>,
* passing template arguments results in a slightly faster computation.
*/
#if __CUIDX_C11_ENABLED
template<int griddims, int blockdims> __device__ __forceinline__ uint32_t gtid(void) = delete;
#else
template<int griddims, int blockdims> __device__ __forceinline__ uint32_t gtid(void);
#endif
/*!
* @brief Retrieve the index of the warp the calling thread belongs to, w.r.t to
* warps within the thread's block.
*
* @tparam blockdims The number of block dimensions specified on kernel launch
*
* <b>Example</b>(<b>2D</b>-Blocks)<br/>
* @code
* uint32_t warp_id = cuidx::wid<2>();
* @endcode
* @details <code>cuidx::wid()</code> (without template arguments) works
* for all cases. However, when <code>blockdims < 3</code>,
* passing template arguments results in a slightly faster computation.
*/
#if __CUIDX_C11_ENABLED
template<int blockdims> __device__ __forceinline__ uint32_t wid(void) = delete;
#else
template<int blockdims> __device__ __forceinline__ uint32_t wid(void);
#endif
/*!
* @brief Retrieve the global index of the warp the calling thread belongs to, w.r.t to
* all warps in the grid
*
* @tparam griddims The number of grid dimensions specified on kernel launch
* @tparam blockdims The number of block dimensions specified on kernel launch
*
* <b>Example</b>(<b>2D</b>-Grid, <b>3</b>-Blocks)<br/>
* @code
* uint32_t gwarp_id = cuidx::gwid<2,3>();
* @endcode
* @details <code>cuidx::gwid()</code> (without template arguments) works
* for all cases. However, when <code>blockdims < 3 && griddims < 3</code>,
* passing template arguments results in a slightly faster computation.
*/
#if __CUIDX_C11_ENABLED
template<int griddims, int blockdims> __device__ __forceinline__ uint32_t gwid(void) = delete;
#else
template<int griddims, int blockdims> __device__ __forceinline__ uint32_t gwid(void);
#endif
/*!
* @brief Retrieve the warp lane of the calling thread. i.e. The thread's index
* within its warp.
*/
__device__ __forceinline__ uint32_t lid(void);
/*!
* @brief Retrieve the index of the SM the calling thread is assigned to.
*/
__device__ __forceinline__ uint32_t smid(void);
/*!
* @brief Test whether the calling thread is the leader of its warp. i.e
* the thread at lane 0.
*/
__device__ __forceinline__ bool wleader(void);
/*!
* @brief Test whether the calling thread is the leader of its half-warp. i.e
* the thread at lane 0 or lane 16
*
* @return 0 Calling thread is not a half warp leader <br/>
* 1 Calling thread is the leader of the first half warp (lane 0) <br/>
* 2 Calling thread is the leader of the second half warp (lane 16) <br/>
*/
__device__ __forceinline__ int hwleader(void);
#ifndef DOXYGEN_SHOULD_SKIP_THIS
__device__ __forceinline__ uint32_t bsz(void);
__device__ __forceinline__ uint32_t bid(void);
__device__ __forceinline__ uint32_t tid(void);
__device__ __forceinline__ uint32_t gtid(void);
__device__ __forceinline__ uint32_t wid(void);
__device__ __forceinline__ uint32_t gwid(void);
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
/* specializations */
#ifndef DOXYGEN_SHOULD_SKIP_THIS
template<> __device__ __forceinline__ uint32_t bsz<1>(void);
template<> __device__ __forceinline__ uint32_t bsz<2>(void);
template<> __device__ __forceinline__ uint32_t bsz<3>(void);
template<> __device__ __forceinline__ uint32_t bid<1>(void);
template<> __device__ __forceinline__ uint32_t bid<2>(void);
template<> __device__ __forceinline__ uint32_t bid<3>(void);
template<> __device__ __forceinline__ uint32_t tid<1>(void);
template<> __device__ __forceinline__ uint32_t tid<2>(void);
template<> __device__ __forceinline__ uint32_t tid<3>(void);
template<> __device__ __forceinline__ uint32_t gtid<1, 1>(void);
template<> __device__ __forceinline__ uint32_t gtid<1, 2>(void);
template<> __device__ __forceinline__ uint32_t gtid<1, 3>(void);
template<> __device__ __forceinline__ uint32_t gtid<2, 1>(void);
template<> __device__ __forceinline__ uint32_t gtid<2, 2>(void);
template<> __device__ __forceinline__ uint32_t gtid<2, 3>(void);
template<> __device__ __forceinline__ uint32_t gtid<3, 1>(void);
template<> __device__ __forceinline__ uint32_t gtid<3, 2>(void);
template<> __device__ __forceinline__ uint32_t gtid<3, 3>(void);
template<> __device__ __forceinline__ uint32_t wid<1>(void);
template<> __device__ __forceinline__ uint32_t wid<2>(void);
template<> __device__ __forceinline__ uint32_t wid<3>(void);
template<> __device__ __forceinline__ uint32_t gwid<1, 1>(void);
template<> __device__ __forceinline__ uint32_t gwid<1, 2>(void);
template<> __device__ __forceinline__ uint32_t gwid<1, 3>(void);
template<> __device__ __forceinline__ uint32_t gwid<2, 1>(void);
template<> __device__ __forceinline__ uint32_t gwid<2, 2>(void);
template<> __device__ __forceinline__ uint32_t gwid<2, 3>(void);
template<> __device__ __forceinline__ uint32_t gwid<3, 1>(void);
template<> __device__ __forceinline__ uint32_t gwid<3, 2>(void);
template<> __device__ __forceinline__ uint32_t gwid<3, 3>(void);
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
}
/* implementations */
template<> __device__ __forceinline__ uint32_t cuidx::bsz<1>(void) { return __bsz(1); }
template<> __device__ __forceinline__ uint32_t cuidx::bsz<2>(void) { return __bsz(2); }
template<> __device__ __forceinline__ uint32_t cuidx::bsz<3>(void) { return __bsz(3); }
template<> __device__ __forceinline__ uint32_t cuidx::bid<1>(void) { return __bid(1); }
template<> __device__ __forceinline__ uint32_t cuidx::bid<2>(void) { return __bid(2); }
template<> __device__ __forceinline__ uint32_t cuidx::bid<3>(void) { return __bid(3); }
template<> __device__ __forceinline__ uint32_t cuidx::tid<1>(void) { return __tid(1); }
template<> __device__ __forceinline__ uint32_t cuidx::tid<2>(void) { return __tid(2); }
template<> __device__ __forceinline__ uint32_t cuidx::tid<3>(void) { return __tid(3); }
template<> __device__ __forceinline__ uint32_t cuidx::gtid<1, 1>(void) { return __gtid(1,1); }
template<> __device__ __forceinline__ uint32_t cuidx::gtid<1, 2>(void) { return __gtid(1,2); }
template<> __device__ __forceinline__ uint32_t cuidx::gtid<1, 3>(void) { return __gtid(1,3); }
template<> __device__ __forceinline__ uint32_t cuidx::gtid<2, 1>(void) { return __gtid(2,1); }
template<> __device__ __forceinline__ uint32_t cuidx::gtid<2, 2>(void) { return __gtid(2,2); }
template<> __device__ __forceinline__ uint32_t cuidx::gtid<2, 3>(void) { return __gtid(2,3); }
template<> __device__ __forceinline__ uint32_t cuidx::gtid<3, 1>(void) { return __gtid(3,1); }
template<> __device__ __forceinline__ uint32_t cuidx::gtid<3, 2>(void) { return __gtid(3,2); }
template<> __device__ __forceinline__ uint32_t cuidx::gtid<3, 3>(void) { return __gtid(3,3); }
template<> __device__ __forceinline__ uint32_t cuidx::wid<1>(void) { return __wid(1); }
template<> __device__ __forceinline__ uint32_t cuidx::wid<2>(void) { return __wid(2); }
template<> __device__ __forceinline__ uint32_t cuidx::wid<3>(void) { return __wid(3); }
template<> __device__ __forceinline__ uint32_t cuidx::gwid<1, 1>(void) { return __gwid(1,1); }
template<> __device__ __forceinline__ uint32_t cuidx::gwid<1, 2>(void) { return __gwid(1,2); }
template<> __device__ __forceinline__ uint32_t cuidx::gwid<1, 3>(void) { return __gwid(1,3); }
template<> __device__ __forceinline__ uint32_t cuidx::gwid<2, 1>(void) { return __gwid(2,1); }
template<> __device__ __forceinline__ uint32_t cuidx::gwid<2, 2>(void) { return __gwid(2,2); }
template<> __device__ __forceinline__ uint32_t cuidx::gwid<2, 3>(void) { return __gwid(2,3); }
template<> __device__ __forceinline__ uint32_t cuidx::gwid<3, 1>(void) { return __gwid(3,1); }
template<> __device__ __forceinline__ uint32_t cuidx::gwid<3, 2>(void) { return __gwid(3,2); }
template<> __device__ __forceinline__ uint32_t cuidx::gwid<3, 3>(void) { return __gwid(3,3); }
__device__ __forceinline__ uint32_t cuidx::bsz(void) { return __bsz(3); }
__device__ __forceinline__ uint32_t cuidx::bid(void) { return __bid(3); }
__device__ __forceinline__ uint32_t cuidx::tid(void) { return __tid(3); }
__device__ __forceinline__ uint32_t cuidx::gtid(void) { return __gtid(3, 3); }
__device__ __forceinline__ uint32_t cuidx::wid(void) { return __wid(3); }
__device__ __forceinline__ uint32_t cuidx::gwid(void) { return __gwid(3, 3); }
__device__ __forceinline__ bool cuidx::wleader(void)
{
return cuidx::lid() == 0;
}
__device__ __forceinline__ int cuidx::hwleader(void)
{
uint32_t lane = cuidx::lid();
return (!lane)? 1 : (lane == HALFWARPSIZE)? 2 : 0;
}
__device__ __forceinline__ uint32_t cuidx::lid(void)
{
uint32_t lane;
asm volatile ("mov.u32 %0, %laneid;" : "=r"(lane));
return lane;
}
__device__ __forceinline__ uint32_t cuidx::smid(void)
{
uint32_t sm;
asm volatile("mov.u32 %0, %%smid;" : "=r"(sm));
return sm;
}
#endif /* __CUIDX_CUH__ */