-
Notifications
You must be signed in to change notification settings - Fork 0
/
barracuda.cu
4185 lines (3564 loc) · 134 KB
/
barracuda.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
/*
Barracuda - A Short Sequence Aligner for NVIDIA Graphics Cards
Module: barracuda.cu - CUDA alignment and samse kernels
Copyright (C) 2012, University of Cambridge Metabolic Research Labs.
Contributers: Petr Klus, Dag Lyberg, Simon Lam and Brian Lam
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 3
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
This program is based on a modified version of BWA
File Creation date: 2012.6.8
*/
#define PACKAGE_VERSION "0.6.3 beta"
#include <stdio.h>
#include <unistd.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include <stdint.h>
#include "bwtaln.h"
#include "bwtgap.h"
#include "utils.h"
#include "barracuda.h"
#include "barracuda.cuh"
///////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
// Performance switches
#define BWT_2_OCC_ENABLE 0 // enable looking up of k and l in the same time for counting character occurrence (slower, so disable by default)
#define BWT_TABLE_LOOKUP_ENABLE 1 // use lookup table when instead of counting character occurrence, (faster so enable by default)
//The followings are settings for memory allocations and memory requirements
#define MIN_MEM_REQUIREMENT 768 // minimal global memory requirement in (MiB). Currently at 768MB
#define CUDA_TESLA 1350 // enlarged workspace buffer. Currently at 1350MB will be halved if not enough mem available
#define SEQUENCE_TABLE_SIZE_EXPONENTIAL 23// DO NOT CHANGE! buffer size in (2^)units for sequences and alignment storages (batch size)
// Maximum exponential is up to 30 [~ 1 GBytes] for non-debug, non alignment
// Maximum exponential is up to 26 [~ 128MBytes] for debug
// Maximum exponential is up to 23 for alignment with 4GB RAM(default : 23)
//The followings are for DEBUG only
#define OUTPUT_ALIGNMENTS 1 // should leave ON for outputting alignment
#define STDOUT_STRING_RESULT 0 // output alignment in text format (in SA coordinates, not compatible with SAM output modules(samse/pe)
#define STDOUT_BINARY_RESULT 1 //output alignment for samse/sampe (leave ON)
#define CUDA_SAMSE 1 //Enable CUDA SAMSE code, debug only (leave ON)
// how much debugging information shall the kernel output? kernel output only works for fermi and above
#define DEBUG_LEVEL 0
// how long should a subsequence be for one kernel launch
// For multikernel design
#define PASS_LENGTH 32 // pass size, also initial seed size
#define SPLIT_ENGAGE PASS_LENGTH + 6 //when splitting starts to happen
#define MAX_SEED_LENGTH 50 // not tested beyond 50
//Global variables for inexact match <<do not change>>
#define STATE_M 0
#define STATE_I 1
#define STATE_D 2
///////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
//CUDA global variables
__device__ __constant__ bwt_t bwt_cuda;
__device__ __constant__ bwt_t rbwt_cuda;
__device__ __constant__ barracuda_gap_opt_t options_cuda;
//Texture Maps
// uint4 is used because the maximum width for CUDA texture bind of 1D memory is 2^27,
// and uint4 the structure 4xinteger is x,y,z,w coordinates and is 16 bytes long,
// therefore effectively there are 2^27x16bytes memory can be access = 2GBytes memory.
texture<uint4, 1, cudaReadModeElementType> bwt_occ_array;
texture<uint4, 1, cudaReadModeElementType> rbwt_occ_array;
texture<unsigned int, 1, cudaReadModeElementType> sequences_array;
texture<uint2, 1, cudaReadModeElementType> sequences_index_array;
///////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
unsigned long long copy_bwts_to_cuda_memory( const char * prefix, unsigned int ** bwt, unsigned int ** rbwt, int mem_available, bwtint_t* forward_seq_len, bwtint_t* backward_seq_len)
// bwt occurrence array to global and bind to texture, bwt structure to constant memory
{
bwt_t * bwt_src;
char str[100];
unsigned long long size_read = 0;
#if DEBUG_LEVEL > 0
fprintf(stderr,"[aln_debug] mem left: %d\n", mem_available);
#endif
// if ( bwt != 0 )
{
//Original BWT
//Load bwt occurrence array from from disk
strcpy(str, prefix); strcat(str, ".bwt"); bwt_src = bwt_restore_bwt(str);
size_read += bwt_src->bwt_size*sizeof(uint32_t);
mem_available = mem_available - int (size_read>>20);
*forward_seq_len = bwt_src->seq_len;
if(mem_available > 0)
{
//Allocate memory for bwt
cudaMalloc((void**)bwt, bwt_src->bwt_size*sizeof(uint32_t));
//copy bwt occurrence array from host to device and dump the bwt to save CPU memory
cudaMemcpy (*bwt, bwt_src->bwt, bwt_src->bwt_size*sizeof(uint32_t), cudaMemcpyHostToDevice);
//bind global variable bwt to texture memory bwt_occ_array
cudaBindTexture(0, bwt_occ_array, *bwt, bwt_src->bwt_size*sizeof(uint32_t));
//copy bwt structure data to constant memory bwt_cuda structure
cudaMemcpyToSymbol ( bwt_cuda, bwt_src, sizeof(bwt_t), 0, cudaMemcpyHostToDevice);
bwt_destroy(bwt_src);
}
else
{
fprintf(stderr,"[aln_core] Not enough device memory to perform alignment.\n");
return 0;
}
#if DEBUG_LEVEL > 0
fprintf(stderr,"[aln_debug] bwt loaded, mem left: %d\n", mem_available);
#endif
}
// if ( rbwt != 0 )
{
//Reversed BWT
//Load bwt occurrence array from from disk
strcpy(str, prefix); strcat(str, ".rbwt"); bwt_src = bwt_restore_bwt(str);
size_read += bwt_src->bwt_size*sizeof(uint32_t);
mem_available = mem_available - (bwt_src->bwt_size*sizeof(uint32_t)>>20);
#if DEBUG_LEVEL > 0
fprintf(stderr,"[aln_debug] rbwt loaded mem left: %d\n", mem_available);
#endif
if (mem_available > 0)
{
*backward_seq_len = bwt_src->seq_len;
//Allocate memory for bwt
cudaMalloc((void**)rbwt, bwt_src->bwt_size*sizeof(uint32_t));
//copy reverse bwt occurrence array from host to device and dump the bwt to save CPU memory
cudaMemcpy (*rbwt, bwt_src->bwt, bwt_src->bwt_size*sizeof(uint32_t), cudaMemcpyHostToDevice);
//bind global variable bwt to texture memory bwt_occ_array
cudaBindTexture(0, rbwt_occ_array, *rbwt, bwt_src->bwt_size*sizeof(uint32_t));
//copy bwt structure data to constant memory bwt_cuda structure
cudaMemcpyToSymbol ( rbwt_cuda, bwt_src, sizeof(bwt_t), 0, cudaMemcpyHostToDevice);
bwt_destroy(bwt_src);
}
else
{
fprintf(stderr,"[aln_core] Not enough device memory to perform alignment.\n");
return 0;
}
}
return size_read;
}
void free_bwts_from_cuda_memory( unsigned int * bwt , unsigned int * rbwt )
{
if ( bwt != 0 )
{
cudaUnbindTexture(bwt_occ_array);
cudaFree(bwt);
}
if ( rbwt != 0 )
{
cudaUnbindTexture(rbwt_occ_array);
cudaFree(rbwt);
}
}
void swap2(bwt_aln1_t *x, bwt_aln1_t *y)
{
bwt_aln1_t temp;
temp = *x;
*x = *y;
*y = temp;
}
int choose_pivot2(int i,int j)
{
return((i+j) /2);
}
void aln_quicksort2(bwt_aln1_t *aln, int m, int n)
//This function sorts the alignment array from barracuda to make it compatible with SAMSE/SAMPE cores
{
int key,i,j,k;
if (m < n)
{
k = choose_pivot2(m, n);
swap2(&aln[m],&aln[k]);
key = aln[m].score;
i = m+1;
j = n;
while(i <= j)
{
while((i <= n) && (aln[i].score <= key))
i++;
while((j >= m) && (aln[j].score > key))
j--;
if(i < j)
swap2(&aln[i],&aln[j]);
}
// swap two elements
swap2(&aln[m],&aln[j]);
// recursively sort the lesser lists
aln_quicksort2(aln, m, j-1);
aln_quicksort2(aln, j+1, n);
}
}
#define write_to_half_byte_array(array,index,data) \
(array)[(index)>>1]=(unsigned char)(((index)&0x1)?(((array)[(index)>>1]&0xF0)|((data)&0x0F)):(((data)<<4)|((array)[(index)>>1]&0x0F)))
int copy_sequences_to_cuda_memory ( bwa_seqio_t *bs, uint2 * global_sequences_index, uint2 * main_sequences_index, unsigned char * global_sequences, unsigned char * main_sequences, unsigned int * read_size, unsigned short & max_length, int mid, int buffer)
{
//sum of length of sequences up the the moment
unsigned int accumulated_length = 0;
//sequence's read length
unsigned short read_length = 0;
unsigned int number_of_sequences = 0;
while (bwa_read_seq_one_half_byte(bs,main_sequences,accumulated_length,&read_length, mid)>0)
{
main_sequences_index[number_of_sequences].x = accumulated_length;
main_sequences_index[number_of_sequences].y = read_length;
if (read_length > max_length) max_length = read_length;
accumulated_length += read_length;
number_of_sequences++;
if ( accumulated_length + MAX_READ_LENGTH > (1ul<<(buffer+1)) ) break;
}
//copy main_sequences_width from host to device
cudaUnbindTexture(sequences_index_array);
cudaMemcpy(global_sequences_index, main_sequences_index, (number_of_sequences)*sizeof(uint2), cudaMemcpyHostToDevice);
cudaBindTexture(0, sequences_index_array, global_sequences_index, (number_of_sequences)*sizeof(uint2));
//copy main_sequences from host to device, sequences array length should be accumulated_length/2
cudaUnbindTexture(sequences_array);
cudaMemcpy(global_sequences, main_sequences, (1ul<<(buffer))*sizeof(unsigned char), cudaMemcpyHostToDevice);
cudaBindTexture(0, sequences_array, global_sequences, (1ul<<(buffer))*sizeof(unsigned char));
if ( read_size ) *read_size = accumulated_length;
return number_of_sequences;
}
//CUDA DEVICE CODE STARTING FROM THIS LINE
/////////////////////////////////////////////////////////////////////////////
__device__ unsigned char read_char(unsigned int pos, unsigned int * lastpos, unsigned int * data )
// read character back from sequence arrays
// which is packed as half bytes and stored as in a unsigned int array
{
unsigned char c;
unsigned int pos_shifted = pos >> 3;
unsigned int tmp = *data;
if (*lastpos!=pos_shifted)
{
*data = tmp = tex1Dfetch(sequences_array, pos_shifted);
*lastpos=pos_shifted;
}
switch (pos&0x7)
{
case 7:
c = tmp>>24;
break;
case 6:
c = tmp>>28;
break;
case 5:
c = tmp>>16;
break;
case 4:
c = tmp>>20;
break;
case 3:
c = tmp>>8;
break;
case 2:
c = tmp>>12;
break;
case 1:
c = tmp;
break;
case 0:
c = tmp>>4;
break;
}
return c&0xF;
}
__device__ inline unsigned int numbits(unsigned int i, unsigned char c)
// with y of 32 bits which is a string sequence encoded with 2 bits per alphabet,
// count the number of occurrence of c ( one pattern of 2 bits alphabet ) in y
{
i = ((c&2)?i:~i)>>1&((c&1)?i:~i)&0x55555555;
i = (i&0x33333333)+(i>>2&0x33333333);
return((i+(i>>4)&0x0F0F0F0F)*0x01010101)>>24;
}
#define __occ_cuda_aux4(b) (bwt_cuda.cnt_table[(b)&0xff]+bwt_cuda.cnt_table[(b)>>8&0xff]+bwt_cuda.cnt_table[(b)>>16&0xff]+bwt_cuda.cnt_table[(b)>>24])
#define BWTOCC(a) (tex1Dfetch(bwt_occ_array,a))
#define RBWTOCC(a) (tex1Dfetch(rbwt_occ_array,a))
__device__ uint4 bwt_cuda_occ4(bwtint_t k)
// return occurrence of c in bwt with k smallest suffix by reading it from texture memory
{
// total number of character c in the up to the interval of k
uint4 tmp;
uint4 n = {0,0,0,0};
unsigned int i = 0;
unsigned int m = 0;
// remarks: uint4 in CUDA is 4 x integer ( a.x,a.y,a.z,a.w )
// uint4 is used because CUDA 1D texture array is limited to width for 2^27
// to access 2GB of memory, a structure uint4 is needed
// tmp variable
unsigned int tmp1,tmp2;//, tmp3;
if (k == bwt_cuda.seq_len)
{
n.x = bwt_cuda.L2[1]-bwt_cuda.L2[0];
n.y = bwt_cuda.L2[2]-bwt_cuda.L2[1];
n.z = bwt_cuda.L2[3]-bwt_cuda.L2[2];
n.w = bwt_cuda.L2[4]-bwt_cuda.L2[3];
return n;
}
if (k == (bwtint_t)(-1)) return n;
if (k >= bwt_cuda.primary) --k; // because $ is not in bwt
i = ((k>>7)*3);
// count the number of character c within the 128bits interval
tmp = BWTOCC(i+1);
if (k&0x40)
{
m = __occ_cuda_aux4(tmp.x);
m += __occ_cuda_aux4(tmp.y);
m += __occ_cuda_aux4(tmp.z);
m += __occ_cuda_aux4(tmp.w);
tmp = BWTOCC(i+2);
}
if (k&0x20)
{
m += __occ_cuda_aux4(tmp.x);
m += __occ_cuda_aux4(tmp.y);
tmp1=tmp.z;
tmp2=tmp.w;
} else {
tmp1=tmp.x;
tmp2=tmp.y;
}
if (k&0x10)
{
m += __occ_cuda_aux4(tmp1);
tmp1=tmp2;
}
// just shift away the unwanted character, no need to shift back
// number of c in tmp1 will still be correct
m += __occ_cuda_aux4(tmp1>>(((~k)&15)<<1));
n.x = m&0xff; n.y = m>>8&0xff; n.z = m>>16&0xff; n.w = m>>24;
// retrieve the total count from index the number of character C in the up k/128bits interval
tmp = BWTOCC(i);
n.x += tmp.x; n.x -= ~k&15; n.y += tmp.y; n.z += tmp.z; n.w += tmp.w;
return n;
}
__device__ bwtint_t bwt_cuda_occ(bwtint_t k, ubyte_t c)
// return occurrence of c in bwt with k smallest suffix by reading it from texture memory
{
#if BWT_TABLE_LOOKUP_ENABLE == 1
uint4 ok = bwt_cuda_occ4(k);
switch ( c )
{
case 0:
return ok.x;
case 1:
return ok.y;
case 2:
return ok.z;
case 3:
return ok.w;
}
return 0;
#else // USE_LOOKUP_TABLE == 1
// total number of character c in the up to the interval of k
unsigned int n = 0;
unsigned int i = 0;
// remarks: uint4 in CUDA is 4 x integer ( a.x,a.y,a.z,a.w )
// uint4 is used because CUDA 1D texture array is limited to width for 2^27
// to access 2GB of memory, a structure uint4 is needed
uint4 tmp;
// tmp variable
unsigned int tmp1,tmp2;
if (k == bwt_cuda.seq_len) return bwt_cuda.L2[c+1] - bwt_cuda.L2[c];
if (k == (bwtint_t)(-1)) return 0;
if (k >= bwt_cuda.primary) --k; // because $ is not in bwt
i = ((k>>7)*3);
// count the number of character c within the 128bits interval
tmp = BWTOCC(i+1);
if (k&0x40)
{
n += numbits(tmp.x, c);
n += numbits(tmp.y, c);
n += numbits(tmp.z, c);
n += numbits(tmp.w, c);
tmp = BWTOCC(i+2);
}
if (k&0x20)
{
n += numbits(tmp.x, c);
n += numbits(tmp.y, c);
tmp1=tmp.z;
tmp2=tmp.w;
} else {
tmp1=tmp.x;
tmp2=tmp.y;
}
if (k&0x10)
{
n += numbits(tmp1, c);
tmp1=tmp2;
}
// just shift away the unwanted character, no need to shift back
// number of c in tmp1 will still be correct
n += numbits(tmp1>>(((~k)&15)<<1), c);
// retrieve the total count from index the number of character C in the up k/128bits interval
tmp = BWTOCC(i);
switch ( c )
{
case 0:
n += tmp.x;
// corrected for the masked bits
n -= ~k&15;
break;
case 1:
n += tmp.y;
break;
case 2:
n += tmp.z;
break;
case 3:
n += tmp.w;
break;
}
return n;
#endif // USE_LOOKUP_TABLE == 1
}
__device__ uint4 rbwt_cuda_occ4(bwtint_t k)
// return occurrence of c in bwt with k smallest suffix by reading it from texture memory
{
// total number of character c in the up to the interval of k
uint4 tmp;
uint4 n = {0,0,0,0};
unsigned int i = 0;
unsigned int m = 0;
// remarks: uint4 in CUDA is 4 x integer ( a.x,a.y,a.z,a.w )
// uint4 is used because CUDA 1D texture array is limited to width for 2^27
// to access 2GB of memory, a structure uint4 is needed
// tmp variable
unsigned int tmp1,tmp2; //, tmp3;
if (k == rbwt_cuda.seq_len)
{
n.x = rbwt_cuda.L2[1]-rbwt_cuda.L2[0];
n.y = rbwt_cuda.L2[2]-rbwt_cuda.L2[1];
n.z = rbwt_cuda.L2[3]-rbwt_cuda.L2[2];
n.w = rbwt_cuda.L2[4]-rbwt_cuda.L2[3];
return n;
}
if (k == (bwtint_t)(-1)) return n;
if (k >= rbwt_cuda.primary) --k; // because $ is not in bwt
i = ((k>>7)*3);
// tmp3 = k>> 7;
// i = tmp3+tmp3+tmp3;
// count the number of character c within the 128bits interval
tmp = RBWTOCC(i+1);
if (k&0x40)
{
m = __occ_cuda_aux4(tmp.x);
m += __occ_cuda_aux4(tmp.y);
m += __occ_cuda_aux4(tmp.z);
m += __occ_cuda_aux4(tmp.w);
tmp = RBWTOCC(i+2);
}
if (k&0x20)
{
m += __occ_cuda_aux4(tmp.x);
m += __occ_cuda_aux4(tmp.y);
tmp1=tmp.z;
tmp2=tmp.w;
} else {
tmp1=tmp.x;
tmp2=tmp.y;
}
if (k&0x10)
{
m += __occ_cuda_aux4(tmp1);
tmp1=tmp2;
}
// just shift away the unwanted character, no need to shift back
// number of c in tmp1 will still be correct
m += __occ_cuda_aux4(tmp1>>(((~k)&15)<<1));
n.x = m&0xff; n.y = m>>8&0xff; n.z = m>>16&0xff; n.w = m>>24;
// retrieve the total count from index the number of character C in the up k/128bits interval
tmp = RBWTOCC(i);
n.x += tmp.x; n.x -= ~k&15; n.y += tmp.y; n.z += tmp.z; n.w += tmp.w;
return n;
}
__device__ inline bwtint_t rbwt_cuda_occ(bwtint_t k, ubyte_t c )
// return occurrence of c in bwt with k smallest suffix by reading it from texture memory
{
#if BWT_TABLE_LOOKUP_ENABLE == 1
uint4 ok = rbwt_cuda_occ4(k);
switch ( c )
{
case 0:
return ok.x;
case 1:
return ok.y;
case 2:
return ok.z;
case 3:
return ok.w;
}
return 0;
#else // USE_LOOKUP_TABLE == 1
// total number of character c in the up to the interval of k
unsigned int n = 0;
unsigned int i = 0;
// remarks: uint4 in CUDA is 4 x integer ( a.x,a.y,a.z,a.w )
// uint4 is used because CUDA 1D texture array is limited to width for 2^27
// to access 2GB of memory, a structure uint4 is needed
uint4 tmp;
// tmp variable
unsigned int tmp1, tmp2;
if (k == bwt_cuda.seq_len)
return rbwt_cuda.L2[c + 1] - rbwt_cuda.L2[c];
if (k == (bwtint_t) (-1))
return 0;
if (k >= rbwt_cuda.primary)
--k; // because $ is not in bwt
i = ((k >> 7) * 3);
// count the number of character c within the 128bits interval
tmp = RBWTOCC(i+1);
if (k & 0x40) {
n += numbits(tmp.x, c);
n += numbits(tmp.y, c);
n += numbits(tmp.z, c);
n += numbits(tmp.w, c);
tmp = RBWTOCC(i+2);
}
if (k & 0x20) {
n += numbits(tmp.x, c);
n += numbits(tmp.y, c);
tmp1 = tmp.z;
tmp2 = tmp.w;
} else {
tmp1 = tmp.x;
tmp2 = tmp.y;
}
if (k & 0x10) {
n += numbits(tmp1, c);
tmp1 = tmp2;
}
// just shift away the unwanted character, no need to shift back
// number of c in tmp1 will still be correct
n += numbits(tmp1 >> (((~k) & 15) << 1), c);
// retrieve the total count from index the number of character C in the up k/128bits interval
tmp = RBWTOCC(i);
switch (c) {
case 0:
n += tmp.x;
// corrected for the masked bits
n -= ~k & 15;
break;
case 1:
n += tmp.y;
break;
case 2:
n += tmp.z;
break;
case 3:
n += tmp.w;
break;
}
return n;
#endif // USE_LOOKUP_TABLE == 1
}
#if BWT_2_OCC_ENABLE == 1
__device__ void bwts_cuda_2occ(unsigned int k, unsigned int l, unsigned char c, unsigned int *ok, unsigned int *ol, unsigned short bwt_type)
{
unsigned int _k, _l;
unsigned int i;
// tmp variable
uint4 c_count;
uint4 dat0, dat1;
// total number of character c in the up to the interval of k
unsigned int n, m, count;
// remarks: uint4 in CUDA is 4 x integer ( a.x,a.y,a.z,a.w )
// uint4 is used because CUDA 1D texture array is limited to width for 2^27
// to access 2GB of memory, a structure uint4 is needed
uint4 tmpn,tmpm;
// tmp variable
unsigned int tmp1,tmp2;
if ( bwt_type == 0 )
{
if (k == l) {
*ok = *ol = bwt_cuda_occ(k, c);
return;
}
_k = (k >= bwt_cuda.primary)? k-1 : k;
_l = (l >= bwt_cuda.primary)? l-1 : l;
if ( _l>>7 != _k>>7 ||
k == (bwtint_t)(-1) ||
l == (bwtint_t)(-1)
)
{
*ok = bwt_cuda_occ(k, c);
*ol = bwt_cuda_occ(l, c);
return;
}
k = _k;
l = _l;
i = ((k>>7)*3);
// count the number of character c within the 128bits interval
c_count = BWTOCC(i);
dat0 = BWTOCC(i+1);
if ( l & 0x40 ) dat1 = BWTOCC(i+2);
}
else
{
if (k == l) {
*ok = *ol = rbwt_cuda_occ(k, c);
return;
}
_k = (k >= rbwt_cuda.primary)? k-1 : k;
_l = (l >= rbwt_cuda.primary)? l-1 : l;
if ( _l>>7 != _k>>7 ||
k == (bwtint_t)(-1) ||
l == (bwtint_t)(-1)
)
{
*ok = rbwt_cuda_occ(k, c);
*ol = rbwt_cuda_occ(l, c);
return;
}
k = _k;
l = _l;
i = ((k>>7)*3);
// count the number of character c within the 128bits interval
c_count = RBWTOCC(i);
dat0 = RBWTOCC(i+1);
if (l&0x40) dat1 = RBWTOCC(i+2);
}
n = m = 0;
tmpn = dat0;
tmpm = dat0;
if (l&0x40)
{
count = numbits(tmpn.x, c);
count += numbits(tmpn.y, c);
count += numbits(tmpn.z, c);
count += numbits(tmpn.w, c);
m = count;
tmpm = dat1;
if (k&0x40)
{
n = count;
tmpn = dat1;
}
}
if (k&0x20)
{
n += numbits(tmpn.x, c);
n += numbits(tmpn.y, c);
tmp1=tmpn.z;
tmp2=tmpn.w;
} else {
tmp1=tmpn.x;
tmp2=tmpn.y;
}
if (k&0x10)
{
n += numbits(tmp1, c);
tmp1=tmp2;
}
// just shift away the unwanted character, no need to shift back
// number of c in tmp1 will still be correct
n += numbits(tmp1>>(((~k)&15)<<1), c);
if (l&0x20)
{
m += numbits(tmpm.x, c);
m += numbits(tmpm.y, c);
tmp1=tmpm.z;
tmp2=tmpm.w;
} else {
tmp1=tmpm.x;
tmp2=tmpm.y;
}
if (l&0x10)
{
m += numbits(tmp1, c);
tmp1=tmp2;
}
// just shift away the unwanted character, no need to shift back
// number of c in tmp1 will still be correct
m += numbits(tmp1>>(((~l)&15)<<1), c);
// retrieve the total count from index the number of character C in the up k/128bits interval
switch ( c )
{
case 0:
n += c_count.x;
m += c_count.x;
// corrected for the masked bits
n -= ~k&15;
m -= ~l&15;
break;
case 1:
n += c_count.y;
m += c_count.y;
break;
case 2:
n += c_count.z;
m += c_count.z;
break;
case 3:
n += c_count.w;
m += c_count.w;
break;
}
*ok=n;
*ol=m;
}
#endif // BWT_2_OCC_ENABLE == 1
__device__ void bwt_cuda_device_calculate_width (unsigned char* sequence, unsigned short sequence_type, unsigned int * widths, unsigned char * bids, unsigned short length)
//Calculate bids and widths for worst case bound, returns widths[senquence length] and bids[sequence length]
{
unsigned short bid;
//suffix array interval k(lower bound) and l(upper bound)
unsigned int k, l;
unsigned int i;
// do calculation and update w and bid
bid = 0;
k = 0;
l = bwt_cuda.seq_len;
unsigned short bwt_type = sequence_type;
for (i = 0; i < length; ++i) {
unsigned char c = sequence[i];
if (c < 4) {
#if BWT_2_OCC_ENABLE == 1
//unsigned int ok, ol;
//bwts_cuda_2occ(k - 1, l, c, &ok, &ol, bwt_type);
uint4 ok, ol;
unsigned int ok1, ol1;
bwts_cuda_2occ4(k - 1, l, &ok, &ol, bwt_type);
switch ( c )
{
case 0:
ok1 = ok.x;
ol1 = ol.x;
break;
case 1:
ok1 = ok.y;
ol1 = ol.y;
break;
case 2:
ok1 = ok.z;
ol1 = ol.z;
break;
case 3:
ok1 = ok.w;
ol1 = ol.w;
break;
}
k = bwt_cuda.L2[c] + ok1 + 1;
l = bwt_cuda.L2[c] + ol1;
#else // BWT_2_OCC_ENABLE == 1
if ( bwt_type == 0 )
{
k = bwt_cuda.L2[c] + bwt_cuda_occ(k - 1, c) + 1;
l = bwt_cuda.L2[c] + bwt_cuda_occ(l, c);
}
else
{
k = rbwt_cuda.L2[c] + rbwt_cuda_occ(k - 1, c) + 1;
l = rbwt_cuda.L2[c] + rbwt_cuda_occ(l, c);
}
#endif // BWT_2_OCC_ENABLE == 1
}
if (k > l || c > 3) {
k = 0;
l = bwt_cuda.seq_len;
++bid;
}
widths[i] = l - k + 1;
bids[i] = bid;
}
widths[length] = k + 1;
bids[length] = bid;
return;
}
__device__ void bwt_cuda_device_calculate_width_limit (init_info_t* init_info,unsigned char* sequence, unsigned short sequence_type, unsigned int * widths, unsigned char * bids, unsigned short length)
//Calculate bids and widths for worst case bound, returns widths[senquence length] and bids[sequence length]
{
unsigned short bid;
//suffix array interval k(lower bound) and l(upper bound)
unsigned int k, l;
unsigned int i;
// do calculation and update w and bid
bid = 0;
//k = init_info->lim_k;
//l = init_info->lim_l;
k = 0;
l = bwt_cuda.seq_len;
unsigned short bwt_type = sequence_type;
for (i = 0; i < length; ++i) {
unsigned char c = sequence[i];
if (c < 4) {
#if BWT_2_OCC_ENABLE == 1
//unsigned int ok, ol;
//bwts_cuda_2occ(k - 1, l, c, &ok, &ol, bwt_type);
uint4 ok, ol;
unsigned int ok1, ol1;
bwts_cuda_2occ4(k - 1, l, &ok, &ol, bwt_type);
switch ( c )
{
case 0:
ok1 = ok.x;
ol1 = ol.x;
break;
case 1:
ok1 = ok.y;
ol1 = ol.y;
break;
case 2:
ok1 = ok.z;
ol1 = ol.z;
break;
case 3:
ok1 = ok.w;
ol1 = ol.w;
break;
}
k = bwt_cuda.L2[c] + ok1 + 1;
l = bwt_cuda.L2[c] + ol1;
#else // BWT_2_OCC_ENABLE == 1
if ( bwt_type == 0 )
{
k = bwt_cuda.L2[c] + bwt_cuda_occ(k - 1, c) + 1;
l = bwt_cuda.L2[c] + bwt_cuda_occ(l, c);
}
else
{
k = rbwt_cuda.L2[c] + rbwt_cuda_occ(k - 1, c) + 1;
l = rbwt_cuda.L2[c] + rbwt_cuda_occ(l, c);
}
#endif // BWT_2_OCC_ENABLE == 1
}
if (k > l || c > 3) {
k = 0;
l = bwt_cuda.seq_len;
++bid;
}
widths[i] = l - k + 1;
bids[i] = bid;
}
widths[length] = k + 1;
bids[length] = bid;
return;
}
__device__ int bwa_cuda_cal_maxdiff(int l, double err, double thres)
{
double elambda = exp(-l * err);
double sum, y = 1.0;
int k, x = 1;
for (k = 1, sum = elambda; k < 1000; ++k) {
y *= l * err;
x *= k;
sum += elambda * y / x;
if (1.0 - sum < thres) return k;
}
return 2;
}
__device__ void gap_stack_shadow_cuda(int x, int len, bwtint_t max, int last_diff_pos, unsigned int * width, unsigned char * bid)
{
int i, j;
for (i = j = 0; i < last_diff_pos; ++i)
{
if (width[i] > x)
{
width[i] -= x;
}
else if (width[i] == x)
{
bid[i] = 1;
width[i] = max - (++j);
} // else should not happen
}
}
__device__ unsigned int int_log2_cuda(uint32_t v)
//integer log
{
unsigned int c = 0;
if (v & 0xffff0000u) { v >>= 16; c |= 16; }
if (v & 0xff00) { v >>= 8; c |= 8; }
if (v & 0xf0) { v >>= 4; c |= 4; }
if (v & 0xc) { v >>= 2; c |= 2; }
if (v & 0x2) c |= 1;
return c;
}
__device__ int bwt_cuda_match_exact(unsigned short bwt_type, unsigned int length, const unsigned char * str, bwtint_t *k0, bwtint_t *l0)
//exact match algorithm
{
int i;
unsigned int k, l;
k = *k0; l = *l0;
for (i = length - 1; i >= 0; --i)
{
unsigned char c = str[i];
//if (c > 3) return 0; // there is an N here. no match
#if BWT_2_OCC_ENABLE == 1
unsigned int ok, ol;
bwts_cuda_2occ(k - 1, l, c, &ok, &ol, bwt_type);
if ( bwt_type == 0 )
{
k = bwt_cuda.L2[c] + ok + 1;
l = bwt_cuda.L2[c] + ol;
}
else
{
k = rbwt_cuda.L2[c] + ok + 1;
l = rbwt_cuda.L2[c] + ol;
}
#else // BWT_2_OCC_ENABLE == 1
if ( bwt_type == 0 )
{
k = bwt_cuda.L2[c] + bwt_cuda_occ(k - 1, c) + 1;