-
Notifications
You must be signed in to change notification settings - Fork 1
/
libcubwt.cu
3124 lines (2458 loc) · 148 KB
/
libcubwt.cu
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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*--
This file is a part of libcubwt, a library for CUDA accelerated
burrows wheeler transform construction and inversion.
Copyright (c) 2022-2024 Ilya Grebnov <[email protected]>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Please see the file LICENSE for full copyright and license details.
--*/
#include "libcubwt.cuh"
#if defined(_MSC_VER) && defined(__INTELLISENSE__)
#define __launch_bounds__(block_size) /* */
#define __CUDACC__
#include <vector_functions.h>
#include <device_functions.h>
#include <device_launch_parameters.h>
#endif
#include <cub/cub.cuh>
#include <cuda.h>
#include <utility>
#if defined(__GNUC__) || defined(__clang__) || defined(__CUDACC__)
#define RESTRICT __restrict__
#elif defined(_MSC_VER) || defined(__INTEL_COMPILER)
#define RESTRICT __restrict
#else
#define RESTRICT /* */
#endif
#ifndef __CUDA_ARCH__
#define CUDA_DEVICE_ARCH 0
#else
#define CUDA_DEVICE_ARCH __CUDA_ARCH__
#endif
#if CUDA_DEVICE_ARCH == 750
#define CUDA_SM_THREADS (1024)
#elif CUDA_DEVICE_ARCH == 860 || CUDA_DEVICE_ARCH == 870 || CUDA_DEVICE_ARCH == 890
#define CUDA_SM_THREADS (1536)
#else
#define CUDA_SM_THREADS (2048)
#endif
#if CUDA_DEVICE_ARCH == 860 || CUDA_DEVICE_ARCH == 870 || CUDA_DEVICE_ARCH == 890
#define CUDA_BLOCK_THREADS (768)
#else
#define CUDA_BLOCK_THREADS (512)
#endif
#define CUDA_WARP_THREADS (32)
#define CUDA_DEVICE_PADDING (12 * 768)
#define ALPHABET_SIZE (256)
template <typename BaseTypeT, typename OffsetTypeT>
struct OffsetToPointerOperator
{
BaseTypeT * d_base;
__host__ __device__ __forceinline__ BaseTypeT * operator()(const OffsetTypeT& offset) const
{
return d_base + offset;
}
};
typedef struct LIBCUBWT_DEVICE_STORAGE
{
void * device_rsort_temp_storage;
size_t device_rsort_temp_storage_size;
void * device_ssort_temp_storage;
size_t device_ssort_temp_storage_size;
uint8_t * device_T;
uint8_t * device_heads;
uint32_t * device_SA;
uint32_t * device_ISA;
uint32_t * device_keys;
uint32_t * device_offsets;
uint32_t * device_temp_SA;
uint32_t * device_temp_ISA;
uint32_t * device_temp_keys;
uint64_t * device_SA_temp_SA;
uint64_t * device_keys_temp_keys;
uint64_t * device_offsets_ISA;
uint4 * device_descriptors_large;
uint4 * device_descriptors_copy;
uint2 * device_descriptors_small;
void * device_storage;
size_t device_storage_size;
int32_t device_L2_cache_bits;
int32_t device_multiprocessor_count;
int32_t device_multiprocessor_max_blocks;
int32_t device_multiprocessor_max_threads;
void * host_pinned_storage;
size_t host_pinned_storage_size;
int64_t max_length;
uint32_t num_unsorted_segments;
uint32_t num_unsorted_suffixes;
uint32_t cuda_block_threads;
cudaStream_t cuda_stream;
} LIBCUBWT_DEVICE_STORAGE;
static int64_t libcubwt_get_error_code(cudaError_t status)
{
return
status == cudaErrorMemoryAllocation ? LIBCUBWT_GPU_NOT_ENOUGH_MEMORY :
status == cudaErrorDevicesUnavailable ? LIBCUBWT_GPU_NOT_SUPPORTED :
status == cudaErrorNoDevice ? LIBCUBWT_GPU_NOT_SUPPORTED :
LIBCUBWT_GPU_ERROR;
}
static cudaError_t libcubwt_cuda_safe_call(const char * filename, int32_t line, cudaError_t result, cudaError_t status = cudaSuccess)
{
#if !defined(NDEBUG)
if (result != cudaSuccess)
{
fprintf(stderr, "%s(%d): libcubwt_cuda_safe_call failed %d: '%s'.\n", filename, line, result, cudaGetErrorString(result));
fflush(stderr);
}
#else
(void)(filename); (void)(line);
#endif
return result != cudaSuccess ? result : status;
}
template <typename T>
static __device__ __forceinline__ T libcubwt_warp_reduce_sum(T value)
{
#if CUDA_DEVICE_ARCH >= 800 && !defined(__CUDA__)
return __reduce_add_sync((uint32_t)-1, value);
#else
#pragma unroll
for (uint32_t mask = CUDA_WARP_THREADS / 2; mask > 0; mask >>= 1)
{
value = cub::Sum()(value, __shfl_xor_sync((uint32_t)-1, value, mask, CUDA_WARP_THREADS));
}
return value;
#endif
}
template <typename T>
static __device__ __forceinline__ T libcubwt_warp_reduce_max(T value)
{
#if CUDA_DEVICE_ARCH >= 800 && !defined(__CUDA__)
return __reduce_max_sync((uint32_t)-1, value);
#else
#pragma unroll
for (uint32_t mask = CUDA_WARP_THREADS / 2; mask > 0; mask >>= 1)
{
value = cub::Max()(value, __shfl_xor_sync((uint32_t)-1, value, mask, CUDA_WARP_THREADS));
}
return value;
#endif
}
template <typename T>
static __device__ __forceinline__ void libcubwt_delay_or_prevent_hoisting(T delay)
{
#if CUDA_DEVICE_ARCH >= 700
__nanosleep(delay);
#else
__threadfence_block(); (void)(delay);
#endif
}
template <int INPUT_BITS>
static __device__ __forceinline__ uint32_t libcubwt_match_any_sync(const uint32_t sync_mask, const uint32_t input)
{
uint32_t peers_mask = sync_mask;
#pragma unroll
for (uint32_t bit = 0; bit < INPUT_BITS; bit += 1)
{
uint32_t peers, bit_mask = 1 << bit;
asm("{\n"
" .reg .pred p;\n"
" and.b32 %0, %1, %2;\n"
" setp.ne.u32 p, %0, 0;\n"
" vote.ballot.sync.b32 %0, p, %3;\n"
" @!p not.b32 %0, %0;\n"
" }"
: "=r"(peers) : "r"(input), "r"(bit_mask), "r"(sync_mask));
peers_mask &= peers;
}
return peers_mask;
}
static __device__ __forceinline__ uint32_t libcubwt_xxhash32_b32(uint32_t data, uint32_t seed)
{
uint32_t x = (data * 0xc2b2ae3du) + seed + (uint32_t)sizeof(data) + 0x165667b1u;
x = (x << 17) | (x >> 15);
x *= 0x27d4eb2fu; x ^= x >> 15;
x *= 0x85ebca77u; x ^= x >> 13;
x *= 0xc2b2ae3du; x ^= x >> 16;
return x;
}
static __device__ __forceinline__ uint4 libcubwt_shift_left_b128(uint4 v, uint32_t shift)
{
if ((shift & 8) > 0) { v.w = v.y; v.z = v.x; v.y = 0 ; v.x = 0; }
if ((shift & 4) > 0) { v.w = v.z; v.z = v.y; v.y = v.x; v.x = 0; }
shift = (shift << 3) & 0x18u;
v.w = __funnelshift_l(v.z, v.w, shift);
v.z = __funnelshift_l(v.y, v.z, shift);
v.y = __funnelshift_l(v.x, v.y, shift);
v.x = __funnelshift_l(0x0, v.x, shift);
return v;
}
static __device__ __forceinline__ uint4 libcubwt_shift_right_b128(uint4 v, uint32_t shift)
{
if ((shift & 8) > 0) { v.x = v.z; v.y = v.w; v.z = 0 ; v.w = 0; }
if ((shift & 4) > 0) { v.x = v.y; v.y = v.z; v.z = v.w; v.w = 0; }
shift = (shift << 3) & 0x18u;
v.x = __funnelshift_r(v.x, v.y, shift);
v.y = __funnelshift_r(v.y, v.z, shift);
v.z = __funnelshift_r(v.z, v.w, shift);
v.w = __funnelshift_r(v.w, 0x0, shift);
return v;
}
__global__ __launch_bounds__(CUDA_BLOCK_THREADS, CUDA_SM_THREADS / CUDA_BLOCK_THREADS)
static void libcubwt_gather_values_uint32_kernel(const uint32_t * device_idx, const uint32_t * RESTRICT device_src, uint32_t * device_dst, uint32_t m)
{
const uint32_t block_index = blockIdx.x * CUDA_BLOCK_THREADS * 4;
device_idx += block_index; device_dst += block_index; m -= block_index;
if (m >= CUDA_BLOCK_THREADS * 4)
{
const uint4 indexes = *(uint4 *)(device_idx + threadIdx.x * 4);
*(uint4 *)(device_dst + threadIdx.x * 4) = make_uint4(
__ldg(device_src + indexes.x),
__ldg(device_src + indexes.y),
__ldg(device_src + indexes.z),
__ldg(device_src + indexes.w));
}
else
{
for (uint32_t thread_index = threadIdx.x; thread_index < m; thread_index += CUDA_BLOCK_THREADS)
{
device_dst[thread_index] = __ldg(device_src + device_idx[thread_index]);
}
}
}
__global__ __launch_bounds__(CUDA_BLOCK_THREADS, CUDA_SM_THREADS / CUDA_BLOCK_THREADS)
static void libcubwt_scatter_values_uint32_kernel(const uint32_t * RESTRICT device_idx, const uint32_t * RESTRICT device_src, uint32_t * RESTRICT device_dst, uint32_t m)
{
const uint32_t block_index = blockIdx.x * CUDA_BLOCK_THREADS * 4;
device_idx += block_index; device_src += block_index; m -= block_index;
if (m >= CUDA_BLOCK_THREADS * 4)
{
const uint4 indexes = __ldg((uint4 *)(device_idx + threadIdx.x * 4));
const uint4 values = __ldg((uint4 *)(device_src + threadIdx.x * 4));
device_dst[indexes.x] = values.x;
device_dst[indexes.y] = values.y;
device_dst[indexes.z] = values.z;
device_dst[indexes.w] = values.w;
}
else
{
for (uint32_t thread_index = threadIdx.x; thread_index < m; thread_index += CUDA_BLOCK_THREADS)
{
device_dst[__ldg(device_idx + thread_index)] = __ldg(device_src + thread_index);
}
}
}
__global__ __launch_bounds__(CUDA_BLOCK_THREADS, CUDA_SM_THREADS / CUDA_BLOCK_THREADS)
static void libcubwt_permute_block_values_uint32_kernel(const uint32_t * RESTRICT device_idx, const uint32_t * RESTRICT device_src, uint32_t * RESTRICT device_dst, uint32_t n)
{
__shared__ __align__(32) uint32_t cache[16 * CUDA_BLOCK_THREADS];
const uint32_t block_index = blockIdx.x * CUDA_BLOCK_THREADS * 16;
device_idx += block_index; device_src += block_index; device_dst += block_index; n -= block_index;
if (n >= CUDA_BLOCK_THREADS * 16)
{
{
const uint32_t * RESTRICT thread_idx = device_idx + threadIdx.x * 4;
const uint32_t * RESTRICT thread_src = device_src + threadIdx.x * 4;
uint32_t * RESTRICT thread_cache = cache - block_index;
#pragma unroll
for (uint32_t round = 0; round < 4; round += 1)
{
const uint4 indexes = __ldg((uint4 *)(thread_idx));
const uint4 values = __ldg((uint4 *)(thread_src));
thread_cache[indexes.x] = values.x;
thread_cache[indexes.y] = values.y;
thread_cache[indexes.z] = values.z;
thread_cache[indexes.w] = values.w;
thread_idx += 4 * CUDA_BLOCK_THREADS; thread_src += 4 * CUDA_BLOCK_THREADS;
}
}
__syncthreads();
{
const uint32_t * RESTRICT thread_cache = cache + threadIdx.x * 4;
uint32_t * RESTRICT thread_dst = device_dst + threadIdx.x * 4;
#pragma unroll
for (uint32_t round = 0; round < 4; round += 1)
{
*(uint4 *)(thread_dst) = *(uint4 *)(thread_cache);
thread_cache += 4 * CUDA_BLOCK_THREADS; thread_dst += 4 * CUDA_BLOCK_THREADS;
}
}
}
else
{
{
uint32_t * RESTRICT thread_cache = cache - block_index;
for (uint32_t thread_index = threadIdx.x; thread_index < n; thread_index += CUDA_BLOCK_THREADS)
{
thread_cache[__ldg(device_idx + thread_index)] = __ldg(device_src + thread_index);
}
}
__syncthreads();
{
for (uint32_t thread_index = threadIdx.x; thread_index < n; thread_index += CUDA_BLOCK_THREADS)
{
device_dst[thread_index] = cache[thread_index];
}
}
}
}
__global__ __launch_bounds__(CUDA_BLOCK_THREADS, CUDA_SM_THREADS / CUDA_BLOCK_THREADS)
static void libcubwt_scatter_values_uint64_kernel(const uint32_t * RESTRICT device_idx, const uint64_t * RESTRICT device_src, uint64_t * RESTRICT device_dst, uint32_t m)
{
const uint32_t block_index = blockIdx.x * CUDA_BLOCK_THREADS * 2;
device_idx += block_index; device_src += block_index; m -= block_index;
if (m >= CUDA_BLOCK_THREADS * 2)
{
const uint2 indexes = __ldg((uint2 *)(device_idx + threadIdx.x * 2));
const ulonglong2 values = __ldg((ulonglong2 *)(device_src + threadIdx.x * 2));
device_dst[indexes.x] = values.x;
device_dst[indexes.y] = values.y;
}
else
{
for (uint32_t thread_index = threadIdx.x; thread_index < m; thread_index += CUDA_BLOCK_THREADS)
{
device_dst[__ldg(device_idx + thread_index)] = __ldg(device_src + thread_index);
}
}
}
static cudaError_t libcubwt_gather_scatter_values_uint32(LIBCUBWT_DEVICE_STORAGE * storage, uint32_t * device_src_idx, uint32_t * device_src, uint32_t * device_dst_idx, uint32_t * device_dst, int64_t m, int64_t n, uint32_t * device_temp1, uint32_t * device_temp2)
{
cudaError_t status = cudaSuccess;
cub::DoubleBuffer<uint32_t> db_src_index_value(device_src_idx, device_temp1);
cub::DoubleBuffer<uint32_t> db_dst_index(device_dst_idx, device_temp2);
int32_t sort_end_bit = 0; while ((n - 1) >= ((int64_t)1 << sort_end_bit)) { sort_end_bit += 1; }
int32_t sort_aligned_bits = (sort_end_bit > storage->device_L2_cache_bits - 2) ? (sort_end_bit - storage->device_L2_cache_bits + 2 + 7) & (-8) : 0;
int32_t sort_start_bit = std::max(0, sort_end_bit - sort_aligned_bits);
if (sort_start_bit < sort_end_bit)
{
status = libcubwt_cuda_safe_call(__FILE__, __LINE__, cub::DeviceRadixSort::SortPairs(
storage->device_rsort_temp_storage, storage->device_rsort_temp_storage_size,
db_src_index_value, db_dst_index,
(uint32_t)m,
sort_start_bit, sort_end_bit,
storage->cuda_stream));
}
if (status == cudaSuccess)
{
int64_t n_gather_scatter_blocks = (m + storage->cuda_block_threads * 4 - 1) / (storage->cuda_block_threads * 4);
libcubwt_gather_values_uint32_kernel<<<(uint32_t)n_gather_scatter_blocks, storage->cuda_block_threads, 0, storage->cuda_stream>>>(db_src_index_value.Current(), device_src, db_src_index_value.Current(), (uint32_t)m);
if (sort_start_bit < sort_end_bit)
{
status = libcubwt_cuda_safe_call(__FILE__, __LINE__, cub::DeviceRadixSort::SortPairs(
storage->device_rsort_temp_storage, storage->device_rsort_temp_storage_size,
db_dst_index, db_src_index_value,
(uint32_t)m,
sort_start_bit, sort_end_bit,
storage->cuda_stream));
}
if (status == cudaSuccess)
{
libcubwt_scatter_values_uint32_kernel<<<(uint32_t)n_gather_scatter_blocks, storage->cuda_block_threads, 0, storage->cuda_stream>>>(db_dst_index.Current(), db_src_index_value.Current(), device_dst, (uint32_t)m);
}
}
return status;
}
static cudaError_t libcubwt_scatter_values_uint32(LIBCUBWT_DEVICE_STORAGE * storage, uint32_t * device_idx, uint32_t * device_src, uint32_t * device_dst, int64_t m, int64_t n, uint32_t * device_temp1, uint32_t * device_temp2)
{
cudaError_t status = cudaSuccess;
cub::DoubleBuffer<uint32_t> db_index(device_idx, device_temp1);
cub::DoubleBuffer<uint32_t> db_value(device_src, device_temp2);
int32_t sort_end_bit = 0; while ((n - 1) >= ((int64_t)1 << sort_end_bit)) { sort_end_bit += 1; }
int32_t sort_aligned_bits = (sort_end_bit > storage->device_L2_cache_bits - 2) ? (sort_end_bit - storage->device_L2_cache_bits + 2 + 7) & (-8) : 0;
int32_t sort_start_bit = std::max(0, sort_end_bit - sort_aligned_bits);
if (sort_start_bit < sort_end_bit)
{
status = libcubwt_cuda_safe_call(__FILE__, __LINE__, cub::DeviceRadixSort::SortPairs(
storage->device_rsort_temp_storage, storage->device_rsort_temp_storage_size,
db_index, db_value,
(uint32_t)m,
sort_start_bit, sort_end_bit,
storage->cuda_stream));
}
if (status == cudaSuccess)
{
int64_t n_scatter_blocks = (m + storage->cuda_block_threads * 4 - 1) / (storage->cuda_block_threads * 4);
libcubwt_scatter_values_uint32_kernel<<<(uint32_t)n_scatter_blocks, storage->cuda_block_threads, 0, storage->cuda_stream>>>(db_index.Current(), db_value.Current(), device_dst, (uint32_t)m);
}
return status;
}
static cudaError_t libcubwt_permute_values_uint32(LIBCUBWT_DEVICE_STORAGE * storage, uint32_t * device_idx, uint32_t * device_src, uint32_t * device_dst, int64_t n, uint32_t * device_temp1, uint32_t * device_temp2)
{
cudaError_t status = cudaSuccess;
cub::DoubleBuffer<uint32_t> db_index(device_idx, device_temp1);
cub::DoubleBuffer<uint32_t> db_value(device_src, device_temp2);
int32_t sort_end_bit = 0; while ((n - 1) >= ((int64_t)1 << sort_end_bit)) { sort_end_bit += 1; }
int32_t sort_aligned_bits = (sort_end_bit > storage->device_L2_cache_bits - 2) ? (sort_end_bit - storage->device_L2_cache_bits + 2 + 7) & (-8) : 0;
int32_t sort_start_bit = std::max(0, sort_end_bit - sort_aligned_bits);
if (sort_start_bit < sort_end_bit)
{
status = libcubwt_cuda_safe_call(__FILE__, __LINE__, cub::DeviceRadixSort::SortPairs(
storage->device_rsort_temp_storage, storage->device_rsort_temp_storage_size,
db_index, db_value,
(uint32_t)n,
sort_start_bit, sort_end_bit,
storage->cuda_stream));
}
if (status == cudaSuccess)
{
if (((storage->cuda_block_threads * 16) % ((int64_t)1 << sort_start_bit)) == 0)
{
int64_t n_permute_blocks = (n + storage->cuda_block_threads * 16 - 1) / (storage->cuda_block_threads * 16);
libcubwt_permute_block_values_uint32_kernel<<<(uint32_t)n_permute_blocks, storage->cuda_block_threads, 0, storage->cuda_stream>>>(db_index.Current(), db_value.Current(), device_dst, (uint32_t)n);
}
else
{
int64_t n_scatter_blocks = (n + storage->cuda_block_threads * 4 - 1) / (storage->cuda_block_threads * 4);
libcubwt_scatter_values_uint32_kernel<<<(uint32_t)n_scatter_blocks, storage->cuda_block_threads, 0, storage->cuda_stream>>>(db_index.Current(), db_value.Current(), device_dst, (uint32_t)n);
}
}
return status;
}
static cudaError_t libcubwt_scatter_values_uint64(LIBCUBWT_DEVICE_STORAGE * storage, cub::DoubleBuffer<uint32_t> & db_index, cub::DoubleBuffer<uint64_t> & db_value, int64_t m, int64_t n, int64_t k = 0)
{
cudaError_t status = cudaSuccess;
int32_t sort_end_bit = 0; while ((n - 1) >= ((int64_t)1 << sort_end_bit)) { sort_end_bit += 1; }
int32_t sort_aligned_bits = (sort_end_bit > storage->device_L2_cache_bits - 3) ? (sort_end_bit - storage->device_L2_cache_bits + 3 + 7) & (-8) : 0;
int32_t sort_start_bit = std::max(0, sort_end_bit - sort_aligned_bits);
if (sort_start_bit < sort_end_bit)
{
status = libcubwt_cuda_safe_call(__FILE__, __LINE__, cub::DeviceRadixSort::SortPairs(
storage->device_rsort_temp_storage, storage->device_rsort_temp_storage_size,
db_index, db_value,
(uint32_t)m,
sort_start_bit, sort_end_bit,
storage->cuda_stream));
}
if (status == cudaSuccess)
{
int64_t n_scatter_blocks = (m + storage->cuda_block_threads * 2 - 1) / (storage->cuda_block_threads * 2);
libcubwt_scatter_values_uint64_kernel<<<(uint32_t)n_scatter_blocks, storage->cuda_block_threads, 0, storage->cuda_stream>>>(db_index.Current(), db_value.Current(), db_value.Alternate() - k, (uint32_t)m);
db_index.selector ^= 1;
db_value.selector ^= 1;
}
return status;
}
template <bool extra_sentinel_bits>
__global__ __launch_bounds__(CUDA_BLOCK_THREADS, CUDA_SM_THREADS / CUDA_BLOCK_THREADS)
static void libcubwt_initialize_device_arrays_kernel(const uint8_t * RESTRICT device_T, uint32_t * RESTRICT device_SA, uint64_t * RESTRICT device_keys)
{
__shared__ __align__(32) uint4 prefixes[4 * CUDA_BLOCK_THREADS];
{
device_T += blockIdx.x * CUDA_BLOCK_THREADS * 12 + threadIdx.x * 16;
if (threadIdx.x < (12 * CUDA_BLOCK_THREADS + 8 + 15) / 16) { prefixes[threadIdx.x] = __ldg((uint4 *)device_T); }
__syncthreads();
}
{
uint32_t * RESTRICT thread_cache = ((uint32_t *)prefixes) + threadIdx.x * 3;
uint4 * RESTRICT thread_prefixes = ((uint4 * )prefixes) + threadIdx.x * 4;
const uint32_t b0 = thread_cache[0];
const uint32_t b1 = thread_cache[1];
const uint32_t b2 = thread_cache[2];
const uint32_t b3 = thread_cache[3];
const uint32_t b4 = thread_cache[4];
__syncthreads();
thread_prefixes[0] = make_uint4
(
__byte_perm(b1, b2, 0x1234) | (extra_sentinel_bits ? (uint32_t)7 : (uint32_t)1), __byte_perm(b0, b1, 0x1234),
__byte_perm(b1, b2, 0x2345) | (extra_sentinel_bits ? (uint32_t)7 : (uint32_t)1), __byte_perm(b0, b1, 0x2345)
);
thread_prefixes[1] = make_uint4
(
__byte_perm(b2, b3, 0x0123) | (extra_sentinel_bits ? (uint32_t)7 : (uint32_t)1), __byte_perm(b1, b2, 0x0123),
__byte_perm(b2, b3, 0x1234) | (extra_sentinel_bits ? (uint32_t)7 : (uint32_t)1), __byte_perm(b1, b2, 0x1234)
);
thread_prefixes[2] = make_uint4
(
__byte_perm(b2, b3, 0x3456) | (extra_sentinel_bits ? (uint32_t)7 : (uint32_t)1), __byte_perm(b1, b2, 0x3456),
__byte_perm(b3, b4, 0x0123) | (extra_sentinel_bits ? (uint32_t)7 : (uint32_t)1), __byte_perm(b2, b3, 0x0123)
);
thread_prefixes[3] = make_uint4
(
__byte_perm(b3, b4, 0x2345) | (extra_sentinel_bits ? (uint32_t)7 : (uint32_t)1), __byte_perm(b2, b3, 0x2345),
__byte_perm(b3, b4, 0x3456) | (extra_sentinel_bits ? (uint32_t)7 : (uint32_t)1), __byte_perm(b2, b3, 0x3456)
);
__syncwarp();
}
{
const uint32_t block_index = blockIdx.x * CUDA_BLOCK_THREADS * 8;
{
uint32_t thread_index = block_index + threadIdx.x * 4; device_SA += thread_index;
((uint4 *)device_SA)[0] = make_uint4(thread_index + 0, thread_index + 1, thread_index + 2, thread_index + 3);
thread_index += CUDA_BLOCK_THREADS * 4; device_SA += CUDA_BLOCK_THREADS * 4;
((uint4 *)device_SA)[0] = make_uint4(thread_index + 0, thread_index + 1, thread_index + 2, thread_index + 3);
}
{
device_keys += block_index;
uint4 * RESTRICT thread_prefixes = (uint4 *)prefixes + ((threadIdx.x / CUDA_WARP_THREADS) * CUDA_WARP_THREADS * 4) + (threadIdx.x % CUDA_WARP_THREADS);
uint4 * RESTRICT thread_keys = (uint4 *)device_keys + ((threadIdx.x / CUDA_WARP_THREADS) * CUDA_WARP_THREADS * 4) + (threadIdx.x % CUDA_WARP_THREADS);
thread_keys[0] = thread_prefixes[0]; thread_keys += CUDA_WARP_THREADS; thread_prefixes += CUDA_WARP_THREADS;
thread_keys[0] = thread_prefixes[0]; thread_keys += CUDA_WARP_THREADS; thread_prefixes += CUDA_WARP_THREADS;
thread_keys[0] = thread_prefixes[0]; thread_keys += CUDA_WARP_THREADS; thread_prefixes += CUDA_WARP_THREADS;
thread_keys[0] = thread_prefixes[0];
}
}
}
__global__ __launch_bounds__(CUDA_BLOCK_THREADS, 1)
static void libcubwt_set_sentinel_values_kernel(uint8_t * RESTRICT device_T_end, uint64_t * RESTRICT device_keys_end, uint64_t k0, uint64_t k1, uint64_t k2, uint64_t k3, uint64_t k4, uint64_t k5, uint64_t k6, uint64_t k7)
{
device_T_end[0] = 0;
device_T_end[1] = 0;
device_T_end[2] = 0;
device_keys_end[-8] = k0;
device_keys_end[-7] = k1;
device_keys_end[-6] = k2;
device_keys_end[-5] = k3;
device_keys_end[-4] = k4;
device_keys_end[-3] = k5;
device_keys_end[-2] = k6;
device_keys_end[-1] = k7;
}
static cudaError_t libcubwt_initialize_device_arrays(LIBCUBWT_DEVICE_STORAGE * storage, const uint8_t * T, int64_t reduced_n, int64_t expanded_n, int64_t input_n)
{
cudaError_t status = cudaSuccess;
if ((status = libcubwt_cuda_safe_call(__FILE__, __LINE__, cudaMemcpyAsync(storage->device_T, T, (size_t)input_n, cudaMemcpyHostToDevice, storage->cuda_stream))) == cudaSuccess)
{
int64_t n_initialize_blocks = 1 + (expanded_n / (storage->cuda_block_threads * 12));
bool extra_sentinel_bits = (expanded_n - input_n >= 2) || (T[input_n - 1] == 0);
if (extra_sentinel_bits)
{
libcubwt_initialize_device_arrays_kernel<true><<<(uint32_t)n_initialize_blocks, storage->cuda_block_threads, 0, storage->cuda_stream>>>(storage->device_T, storage->device_SA, storage->device_keys_temp_keys);
}
else
{
libcubwt_initialize_device_arrays_kernel<false><<<(uint32_t)n_initialize_blocks, storage->cuda_block_threads, 0, storage->cuda_stream>>>(storage->device_T, storage->device_SA, storage->device_keys_temp_keys);
}
{
uint64_t c0 = (expanded_n - 11 < input_n) ? T[expanded_n - 11] : (uint64_t)0;
uint64_t c1 = (expanded_n - 10 < input_n) ? T[expanded_n - 10] : (uint64_t)0;
uint64_t c2 = (expanded_n - 9 < input_n) ? T[expanded_n - 9] : (uint64_t)0;
uint64_t c3 = (expanded_n - 8 < input_n) ? T[expanded_n - 8] : (uint64_t)0;
uint64_t c4 = (expanded_n - 7 < input_n) ? T[expanded_n - 7] : (uint64_t)0;
uint64_t c5 = (expanded_n - 6 < input_n) ? T[expanded_n - 6] : (uint64_t)0;
uint64_t c6 = (expanded_n - 5 < input_n) ? T[expanded_n - 5] : (uint64_t)0;
uint64_t c7 = (expanded_n - 4 < input_n) ? T[expanded_n - 4] : (uint64_t)0;
uint64_t c8 = (expanded_n - 3 < input_n) ? T[expanded_n - 3] : (uint64_t)0;
uint64_t c9 = (expanded_n - 2 < input_n) ? T[expanded_n - 2] : (uint64_t)0;
uint64_t ca = (expanded_n - 1 < input_n) ? T[expanded_n - 1] : (uint64_t)0;
uint64_t k0 = (c0 << 56) | (c1 << 48) | (c2 << 40) | (c3 << 32) | (c4 << 24) | (c5 << 16) | (c6 << 8) | (c7 << 0) | (extra_sentinel_bits ? 7 : 1);
uint64_t k1 = (c1 << 56) | (c2 << 48) | (c3 << 40) | (c4 << 32) | (c5 << 24) | (c6 << 16) | (c7 << 8) | (c8 << 0) | (extra_sentinel_bits ? 7 : 1);
uint64_t k2 = (c3 << 56) | (c4 << 48) | (c5 << 40) | (c6 << 32) | (c7 << 24) | (c8 << 16) | (c9 << 8) | (ca << 0) | (extra_sentinel_bits ? 7 : 0);
uint64_t k3 = (c4 << 56) | (c5 << 48) | (c6 << 40) | (c7 << 32) | (c8 << 24) | (c9 << 16) | (ca << 8) | (extra_sentinel_bits ? 6 : 0);
uint64_t k4 = (c6 << 56) | (c7 << 48) | (c8 << 40) | (c9 << 32) | (ca << 24) | (extra_sentinel_bits ? 4 : 0);
uint64_t k5 = (c7 << 56) | (c8 << 48) | (c9 << 40) | (ca << 32) | (extra_sentinel_bits ? 3 : 0);
uint64_t k6 = (c9 << 56) | (ca << 48) | (extra_sentinel_bits ? 1 : 0);
uint64_t k7 = (ca << 56);
libcubwt_set_sentinel_values_kernel<<<1, 1, 0, storage->cuda_stream>>>(storage->device_T + input_n, storage->device_keys_temp_keys + reduced_n, k0, k1, k2, k3, k4, k5, k6, k7);
}
storage->num_unsorted_segments = (uint32_t)1;
storage->num_unsorted_suffixes = (uint32_t)reduced_n;
}
return status;
}
static cudaError_t libcubwt_sort_suffixes_by_prefix(LIBCUBWT_DEVICE_STORAGE * storage, int64_t n)
{
cub::DoubleBuffer<uint64_t> db_keys(storage->device_keys_temp_keys, storage->device_offsets_ISA);
cub::DoubleBuffer<uint32_t> db_SA(storage->device_SA, storage->device_temp_SA);
cudaError_t status = libcubwt_cuda_safe_call(__FILE__, __LINE__, cub::DeviceRadixSort::SortPairs(
storage->device_rsort_temp_storage, storage->device_rsort_temp_storage_size,
db_keys, db_SA,
(uint32_t)n,
0, 64,
storage->cuda_stream));
if (db_keys.selector)
{
std::swap(storage->device_keys_temp_keys, storage->device_offsets_ISA);
std::swap(storage->device_keys, storage->device_offsets);
std::swap(storage->device_temp_keys, storage->device_ISA);
}
if (db_SA.selector)
{
std::swap(storage->device_SA, storage->device_temp_SA);
}
return status;
}
__global__ __launch_bounds__(CUDA_BLOCK_THREADS, CUDA_SM_THREADS / CUDA_BLOCK_THREADS)
static void libcubwt_rank_and_segment_suffixes_initialization_kernel(uint32_t * RESTRICT device_SA, uint64_t * RESTRICT device_keys, uint8_t * RESTRICT device_heads, uint4 * RESTRICT device_descriptors_large, uint2 * RESTRICT device_descriptors_small, uint32_t n)
{
const uint32_t thread_index = blockIdx.x * CUDA_BLOCK_THREADS + threadIdx.x;
device_descriptors_large += thread_index;
device_descriptors_small += thread_index;
device_descriptors_large[0] = make_uint4(0, 0, 0, 0);
device_descriptors_small[0] = make_uint2(0, 0);
if (blockIdx.x == 0)
{
if (threadIdx.x < CUDA_WARP_THREADS)
{
device_descriptors_large[-CUDA_WARP_THREADS] = make_uint4((uint32_t)-1, 0, 0, 0);
device_descriptors_small[-CUDA_WARP_THREADS] = make_uint2((uint32_t)-1, 0);
}
{
uint64_t key = (threadIdx.x % 2 == 0) ? 0 : (uint64_t)-1;
device_SA += threadIdx.x; device_keys += threadIdx.x; device_heads += threadIdx.x;
if (threadIdx.x < 2)
{
device_keys [-2] = key;
device_heads[-2] = 1;
}
device_SA += n; device_keys += n; device_heads += n;
device_SA [0 * CUDA_BLOCK_THREADS] = n + threadIdx.x + 0 * CUDA_BLOCK_THREADS;
device_SA [1 * CUDA_BLOCK_THREADS] = n + threadIdx.x + 1 * CUDA_BLOCK_THREADS;
device_SA [2 * CUDA_BLOCK_THREADS] = n + threadIdx.x + 2 * CUDA_BLOCK_THREADS;
device_SA [3 * CUDA_BLOCK_THREADS] = n + threadIdx.x + 3 * CUDA_BLOCK_THREADS;
device_keys [0 * CUDA_BLOCK_THREADS] = key;
device_keys [1 * CUDA_BLOCK_THREADS] = key;
device_keys [2 * CUDA_BLOCK_THREADS] = key;
device_keys [3 * CUDA_BLOCK_THREADS] = key;
device_heads[0 * CUDA_BLOCK_THREADS] = 1;
device_heads[1 * CUDA_BLOCK_THREADS] = 1;
device_heads[2 * CUDA_BLOCK_THREADS] = 1;
device_heads[3 * CUDA_BLOCK_THREADS] = 1;
}
}
}
template <bool scatter_ranks_directly>
__global__ __launch_bounds__(CUDA_BLOCK_THREADS, CUDA_SM_THREADS / CUDA_BLOCK_THREADS)
static void libcubwt_rank_and_segment_suffixes_initiatory_kernel(
const uint32_t * RESTRICT device_SA,
const uint64_t * RESTRICT device_keys,
uint8_t * RESTRICT device_heads,
uint32_t * RESTRICT device_ISA,
uint32_t * RESTRICT device_offsets_begin,
uint32_t * RESTRICT device_offsets_end,
uint4 * RESTRICT device_descriptors
)
{
__shared__ __align__(32) uint2 warp_state[1 + CUDA_WARP_THREADS];
uint32_t thread_exclusive_suffix_rank;
uint32_t thread_suffix_rank[4];
uint32_t thread_exclusive_segment_index;
uint32_t thread_segment_index[4];
{
__shared__ __align__(32) ulonglong2 cache[1 + 2 * CUDA_BLOCK_THREADS];
{
device_keys += blockIdx.x * CUDA_BLOCK_THREADS * 4 + threadIdx.x * 2;
if (threadIdx.x == 0) { cache[0] = __ldg((ulonglong2 *)(device_keys - 2)); }
cache[1 + threadIdx.x + 0 * CUDA_BLOCK_THREADS] = __ldg((ulonglong2 *)(device_keys + 0 * CUDA_BLOCK_THREADS));
cache[1 + threadIdx.x + 1 * CUDA_BLOCK_THREADS] = __ldg((ulonglong2 *)(device_keys + 2 * CUDA_BLOCK_THREADS));
}
__syncthreads();
{
const uint32_t block_index = blockIdx.x * CUDA_BLOCK_THREADS * 4;
const uint32_t thread_index = block_index + threadIdx.x * 4;
ulonglong2 key_a = cache[2 * threadIdx.x + 0];
ulonglong2 key_b = cache[2 * threadIdx.x + 1];
ulonglong2 key_c = cache[2 * threadIdx.x + 2];
uchar4 thread_new_heads = make_uchar4(
(key_a.y != key_b.x) ? (uint8_t)1 : (uint8_t)0,
(key_b.x != key_b.y) ? (uint8_t)1 : (uint8_t)0,
(key_b.y != key_c.x) ? (uint8_t)1 : (uint8_t)0,
(key_c.x != key_c.y) ? (uint8_t)1 : (uint8_t)0);
*(uchar4 *)(device_heads + thread_index) = thread_new_heads;
thread_suffix_rank[0] = (thread_new_heads.x != 0) ? (thread_index + 0) : 0;
thread_suffix_rank[1] = (thread_new_heads.y != 0) ? (thread_index + 1) : thread_suffix_rank[0];
thread_suffix_rank[2] = (thread_new_heads.z != 0) ? (thread_index + 2) : thread_suffix_rank[1];
thread_suffix_rank[3] = (thread_new_heads.w != 0) ? (thread_index + 3) : thread_suffix_rank[2];
thread_segment_index[0] = ((thread_new_heads.x != 0) && (key_a.x == key_a.y));
thread_segment_index[1] = thread_segment_index[0] + ((thread_new_heads.y != 0) && (thread_new_heads.x == 0));
thread_segment_index[2] = thread_segment_index[1] + ((thread_new_heads.z != 0) && (thread_new_heads.y == 0));
thread_segment_index[3] = thread_segment_index[2] + ((thread_new_heads.w != 0) && (thread_new_heads.z == 0));
}
}
{
uint32_t thread_inclusive_suffix_rank;
uint32_t thread_inclusive_segment_index;
typedef cub::WarpScan<uint32_t> WarpScan;
__shared__ typename WarpScan::TempStorage warp_scan_storage[CUDA_WARP_THREADS];
WarpScan(warp_scan_storage[threadIdx.x / CUDA_WARP_THREADS]).Scan(thread_suffix_rank[3] , thread_inclusive_suffix_rank , thread_exclusive_suffix_rank , (uint32_t)0, cub::Max());
WarpScan(warp_scan_storage[threadIdx.x / CUDA_WARP_THREADS]).Scan(thread_segment_index[3], thread_inclusive_segment_index, thread_exclusive_segment_index, (uint32_t)0, cub::Sum());
if ((threadIdx.x % CUDA_WARP_THREADS) == (CUDA_WARP_THREADS - 1))
{
warp_state[threadIdx.x / CUDA_WARP_THREADS] = make_uint2(thread_inclusive_suffix_rank, thread_inclusive_segment_index);
}
__syncthreads();
}
{
if (threadIdx.x < CUDA_WARP_THREADS)
{
uint32_t block_exclusive_suffix_rank = 0;
uint32_t block_exclusive_segment_index = 0;
uint32_t warp_inclusive_suffix_rank;
uint32_t warp_inclusive_segment_index;
{
typedef cub::WarpScan<uint32_t> WarpScan;
__shared__ typename WarpScan::TempStorage warp_scan_storage;
uint2 warp_inclusive_state = warp_state[threadIdx.x];
WarpScan(warp_scan_storage).InclusiveScan(warp_inclusive_state.x, warp_inclusive_suffix_rank , cub::Max());
WarpScan(warp_scan_storage).InclusiveScan(warp_inclusive_state.y, warp_inclusive_segment_index, cub::Sum());
}
{
const uint32_t descriptor_status_aggregate_not_ready = 0;
const uint32_t descriptor_status_partial_aggregate_ready = 1;
const uint32_t descriptor_status_full_aggregate_ready = 4;
if (threadIdx.x == ((CUDA_BLOCK_THREADS / CUDA_WARP_THREADS) - 1))
{
cub::ThreadStore<cub::STORE_CG>(device_descriptors + blockIdx.x, make_uint4(descriptor_status_partial_aggregate_ready, 0, warp_inclusive_suffix_rank, warp_inclusive_segment_index));
}
{
uint4 * RESTRICT descriptors_lookback = device_descriptors + blockIdx.x + threadIdx.x;
int32_t full_aggregate_lane, delay = 8;
do
{
descriptors_lookback -= CUDA_WARP_THREADS;
uint4 block_descriptor;
do
{
libcubwt_delay_or_prevent_hoisting(delay <<= 1);
block_descriptor = cub::ThreadLoad<cub::LOAD_CG>(descriptors_lookback);
} while (__any_sync((uint32_t)-1, block_descriptor.x == descriptor_status_aggregate_not_ready));
delay = 0;
{
full_aggregate_lane = 31 - __clz((int32_t)__ballot_sync((uint32_t)-1, block_descriptor.x != descriptor_status_partial_aggregate_ready));
block_descriptor.z = (((int32_t)threadIdx.x) >= full_aggregate_lane) ? block_descriptor.z : 0;
block_descriptor.w = (((int32_t)threadIdx.x) >= full_aggregate_lane) ? block_descriptor.w : 0;
}
{
block_exclusive_suffix_rank = cub::Max()(block_exclusive_suffix_rank , libcubwt_warp_reduce_max(block_descriptor.z));
block_exclusive_segment_index = cub::Sum()(block_exclusive_segment_index, libcubwt_warp_reduce_sum(block_descriptor.w));
}
} while (full_aggregate_lane == -1);
warp_inclusive_suffix_rank = cub::Max()(warp_inclusive_suffix_rank , block_exclusive_suffix_rank );
warp_inclusive_segment_index = cub::Sum()(warp_inclusive_segment_index, block_exclusive_segment_index);
}
if (threadIdx.x == ((CUDA_BLOCK_THREADS / CUDA_WARP_THREADS) - 1))
{
cub::ThreadStore<cub::STORE_CG>(device_descriptors + blockIdx.x, make_uint4(descriptor_status_full_aggregate_ready, 0, warp_inclusive_suffix_rank, warp_inclusive_segment_index));
}
}
{
if (threadIdx.x == 0)
{
warp_state[0] = make_uint2(block_exclusive_suffix_rank, block_exclusive_segment_index);
}
warp_state[1 + threadIdx.x] = make_uint2(warp_inclusive_suffix_rank, warp_inclusive_segment_index);
}
}
__syncthreads();
}
{
uint2 warp_exclusive_state = warp_state[threadIdx.x / CUDA_WARP_THREADS];
thread_exclusive_suffix_rank = cub::Max()(thread_exclusive_suffix_rank , warp_exclusive_state.x);
thread_exclusive_segment_index = cub::Sum()(thread_exclusive_segment_index, warp_exclusive_state.y);
thread_suffix_rank[0] = cub::Max()(thread_suffix_rank[0], thread_exclusive_suffix_rank);
thread_suffix_rank[1] = cub::Max()(thread_suffix_rank[1], thread_exclusive_suffix_rank);
thread_suffix_rank[2] = cub::Max()(thread_suffix_rank[2], thread_exclusive_suffix_rank);
thread_suffix_rank[3] = cub::Max()(thread_suffix_rank[3], thread_exclusive_suffix_rank);
thread_segment_index[0] = cub::Sum()(thread_segment_index[0], thread_exclusive_segment_index);
thread_segment_index[1] = cub::Sum()(thread_segment_index[1], thread_exclusive_segment_index);
thread_segment_index[2] = cub::Sum()(thread_segment_index[2], thread_exclusive_segment_index);
thread_segment_index[3] = cub::Sum()(thread_segment_index[3], thread_exclusive_segment_index);
const uint32_t thread_index = blockIdx.x * CUDA_BLOCK_THREADS * 4 + threadIdx.x * 4;
if (thread_exclusive_segment_index != thread_segment_index[0]) { device_offsets_begin[thread_segment_index[0]] = thread_exclusive_suffix_rank; device_offsets_end[thread_segment_index[0]] = thread_index + 0; }
if (thread_segment_index[0] != thread_segment_index[1]) { device_offsets_begin[thread_segment_index[1]] = thread_suffix_rank[0]; device_offsets_end[thread_segment_index[1]] = thread_index + 1; }
if (thread_segment_index[1] != thread_segment_index[2]) { device_offsets_begin[thread_segment_index[2]] = thread_suffix_rank[1]; device_offsets_end[thread_segment_index[2]] = thread_index + 2; }
if (thread_segment_index[2] != thread_segment_index[3]) { device_offsets_begin[thread_segment_index[3]] = thread_suffix_rank[2]; device_offsets_end[thread_segment_index[3]] = thread_index + 3; }
if (scatter_ranks_directly)
{
const uint4 indexes = __ldg((uint4 *)(device_SA + thread_index));
device_ISA[indexes.x] = thread_suffix_rank[0];
device_ISA[indexes.y] = thread_suffix_rank[1];
device_ISA[indexes.z] = thread_suffix_rank[2];
device_ISA[indexes.w] = thread_suffix_rank[3];
}
else
{
*(uint4 *)(device_ISA + thread_index) = make_uint4(thread_suffix_rank[0], thread_suffix_rank[1], thread_suffix_rank[2], thread_suffix_rank[3]);
}
}
}
template <bool alternate_block_descriptor_statuses, bool scatter_ranks_directly>
__global__ __launch_bounds__(CUDA_BLOCK_THREADS, CUDA_SM_THREADS / CUDA_BLOCK_THREADS)
static void libcubwt_rank_and_segment_suffixes_incremental_kernel(
const uint32_t * RESTRICT device_SA,
const uint32_t * RESTRICT device_keys,
uint8_t * RESTRICT device_heads,
uint32_t * RESTRICT device_out_SA,
uint32_t * RESTRICT device_out_ISA,
uint32_t * RESTRICT device_offsets_begin,
uint32_t * RESTRICT device_offsets_end,
uint4 * RESTRICT device_descriptors,
const uint4 * RESTRICT device_descriptors_copy
)
{
__shared__ __align__(32) uint4 warp_state1[1 + CUDA_WARP_THREADS];
__shared__ __align__(32) uint32_t warp_state2[1 + CUDA_WARP_THREADS];