This repository has been archived by the owner on Mar 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
CudaUtils.cuh
209 lines (184 loc) · 4.95 KB
/
CudaUtils.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
// Copyright 2004-present Facebook. All Rights Reserved.
#pragma once
#include <cuda_runtime.h>
#include "cuda/ComputeCapabilities.cuh"
#include "cuda/CudaStaticAssert.cuh"
namespace facebook { namespace cuda {
/**
Computes ceil(a / b)
*/
template <typename T>
__host__ __device__ __forceinline__ constexpr T ceil(T a, T b) {
return (a + b - 1) / b;
}
/**
Computes floor(a / b)
*/
template <typename T>
__host__ __device__ __forceinline__ constexpr T floor(T a, T b) {
return (a - b + 1) / b;
}
/**
Returns the current thread's warp ID
*/
__device__ __forceinline__ int getWarpId() {
return (threadIdx.z * blockDim.y * blockDim.x +
threadIdx.y * blockDim.x +
threadIdx.x) / warpSize;
}
/**
Returns the number of threads in the current block (linearized).
*/
__device__ __forceinline__ int getThreadsInBlock() {
return (blockDim.x * blockDim.y * blockDim.z);
}
/**
Returns the number of warps in the current block (linearized,
rounded to whole warps).
*/
__device__ __forceinline__ int getWarpsInBlock() {
return ceil(getThreadsInBlock(), WARP_SIZE);
}
/**
Pointer comparison using the PTX intrinsic; min() doesn't work for
T*.
*/
template <typename T>
__device__ __forceinline__ T ptrMin(T a, T b) {
cuda_static_assert(sizeof(T) == 8);
T ptr;
asm("min.u64 %0, %1, %2;" : "=l"(ptr) : "l"(a), "l"(b));
return ptr;
}
/**
Pointer comparison using the PTX intrinsic; max() doesn't work for
T*
*/
template <typename T>
__device__ __forceinline__ T ptrMax(T a, T b) {
cuda_static_assert(sizeof(T) == 8);
T ptr;
asm("max.u64 %0, %1, %2;" : "=l"(ptr) : "l"(a), "l"(b));
return ptr;
}
/**
Return the current thread's lane in the warp
*/
__device__ __forceinline__ int getLaneId() {
int laneId;
asm("mov.s32 %0, %laneid;" : "=r"(laneId) );
return laneId;
}
/**
Return a bitmask with bits set in positions less than the current
thread's lane number in the warp.
*/
__device__ __forceinline__ unsigned getLaneMaskLt() {
unsigned mask;
asm("mov.u32 %0, %%lanemask_lt;" : "=r"(mask));
return mask;
}
/**
Return a bitmask with bits set in positions less than or equal to
the current thread's lane number in the warp.
*/
__device__ __forceinline__ unsigned getLaneMaskLe() {
unsigned mask;
asm("mov.u32 %0, %%lanemask_le;" : "=r"(mask));
return mask;
}
/**
Return a bitmask with bits set in positions greater than the
current thread's lane number in the warp.
*/
__device__ __forceinline__ unsigned getLaneMaskGt() {
unsigned mask;
asm("mov.u32 %0, %%lanemask_gt;" : "=r"(mask));
return mask;
}
/**
Return a bitmask with bits set in positions greater than or equal
to the current thread's lane number in the warp.
*/
__device__ __forceinline__ unsigned getLaneMaskGe() {
unsigned mask;
asm("mov.u32 %0, %%lanemask_ge;" : "=r"(mask));
return mask;
}
/**
Extract a single bit at `pos` from `val`
*/
__device__ __forceinline__ int getBit(int val, int pos) {
int ret;
asm("bfe.u32 %0, %1, %2, 1;" : "=r"(ret) : "r"(val), "r"(pos));
return ret;
}
/**
Insert a single bit into `val` at position `pos`
*/
__device__ __forceinline__
unsigned setBit(unsigned val, unsigned toInsert, int pos) {
unsigned ret;
asm("bfi.b32 %0, %1, %2, %3, 1;" :
"=r"(ret) : "r"(toInsert), "r"(val), "r"(pos));
return ret;
}
/**
Extract a bit field of length `len` at `pos` from `val`
*/
__device__ __forceinline__
unsigned getBitfield(unsigned val, int pos, int len) {
unsigned ret;
asm("bfe.u32 %0, %1, %2, %3;" : "=r"(ret) : "r"(val), "r"(pos), "r"(len));
return ret;
}
/**
Extract a bit field of length `len` at `pos` from `val`
*/
__device__ __forceinline__
unsigned long getBitfield(unsigned long val, int pos, int len) {
unsigned long ret;
asm("bfe.u64 %0, %1, %2, %3;" : "=l"(ret) : "l"(val), "r"(pos), "r"(len));
return ret;
}
/**
Insert `len` bits of `toInsert` into `val` starting at position
`pos`
*/
__device__ __forceinline__
unsigned setBitfield(unsigned val, unsigned toInsert, int pos, int len) {
unsigned ret;
asm("bfi.b32 %0, %1, %2, %3, %4;" :
"=r"(ret) : "r"(toInsert), "r"(val), "r"(pos), "r"(len));
return ret;
}
/**
Insert `len` bits of `toInsert` into `val` starting at position
`pos`
*/
__device__ __forceinline__
unsigned long setBitfield(unsigned long val, unsigned long toInsert,
int pos, int len) {
unsigned long ret;
asm("bfi.b64 %0, %1, %2, %3, %4;" :
"=l"(ret) : "l"(toInsert), "l"(val), "r"(pos), "r"(len));
return ret;
}
/**
Returns the index of the most significant 1 bit in `val`.
*/
__device__ __forceinline__ constexpr int getMSB(int val) {
return
((val >= 1024 && val < 2048) ? 10 :
((val >= 512) ? 9 :
((val >= 256) ? 8 :
((val >= 128) ? 7 :
((val >= 64) ? 6 :
((val >= 32) ? 5 :
((val >= 16) ? 4 :
((val >= 8) ? 3 :
((val >= 4) ? 2 :
((val >= 2) ? 1 :
((val == 1) ? 0 : -1)))))))))));
}
} } // namespace