-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompaction.cuh
More file actions
628 lines (572 loc) · 24.8 KB
/
Copy pathcompaction.cuh
File metadata and controls
628 lines (572 loc) · 24.8 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
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
#pragma once
#include <cub/cub.cuh>
#include <cuda/atomic>
#include <cuda/std/limits>
/*
* @author Kevin Kristensen
*/
/// T should be an unsigned integer type
template <typename T>
constexpr __host__ __device__ __forceinline__ bool evaluate_top_digit(T value)
{
return (value >> (cuda::std::numeric_limits<T>::digits - 1));
}
template <typename T>
constexpr __host__ __device__ __forceinline__ T set_top_digit(T value)
{
return (value | (static_cast<T>(1) << (cuda::std::numeric_limits<T>::digits - 1)));
}
template <typename T>
constexpr __host__ __device__ __forceinline__ T remove_top_digit(T value)
{
return value & ~(static_cast<T>(1) << (cuda::std::numeric_limits<T>::digits - 1));
;
}
/// Compare op for uint64_t indices with MSB potentially set
struct IndexCompareOpT
{
__host__ __device__ __forceinline__ bool operator()(const uint64_t& l, const uint64_t& r) const
{
return remove_top_digit(l) < remove_top_digit(r);
}
};
/// Initializes state for decoupled look-back
template <typename ScanTileStateT>
__global__ void scan_tile_state_init_kernel(ScanTileStateT scan_tile_state, int32_t num_tiles)
{
scan_tile_state.InitializeStatus(num_tiles);
}
/// Determines the partitions of the allocation table along which to merge with the memory pool
template <int32_t TileSize, typename AllocationIndexIteratorT, typename MemoryPoolIndexIteratorT>
__global__ void partition_merge_path_kernel(AllocationIndexIteratorT allocation_indices,
uint64_t num_allocation_indices,
MemoryPoolIndexIteratorT memory_pool_indices,
uint64_t num_memory_pool_indices,
uint64_t* merge_partitions,
uint64_t num_merge_partitions)
{
uint64_t partition_idx = blockDim.x * blockIdx.x + threadIdx.x;
if (partition_idx < num_merge_partitions)
{
uint64_t diagonal =
(cub::min)(TileSize * partition_idx, num_allocation_indices + num_memory_pool_indices);
merge_partitions[partition_idx] = cub::MergePath(allocation_indices,
memory_pool_indices,
num_allocation_indices,
num_memory_pool_indices,
diagonal,
IndexCompareOpT{});
}
}
/*
* The compaction kernel. This is a complicated kernel and should be decomposed into modular
* components in the system.
*/
template <int32_t BlockThreads,
int32_t ItemsPerThread,
typename ChunkT,
typename ScanTileStateT,
typename CounterT>
__global__ void compact_kernel(const uint64_t* allocation_indices,
uint64_t num_allocations,
ChunkT* memory_pool,
uint64_t num_memory_pool_chunks,
const uint64_t* merge_partitions,
ScanTileStateT scan_tile_state,
CounterT* counter)
{
/// Static values
static constexpr int32_t items_per_tile = BlockThreads * ItemsPerThread;
static constexpr auto default_allocation_index = set_top_digit<uint64_t>(0);
/// Using declarations
using block_load_t =
cub::BlockLoad<ChunkT, BlockThreads, ItemsPerThread, cub::BLOCK_LOAD_STRIPED>;
using block_load_storage_t = typename block_load_t::TempStorage;
using block_scan_t = cub::BlockScan<int32_t, BlockThreads>;
using block_scan_storage_t = typename block_scan_t::TempStorage;
using block_exchange_t = cub::BlockExchange<uint64_t, BlockThreads, ItemsPerThread>;
using block_exchange_storage_t = typename block_exchange_t::TempStorage;
using block_store_t =
cub::BlockStore<ChunkT, BlockThreads, ItemsPerThread, cub::BLOCK_STORE_STRIPED>;
using block_store_storage_t = typename block_store_t::TempStorage;
using scan_tile_state_t = ScanTileStateT; // Should be cub::ScanTileState<uint64_t>;
using scan_op_t = cuda::std::plus<uint64_t>;
using tile_prefix_op_t = cub::TilePrefixCallbackOp<uint64_t, scan_op_t, scan_tile_state_t>;
using prefix_op_storage_t = typename tile_prefix_op_t::TempStorage;
using compare_op_t = IndexCompareOpT;
/// Shared memory
__shared__ union
{
uint64_t shared_previous_allocation_index;
uint64_t shared_indices[items_per_tile + 1];
block_scan_storage_t scan;
block_exchange_storage_t exchange;
prefix_op_storage_t prefix_op;
block_store_storage_t store;
block_load_storage_t load;
} temp_storage;
__shared__ uint64_t write_offset;
/// Thread memory
uint64_t thread_indices[ItemsPerThread];
int32_t thread_valid_flags[ItemsPerThread];
int32_t thread_ranks[ItemsPerThread];
ChunkT thread_chunks[ItemsPerThread];
compare_op_t compare_op{};
scan_op_t scan_op{};
int32_t num_valid = 0;
bool is_partial_tile =
((blockIdx.x + 1) * items_per_tile) > (num_allocations + num_memory_pool_chunks);
bool is_last_tile =
((blockIdx.x + 1) * items_per_tile) >= (num_allocations + num_memory_pool_chunks);
/// Determine the bounding box for the range of allocation and memory pool indices to be processed
/// by this thread block
uint64_t allocation_indices_beg = merge_partitions[blockIdx.x + 0];
uint64_t allocation_indices_end = merge_partitions[blockIdx.x + 1];
uint64_t diag0 = items_per_tile * blockIdx.x;
uint64_t diag1 = (cub::min)(num_allocations + num_memory_pool_chunks, diag0 + items_per_tile);
uint64_t memory_pool_indices_beg = diag0 - allocation_indices_beg;
uint64_t memory_pool_indices_end = diag1 - allocation_indices_end;
auto num_allocation_indices_tile =
static_cast<int32_t>(allocation_indices_end - allocation_indices_beg);
auto num_memory_pool_indices_tile =
static_cast<int32_t>(memory_pool_indices_end - memory_pool_indices_beg);
// To carry forward the valid flag from a previous allocation index to a current memory pool
// index, load the previous allocation index into shared memory
if (threadIdx.x == 0)
{
if (allocation_indices_beg > 0)
{
temp_storage.shared_previous_allocation_index =
allocation_indices[allocation_indices_beg - 1];
}
else
{
temp_storage.shared_previous_allocation_index = default_allocation_index;
}
}
__syncthreads();
uint64_t previous_allocation_index = temp_storage.shared_previous_allocation_index;
// Optimized code path for when the entire tile's memory pool belongs to a single allocation
// if (num_allocation_indices_tile == 0 ||
// (num_allocation_indices_tile == 1 && !compare_op(memory_pool[memory_pool_indices_beg],
// allocation_indices[allocation_indices_beg])))
if (num_allocation_indices_tile == 0) // Skip the merge
{
// uint64_t allocation_index = num_allocation_indices_tile == 0
// ? previous_allocation_index
// : allocation_indices[allocation_indices_beg];
// int32_t num_valid = evaluate_top_digit(allocation_index) ? num_memory_pool_indices_tile : 0;
num_valid = evaluate_top_digit(previous_allocation_index) ? num_memory_pool_indices_tile : 0;
// We must load the data before doing the lookback!
if (num_valid != 0)
{
block_load_t{temp_storage.load}.Load(memory_pool + memory_pool_indices_beg,
thread_chunks,
num_valid);
__syncthreads();
}
}
else // Do the merge
{
// Load the allocation and memory pool indices into registers and then from registers to shared
// memory
if (!is_partial_tile)
{
#pragma unroll ItemsPerThread
for (int32_t i = 0; i < ItemsPerThread; ++i)
{
int32_t shared_idx = threadIdx.x + BlockThreads * i;
thread_indices[i] = shared_idx < num_allocation_indices_tile
? allocation_indices[allocation_indices_beg + shared_idx]
: static_cast<uint64_t>(memory_pool_indices_beg + shared_idx -
num_allocation_indices_tile);
}
#pragma unroll ItemsPerThread
for (int32_t i = 0; i < ItemsPerThread; ++i)
{
int32_t shared_idx = threadIdx.x + BlockThreads * i;
temp_storage.shared_indices[shared_idx] = thread_indices[i];
}
}
else
{ // partial tile
#pragma unroll ItemsPerThread
for (int32_t i = 0; i < ItemsPerThread; ++i)
{
int32_t shared_idx = threadIdx.x + BlockThreads * i;
if (shared_idx < (num_allocation_indices_tile + num_memory_pool_indices_tile))
{
thread_indices[i] = shared_idx < num_allocation_indices_tile
? allocation_indices[allocation_indices_beg + shared_idx]
: static_cast<uint64_t>(memory_pool_indices_beg + shared_idx -
num_allocation_indices_tile);
}
}
#pragma unroll ItemsPerThread
for (int32_t i = 0; i < ItemsPerThread; ++i)
{
int32_t shared_idx = threadIdx.x + BlockThreads * i;
if (shared_idx < (num_allocation_indices_tile + num_memory_pool_indices_tile))
{
temp_storage.shared_indices[shared_idx] = thread_indices[i];
}
}
// Initialize the flags (Maybe unnecessary?)
#pragma unroll ItemsPerThread
for (int32_t i = 0; i < ItemsPerThread; ++i)
{
thread_valid_flags[i] = 0;
}
}
__syncthreads();
// Do binary search in shared memory to merge the set of keys for each thread
auto diag0_thread =
static_cast<int32_t>((cub::min)(num_allocation_indices_tile + num_memory_pool_indices_tile,
ItemsPerThread * threadIdx.x));
int32_t allocation_indices_beg_thread =
cub::MergePath(&temp_storage.shared_indices[0],
&temp_storage.shared_indices[num_allocation_indices_tile],
num_allocation_indices_tile,
num_memory_pool_indices_tile,
diag0_thread,
compare_op);
int32_t allocation_indices_end_thread =
num_allocation_indices_tile; // I'm stealing this assignment from CUB, which I don't fully
// understand
int32_t memory_pool_indices_beg_thread = diag0_thread - allocation_indices_beg_thread;
int32_t memory_pool_indices_end_thread = num_memory_pool_indices_tile;
/// Do the 'merge'
uint64_t allocation_index = temp_storage.shared_indices[allocation_indices_beg_thread];
uint64_t memory_pool_index =
temp_storage.shared_indices[num_allocation_indices_tile + memory_pool_indices_beg_thread];
if (allocation_indices_beg_thread > 0)
{
previous_allocation_index = temp_storage.shared_indices[allocation_indices_beg_thread - 1];
}
bool is_valid = evaluate_top_digit(previous_allocation_index);
#pragma unroll ItemsPerThread
for (int32_t i = 0; i < ItemsPerThread; ++i)
{
bool is_memory_pool_index =
(memory_pool_indices_beg_thread < memory_pool_indices_end_thread) &&
((allocation_indices_beg_thread >= allocation_indices_end_thread) ||
compare_op(memory_pool_index, allocation_index));
thread_valid_flags[i] = is_memory_pool_index && is_valid;
thread_indices[i] = is_memory_pool_index ? memory_pool_index : allocation_index;
if (is_memory_pool_index)
{
++memory_pool_indices_beg_thread;
memory_pool_index =
temp_storage.shared_indices[num_allocation_indices_tile + memory_pool_indices_beg_thread];
}
else
{ // allocation index
is_valid = evaluate_top_digit(allocation_index);
++allocation_indices_beg_thread;
allocation_index = temp_storage.shared_indices[allocation_indices_beg_thread];
}
}
__syncthreads();
// Now we will scatter the thread_indices based on their validity before loading data for
// compaction
block_scan_t(temp_storage.scan).ExclusiveSum(thread_valid_flags, thread_ranks, num_valid);
__syncthreads();
block_exchange_t(temp_storage.exchange)
.ScatterToStripedFlagged(thread_indices, thread_indices, thread_ranks, thread_valid_flags);
__syncthreads();
// Load the data from the memory pool (coalesced)
#pragma unroll ItemsPerThread
for (int32_t i = 0; i < ItemsPerThread; ++i)
{
int32_t shared_idx = threadIdx.x + BlockThreads * i;
if (shared_idx < num_valid)
// if (thread_valid_flags[i])
{
thread_chunks[i] = memory_pool[thread_indices[i]];
}
}
}
// Now do decoupled look-back
if (blockIdx.x == 0)
{
if (threadIdx.x == 0)
{
scan_tile_state.SetInclusive(blockIdx.x, num_valid);
write_offset = 0;
// If it's the last tile, update the counter
if (is_last_tile)
{
*counter = num_valid;
}
}
}
else
{
// The first warp does the look-back
if (threadIdx.x < CUB_PTX_WARP_THREADS)
{
// Initialize the prefix op
tile_prefix_op_t prefix_op(scan_tile_state, temp_storage.prefix_op, scan_op);
// De the decoupled look-back
uint64_t exclusive_num_valid = prefix_op(static_cast<uint64_t>(num_valid));
// Update the tile-wide offset
if (threadIdx.x == 0)
{
write_offset = exclusive_num_valid;
// If it's the last tile, update the counter
if (is_last_tile)
{
*counter = prefix_op.GetInclusivePrefix();
}
}
}
}
// Store data, if necessary
if (num_valid != 0)
{
__syncthreads(); // For write offset
// Now that look-back is done, we can write back to the memory pool (if we need to)
if (write_offset != memory_pool_indices_beg)
{
block_store_t(temp_storage.store).Store(memory_pool + write_offset, thread_chunks, num_valid);
// #pragma unroll ItemsPerThread
// for (int32_t i = 0; i < ItemsPerThread; ++i)
// {
// if (thread_valid_flags[i])
// {
// memory_pool[write_offset + thread_ranks[i]] = thread_chunks[i];
// }
// }
}
}
}
/*
* The new compaction kernel (under construction)
*/
// template <int32_t BlockThreads,
// int32_t ItemsPerThread,
// typename ChunkT,
// typename ScanTileStateT,
// typename CounterT>
// __global__ void compact_kernel_new(const uint64_t* old_allocation_indices,
// const uint64_t* new_allocation_indices,
// uint64_t num_new_allocation_indices,
// ChunkT* memory_pool,
// uint64_t num_memory_pool_chunks,
// const uint64_t* merge_partitions,
// ScanTileStateT scan_tile_state,
// CounterT* counter)
// {
// /// Static values
// static constexpr int32_t items_per_tile = BlockThreads * ItemsPerThread;
// /// Using declarations
// using block_load_t =
// cub::BlockLoad<ChunkT, BlockThreads, ItemsPerThread, cub::BLOCK_LOAD_STRIPED>;
// using block_load_storage_t = typename block_load_t::TempStorage;
// using block_store_t =
// cub::BlockStore<ChunkT, BlockThreads, ItemsPerThread, cub::BLOCK_STORE_STRIPED>;
// using block_store_storage_t = typename block_store_t::TempStorage;
// using scan_tile_state_t = ScanTileStateT; // Should be cub::ScanTileState<bool>;
// using scan_op_t = cuda::std::plus<uint64_t>;
// using tile_prefix_op_t = cub::TilePrefixCallbackOp<uint64_t, scan_op_t, scan_tile_state_t>;
// using prefix_op_storage_t = typename tile_prefix_op_t::TempStorage;
// using compare_op_t = IndexCompareOpT;
// /// Shared memory
// __shared__ union
// {
// uint64_t shared_previous_allocation_index;
// uint64_t shared_indices[items_per_tile + 1];
// prefix_op_storage_t prefix_op;
// block_store_storage_t store;
// block_load_storage_t load;
// } temp_storage;
// __shared__ uint64_t write_offset;
// /// Thread memory
// uint64_t thread_new_indices[ItemsPerThread];
// uint64_t thread_old_indices[ItemsPerThread];
// ChunkT thread_chunks[ItemsPerThread];
// compare_op_t compare_op{};
// scan_op_t scan_op{};
// int32_t num_valid = 0;
// bool is_last_tile =
// ((blockIdx.x + 1) * items_per_tile) >= (num_new_allocation_indices + num_memory_pool_chunks);
// /// Determine the bounding box for the range of allocation and memory pool indices to be processed
// /// by this thread block
// uint64_t allocation_indices_beg = merge_partitions[blockIdx.x + 0];
// uint64_t allocation_indices_end = merge_partitions[blockIdx.x + 1];
// uint64_t diag0 = items_per_tile * blockIdx.x;
// uint64_t diag1 =
// (cub::min)(num_new_allocation_indices + num_memory_pool_chunks, diag0 + items_per_tile);
// uint64_t memory_pool_indices_beg = diag0 - allocation_indices_beg;
// uint64_t memory_pool_indices_end = diag1 - allocation_indices_end;
// auto num_allocation_indices_tile =
// static_cast<int32_t>(allocation_indices_end - allocation_indices_beg);
// auto num_memory_pool_indices_tile =
// static_cast<int32_t>(memory_pool_indices_end - memory_pool_indices_beg);
// // To carry forward the valid flag from a previous allocation index to a current memory pool
// // index, load the previous allocation index into shared memory
// if (threadIdx.x == 0)
// {
// if (allocation_indices_beg > 0)
// {
// temp_storage.shared_previous_allocation_index =
// allocation_indices[allocation_indices_beg - 1];
// }
// else // You cannot go backwards from zero
// {
// temp_storage.shared_previous_allocation_index = 0;
// }
// }
// __syncthreads();
// uint64_t previous_allocation_index = temp_storage.shared_previous_allocation_index;
// // Optimized code path for when the entire tile's memory pool belongs to a single allocation
// // if (num_allocation_indices_tile == 0 ||
// // (num_allocation_indices_tile == 1 && !compare_op(memory_pool[memory_pool_indices_beg],
// // allocation_indices[allocation_indices_beg])))
// if (num_allocation_indices_tile == 0) // Skip the merge
// {
// // uint64_t allocation_index = num_allocation_indices_tile == 0
// // ? previous_allocation_index
// // : allocation_indices[allocation_indices_beg];
// // int32_t num_valid = evaluate_top_digit(allocation_index) ? num_memory_pool_indices_tile : 0;
// // We must load the data before doing the lookback!
// block_load_t{temp_storage.load}.Load(memory_pool + memory_pool_indices_beg,
// thread_chunks,
// num_memory_pool_indices_tile);
// __syncthreads();
// }
// else // Do the merge
// {
// // Load the allocation and memory pool indices into registers and then from registers to shared
// // memory
// if (!is_last_tile)
// {
// #pragma unroll ItemsPerThread
// for (int32_t i = 0; i < ItemsPerThread; ++i)
// {
// int32_t shared_idx = threadIdx.x + BlockThreads * i;
// thread_new_indices[i] = shared_idx < num_allocation_indices_tile
// ? new_allocation_indices[allocation_indices_beg + shared_idx]
// : static_cast<uint64_t>(memory_pool_indices_beg + shared_idx -
// num_allocation_indices_tile);
// }
// #pragma unroll ItemsPerThread
// for (int32_t i = 0; i < ItemsPerThread; ++i)
// {
// int32_t shared_idx = threadIdx.x + BlockThreads * i;
// temp_storage.shared_indices[shared_idx] = thread_new_indices[i];
// }
// }
// else // The last tile
// {
// #pragma unroll ItemsPerThread
// for (int32_t i = 0; i < ItemsPerThread; ++i)
// {
// int32_t shared_idx = threadIdx.x + BlockThreads * i;
// if (shared_idx < (num_allocation_indices_tile + num_memory_pool_indices_tile))
// {
// thread_new_indices[i] = shared_idx < num_allocation_indices_tile
// ? allocation_indices[allocation_indices_beg + shared_idx]
// : static_cast<uint64_t>(memory_pool_indices_beg + shared_idx -
// num_allocation_indices_tile);
// }
// }
// #pragma unroll ItemsPerThread
// for (int32_t i = 0; i < ItemsPerThread; ++i)
// {
// int32_t shared_idx = threadIdx.x + BlockThreads * i;
// if (shared_idx < (num_allocation_indices_tile + num_memory_pool_indices_tile))
// {
// temp_storage.shared_indices[shared_idx] = thread_new_indices[i];
// }
// }
// // Initialize the flags (Maybe unnecessary?)
// #pragma unroll ItemsPerThread
// for (int32_t i = 0; i < ItemsPerThread; ++i)
// {
// thread_valid_flags[i] = 0;
// }
// }
// __syncthreads();
// // Do binary search in shared memory to merge the set of keys for each thread
// auto diag0_thread =
// static_cast<int32_t>((cub::min)(num_allocation_indices_tile + num_memory_pool_indices_tile,
// ItemsPerThread * threadIdx.x));
// int32_t allocation_indices_beg_thread =
// cub::MergePath(&temp_storage.shared_indices[0],
// &temp_storage.shared_indices[num_allocation_indices_tile],
// num_allocation_indices_tile,
// num_memory_pool_indices_tile,
// diag0_thread,
// compare_op);
// int32_t allocation_indices_end_thread =
// num_allocation_indices_tile; // I'm stealing this assignment from CUB, which I don't fully
// // understand
// int32_t memory_pool_indices_beg_thread = diag0_thread - allocation_indices_beg_thread;
// int32_t memory_pool_indices_end_thread = num_memory_pool_indices_tile;
// /// Do the 'merge'
// uint64_t allocation_index = temp_storage.shared_indices[allocation_indices_beg_thread];
// uint64_t memory_pool_index =
// temp_storage.shared_indices[num_allocation_indices_tile + memory_pool_indices_beg_thread];
// if (allocation_indices_beg_thread > 0)
// {
// previous_allocation_index = temp_storage.shared_indices[allocation_indices_beg_thread - 1];
// }
// uint64_t old_index = old_allocation_indices[previous_allocation_index];
// #pragma unroll ItemsPerThread
// for (int32_t i = 0; i < ItemsPerThread; ++i)
// {
// bool is_memory_pool_index =
// (memory_pool_indices_beg_thread < memory_pool_indices_end_thread) &&
// ((allocation_indices_beg_thread >= allocation_indices_end_thread) ||
// compare_op(memory_pool_index, allocation_index));
// thread_valid_flags[i] = is_memory_pool_index && is_valid;
// thread_new_indices[i] = is_memory_pool_index ? memory_pool_index : allocation_index;
// if (is_memory_pool_index)
// {
// ++memory_pool_indices_beg_thread;
// memory_pool_index =
// temp_storage.shared_indices[num_allocation_indices_tile + memory_pool_indices_beg_thread];
// }
// else
// { // allocation index
// is_valid = evaluate_top_digit(allocation_index);
// ++allocation_indices_beg_thread;
// allocation_index = temp_storage.shared_indices[allocation_indices_beg_thread];
// }
// }
// __syncthreads();
// // Load the data from the memory pool (coalesced)
// #pragma unroll ItemsPerThread
// for (int32_t i = 0; i < ItemsPerThread; ++i)
// {
// int32_t shared_idx = threadIdx.x + BlockThreads * i;
// if (shared_idx < num_valid)
// // if (thread_valid_flags[i])
// {
// thread_chunks[i] = memory_pool[thread_old_indices[i]];
// }
// }
// }
// // Now do decoupled look-back
// if (blockIdx.x == 0)
// {
// if (threadIdx.x == 0)
// {
// scan_tile_state.SetInclusive(blockIdx.x, true);
// }
// }
// else
// {
// // The first warp does the look-back
// if (threadIdx.x < CUB_PTX_WARP_THREADS)
// {
// // Initialize the prefix op
// tile_prefix_op_t prefix_op(scan_tile_state, temp_storage.prefix_op, scan_op);
// // De the decoupled look-back
// prefix_op(true);
// }
// }
// __syncthreads();
// // Store data (TODO)
// }