-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultisplit2_compaction.cuh
574 lines (476 loc) · 24.7 KB
/
multisplit2_compaction.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/*
GpuMultisplit is the proprietary property of The Regents of the University of California ("The Regents") and is copyright © 2016 The Regents of the University of California, Davis campus. All Rights Reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted by nonprofit educational or research institutions for noncommercial use only, provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* The name or other trademarks of The Regents may not be used to endorse or promote products derived from this software without specific prior written permission.
The end-user understands that the program was developed for research purposes and is advised not to rely exclusively on the program for any reason.
THE SOFTWARE PROVIDED IS ON AN "AS IS" BASIS, AND THE REGENTS HAVE NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. THE REGENTS SPECIFICALLY DISCLAIM ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES, INCLUDING BUT NOT LIMITED TO PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, LOSS OF USE, DATA OR PROFITS, OR BUSINESS INTERRUPTION, HOWEVER CAUSED AND UNDER ANY THEORY OF LIABILITY WHETHER IN CONTRACT, STRICT LIABILITY OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
If you do not agree to these terms, do not download or use the software. This license may be modified only in a writing signed by authorized signatory of both parties.
For license information please contact [email protected] re T11-005.
*/
#ifndef __MULTISPLIT2_COMPACTION_CUH_
#define __MULTISPLIT2_COMPACTION_CUH_
#include <cub/cub.cuh>
#include <stdint.h>
#define COMPACTION_MULTISPLIT2_NUM_WARPS 8
#define COMPACTION_MULTISPLIT2_NUM_ROLLS 4
#define COMPACTION_MULTISPLIT2_NUM_TILES 3
#define COMPACTION_MULTISPLIT2_NUM_WARPS_PAIRS 8
#define COMPACTION_MULTISPLIT2_NUM_ROLLS_PAIRS 4
#define COMPACTION_MULTISPLIT2_NUM_TILES_PAIRS 1
template<
uint32_t NUM_TILES_,
uint32_t NUM_ROLLS_,
typename bucket_t,
typename KeyT>
__launch_bounds__(256)
__global__ void
multisplit2_compaction_prescan_protected(
KeyT* d_key_in,
uint32_t num_elements,
uint32_t* d_histogram,
bucket_t bucket_identifier)
{
uint32_t laneId = threadIdx.x & 0x1F;
uint32_t warpId = threadIdx.x >> 5;
uint32_t binCounter = 0;
if(blockIdx.x == (gridDim.x - 1)) // last block
{
for(int i_tile = 0; i_tile < NUM_TILES_; i_tile++)
{
#pragma unroll
for(int i_roll = 0; i_roll < NUM_ROLLS_; i_roll++)
{
uint32_t temp_address = (blockIdx.x * blockDim.x * NUM_TILES_ * NUM_ROLLS_) + \
(((warpId * NUM_TILES_ * NUM_ROLLS_) + \
(i_tile * NUM_ROLLS_) + \
i_roll) << 5) + \
laneId;
KeyT input_key = (temp_address < num_elements)?(d_key_in[temp_address]):0xFFFFFFFF;
// it's safe if we put the invalid element into the last bucket
uint32_t myBucket = (temp_address < num_elements)?bucket_identifier(input_key):1u;
uint32_t rx_buffer = __ballot((myBucket) & 0x01);
uint32_t myHisto = (((laneId) & 0x01)?rx_buffer:(~rx_buffer));
binCounter += __popc(myHisto);
}
}
}
else{ // all other blocks
for(int i_tile = 0; i_tile < NUM_TILES_; i_tile++)
{
#pragma unroll
for(int i_roll = 0; i_roll < NUM_ROLLS_; i_roll++)
{
KeyT input_key = (d_key_in[\
(blockIdx.x * blockDim.x * NUM_TILES_ * NUM_ROLLS_) + \
(((warpId * NUM_TILES_ * NUM_ROLLS_) + \
(i_tile * NUM_ROLLS_) + \
i_roll) << 5) + \
laneId]);
uint32_t myBucket = bucket_identifier(input_key);
uint32_t rx_buffer = __ballot((myBucket) & 0x01);
uint32_t myHisto = (((laneId) & 0x01)?rx_buffer:(~rx_buffer));
binCounter += __popc(myHisto);
}
}
}
// writing back results per warp into gmem:
if(laneId < 2)
{
d_histogram[(laneId * (blockDim.x >> 5) * gridDim.x) + ((blockDim.x >> 5) * blockIdx.x) + warpId] = binCounter;
}
}
template<
uint32_t NUM_WARPS_,
uint32_t NUM_TILES_,
uint32_t NUM_ROLLS_,
typename bucket_t,
typename KeyT>
__global__ void
multisplit2_compaction_postscan_4rolls_protected(
const KeyT* __restrict__ d_key_in,
KeyT* __restrict__ d_key_out,
uint32_t num_elements,
const uint32_t* __restrict__ d_histogram,
bucket_t bucket_identifier)
{
uint32_t laneId = threadIdx.x & 0x1F;
uint32_t warpId = threadIdx.x >> 5;
__shared__ KeyT smem[(32 * NUM_WARPS_ * NUM_ROLLS_)];
KeyT *keys_ms_smem = smem;
KeyT input_key[NUM_ROLLS_]; // stores all keys regarding to this thread
// ==== Loading back histogram results from gmem into smem:
uint32_t global_offset = 0;
if(laneId < 2) // warp -> bucket (uncoalesced gmem access)
global_offset = __ldg(&d_histogram[laneId * gridDim.x * NUM_WARPS_ + blockIdx.x * NUM_WARPS_ + warpId]);
if(blockIdx.x == (gridDim.x - 1))// last block ===============================================
{
for(int i_tile = 0; i_tile < NUM_TILES_; i_tile++)
{
uint32_t binCounter = 0; // total count of elements within its in-charge bucket
uint32_t myLocalIndex_list = 0; // each byte contains localIndex per roll
uint32_t myBucket_list = 0; // each byte contains bucket index per roll
uint32_t Histogram_list = 0;
#pragma unroll
for(int i_roll = 0; i_roll < NUM_ROLLS_; i_roll++)
{
uint32_t temp_address = (blockIdx.x * blockDim.x * NUM_TILES_ * NUM_ROLLS_) + \
(((warpId * NUM_TILES_ * NUM_ROLLS_) + \
(i_tile * NUM_ROLLS_) + \
i_roll) << 5) + \
laneId;
input_key[i_roll] = (temp_address < num_elements)?__ldg(&d_key_in[temp_address]):0xFFFFFFFF;
uint32_t myBucket = (temp_address < num_elements)?bucket_identifier(input_key[i_roll]):(1u);
myBucket_list |= (myBucket << (i_roll << 3)); // each byte myBucket per roll
uint32_t rx_buffer = __ballot((myBucket) & 0x01);
uint32_t myHisto = (((laneId) & 0x01)?rx_buffer:(~rx_buffer));
uint32_t myIndexBmp = (((myBucket) & 0x01)?rx_buffer:(~rx_buffer));
myHisto = __popc(myHisto);
binCounter += myHisto;
Histogram_list |= (myHisto << (i_roll << 3));
// each byte contains local index per roll
myLocalIndex_list |= ((__popc(myIndexBmp & (0xFFFFFFFF >> (31-laneId))) - 1) << (i_roll << 3));
}
// Byte-wide exlusive scan over Histogram_list: (for each roll)
Histogram_list = (Histogram_list << 8) + (Histogram_list << 16) + (Histogram_list << 24);
// inclusive scan on binCounters:
uint32_t scanned_binCount = binCounter;
uint32_t temp_scan = __shfl_up(scanned_binCount, 1, 32);
if(laneId >= 1) scanned_binCount += temp_scan;
// making it exclusive
scanned_binCount -= binCounter;
#pragma unroll
for(int i_roll = 0; i_roll < NUM_ROLLS_; i_roll++)
{
uint32_t temp_scanned_roll_histo = __shfl(Histogram_list, (myBucket_list & 0xFF));// tile-wide offset: same bucket
uint32_t myNewBlockIndex = ((temp_scanned_roll_histo >> (i_roll << 3)) & 0xFF);
myNewBlockIndex += (myLocalIndex_list & 0xFF); // among my current roll
myNewBlockIndex += __shfl(scanned_binCount, (myBucket_list & 0xFF)); // tile-wide offset:previous buckets
myNewBlockIndex += ((warpId * NUM_ROLLS_) << 5);
myBucket_list >>= 8;
myLocalIndex_list >>= 8;
keys_ms_smem[myNewBlockIndex] = input_key[i_roll];
}
#pragma unroll
for(int i_roll = 0; i_roll < NUM_ROLLS_; i_roll++)
{
uint32_t temp_address_smem = laneId + (i_roll << 5) +((warpId * NUM_ROLLS_) << 5);
KeyT temp_input = keys_ms_smem[temp_address_smem];
uint32_t myBucket_temp = ((laneId + (i_roll<<5) + ((i_tile * NUM_ROLLS_) << 5) + ((warpId * NUM_TILES_ * NUM_ROLLS_)<<5) + blockIdx.x * ((NUM_WARPS_*NUM_ROLLS_*NUM_TILES_)<<5)) < num_elements)?bucket_identifier(temp_input):(1u);
uint32_t myLocalIndex_temp = __shfl(global_offset, myBucket_temp);
myLocalIndex_temp += (laneId + (i_roll << 5)); // local offset
myLocalIndex_temp -= __shfl(scanned_binCount, myBucket_temp, 32);
if(myLocalIndex_temp < num_elements) // protected writing
d_key_out[myLocalIndex_temp] = temp_input;
}
// updating the global offsets for next tile
global_offset += binCounter;
}
}//====================================================================================
else{
for(int i_tile = 0; i_tile < NUM_TILES_; i_tile++)
{
uint32_t binCounter = 0; // total count of elements within its in-charge bucket
uint32_t myLocalIndex_list = 0; // each byte contains localIndex per roll
uint32_t myBucket_list = 0; // each byte contains bucket index per roll
uint32_t Histogram_list = 0;
#pragma unroll
for(int i_roll = 0; i_roll < NUM_ROLLS_; i_roll++)
{
input_key[i_roll] = __ldg(&d_key_in[\
(blockIdx.x * blockDim.x * NUM_TILES_ * NUM_ROLLS_) + \
(((warpId * NUM_TILES_ * NUM_ROLLS_) + \
(i_tile * NUM_ROLLS_) + \
i_roll) << 5) + \
laneId]);
uint32_t myBucket = bucket_identifier(input_key[i_roll]);
myBucket_list |= (myBucket << (i_roll << 3)); // each byte myBucket per roll
uint32_t rx_buffer = __ballot((myBucket) & 0x01);
uint32_t myHisto = (((laneId) & 0x01)?rx_buffer:(~rx_buffer));
uint32_t myIndexBmp = (((myBucket) & 0x01)?rx_buffer:(~rx_buffer));
myHisto = __popc(myHisto);
binCounter += myHisto;
Histogram_list |= (myHisto << (i_roll << 3));
// each byte contains local index per roll
myLocalIndex_list |= ((__popc(myIndexBmp & (0xFFFFFFFF >> (31-laneId))) - 1) << (i_roll << 3));
}
// Byte-wide exlusive scan over Histogram_list: (for each roll)
Histogram_list = (Histogram_list << 8) + (Histogram_list << 16) + (Histogram_list << 24);
// inclusive scan on binCounters:
uint32_t scanned_binCount = binCounter;
uint32_t temp_scan = __shfl_up(scanned_binCount, 1, 32);
if(laneId >= 1) scanned_binCount += temp_scan;
// making it exclusive
scanned_binCount -= binCounter;
#pragma unroll
for(int i_roll = 0; i_roll < NUM_ROLLS_; i_roll++)
{
uint32_t temp_scanned_roll_histo = __shfl(Histogram_list, (myBucket_list & 0xFF));// tile-wide offset: same bucket
uint32_t myNewBlockIndex = ((temp_scanned_roll_histo >> (i_roll << 3)) & 0xFF);
myNewBlockIndex += (myLocalIndex_list & 0xFF); // among my current roll
myNewBlockIndex += __shfl(scanned_binCount, (myBucket_list & 0xFF)); // tile-wide offset:previous buckets
myNewBlockIndex += ((warpId * NUM_ROLLS_) << 5);
myBucket_list >>= 8;
myLocalIndex_list >>= 8;
keys_ms_smem[myNewBlockIndex] = input_key[i_roll];
}
#pragma unroll
for(int i_roll = 0; i_roll < NUM_ROLLS_; i_roll++)
{
KeyT temp_input = keys_ms_smem[laneId + (i_roll << 5) +((warpId * NUM_ROLLS_) << 5)];
uint32_t myBucket_temp = bucket_identifier(temp_input);
uint32_t myLocalIndex_temp = __shfl(global_offset, myBucket_temp);
myLocalIndex_temp += (laneId + (i_roll << 5)); // local offset
myLocalIndex_temp -= __shfl(scanned_binCount, myBucket_temp, 32);
d_key_out[myLocalIndex_temp] = temp_input;
}
// updating the global offsets for next tile
global_offset += binCounter;
}
}
}
template<
uint32_t NUM_WARPS_,
uint32_t NUM_TILES_,
uint32_t NUM_ROLLS_,
typename bucket_t,
typename KeyT,
typename ValueT>
__global__ void
multisplit2_compaction_postscan_4rolls_pairs_protected(
const KeyT* __restrict__ d_key_in,
const ValueT* __restrict__ d_value_in,
KeyT* __restrict__ d_key_out,
ValueT* __restrict__ d_value_out,
uint32_t num_elements,
const uint32_t* __restrict__ d_histogram,
bucket_t bucket_identifier)
{
uint32_t laneId = threadIdx.x & 0x1F;
uint32_t warpId = threadIdx.x >> 5;
__shared__ KeyT smem[(64 * NUM_WARPS_ * NUM_ROLLS_)];
KeyT *keys_ms_smem = smem;
ValueT *values_ms_smem = &smem[32 * NUM_WARPS_ * NUM_ROLLS_];
KeyT input_key[NUM_ROLLS_]; // stores all keys regarding to this thread
ValueT input_value[NUM_ROLLS_]; // stores all values regarding to this thread
// ==== Loading back histogram results from gmem into smem:
uint32_t global_offset = 0;
if(laneId < 2) // warp -> bucket (uncoalesced gmem access)
global_offset = __ldg(&d_histogram[laneId * gridDim.x * NUM_WARPS_ + blockIdx.x * NUM_WARPS_ + warpId]);
if(blockIdx.x == (gridDim.x - 1)) // for last block: =========================================
{
for(int i_tile = 0; i_tile < NUM_TILES_; i_tile++)
{
uint32_t binCounter = 0; // total count of elements within its in-charge bucket
uint32_t myLocalIndex_list = 0; // each byte contains localIndex per roll
uint32_t myBucket_list = 0; // each byte contains bucket index per roll
uint32_t Histogram_list = 0;
#pragma unroll
for(int i_roll = 0; i_roll < NUM_ROLLS_; i_roll++)
{
uint32_t temp_address = (blockIdx.x * blockDim.x * NUM_TILES_ * NUM_ROLLS_) + \
(((warpId * NUM_TILES_ * NUM_ROLLS_) + \
(i_tile * NUM_ROLLS_) + \
i_roll) << 5) + \
laneId;
input_key[i_roll] = (temp_address < num_elements)?__ldg(&d_key_in[temp_address]):0xFFFFFFFF;
input_value[i_roll] = (temp_address < num_elements)?__ldg(&d_value_in[temp_address]):0xFFFFFFFF;
uint32_t myBucket = (temp_address < num_elements)?bucket_identifier(input_key[i_roll]):(1u);
myBucket_list |= (myBucket << (i_roll << 3)); // each byte myBucket per roll
uint32_t rx_buffer = __ballot((myBucket) & 0x01);
uint32_t myHisto = (((laneId) & 0x01)?rx_buffer:(~rx_buffer));
uint32_t myIndexBmp = (((myBucket) & 0x01)?rx_buffer:(~rx_buffer));
myHisto = __popc(myHisto);
binCounter += myHisto;
Histogram_list |= (myHisto << (i_roll << 3));
// each byte contains local index per roll
myLocalIndex_list |= ((__popc(myIndexBmp & (0xFFFFFFFF >> (31-laneId))) - 1) << (i_roll << 3));
}
// Byte-wide exlusive scan over Histogram_list: (for each roll)
Histogram_list = (Histogram_list << 8) + (Histogram_list << 16) + (Histogram_list << 24);
// inclusive scan on binCounters:
uint32_t scanned_binCount = binCounter;
uint32_t temp_scan = __shfl_up(scanned_binCount, 1, 32);
if(laneId >= 1) scanned_binCount += temp_scan;
// making it exclusive
scanned_binCount -= binCounter;
#pragma unroll
for(int i_roll = 0; i_roll < NUM_ROLLS_; i_roll++)
{
uint32_t temp_scanned_roll_histo = __shfl(Histogram_list, (myBucket_list & 0xFF));// tile-wide offset: same bucket
uint32_t myNewBlockIndex = ((temp_scanned_roll_histo >> (i_roll << 3)) & 0xFF);
myNewBlockIndex += (myLocalIndex_list & 0xFF); // among my current roll
myNewBlockIndex += __shfl(scanned_binCount, (myBucket_list & 0xFF)); // tile-wide offset:previous buckets
myNewBlockIndex += ((warpId * NUM_ROLLS_) << 5);
myBucket_list >>= 8;
myLocalIndex_list >>= 8;
keys_ms_smem[myNewBlockIndex] = input_key[i_roll];
values_ms_smem[myNewBlockIndex] = input_value[i_roll];
}
#pragma unroll
for(int i_roll = 0; i_roll < NUM_ROLLS_; i_roll++)
{
KeyT temp_key = keys_ms_smem[laneId + (i_roll << 5) +((warpId * NUM_ROLLS_) << 5)];
ValueT temp_value = values_ms_smem[laneId + (i_roll << 5) +((warpId * NUM_ROLLS_) << 5)];
uint32_t myBucket_temp = ((laneId + (i_roll<<5) + ((i_tile * NUM_ROLLS_) << 5) + ((warpId * NUM_TILES_ * NUM_ROLLS_)<<5) + blockIdx.x * ((NUM_WARPS_*NUM_ROLLS_*NUM_TILES_)<<5)) < num_elements)?bucket_identifier(temp_key):(1u);
uint32_t myLocalIndex_temp = __shfl(global_offset, myBucket_temp);
myLocalIndex_temp += (laneId + (i_roll << 5)); // local offset
myLocalIndex_temp -= __shfl(scanned_binCount, myBucket_temp, 32);
if(myLocalIndex_temp < num_elements){
d_key_out[myLocalIndex_temp] = temp_key;
d_value_out[myLocalIndex_temp] = temp_value;
}
}
// updating the global offsets for next tile
global_offset += binCounter;
}
}
else{ // ==========================================================================
for(int i_tile = 0; i_tile < NUM_TILES_; i_tile++)
{
uint32_t binCounter = 0; // total count of elements within its in-charge bucket
uint32_t myLocalIndex_list = 0; // each byte contains localIndex per roll
uint32_t myBucket_list = 0; // each byte contains bucket index per roll
uint32_t Histogram_list = 0;
#pragma unroll
for(int i_roll = 0; i_roll < NUM_ROLLS_; i_roll++)
{
uint32_t temp_address = (blockIdx.x * blockDim.x * NUM_TILES_ * NUM_ROLLS_) + \
(((warpId * NUM_TILES_ * NUM_ROLLS_) + \
(i_tile * NUM_ROLLS_) + \
i_roll) << 5) + \
laneId;
input_key[i_roll] = __ldg(&d_key_in[temp_address]);
input_value[i_roll] = __ldg(&d_value_in[temp_address]);
uint32_t myBucket = bucket_identifier(input_key[i_roll]);
myBucket_list |= (myBucket << (i_roll << 3)); // each byte myBucket per roll
uint32_t rx_buffer = __ballot((myBucket) & 0x01);
uint32_t myHisto = (((laneId) & 0x01)?rx_buffer:(~rx_buffer));
uint32_t myIndexBmp = (((myBucket) & 0x01)?rx_buffer:(~rx_buffer));
myHisto = __popc(myHisto);
binCounter += myHisto;
Histogram_list |= (myHisto << (i_roll << 3));
// each byte contains local index per roll
myLocalIndex_list |= ((__popc(myIndexBmp & (0xFFFFFFFF >> (31-laneId))) - 1) << (i_roll << 3));
}
// Byte-wide exlusive scan over Histogram_list: (for each roll)
Histogram_list = (Histogram_list << 8) + (Histogram_list << 16) + (Histogram_list << 24);
// inclusive scan on binCounters:
uint32_t scanned_binCount = binCounter;
uint32_t temp_scan = __shfl_up(scanned_binCount, 1, 32);
if(laneId >= 1) scanned_binCount += temp_scan;
// making it exclusive
scanned_binCount -= binCounter;
#pragma unroll
for(int i_roll = 0; i_roll < NUM_ROLLS_; i_roll++)
{
uint32_t temp_scanned_roll_histo = __shfl(Histogram_list, (myBucket_list & 0xFF));// tile-wide offset: same bucket
uint32_t myNewBlockIndex = ((temp_scanned_roll_histo >> (i_roll << 3)) & 0xFF);
myNewBlockIndex += (myLocalIndex_list & 0xFF); // among my current roll
myNewBlockIndex += __shfl(scanned_binCount, (myBucket_list & 0xFF)); // tile-wide offset:previous buckets
myNewBlockIndex += ((warpId * NUM_ROLLS_) << 5);
myBucket_list >>= 8;
myLocalIndex_list >>= 8;
keys_ms_smem[myNewBlockIndex] = input_key[i_roll];
values_ms_smem[myNewBlockIndex] = input_value[i_roll];
}
#pragma unroll
for(int i_roll = 0; i_roll < NUM_ROLLS_; i_roll++)
{
KeyT temp_key = keys_ms_smem[laneId + (i_roll << 5) +((warpId * NUM_ROLLS_) << 5)];
ValueT temp_value = values_ms_smem[laneId + (i_roll << 5) +((warpId * NUM_ROLLS_) << 5)];
uint32_t myBucket_temp = bucket_identifier(temp_key);
uint32_t myLocalIndex_temp = __shfl(global_offset, myBucket_temp);
myLocalIndex_temp += (laneId + (i_roll << 5)); // local offset
myLocalIndex_temp -= __shfl(scanned_binCount, myBucket_temp, 32);
d_key_out[myLocalIndex_temp] = temp_key;
d_value_out[myLocalIndex_temp] = temp_value;
}
// updating the global offsets for next tile
global_offset += binCounter;
}
}
}
//=====================================================
// external wrapper for calling our kernels:
//=====================================================
class compaction_context{
public:
void *d_temp_storage;
size_t temp_storage_bytes;
uint32_t *d_histogram;
compaction_context()
{
d_temp_storage = NULL;
temp_storage_bytes = 0;
d_histogram = NULL;
}
~compaction_context(){}
};
void compaction_allocate_key_only(uint32_t numElements, compaction_context& context)
{
uint32_t size_sub_prob = 32 * COMPACTION_MULTISPLIT2_NUM_ROLLS * COMPACTION_MULTISPLIT2_NUM_TILES;
uint32_t size_block = size_sub_prob * COMPACTION_MULTISPLIT2_NUM_WARPS;
uint32_t num_sub_prob_per_block = size_block/size_sub_prob;
uint32_t num_sub_prob = (numElements + size_sub_prob - 1)/(size_sub_prob);
num_sub_prob = (num_sub_prob + num_sub_prob_per_block - 1)/num_sub_prob_per_block*num_sub_prob_per_block;
cudaMalloc((void**)&context.d_histogram, sizeof(uint32_t)*2*num_sub_prob);
cub::DeviceScan::ExclusiveSum(context.d_temp_storage, context.temp_storage_bytes, context.d_histogram, context.d_histogram, 2 * num_sub_prob);
cudaMalloc((void**)&context.d_temp_storage, context.temp_storage_bytes);
}
void compaction_allocate_key_value(uint32_t numElements, compaction_context& context)
{
uint32_t size_sub_prob = 32 * COMPACTION_MULTISPLIT2_NUM_ROLLS_PAIRS * COMPACTION_MULTISPLIT2_NUM_TILES_PAIRS;
uint32_t size_block = size_sub_prob * COMPACTION_MULTISPLIT2_NUM_WARPS_PAIRS;
uint32_t num_sub_prob_per_block = size_block/size_sub_prob;
uint32_t num_sub_prob = (numElements + size_sub_prob - 1)/(size_sub_prob);
num_sub_prob = (num_sub_prob + num_sub_prob_per_block - 1)/num_sub_prob_per_block*num_sub_prob_per_block;
cudaMalloc((void**)&context.d_histogram, sizeof(unsigned int)*2*num_sub_prob);
cub::DeviceScan::ExclusiveSum(context.d_temp_storage, context.temp_storage_bytes, context.d_histogram, context.d_histogram, 2 * num_sub_prob);
cudaMalloc((void**)&context.d_temp_storage, context.temp_storage_bytes);
}
void compaction_release_memory(compaction_context& context)
{
cudaFree(context.d_histogram);
cudaFree(context.d_temp_storage);
}
template<typename key_t, typename bucket_t>
unsigned int compaction_key_only(key_t* d_key_in, key_t* d_key_out, uint32_t numElements, compaction_context& context, bucket_t bucket_identifier)
{
uint32_t size_sub_prob = 32 * COMPACTION_MULTISPLIT2_NUM_ROLLS * COMPACTION_MULTISPLIT2_NUM_TILES;
uint32_t size_block = size_sub_prob * COMPACTION_MULTISPLIT2_NUM_WARPS;
uint32_t num_sub_prob_per_block = size_block/size_sub_prob;
uint32_t num_sub_prob = (numElements + size_sub_prob - 1)/(size_sub_prob);
num_sub_prob = (num_sub_prob + num_sub_prob_per_block - 1)/num_sub_prob_per_block*num_sub_prob_per_block;
uint32_t num_blocks = (numElements + size_block - 1)/size_block;
// pre-scan stage:
multisplit2_compaction_prescan_protected<COMPACTION_MULTISPLIT2_NUM_TILES, COMPACTION_MULTISPLIT2_NUM_ROLLS><<<num_blocks, 32*COMPACTION_MULTISPLIT2_NUM_WARPS>>>(d_key_in, numElements, context.d_histogram, bucket_identifier);
// scan stage:
cub::DeviceScan::ExclusiveSum(context.d_temp_storage, context.temp_storage_bytes, context.d_histogram, context.d_histogram, 2 * num_sub_prob);
uint32_t n_bucket_zero = 0;
cudaMemcpy(&n_bucket_zero, context.d_histogram + num_sub_prob, sizeof(uint32_t), cudaMemcpyDeviceToHost);
// post-scan stage:
multisplit2_compaction_postscan_4rolls_protected<COMPACTION_MULTISPLIT2_NUM_WARPS, COMPACTION_MULTISPLIT2_NUM_TILES, COMPACTION_MULTISPLIT2_NUM_ROLLS><<<num_blocks, 32*COMPACTION_MULTISPLIT2_NUM_WARPS>>>(d_key_in, d_key_out, numElements, context.d_histogram, bucket_identifier);
return n_bucket_zero;
}
template<typename key_t, typename value_t, typename bucket_t>
unsigned int compaction_key_value(key_t* d_key_in, value_t* d_value_in, key_t* d_key_out, value_t* d_value_out, uint32_t numElements, compaction_context& context, bucket_t bucket_identifier)
{
uint32_t size_sub_prob = 32 * COMPACTION_MULTISPLIT2_NUM_ROLLS_PAIRS * COMPACTION_MULTISPLIT2_NUM_TILES_PAIRS;
uint32_t size_block = size_sub_prob * COMPACTION_MULTISPLIT2_NUM_WARPS_PAIRS;
uint32_t num_sub_prob_per_block = size_block/size_sub_prob;
uint32_t num_sub_prob = (numElements + size_sub_prob - 1)/(size_sub_prob);
num_sub_prob = (num_sub_prob + num_sub_prob_per_block - 1)/num_sub_prob_per_block*num_sub_prob_per_block;
uint32_t num_blocks = (numElements + size_block - 1)/size_block;
// pre-scan stage:
multisplit2_compaction_prescan_protected<COMPACTION_MULTISPLIT2_NUM_TILES_PAIRS, COMPACTION_MULTISPLIT2_NUM_ROLLS_PAIRS><<<num_blocks, 32*COMPACTION_MULTISPLIT2_NUM_WARPS_PAIRS>>>(d_key_in, numElements, context.d_histogram, bucket_identifier);
// scan stage:
cub::DeviceScan::ExclusiveSum(context.d_temp_storage, context.temp_storage_bytes, context.d_histogram, context.d_histogram, 2 * num_sub_prob);
uint32_t n_bucket_zero = 0;
cudaMemcpy(&n_bucket_zero, context.d_histogram + num_sub_prob, sizeof(uint32_t), cudaMemcpyDeviceToHost);
// post-scan stage:
multisplit2_compaction_postscan_4rolls_pairs_protected<COMPACTION_MULTISPLIT2_NUM_WARPS_PAIRS, COMPACTION_MULTISPLIT2_NUM_TILES_PAIRS, COMPACTION_MULTISPLIT2_NUM_ROLLS_PAIRS><<<num_blocks, 32*COMPACTION_MULTISPLIT2_NUM_WARPS_PAIRS>>>(d_key_in, d_value_in, d_key_out, d_value_out, numElements, context.d_histogram, bucket_identifier);
return n_bucket_zero;
}
#endif