-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmkperm.cuh
393 lines (305 loc) · 14.2 KB
/
mkperm.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
/*
kernels/mkperm.cuh -- Efficient CUDA kernels for sorting arrays
with low-valued entries
Copyright (c) 2021 Wenzel Jakob <wenzel.jakob@epfl.ch>
All rights reserved. Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file.
*/
#include "common.h"
// Determine bit mask of lanes with a matching value
__device__ __inline__ uint32_t get_peers(uint32_t active, uint32_t value) {
#if __CUDA_ARCH__ >= 700
return __match_any_sync(active, value);
#else
/* Emulate __match_any_sync intrinsics. Based on "Voting And
Shuffling For Fewer Atomic Operations" by Elmar Westphal. */
do {
// Find lowest-numbered active lane
int first_active = __ffs(active) - 1;
// Fetch its value and compare to ours
bool match = (value == __shfl_sync(active, value, first_active));
// Determine, which lanes had a match
uint32_t peers = __ballot_sync(active, match);
// Key of the current lane was chosen, return the active mask
if (match)
return peers;
// Remove lanes with matching values from the pool
active ^= peers;
} while (true);
#endif
}
/// Accumulate 'value' into histogram 'buckets', using a minimal number of memory operations
inline __device__ uint32_t reduce(uint32_t active, uint32_t value, uint32_t *buckets) {
uint32_t peers = get_peers(active, value);
// Thread's position within warp
uint32_t lane_idx = threadIdx.x & (warpSize - 1);
// Designate a leader thread within the set of peers
uint32_t leader_idx = __ffs(peers) - 1;
// If the current thread is the leader, perform atomic op.
uint32_t offset = 0;
if (lane_idx == leader_idx) {
offset = buckets[value];
buckets[value] = offset + __popc(peers);
}
// Fetch offset into output array from leader
offset = __shfl_sync(peers, offset, leader_idx);
// Determine current thread's position within peer group
uint32_t rel_pos = __popc(peers << (32 - lane_idx));
return offset + rel_pos;
}
/// Atomically accumulate 'value' into histogram 'buckets', using a minimal number of atomic operations
inline __device__ uint32_t reduce_atomic(uint32_t active, uint32_t value, uint32_t *buckets) {
uint32_t peers = get_peers(active, value);
// Thread's position within warp
uint32_t lane_idx = threadIdx.x & (warpSize - 1);
// Designate a leader thread within the set of peers
uint32_t leader_idx = __ffs(peers) - 1;
// If the current thread is the leader, perform atomic op.
uint32_t offset = 0;
if (lane_idx == leader_idx)
offset = atomicAdd(buckets + value, __popc(peers));
// Fetch offset into output array from leader
offset = __shfl_sync(peers, offset, leader_idx);
// Determine current thread's position within peer group
uint32_t rel_pos = __popc(peers << (32 - lane_idx));
return offset + rel_pos;
}
/**
* \brief Generate a histogram of values in the range (0 .. bucket_count - 1).
*
* "Tiny" variant, which uses shared memory atomics to produce a stable
* permutation. Handles up to 512 buckets with 64KiB of shared memory. Should be
* combined with \ref mkperm_phase_4_tiny.
*/
KERNEL void mkperm_phase_1_tiny(const uint32_t *values,
uint32_t *buckets,
uint32_t size,
uint32_t size_per_block,
uint32_t bucket_count) {
uint32_t *shared = SharedMemory<uint32_t>::get();
uint32_t thread_id = threadIdx.x,
thread_count = blockDim.x,
block_start = blockIdx.x * size_per_block,
block_end = block_start + size_per_block,
warp_count = thread_count / warpSize,
warp_id = thread_id / warpSize;
for (uint32_t i = thread_id; i < bucket_count * warp_count; i += thread_count)
shared[i] = 0;
__syncthreads();
uint32_t *shared_warp = shared + warp_id * bucket_count;
for (uint32_t i = block_start + thread_id; i < block_end; i += thread_count) {
bool active = i < size;
/* This assumes that the whole warp does an iteration or exits
(i.e. size_per_block must be a multiple of 32) */
uint32_t active_mask = __ballot_sync(0xFFFFFFFF, active);
if (active)
reduce(active_mask, values[i], shared_warp);
__syncwarp();
}
__syncthreads();
uint32_t *out = buckets + blockIdx.x * bucket_count * warp_count;
for (uint32_t i = thread_id; i < bucket_count * warp_count; i += thread_count)
out[i] = shared[i];
}
/**
* \brief Generate a histogram of values in the range (0 .. bucket_count - 1).
*
* "Small" variant, which uses shared memory atomics and handles up to 16K
* buckets with 64KiB of shared memory. The permutation can be somewhat
* unstable due to scheduling variations when performing atomic operations
* (although some effort is made to keep it stable within each group of 32
* elements by performing an intra-warp reduction.) Should be combined with
* \ref mkperm_phase_4_small.
*/
KERNEL void mkperm_phase_1_small(const uint32_t *values,
uint32_t *buckets,
uint32_t size,
uint32_t size_per_block,
uint32_t bucket_count) {
uint32_t *shared = SharedMemory<uint32_t>::get();
uint32_t thread_id = threadIdx.x,
thread_count = blockDim.x,
block_start = blockIdx.x * size_per_block,
block_end = block_start + size_per_block;
for (uint32_t i = thread_id; i < bucket_count; i += thread_count)
shared[i] = 0;
__syncthreads();
for (uint32_t i = block_start + thread_id; i < block_end; i += thread_count) {
bool active = i < size;
/* This assumes that the whole warp does an iteration or exits
(i.e. size_per_block must be a multiple of 32) */
uint32_t active_mask = __ballot_sync(0xFFFFFFFF, active);
if (active)
reduce_atomic(active_mask, values[i], shared);
}
__syncthreads();
uint32_t *out = buckets + blockIdx.x * bucket_count;
for (uint32_t i = thread_id; i < bucket_count; i += thread_count)
out[i] = shared[i];
}
/**
* \brief Generate a histogram of values in the range (0 .. bucket_count - 1).
*
* "Large" variant, which uses global memory atomics and handles arbitrarily
* many elements (though this is somewhat slower than the previous two shared
* memory variants). The permutation can be somewhat unstable due to scheduling
* variations when performing atomic operations (although some effort is made
* to keep it stable within each group of 32 elements by performing an
* intra-warp reduction.) Should be combined with \ref mkperm_phase_4_large.
*/
KERNEL void mkperm_phase_1_large(const uint32_t *values,
uint32_t *buckets_, uint32_t size,
uint32_t size_per_block, uint32_t bucket_count) {
uint32_t thread_id = threadIdx.x,
thread_count = blockDim.x,
block_start = blockIdx.x * size_per_block,
block_end = block_start + size_per_block;
uint32_t *buckets = buckets_ + blockIdx.x * bucket_count;
for (uint32_t i = block_start + thread_id; i < block_end; i += thread_count) {
bool active = i < size;
/* This assumes that the whole warp does an iteration or exits
(i.e. size_per_block must be a multiple of 32) */
uint32_t active_mask = __ballot_sync(0xFFFFFFFF, active);
if (active)
reduce_atomic(active_mask, values[i], buckets);
}
}
/// Detect non-empty buckets and record their offsets
KERNEL void mkperm_phase_3(uint32_t *buckets,
uint32_t bucket_count,
uint32_t bucket_count_rounded,
uint32_t perm_size,
uint32_t *counter,
uint4 *offsets) {
uint32_t *shared = SharedMemory<uint32_t>::get();
// Thread's position within warp
uint32_t lane_idx = threadIdx.x & (warpSize - 1);
for (uint32_t i = blockIdx.x * blockDim.x + threadIdx.x;
i < bucket_count_rounded; i += blockDim.x * gridDim.x) {
uint32_t offset_a, offset_b;
offset_a = (i < bucket_count) ? buckets[i] : perm_size;
shared[threadIdx.x] = offset_a;
__syncthreads();
if (threadIdx.x + 1 < blockDim.x)
offset_b = shared[threadIdx.x + 1];
else
offset_b = (i + 1 < bucket_count) ? buckets[i + 1] : perm_size;
// Did we find a non-empty bucket?
bool found = offset_a != offset_b;
// Peers within the same warp that also found one
uint32_t peers = __ballot_sync(0xFFFFFFFF, found);
if (found) {
// Designate a leader thread within the set of peers
uint32_t leader_idx = __ffs(peers) - 1;
// If the current thread is the leader, perform atomic op.
uint32_t offset = 0;
if (lane_idx == leader_idx)
offset = atomicAdd(counter, __popc(peers));
// Fetch offset into output array from leader
offset = __shfl_sync(peers, offset, leader_idx);
// Determine current thread's position within peer group
offset += __popc(peers << (32 - lane_idx));
offsets[offset] = make_uint4(i, offset_a, offset_b - offset_a, 0);
}
}
}
/// Generate a sorting permutation based on offsets generated by \ref mkperm_phase_1_tiny()
KERNEL void mkperm_phase_4_tiny(const uint32_t *values,
const uint32_t *buckets_,
uint32_t *perm,
uint32_t size,
uint32_t size_per_block,
uint32_t bucket_count) {
uint32_t *shared = SharedMemory<uint32_t>::get();
uint32_t thread_id = threadIdx.x,
thread_count = blockDim.x,
block_start = blockIdx.x * size_per_block,
block_end = block_start + size_per_block,
warp_count = thread_count / warpSize,
warp_id = thread_id / warpSize;
const uint32_t *buckets = buckets_ + blockIdx.x * bucket_count * warp_count;
for (uint32_t i = thread_id; i < bucket_count * warp_count; i += thread_count)
shared[i] = buckets[i];
__syncthreads();
uint32_t *shared_warp = shared + warp_id * bucket_count;
for (uint32_t i = block_start + thread_id; i < block_end; i += thread_count) {
bool active = i < size;
/* This assumes that the whole warp does an iteration or exits
(i.e. size_per_block must be a multiple of 32) */
uint32_t active_mask = __ballot_sync(0xFFFFFFFF, active);
if (active) {
uint32_t offset = reduce(active_mask, values[i], shared_warp);
perm[offset] = i;
}
}
}
/// Generate a sorting permutation based on offsets generated by \ref mkperm_phase_1_small()
KERNEL void mkperm_phase_4_small(const uint32_t *values,
const uint32_t *buckets_,
uint32_t *perm,
uint32_t size,
uint32_t size_per_block,
uint32_t bucket_count) {
uint32_t *shared = SharedMemory<uint32_t>::get();
uint32_t thread_id = threadIdx.x,
thread_count = blockDim.x,
block_start = blockIdx.x * size_per_block,
block_end = block_start + size_per_block;
const uint32_t *buckets = buckets_ + blockIdx.x * bucket_count;
for (uint32_t i = thread_id; i < bucket_count; i += thread_count)
shared[i] = buckets[i];
__syncthreads();
for (uint32_t i = block_start + thread_id; i < block_end; i += thread_count) {
bool active = i < size;
/* This assumes that the whole warp does an iteration or exits
(i.e. size_per_block must be a multiple of 32) */
uint32_t active_mask = __ballot_sync(0xFFFFFFFF, active);
if (active) {
uint32_t offset = reduce_atomic(active_mask, values[i], shared);
perm[offset] = i;
}
}
}
/// Generate a sorting permutation based on offsets generated by \ref mkperm_phase_1_large()
KERNEL void mkperm_phase_4_large(const uint32_t *values,
uint32_t *buckets_,
uint32_t *perm,
uint32_t size,
uint32_t size_per_block,
uint32_t bucket_count) {
uint32_t thread_id = threadIdx.x,
thread_count = blockDim.x,
block_start = blockIdx.x * size_per_block,
block_end = block_start + size_per_block;
uint32_t *buckets = buckets_ + blockIdx.x * bucket_count;
for (uint32_t i = block_start + thread_id; i < block_end; i += thread_count) {
bool active = i < size;
/* This assumes that the whole warp does an iteration or exits
(i.e. size_per_block must be a multiple of 32) */
uint32_t active_mask = __ballot_sync(0xFFFFFFFF, active);
if (active) {
uint32_t offset = reduce_atomic(active_mask, values[i], buckets);
perm[offset] = i;
}
}
}
KERNEL void transpose(const uint32_t *in,
uint32_t *out,
uint32_t i_rows,
uint32_t i_cols) {
uint32_t *shared = SharedMemory<uint32_t>::get();
const uint32_t dim = 16;
uint32_t i_c = blockIdx.x * dim + threadIdx.x,
i_r = blockIdx.y * dim + threadIdx.y;
bool valid = i_r < i_rows && i_c < i_cols;
if (valid)
shared[threadIdx.y * (dim + 1) + threadIdx.x] = in[i_r * i_cols + i_c];
__syncthreads();
uint32_t o_rows = i_cols,
o_cols = i_rows,
o_c = blockIdx.y * dim + threadIdx.x,
o_r = blockIdx.x * dim + threadIdx.y;
valid = o_r < o_rows && o_c < o_cols;
if (valid)
out[o_r * o_cols + o_c] = shared[threadIdx.x * (dim + 1) + threadIdx.y];
}