-
Notifications
You must be signed in to change notification settings - Fork 0
/
kernel.cpp
1132 lines (943 loc) · 36.5 KB
/
kernel.cpp
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
#include <kernel.hpp>
__inline__ __device__ short
gpu_bsw::warpReduceMax_with_index_reverse(short val, short& myIndex, short& myIndex2, unsigned lengthSeqB)
{
int warpSize = 32;
short myMax = 0;
short newInd = 0;
short newInd2 = 0;
short ind = myIndex;
short ind2 = myIndex2;
myMax = val;
unsigned mask = __ballot_sync(0xffffffff, threadIdx.x < lengthSeqB); // blockDim.x
// unsigned newmask;
for(int offset = warpSize / 2; offset > 0; offset /= 2)
{
int tempVal = __shfl_down_sync(mask, val, offset);
val = max(val,tempVal);
newInd = __shfl_down_sync(mask, ind, offset);
newInd2 = __shfl_down_sync(mask, ind2, offset);
// if(threadIdx.x == 0)printf("index1:%d, index2:%d, max:%d\n", newInd, newInd2, val);
if(val != myMax)
{
ind = newInd;
ind2 = newInd2;
myMax = val;
}
else if((val == tempVal) ) // this is kind of redundant and has been done purely to match the results
// with SSW to get the smallest alignment with highest score. Theoreticaly
// all the alignmnts with same score are same.
{
if(newInd2 > ind2){
ind = newInd;
ind2 = newInd2;
}
}
}
myIndex = ind;
myIndex2 = ind2;
val = myMax;
return val;
}
__inline__ __device__ short
gpu_bsw::warpReduceMax_with_index(short val, short& myIndex, short& myIndex2, unsigned lengthSeqB)
{
int warpSize = 32;
short myMax = 0;
short newInd = 0;
short newInd2 = 0;
short ind = myIndex;
short ind2 = myIndex2;
myMax = val;
unsigned mask = __ballot_sync(0xffffffff, threadIdx.x < lengthSeqB); // blockDim.x
// unsigned newmask;
for(int offset = warpSize / 2; offset > 0; offset /= 2)
{
int tempVal = __shfl_down_sync(mask, val, offset);
val = max(val,tempVal);
newInd = __shfl_down_sync(mask, ind, offset);
newInd2 = __shfl_down_sync(mask, ind2, offset);
if(val != myMax)
{
ind = newInd;
ind2 = newInd2;
myMax = val;
}
else if((val == tempVal) ) // this is kind of redundant and has been done purely to match the results
// with SSW to get the smallest alignment with highest score. Theoreticaly
// all the alignmnts with same score are same.
{
if(newInd < ind){
ind = newInd;
ind2 = newInd2;
}
}
}
myIndex = ind;
myIndex2 = ind2;
val = myMax;
return val;
}
__device__ short
gpu_bsw::blockShuffleReduce_with_index_reverse(short myVal, short& myIndex, short& myIndex2, unsigned lengthSeqB)
{
int laneId = threadIdx.x % 32;
int warpId = threadIdx.x / 32;
__shared__ short locTots[32];
__shared__ short locInds[32];
__shared__ short locInds2[32];
short myInd = myIndex;
short myInd2 = myIndex2;
myVal = warpReduceMax_with_index_reverse(myVal, myInd, myInd2, lengthSeqB);
__syncthreads();
if(laneId == 0)
locTots[warpId] = myVal;
if(laneId == 0)
locInds[warpId] = myInd;
if(laneId == 0)
locInds2[warpId] = myInd2;
__syncthreads();
unsigned check =
((32 + blockDim.x - 1) / 32); // mimicing the ceil function for floats
// float check = ((float)blockDim.x / 32);
if(threadIdx.x < check) /////******//////
{
myVal = locTots[threadIdx.x];
myInd = locInds[threadIdx.x];
myInd2 = locInds2[threadIdx.x];
}
else
{
myVal = 0;
myInd = -1;
myInd2 = -1;
}
__syncthreads();
if(warpId == 0)
{
myVal = warpReduceMax_with_index_reverse(myVal, myInd, myInd2, lengthSeqB);
myIndex = myInd;
myIndex2 = myInd2;
}
__syncthreads();
return myVal;
}
__device__ short
gpu_bsw::blockShuffleReduce_with_index(short myVal, short& myIndex, short& myIndex2, unsigned lengthSeqB)
{
int laneId = threadIdx.x % 32;
int warpId = threadIdx.x / 32;
__shared__ short locTots[32];
__shared__ short locInds[32];
__shared__ short locInds2[32];
short myInd = myIndex;
short myInd2 = myIndex2;
myVal = warpReduceMax_with_index(myVal, myInd, myInd2, lengthSeqB);
__syncthreads();
if(laneId == 0)
locTots[warpId] = myVal;
if(laneId == 0)
locInds[warpId] = myInd;
if(laneId == 0)
locInds2[warpId] = myInd2;
__syncthreads();
unsigned check =
((32 + blockDim.x - 1) / 32); // mimicing the ceil function for floats
// float check = ((float)blockDim.x / 32);
if(threadIdx.x < check) /////******//////
{
myVal = locTots[threadIdx.x];
myInd = locInds[threadIdx.x];
myInd2 = locInds2[threadIdx.x];
}
else
{
myVal = 0;
myInd = -1;
myInd2 = -1;
}
__syncthreads();
if(warpId == 0)
{
myVal = warpReduceMax_with_index(myVal, myInd, myInd2, lengthSeqB);
myIndex = myInd;
myIndex2 = myInd2;
}
__syncthreads();
return myVal;
}
__device__ __host__ short
gpu_bsw::findMaxFour(short first, short second, short third, short fourth, int* ind)
{
short array[4];
array[0] = first; //diag_score
array[1] = second; //_curr_F
array[2] = third; //_curr_E
array[3] = fourth; // 0
short max;
// to make sure that if the H score is 0, then * will be put in the H_ptr matrix for correct termination of traceback
if (array[0] > 0 ) {
max = array[0];
*ind = 0;
} else {
max = 0;
*ind = 3;
}
for (int i=1; i<4; i++){
if (array[i] > max){
max = array[i];
*ind = i;
}
}
return max;
}
__device__ short
gpu_bsw::intToCharPlusWrite(int num, char* CIGAR, short cigar_position)
{
int last_digit = 0;
int digit_length = 0;
char digit_array[5];
// convert the int num to ASCII digit by digit and record in a digit_array
while (num != 0){
last_digit = num%10;
digit_array[digit_length] = char('0' + last_digit);
num = num/10;
digit_length++;
}
//write each char of the digit_array to the CIGAR string
for (int q = 0; q < digit_length; q++){
CIGAR[cigar_position]=digit_array[q];
cigar_position++;
}
return cigar_position;
}
__device__ void
gpu_bsw::createCIGAR(char* longCIGAR, char* CIGAR, int maxCIGAR,
const char* seqA, const char* seqB, unsigned lengthShorterSeq, unsigned lengthLongerSeq,
bool seqBShorter, short first_j, short last_j, short first_i, short last_i)
{
short cigar_position = 0;
short beg_S;
short end_S;
if (seqBShorter){
beg_S = lengthShorterSeq - first_j-1;
end_S = last_j;
} else {
beg_S = lengthLongerSeq - first_i-1;
end_S = last_i;
}
if ( beg_S != 0){
CIGAR[0]='S';
cigar_position++ ;
cigar_position = intToCharPlusWrite(beg_S, CIGAR, cigar_position);
}
int p = 0;
while(longCIGAR[p] != '\0'){
int letter_count = 1;
while (longCIGAR[p] == longCIGAR[p+1]){
letter_count++;
p++;
}
CIGAR[cigar_position]=longCIGAR[p];
cigar_position++ ;
cigar_position = intToCharPlusWrite(letter_count, CIGAR, cigar_position);
p++;
}
if ( end_S != 0){
CIGAR[cigar_position]='S';
cigar_position++ ;
cigar_position = intToCharPlusWrite(end_S, CIGAR, cigar_position);
}
cigar_position--;
char temp;
//code to reverse the cigar by swapping i and length of cigar - i
for(int i = 0; i<(cigar_position)/2+1;i++){
temp = CIGAR[i];
CIGAR[i]=CIGAR[cigar_position-i];
CIGAR[cigar_position-i] = temp;
}
CIGAR[cigar_position+1]='\0';
}
__device__ void
gpu_bsw::traceBack(short current_i, short current_j, char* seqA_array, char* seqB_array, unsigned* prefix_lengthA,
unsigned* prefix_lengthB, short* seqA_align_begin, short* seqA_align_end,
short* seqB_align_begin, short* seqB_align_end, unsigned const maxMatrixSize, int maxCIGAR,
char* longCIGAR, char* CIGAR, char* H_ptr, uint32_t* diagOffset)
{
int myId = blockIdx.x;
int myTId = threadIdx.x;
char* seqA;
char* seqB;
int lengthSeqA;
int lengthSeqB;
if(myId == 0)
{
lengthSeqA = prefix_lengthA[0];
lengthSeqB = prefix_lengthB[0];
seqA = seqA_array;
seqB = seqB_array;
}
else
{
lengthSeqA = prefix_lengthA[myId] - prefix_lengthA[myId - 1];
lengthSeqB = prefix_lengthB[myId] - prefix_lengthB[myId - 1];
seqA = seqA_array + prefix_lengthA[myId - 1];
seqB = seqB_array + prefix_lengthB[myId - 1];
}
unsigned short current_diagId; // = current_i+current_j;
unsigned short current_locOffset; // = 0;
unsigned maxSize = lengthSeqA > lengthSeqB ? lengthSeqA : lengthSeqB;
const char* longerSeq = lengthSeqA < lengthSeqB ? seqB : seqA;
const char* shorterSeq = lengthSeqA < lengthSeqB ? seqA : seqB;
unsigned lengthShorterSeq = lengthSeqA < lengthSeqB ? lengthSeqA : lengthSeqB;
unsigned lengthLongerSeq = lengthSeqA < lengthSeqB ? lengthSeqB : lengthSeqA;
bool seqBShorter = lengthSeqA < lengthSeqB ? false : true; //need to keep track of whether query or ref is shorter for I or D in CIGAR
current_diagId = current_i + current_j;
current_locOffset = 0;
if(current_diagId < maxSize)
{
current_locOffset = current_j;
}
else
{
unsigned short myOff = current_diagId - maxSize+1;
current_locOffset = current_j - myOff;
}
char temp_H;
temp_H = H_ptr[diagOffset[current_diagId] + current_locOffset];
short next_i;
short next_j;
short first_j = current_j; //recording the first_j, first_i values for use in calculating S
short first_i = current_i;
char matrix = 'H'; //initialize with H
int counter = 0;
short prev_i;
short prev_j;
bool continueTrace = true;
while(continueTrace && (current_i != 0) && (current_j !=0))
{
temp_H = H_ptr[diagOffset[current_diagId] + current_locOffset];
//write the current value into longCIGAR then assign next_i
if (matrix == 'H') {
switch (temp_H & 0b00001100){
case 0b00001100 :
matrix = 'H';
longCIGAR[counter] = shorterSeq[current_j] == longerSeq[current_i] ? '=' : 'X';
counter++;
next_i = current_i - 1;
next_j = current_j - 1;
break;
case 0b00001000 :
matrix = 'F';
next_i = current_i;
next_j = current_j;
break;
case 0b00000100 :
matrix = 'E';
next_i = current_i;
next_j = current_j;
break;
case 0b00000000 :
continueTrace = false;
break;
}
} else if (matrix == 'E'){
switch (temp_H & 0b00000010){
case 0b00000010 :
longCIGAR[counter] = seqBShorter ? 'I' : 'D';
counter++;
next_i = current_i;
next_j = current_j - 1;
break;
case 0b00000000 :
matrix = 'H';
longCIGAR[counter] = seqBShorter ? 'I' : 'D';
counter++;
next_i = current_i;
next_j = current_j - 1;
break;
}
} else if (matrix == 'F'){
switch (temp_H & 0b00000001){
case 0b00000001 :
longCIGAR[counter] = seqBShorter ? 'D' : 'I';
counter++;
next_i = current_i - 1;
next_j = current_j;
break;
case 0b00000000 :
matrix = 'H';
longCIGAR[counter] = seqBShorter ? 'D' : 'I';
counter++;
next_i = current_i - 1;
next_j = current_j;
break;
}
}
if (continueTrace != false){
prev_i = current_i; //record current values in case this is the stop location
prev_j = current_j;
current_i = next_i;
current_j = next_j;
current_diagId = current_i + current_j;
current_locOffset = 0;
if(current_diagId < maxSize)
{
current_locOffset = current_j;
} else {
unsigned short myOff2 = current_diagId - maxSize+1;
current_locOffset = current_j - myOff2;
}
}
}
//handle edge cases
if ((current_i == 0) || (current_j == 0)) {
if (shorterSeq[current_j] == longerSeq[current_i] ){
longCIGAR[counter] = '=';
longCIGAR[counter+1] = '\0';
prev_j = current_j;
prev_i = current_i;
}
else {
longCIGAR[counter]='\0';
}
} else {
longCIGAR[counter] = '\0';
}
current_i ++; current_j++; next_i ++; next_j ++;
if(lengthSeqA < lengthSeqB){
seqB_align_begin[myId] = prev_i;
seqA_align_begin[myId] = prev_j;
}else{
seqA_align_begin[myId] = prev_i;
seqB_align_begin[myId] = prev_j;
}
if (myTId == 0){
gpu_bsw::createCIGAR(longCIGAR, CIGAR, maxCIGAR, seqA, seqB, lengthShorterSeq, lengthLongerSeq, seqBShorter, first_j, prev_j, first_i, prev_i);
}
}
__global__ void
gpu_bsw::sequence_dna_kernel_traceback(char* seqA_array, char* seqB_array, unsigned* prefix_lengthA,
unsigned* prefix_lengthB, short* seqA_align_begin, short* seqA_align_end,
short* seqB_align_begin, short* seqB_align_end, short* top_scores,
char* longCIGAR_array, char* CIGAR_array, char* H_ptr_array,
int maxCIGAR, unsigned const maxMatrixSize, short matchScore, short misMatchScore, short startGap, short extendGap)
{
int block_Id = blockIdx.x;
int thread_Id = threadIdx.x;
short laneId = threadIdx.x%32;
short warpId = threadIdx.x/32;
unsigned lengthSeqA;
unsigned lengthSeqB;
// local pointers
char* seqA;
char* seqB;
char* H_ptr;
char* CIGAR, *longCIGAR;
extern __shared__ char is_valid_array[];
char* is_valid = &is_valid_array[0];
// setting up block local sequences and their lengths.
if(block_Id == 0)
{
lengthSeqA = prefix_lengthA[0];
lengthSeqB = prefix_lengthB[0];
seqA = seqA_array;
seqB = seqB_array;
}
else
{
lengthSeqA = prefix_lengthA[block_Id] - prefix_lengthA[block_Id - 1];
lengthSeqB = prefix_lengthB[block_Id] - prefix_lengthB[block_Id - 1];
seqA = seqA_array + prefix_lengthA[block_Id - 1];
seqB = seqB_array + prefix_lengthB[block_Id - 1];
}
// what is the max length and what is the min length
unsigned maxSize = lengthSeqA > lengthSeqB ? lengthSeqA : lengthSeqB;
unsigned minSize = lengthSeqA < lengthSeqB ? lengthSeqA : lengthSeqB;
H_ptr = H_ptr_array + (block_Id * maxMatrixSize);
longCIGAR = longCIGAR_array + (block_Id * maxCIGAR);
CIGAR = CIGAR_array + (block_Id * maxCIGAR);
char* longer_seq;
uint32_t* diagOffset = (uint32_t*) (&is_valid_array[3 * (minSize + 1) * sizeof(uint32_t)]);
// shared memory space for storing longer of the two strings
memset(is_valid, 0, minSize);
is_valid += minSize;
memset(is_valid, 1, minSize);
is_valid += minSize;
memset(is_valid, 0, minSize);
char myColumnChar;
// the shorter of the two strings is stored in thread registers
char H_temp = 0; //temp value of H stored in register until H, E and F are set then written to global; set all bits to 0 initially
if(lengthSeqA < lengthSeqB)
{
if(thread_Id < lengthSeqA)
myColumnChar = seqA[thread_Id]; // read only once
longer_seq = seqB;
}
else
{
if(thread_Id < lengthSeqB)
myColumnChar = seqB[thread_Id];
longer_seq = seqA;
}
__syncthreads(); // this is required here so that complete sequence has been copied to shared memory
int i = 0;
int j = thread_Id;
short thread_max = 0; // to maintain the thread max score
short thread_max_i = 0; // to maintain the DP coordinate i for the longer string
short thread_max_j = 0;// to maintain the DP cooirdinate j for the shorter string
int ind;
//set up the prefixSum for diagonal offset look up table for H_ptr
int locSum = 0;
//create prefixSum table by cycling through the threads in batches
for (int cyc = 0; cyc <= (lengthSeqA + lengthSeqB+1)/minSize + 1; cyc++){
int locDiagId = thread_Id+cyc*minSize;
if (locDiagId < lengthSeqA + lengthSeqB ){
if(locDiagId <= minSize){
locSum = (locDiagId) * (locDiagId + 1)/2;
diagOffset[locDiagId]= locSum;
//printf("LEFT CORNER inside loop thread_Id = %d cyc = %d locSum = %d locDiagId = %d\n", thread_Id, cyc, locSum, locDiagId);
}
else if (locDiagId > maxSize + 1){
int n = (maxSize+minSize) - locDiagId-1;
int finalcell = (maxSize) * (minSize)+1;
locSum = finalcell - n*(n+1)/2;
diagOffset[locDiagId] = locSum;
//printf("RIGHT CORNER inside loop thread_Id = %d cyc = %d locSum = %d locDiagId = %d\n", thread_Id, cyc, locSum, locDiagId);
}
else {
locSum = ((minSize)*(minSize+1)/2) +(minSize)*(locDiagId-minSize);
diagOffset[locDiagId] = locSum;
//printf("MIDDLE SECTION inside loop thread_Id = %d cyc = %d locSum = %d locDiagId = %d\n", thread_Id, cyc, locSum, locDiagId);
}
}
}
__syncthreads(); //to make sure prefixSum is calculated before the threads start calculations.
//initializing registers for storing diagonal values for three recent most diagonals (separate tables for H, E, F
short _curr_H = 0, _curr_F = -100, _curr_E = -100; //-100 acts as neg infinity
short _prev_H = 0, _prev_F = -100, _prev_E = -100;
short _prev_prev_H = 0, _prev_prev_F = -100, _prev_prev_E = -100;
short _temp_Val = 0;
__shared__ short sh_prev_E[32]; // one such element is required per warp
__shared__ short sh_prev_H[32];
__shared__ short sh_prev_prev_H[32];
__shared__ short local_spill_prev_E[1024];// each threads local spill,
__shared__ short local_spill_prev_H[1024];
__shared__ short local_spill_prev_prev_H[1024];
__syncthreads(); // to make sure all shmem allocations have been initialized
for(int diag = 0; diag < lengthSeqA + lengthSeqB-1; diag++)
{ // iterate for the number of anti-diagonals
unsigned short diagId = i + j;
unsigned short locOffset = 0;
if(diagId < maxSize)
{
locOffset = j;
}
else
{
unsigned short myOff = diagId - maxSize+1;
locOffset = j - myOff;
}
is_valid = is_valid - (diag < minSize || diag >= maxSize); //move the pointer to left by 1 if cnd true
_temp_Val = _prev_H; // value exchange happens here to setup registers for next iteration
_prev_H = _curr_H;
_curr_H = _prev_prev_H;
_prev_prev_H = _temp_Val;
_curr_H = 0;
_temp_Val = _prev_E;
_prev_E = _curr_E;
_curr_E = _prev_prev_E;
_prev_prev_E = _temp_Val;
_curr_E = -100;
_temp_Val = _prev_F;
_prev_F = _curr_F;
_curr_F = _prev_prev_F;
_prev_prev_F = _temp_Val;
_curr_F = -100;
if(laneId == 31)
{ // if you are the last thread in your warp then spill your values to shmem
sh_prev_E[warpId] = _prev_E;
sh_prev_H[warpId] = _prev_H;
sh_prev_prev_H[warpId] = _prev_prev_H;
}
if(diag >= maxSize)
{ // if you are invalid in this iteration, spill your values to shmem
local_spill_prev_E[thread_Id] = _prev_E;
local_spill_prev_H[thread_Id] = _prev_H;
local_spill_prev_prev_H[thread_Id] = _prev_prev_H;
}
__syncthreads(); // this is needed so that all the shmem writes are completed.
if(is_valid[thread_Id] && thread_Id < minSize)
{
unsigned mask = __ballot_sync(__activemask(), (is_valid[thread_Id] &&( thread_Id < minSize)));
short fVal = _prev_F + extendGap;
short hfVal = _prev_H + startGap;
short valeShfl = __shfl_sync(mask, _prev_E, laneId- 1, 32);
short valheShfl = __shfl_sync(mask, _prev_H, laneId - 1, 32);
short eVal=0, heVal = 0;
if(diag >= maxSize) // when the previous thread has phased out, get value from shmem
{
eVal = local_spill_prev_E[thread_Id - 1] + extendGap;
heVal = local_spill_prev_H[thread_Id - 1]+ startGap;
}
else
{
eVal =((warpId !=0 && laneId == 0)?sh_prev_E[warpId-1]: valeShfl) + extendGap;
heVal =((warpId !=0 && laneId == 0)?sh_prev_H[warpId-1]:valheShfl) + startGap;
}
if(warpId == 0 && laneId == 0) // make sure that values for lane 0 in warp 0 is not undefined
{
eVal = 0;
heVal = 0;
}
_curr_F = (fVal > hfVal) ? fVal : hfVal;
if (fVal > hfVal){ //record F value in H_temp 0b00000001
H_temp = H_temp | 1;
} else { //record F value in H_temp 0b00000000
H_temp = H_temp & (~1);
}
_curr_E = (eVal > heVal) ? eVal : heVal;
if (j!=0){
if (eVal > heVal) { //record E value in H_temp 0b00000010
H_temp = H_temp | 2;
} else { //record E value in H_temp 0b00000000
H_temp = H_temp & (~2);
}
}
short testShufll = __shfl_sync(mask, _prev_prev_H, laneId - 1, 32);
short final_prev_prev_H = 0;
if(diag >= maxSize)
{
final_prev_prev_H = local_spill_prev_prev_H[thread_Id - 1];
}
else
{
final_prev_prev_H =(warpId !=0 && laneId == 0)?sh_prev_prev_H[warpId-1]:testShufll;
}
if(warpId == 0 && laneId == 0) final_prev_prev_H = 0;
short diag_score = final_prev_prev_H + ((longer_seq[i] == myColumnChar) ? matchScore : misMatchScore);
_curr_H = findMaxFour(diag_score, _curr_F, _curr_E, 0, &ind);
if (ind == 0) { // diagonal cell is max, set bits to 0b00001100
H_temp = H_temp | 4; // set bit 0b00000100
H_temp = H_temp | 8; // set bit 0b00001000
//printf("\\");
} else if (ind == 1) { // left cell is max, set bits to 0b00001000
H_temp = H_temp & (~4); // clear bit
H_temp = H_temp | 8; // set bit 0b00001000
//printf("-");
} else if (ind == 2) { // top cell is max, set bits to 0b00000100
H_temp = H_temp & (~8); //clear bit
H_temp = H_temp | 4; // set bit 0b00000100
//printf("|");
} else { // score is 0, set bits to 0b00000000
H_temp = H_temp & (~8); //clear bit
H_temp = H_temp & (~4); //clear bit
//printf("*");
}
H_ptr[diagOffset[diagId] + locOffset] = H_temp;
//thread_max_i = (thread_max >= _curr_H) ? thread_max_i : i;
//thread_max_j = (thread_max >= _curr_H) ? thread_max_j : thread_Id;
//thread_max = (thread_max >= _curr_H) ? thread_max : _curr_H;
if (_curr_H > thread_max) {
thread_max_i = i;
thread_max_j = thread_Id;
thread_max = _curr_H;
}
i++;
}
__syncthreads();
}
__syncthreads();
thread_max = blockShuffleReduce_with_index(thread_max, thread_max_i, thread_max_j, minSize); // thread 0 will have the correct values
if(thread_Id == 0)
{
short current_i = thread_max_i;
short current_j = thread_max_j;
if(lengthSeqA < lengthSeqB)
{
seqB_align_end[block_Id] = thread_max_i;
seqA_align_end[block_Id] = thread_max_j;
top_scores[block_Id] = thread_max;
}
else
{
seqA_align_end[block_Id] = thread_max_i;
seqB_align_end[block_Id] = thread_max_j;
top_scores[block_Id] = thread_max;
}
gpu_bsw::traceBack(current_i, current_j, seqA_array, seqB_array, prefix_lengthA,
prefix_lengthB, seqA_align_begin, seqA_align_end,
seqB_align_begin, seqB_align_end, maxMatrixSize, maxCIGAR,
longCIGAR, CIGAR, H_ptr, diagOffset);
}
__syncthreads();
}
__global__ void
gpu_bsw::sequence_aa_kernel_traceback(char* seqA_array, char* seqB_array, unsigned* prefix_lengthA,
unsigned* prefix_lengthB, short* seqA_align_begin, short* seqA_align_end,
short* seqB_align_begin, short* seqB_align_end, short* top_scores, char* longCIGAR_array,
char* CIGAR_array, char* H_ptr_array, int maxCIGAR, unsigned const maxMatrixSize,
short startGap, short extendGap, short* scoring_matrix, short* encoding_matrix)
{
int block_Id = blockIdx.x;
int thread_Id = threadIdx.x;
short laneId = threadIdx.x%32;
short warpId = threadIdx.x/32;
unsigned lengthSeqA;
unsigned lengthSeqB;
// local pointers
char* seqA;
char* seqB;
char* longer_seq;
char* H_ptr;
char* CIGAR, *longCIGAR;
extern __shared__ char is_valid_array[];
char* is_valid = &is_valid_array[0];
// setting up block local sequences and their lengths.
if(block_Id == 0)
{
lengthSeqA = prefix_lengthA[0];
lengthSeqB = prefix_lengthB[0];
seqA = seqA_array;
seqB = seqB_array;
}
else
{
lengthSeqA = prefix_lengthA[block_Id] - prefix_lengthA[block_Id - 1];
lengthSeqB = prefix_lengthB[block_Id] - prefix_lengthB[block_Id - 1];
seqA = seqA_array + prefix_lengthA[block_Id - 1];
seqB = seqB_array + prefix_lengthB[block_Id - 1];
}
// what is the max length and what is the min length
unsigned maxSize = lengthSeqA > lengthSeqB ? lengthSeqA : lengthSeqB;
unsigned minSize = lengthSeqA < lengthSeqB ? lengthSeqA : lengthSeqB;
H_ptr = H_ptr_array + (block_Id * maxMatrixSize);
longCIGAR = longCIGAR_array + (block_Id * maxCIGAR);
CIGAR = CIGAR_array + (block_Id * maxCIGAR);
uint32_t* diagOffset = (uint32_t*) (&is_valid_array[3 * (minSize + 1) * sizeof(uint32_t)]);
// shared memory space for storing longer of the two strings
memset(is_valid, 0, minSize);
is_valid += minSize;
memset(is_valid, 1, minSize);
is_valid += minSize;
memset(is_valid, 0, minSize);
char myColumnChar;
char H_temp = 0;
// the shorter of the two strings is stored in thread registers
if(lengthSeqA < lengthSeqB)
{
if(thread_Id < lengthSeqA)
myColumnChar = seqA[thread_Id]; // read only once
longer_seq = seqB;
}
else
{
if(thread_Id < lengthSeqB)
myColumnChar = seqB[thread_Id];
longer_seq = seqA;
}
__syncthreads(); // this is required here so that complete sequence has been copied to shared memory
int i = 0;
int j = thread_Id;
short thread_max = 0; // to maintain the thread max score
short thread_max_i = 0; // to maintain the DP coordinate i for the longer string
short thread_max_j = 0;// to maintain the DP cooirdinate j for the shorter string
int ind;
//set up the prefixSum for diagonal offset look up table for H_ptr, E_ptr, F_ptr
int locSum = 0;
for (int cyc = 0; cyc <= (lengthSeqA + lengthSeqB+1)/minSize + 1; cyc++){
int locDiagId = thread_Id+cyc*minSize;
if (locDiagId < lengthSeqA + lengthSeqB ){
if(locDiagId <= minSize){
locSum = (locDiagId) * (locDiagId + 1)/2;
diagOffset[locDiagId]= locSum;
//printf("LEFT CORNER inside loop thread_Id = %d cyc = %d locSum = %d locDiagId = %d\n", thread_Id, cyc, locSum, locDiagId);
}
else if (locDiagId > maxSize + 1){
int n = (maxSize+minSize) - locDiagId-1;
int finalcell = (maxSize) * (minSize)+1;
locSum = finalcell - n*(n+1)/2;
diagOffset[locDiagId] = locSum;
//printf("RIGHT CORNER inside loop thread_Id = %d cyc = %d locSum = %d locDiagId = %d\n", thread_Id, cyc, locSum, locDiagId);
}
else {
locSum = ((minSize)*(minSize+1)/2) +(minSize)*(locDiagId-minSize);
diagOffset[locDiagId] = locSum;
//printf("MIDDLE SECTION inside loop thread_Id = %d cyc = %d locSum = %d locDiagId = %d\n", thread_Id, cyc, locSum, locDiagId);
}
}
}
__syncthreads(); //to make sure prefixSum is calculated before the threads start calculations.
//initializing registers for storing diagonal values for three recent most diagonals (separate tables for
//H, E and F)
short _curr_H = 0, _curr_F = -100, _curr_E = -100;
short _prev_H = 0, _prev_F = -100, _prev_E = -100;
short _prev_prev_H = 0, _prev_prev_F = -100, _prev_prev_E = -100;
short _temp_Val = 0;
__shared__ short sh_prev_E[32]; // one such element is required per warp
__shared__ short sh_prev_H[32];
__shared__ short sh_prev_prev_H[32];
__shared__ short local_spill_prev_E[1024];// each threads local spill,
__shared__ short local_spill_prev_H[1024];
__shared__ short local_spill_prev_prev_H[1024];
__shared__ short sh_aa_encoding[ENCOD_MAT_SIZE];// length = 91
__shared__ short sh_aa_scoring[SCORE_MAT_SIZE];
int max_threads = blockDim.x;
for(int p = thread_Id; p < SCORE_MAT_SIZE; p+=max_threads){
sh_aa_scoring[p] = scoring_matrix[p];
}
for(int p = thread_Id; p < ENCOD_MAT_SIZE; p+=max_threads){
sh_aa_encoding[p] = encoding_matrix[p];
}
__syncthreads(); // to make sure all shmem allocations have been initialized
for(int diag = 0; diag < lengthSeqA + lengthSeqB - 1; diag++)
{ // iterate for the number of anti-diagonals
unsigned short diagId = i + j;
unsigned short locOffset = 0;
if(diagId < maxSize)
{
locOffset = j;
}
else
{
unsigned short myOff = diagId - maxSize+1;
locOffset = j - myOff;
}
is_valid = is_valid - (diag < minSize || diag >= maxSize); //move the pointer to left by 1 if cnd true
_temp_Val = _prev_H; // value exchange happens here to setup registers for next iteration
_prev_H = _curr_H;
_curr_H = _prev_prev_H;
_prev_prev_H = _temp_Val;
_curr_H = 0;
_temp_Val = _prev_E;
_prev_E = _curr_E;
_curr_E = _prev_prev_E;
_prev_prev_E = _temp_Val;
_curr_E = -100;
_temp_Val = _prev_F;
_prev_F = _curr_F;
_curr_F = _prev_prev_F;
_prev_prev_F = _temp_Val;
_curr_F = -100;
if(laneId == 31)
{ // if you are the last thread in your warp then spill your values to shmem
sh_prev_E[warpId] = _prev_E;
sh_prev_H[warpId] = _prev_H;
sh_prev_prev_H[warpId] = _prev_prev_H;
}
if(diag >= maxSize)
{ // if you are invalid in this iteration, spill your values to shmem
local_spill_prev_E[thread_Id] = _prev_E;
local_spill_prev_H[thread_Id] = _prev_H;
local_spill_prev_prev_H[thread_Id] = _prev_prev_H;
}
__syncthreads(); // this is needed so that all the shmem writes are completed.
if(is_valid[thread_Id] && thread_Id < minSize)
{
unsigned mask = __ballot_sync(__activemask(), (is_valid[thread_Id] &&( thread_Id < minSize)));