-
Notifications
You must be signed in to change notification settings - Fork 9
/
ksw.cpp
3149 lines (2536 loc) · 79.1 KB
/
ksw.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
/* The MIT License
Copyright (c) 2011 by Attractive Chaos <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <stdlib.h>
#include <stdint.h>
#include <emmintrin.h>
#include <smmintrin.h>
#include <stdio.h>
#include <string.h>
#include "ksw.h"
#ifdef __GNUC__
#define LIKELY(x) __builtin_expect((x),1)
#define UNLIKELY(x) __builtin_expect((x),0)
#else
#define LIKELY(x) (x)
#define UNLIKELY(x) (x)
#endif
unsigned char seq_nt4_table[256] = {
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 0, 4, 1, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 0, 4, 1, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4
};
const kswr_t g_defr = { 0, -1, -1, -1, -1, -1, -1 };
/********************
* Global alignment *
********************/
static inline uint32_t *push_cigar(int *n_cigar, int *m_cigar, uint32_t *cigar, int op, int len)
{
if (*n_cigar == 0 || op != (cigar[(*n_cigar) - 1]&0xf)) {
if (*n_cigar == *m_cigar) {
*m_cigar = *m_cigar? (*m_cigar)<<1 : 4;
cigar = (uint32_t *)realloc(cigar, (*m_cigar) << 2);
}
cigar[(*n_cigar)++] = len<<4 | op;
} else cigar[(*n_cigar)-1] += len<<4;
return cigar;
}
typedef union __m128i_16 {
__m128i m;
int16_t v[8];
} __m128i_16_t;
typedef struct {
__m128i* alloc;
__m128i* mem;
} aligned_sse;
inline void malloc_16bit_aligned(aligned_sse* ptr, long long size)
{
ptr->alloc = (__m128i*)malloc(sizeof(__m128i)*size + 2);
ptr->mem = (__m128i*)(((uintptr_t)ptr->alloc + 1) & (~ (uintptr_t)1));
}
inline void free_16bit_aligned(aligned_sse* ptr)
{
free(ptr->alloc);
}
///qlen = tlen + 2*w
int ksw_semi_global_sse(int qlen, const char *query, int tlen, const char *target, int m, const int8_t *mat,
int gapo, int gape, int w, kswr_t* qry, int *n_cigar_, uint32_t **cigar_)
{
int beg, end;
int i, j, k = MINUS_INF_SSE, gapoe = gapo + gape, score;
///可以把band_length扩展到能被4整除的数
int band_length = 2*w+1;
///单个sse element大小为16 bit, 所以sse vector中共有8个元素
const int32_t segWidth = 8; /* number of values in vector unit */
///sse vector的个数
const int32_t segLen = (band_length + segWidth - 1) / segWidth;
aligned_sse vProfile, vProfile_tmp, pvHStore, pvHLoad, pvE;
malloc_16bit_aligned(&vProfile, m*segLen);
malloc_16bit_aligned(&vProfile_tmp, m);
malloc_16bit_aligned(&pvHStore, m*segLen);
malloc_16bit_aligned(&pvHLoad, m*segLen);
malloc_16bit_aligned(&pvE, m*segLen);
__m128i vNegLimit = _mm_set1_epi16(MINUS_INF_SSE);
const __m128i vGapO = _mm_set1_epi16((short)gapo);
const __m128i vGapE = _mm_set1_epi16((short)gape);
int segNum = 0;
int index = 0;
///n是字符集大小
///band_length可以向上扩展到4的倍数
for (k=0; k<m; ++k)
{
const int8_t *p = &mat[k * m];
///sse vector个数
for (i=0; i<segLen; ++i)
{
///是个union
__m128i_16_t t;
j = i;
for (segNum=0; segNum<segWidth; ++segNum)
{
///注意这里是qlen, 而不是band_length
t.v[segNum] = j >= qlen ? 0 : p[seq_nt4_table[(int)query[j]]];
j += segLen;
}
_mm_store_si128(&vProfile.mem[index], t.m);
++index;
}
}
/* initialize H and E */
__m128i_16_t h;
__m128i_16_t e;
for (segNum=0; segNum<segWidth; ++segNum) {
h.v[segNum] = 0;
e.v[segNum] = h.v[segNum] - gapoe;
}
index = 0;
for (i=0; i<segLen; ++i) {
_mm_store_si128(&pvHStore.mem[index], h.m);
_mm_store_si128(&pvE.mem[index], e.m);
++index;
}
__m128i tmp_E;
int8_t tmp_score;
for (i = 0; LIKELY(i < tlen); ++i) // target sequence is in the outer loop
{
__m128i vE;
/* Initialize F value to -inf. Any errors to vH values will be
* corrected in the Lazy_F loop. */
__m128i vF = vNegLimit;
__m128i vH = pvHStore.mem[0];
/* Correct part of the vProfile */
const __m128i* vP = vProfile.mem + seq_nt4_table[(int)target[i]] * segLen;
/* Swap the 2 H buffers. */
__m128i* pv = pvHLoad.mem;
pvHLoad.mem = pvHStore.mem;
pvHStore.mem = pv;
/**
beg = i;
end = i + band_length; // only loop through [beg,end) of the query sequence
**/
/* inner loop to process the query sequence */
for (j=0; j<segLen; ++j)
{
///vH[i, j] = vH[i-1, j-1] + vP[i, j]
vH = _mm_add_epi16(vH, _mm_load_si128(vP + j));
///vE[i, j]
vE = _mm_load_si128(pvE.mem + j);
/* Get max from vH, vE and vF. */
vH = _mm_max_epi16(vH, vE);
vH = _mm_max_epi16(vH, vF);
/* Save vH values. */
///vH应该是一点都不要动的
_mm_store_si128(pvHStore.mem + j, vH);
/* Update vE value. */
vH = _mm_sub_epi16(vH, vGapO);
vE = _mm_sub_epi16(vE, vGapE);
vE = _mm_max_epi16(vE, vH);
///calculate vE[i+1, j] from vE[i, j] & vH[i, j]
///vE的存储是要移位的啊
///vE[j]是要存到vE[j-1]处的
_mm_store_si128(pvE.mem + j, vE);
/* Update vF value. */
///calculate vF[i, j+1] from vF[i, j]
vF = _mm_sub_epi16(vF, vGapE);
vF = _mm_max_epi16(vF, vH);
/* Load the next vH. */
vH = _mm_load_si128(pvHLoad.mem + j);
}
///pvE和vP最好都要动一下
///pvE好办,但是vP难办啊, 因为vP有m个
for (k=0; k<m; ++k)
{
_mm_store_si128(vProfile_tmp.mem+k, _mm_srli_si128(vProfile.mem[k * segLen], 2));
j = segWidth*segLen+i;
const int8_t *p = &mat[k * m];
tmp_score = j >= qlen ? 0 : p[seq_nt4_table[(int)query[j]]];
vProfile_tmp.mem[k] = _mm_insert_epi16(vProfile_tmp.mem[k], tmp_score, 7);
}
tmp_E = _mm_srli_si128(pvE.mem[0], 2);
tmp_E = _mm_insert_epi16(tmp_E, MINUS_INF_SSE, 7);
__m128i* tmp_vP;
for (j=1; j<segLen; ++j)
{
_mm_store_si128(pvE.mem + j - 1, pvE.mem[j]);
for (k=0; k<m; ++k)
{
tmp_vP = vProfile.mem + k * segLen;
_mm_store_si128(tmp_vP + j - 1, tmp_vP[j]);
}
}
_mm_store_si128(pvE.mem + segLen-1, tmp_E);
for (k=0; k<m; ++k)
{
tmp_vP = vProfile.mem + k * segLen;
_mm_store_si128(tmp_vP+segLen-1, vProfile_tmp.mem[k]);
}
///下面correct步不涉及到vP和vE, 所以应该没关系
/* Lazy_F loop: has been revised to disallow adjecent insertion and
* then deletion, so don't update E(i, i), learn from SWPS3 */
///外循环是单个sse vector的长度
///不更新E会导致E偏小
for (k=0; k<segWidth; ++k)
{
///此时的vF是最后一个vF sse vector
///现在要用这个vF sse vector来矫正第一个vF sse vector
vF = _mm_slli_si128(vF, 2);
vF = _mm_insert_epi16(vF, MINUS_INF_SSE, 0);
///刚开始的时候是vF[0, j]
///内循环是sse vector的个数
for (j=0; j<segLen; ++j)
{
///vH[0, j]和vF[0, j]
///谁大谁就是真的
vH = _mm_load_si128(pvHStore.mem + j);
vH = _mm_max_epi16(vH,vF);
_mm_store_si128(pvHStore.mem + j, vH);
vH = _mm_sub_epi16(vH, vGapO);
vF = _mm_sub_epi16(vF, vGapE);
if (! _mm_movemask_epi8(_mm_cmpgt_epi16(vF, vH))) goto end;
}
}
end:
i;
}
int16_t *t = (int16_t*)pvHStore.mem;
score = t[0];
for (i = 0; i < band_length; i++)
{
if (t[i] > score)
{
score = t[i];
}
}
free_16bit_aligned(&vProfile);
free_16bit_aligned(&vProfile_tmp);
free_16bit_aligned(&pvHStore);
free_16bit_aligned(&pvHLoad);
free_16bit_aligned(&pvE);
qry->score = score;
return score;
}
void output_stripe(__m128i* vector, int32_t segWidth, int32_t segLen, int32_t startIndice)
{
int i, j;
for (j = 0; j < segLen; j++)
{
fprintf(stderr, "j: %d, %d\n", startIndice, (int16_t)_mm_extract_epi16(vector[j], 0));
startIndice++;
}
for (j = 0; j < segLen; j++)
{
fprintf(stderr, "j: %d, %d\n", startIndice, (int16_t)_mm_extract_epi16(vector[j], 1));
startIndice++;
}
for (j = 0; j < segLen; j++)
{
fprintf(stderr, "j: %d, %d\n", startIndice, (int16_t)_mm_extract_epi16(vector[j], 2));
startIndice++;
}
for (j = 0; j < segLen; j++)
{
fprintf(stderr, "j: %d, %d\n", startIndice, (int16_t)_mm_extract_epi16(vector[j], 3));
startIndice++;
}
for (j = 0; j < segLen; j++)
{
fprintf(stderr, "j: %d, %d\n", startIndice, (int16_t)_mm_extract_epi16(vector[j], 4));
startIndice++;
}
for (j = 0; j < segLen; j++)
{
fprintf(stderr, "j: %d, %d\n", startIndice, (int16_t)_mm_extract_epi16(vector[j], 5));
startIndice++;
}
for (j = 0; j < segLen; j++)
{
fprintf(stderr, "j: %d, %d\n", startIndice, (int16_t)_mm_extract_epi16(vector[j], 6));
startIndice++;
}
for (j = 0; j < segLen; j++)
{
fprintf(stderr, "j: %d, %d\n", startIndice, (int16_t)_mm_extract_epi16(vector[j], 7));
startIndice++;
}
}
void output_stripe_single_word(__m128i vector, int32_t segLen, int32_t startIndice, int32_t word_j)
{
startIndice+=word_j;
fprintf(stderr, "j: %d, %d\n", startIndice, (int16_t)_mm_extract_epi16(vector, 0));
startIndice+=segLen;
fprintf(stderr, "j: %d, %d\n", startIndice, (int16_t)_mm_extract_epi16(vector, 1));
startIndice+=segLen;
fprintf(stderr, "j: %d, %d\n", startIndice, (int16_t)_mm_extract_epi16(vector, 2));
startIndice+=segLen;
fprintf(stderr, "j: %d, %d\n", startIndice, (int16_t)_mm_extract_epi16(vector, 3));
startIndice+=segLen;
fprintf(stderr, "j: %d, %d\n", startIndice, (int16_t)_mm_extract_epi16(vector, 4));
startIndice+=segLen;
fprintf(stderr, "j: %d, %d\n", startIndice, (int16_t)_mm_extract_epi16(vector, 5));
startIndice+=segLen;
fprintf(stderr, "j: %d, %d\n", startIndice, (int16_t)_mm_extract_epi16(vector, 6));
startIndice+=segLen;
fprintf(stderr, "j: %d, %d\n", startIndice, (int16_t)_mm_extract_epi16(vector, 7));
}
///qlen = tlen + 2*w
int ksw_semi_global_sse_debug_debug(int qlen, const char *query, int tlen, const char *target, int m, const int8_t *mat,
int gapo, int gape, int w, kswr_t* qry, int *n_cigar_, uint32_t **cigar_)
{
int beg, end;
int i, j, k = MINUS_INF_SSE, gapoe = gapo + gape, score;
///可以把band_length扩展到能被4整除的数
int band_length = 2*w+1;
///单个sse element大小为16 bit, 所以sse vector中共有8个元素
const int32_t segWidth = 8; /* number of values in vector unit */
///sse vector的个数
const int32_t segLen = (band_length + segWidth - 1) / segWidth;
aligned_sse vProfile, vProfile_tmp, pvHStore, pvHLoad, pvE, pvE_tmp;
malloc_16bit_aligned(&vProfile, m*segLen);
malloc_16bit_aligned(&vProfile_tmp, m);
malloc_16bit_aligned(&pvHStore, m*segLen);
malloc_16bit_aligned(&pvHLoad, m*segLen);
malloc_16bit_aligned(&pvE, m*segLen);
malloc_16bit_aligned(&pvE_tmp, m*segLen);
__m128i vNegLimit = _mm_set1_epi16(MINUS_INF_SSE);
const __m128i vGapOE = _mm_set1_epi16((short)gapoe);
const __m128i vGapE = _mm_set1_epi16((short)gape);
int segNum = 0;
int index = 0;
///n是字符集大小
///band_length可以向上扩展到4的倍数
for (k=0; k<m; ++k)
{
const int8_t *p = &mat[k * m];
///sse vector个数
for (i=0; i<segLen; ++i)
{
///是个union
__m128i_16_t t;
j = i;
for (segNum=0; segNum<segWidth; ++segNum)
{
///注意这里是qlen, 而不是band_length
t.v[segNum] = j >= qlen ? 0 : p[seq_nt4_table[(int)query[j]]];
j += segLen;
}
_mm_store_si128(&vProfile.mem[index], t.m);
++index;
}
}
/* initialize H and E */
__m128i_16_t h;
__m128i_16_t e;
for (segNum=0; segNum<segWidth; ++segNum) {
h.v[segNum] = 0;
e.v[segNum] = h.v[segNum] - gapoe;
///e.v[segNum] = h.v[segNum] - gapo;
}
index = 0;
for (i=0; i<segLen; ++i) {
_mm_store_si128(&pvHStore.mem[index], h.m);
_mm_store_si128(&pvE.mem[index], e.m);
++index;
}
__m128i tmp_E;
int8_t tmp_score;
for (i = 0; LIKELY(i < tlen); ++i) // target sequence is in the outer loop
{
__m128i vE;
/* Initialize F value to -inf. Any errors to vH values will be
* corrected in the Lazy_F loop. */
__m128i vF = vNegLimit;
__m128i vH = pvHStore.mem[0];
__m128i vHm;
/* Correct part of the vProfile */
//const __m128i* vP = vProfile.mem + seq_nt4_table[(int)target[i]] * segLen;
__m128i* vP = vProfile.mem + seq_nt4_table[(int)target[i]] * segLen;
/* Swap the 2 H buffers. */
__m128i* pv = pvHLoad.mem;
pvHLoad.mem = pvHStore.mem;
pvHStore.mem = pv;
///if (i == 2 || i == 3)
{
fprintf(stderr, "*******i(h): %d*******\n", i);
output_stripe(pvHLoad.mem, segWidth, segLen, i);
fprintf(stderr, "*******i(p): %d*******\n", i);
output_stripe(vP, segWidth, segLen, i);
fprintf(stderr, "*******i(e): %d*******\n", i);
output_stripe(pvE.mem, segWidth, segLen, i);
fprintf(stderr, "segLen: %d, segWidth: %d\n", segLen, segWidth);
}
memcpy(pvE_tmp.mem, pvE.mem, segLen*sizeof(__m128i));
/* inner loop to process the query sequence */
for (j=0; j<segLen; ++j)
{
// At the beginning of the loop: eh[j] = { H(i-1,j-1), E(i,j) }, f = F(i,j) and h1 = H(i,j-1)
// Cells are computed in the following order:
// M(i,j) = H(i-1,j-1) + S(i,j)
// H(i,j) = max{M(i,j), E(i,j), F(i,j)}
// E(i+1,j) = max{M(i,j)-gapo, E(i,j)} - gape
// F(i,j+1) = max{M(i,j)-gapo, F(i,j)} - gape
// We have to separate M(i,j); otherwise the direction may not be recorded correctly.
// However, a CIGAR like "10M3I3D10M" allowed by local() is disallowed by global().
// Such a CIGAR may occur, in theory, if mismatch_penalty > 2*gap_ext_penalty + 2*gap_open_penalty/k.
// In practice, this should happen very rarely given a reasonable scoring system.
///M(i,j) = H(i-1,j-1) + S(i,j)
vHm = _mm_add_epi16(vH, _mm_load_si128(vP + j));
///E(i, j)
vE = _mm_load_si128(pvE.mem + j);
///H(i,j) = max{M(i,j), E(i,j), F(i,j)}
vH = _mm_max_epi16(vHm, vE);
vH = _mm_max_epi16(vH, vF);
_mm_store_si128(pvHStore.mem + j, vH);
///M(i,j)-gapo-gape
vHm = _mm_sub_epi16(vHm, vGapOE);
///E(i,j)-gape
vE = _mm_sub_epi16(vE, vGapE);
///E(i+1,j) = max{M(i,j)-gapo, E(i,j)} - gape
vE = _mm_max_epi16(vE, vHm);
_mm_store_si128(pvE.mem + j, vE);
// if (i == 2)
// {
// fprintf(stderr, "*******-i(f): %d, j: %d*******\n", i, j);
// output_stripe_single_word(vF, segLen, i, j);
// }
///F(i,j) - gape
vF = _mm_sub_epi16(vF, vGapE);
///F(i,j+1) = max{M(i,j)-gapo, F(i,j)} - gape
vF = _mm_max_epi16(vF, vHm);
// if (i == 2)
// {
// fprintf(stderr, "*******+i(f): %d, j: %d*******\n", i, j+1);
// output_stripe_single_word(vF, segLen, i, j+1);
// }
/* Load the next vH. */
vH = _mm_load_si128(pvHLoad.mem + j);
}
///下面correct步不涉及到vP和vE, 所以应该没关系
/* Lazy_F loop: has been revised to disallow adjecent insertion and
* then deletion, so don't update E(i, i), learn from SWPS3 */
// M(i,j) = H(i-1,j-1) + S(i,j)
// H(i,j) = max{M(i,j), E(i,j), F(i,j)}
// E(i+1,j) = max{M(i,j)-gapo, E(i,j)} - gape
// F(i,j+1) = max{M(i,j)-gapo, F(i,j)} - gape
///外循环是单个sse vector的长度
///不更新E会导致E偏小
/**
fprintf(stderr, "*******i(h)+: %d*******\n", i);
output_stripe(pvHStore.mem, segWidth, segLen, i);
**/
for (k=0; k<segWidth; ++k)
{
///此时的vF是最后一个vF sse vector
///现在要用这个vF sse vector来矫正第一个vF sse vector
vF = _mm_slli_si128(vF, 2);
vF = _mm_insert_epi16(vF, MINUS_INF_SSE, 0);
///刚开始的时候是vF[0, j]
///内循环是sse vector的个数
for (j=0; j<segLen; ++j)
{
/**
if (i == 2)
{
fprintf(stderr, "*******new sbsb vF, i: %d, j: %d*******\n", i, j);
output_stripe_single_word(vF, segLen, i, j);
}
**/
/// H(i,j) = max{M(i,j), E(i,j), F(i,j)}
///此时H(i,j)至少是max{M(i,j), E(i,j)}
///所以只要和F(i,j)}比大小
// vH = _mm_load_si128(pvHStore.mem + j);
// if (i == 2)
// {
// fprintf(stderr, "*******new sbsb vH+, i: %d, j: %d*******\n", i, j);
// output_stripe_single_word(vH, segLen, i, j);
// }
// vH = _mm_max_epi16(vH,vF);
// if (i == 2)
// {
// fprintf(stderr, "*******new sbsb vH-, i: %d, j: %d*******\n", i, j);
// output_stripe_single_word(vH, segLen, i, j);
// }
// _mm_store_si128(pvHStore.mem + j, vH);
// ///M(i,j) = H(i-1,j-1) + S(i,j)
// vHm = _mm_load_si128(pvHLoad.mem + j);
// vH = _mm_add_epi16(vHm, _mm_load_si128(vP + j));
///M(i,j) = H(i-1,j-1) + S(i,j)
vHm = _mm_load_si128(pvHLoad.mem + j);
vH = _mm_add_epi16(vHm, _mm_load_si128(vP + j));
vE = _mm_load_si128(pvE_tmp.mem + j);
/// H(i,j) = max{M(i,j), E(i,j), F(i,j)}
vHm = _mm_max_epi16(vH,vF);
vHm = _mm_max_epi16(vHm,vE);
_mm_store_si128(pvHStore.mem + j, vHm);
/**
if (i == 2)
{
fprintf(stderr, "*******new sbsb vH+, i: %d, j: %d*******\n", i, j);
output_stripe_single_word(vH, segLen, i, j);
}
**/
///M(i,j) = H(i-1,j-1) + S(i,j)
// vHm = _mm_load_si128(pvHLoad.mem + j);
// vH = _mm_add_epi16(vHm, _mm_load_si128(vP + j));
/// F(i,j+1) = max{M(i,j)-gapo, F(i,j)} - gape
vH = _mm_sub_epi16(vH, vGapOE);
vF = _mm_sub_epi16(vF, vGapE);
///_mm_cmpgt_epi16(vF, vH): 如果vF > vH, 则返回结果全是1
//_mm_movemask_epi8等于就是提取元素了呗
///只要vH里所有元素都>=vF, 就跳出循环
if (! _mm_movemask_epi8(_mm_cmpgt_epi16(vF, vH)))
{
/**
vF = _mm_max_epi16(vF, vH);
goto end;
**/
///fprintf(stderr, "haha\n");
}
vF = _mm_max_epi16(vF, vH);
}
}
end:
/**
fprintf(stderr, "*******i(h)-: %d*******\n", i);
output_stripe(pvHStore.mem, segWidth, segLen, i);
**/
///pvE和vP最好都要动一下
///pvE好办,但是vP难办啊, 因为vP有m个
for (k=0; k<m; ++k)
{
_mm_store_si128(vProfile_tmp.mem+k, _mm_srli_si128(vProfile.mem[k * segLen], 2));
j = segWidth*segLen+i;
const int8_t *p = &mat[k * m];
tmp_score = j >= qlen ? 0 : p[seq_nt4_table[(int)query[j]]];
vProfile_tmp.mem[k] = _mm_insert_epi16(vProfile_tmp.mem[k], tmp_score, 7);
}
tmp_E = _mm_srli_si128(pvE.mem[0], 2);
tmp_E = _mm_insert_epi16(tmp_E, MINUS_INF_SSE, 7);
__m128i* tmp_vP;
for (j=1; j<segLen; ++j)
{
_mm_store_si128(pvE.mem + j - 1, pvE.mem[j]);
for (k=0; k<m; ++k)
{
tmp_vP = vProfile.mem + k * segLen;
_mm_store_si128(tmp_vP + j - 1, tmp_vP[j]);
}
}
_mm_store_si128(pvE.mem + segLen-1, tmp_E);
for (k=0; k<m; ++k)
{
tmp_vP = vProfile.mem + k * segLen;
_mm_store_si128(tmp_vP+segLen-1, vProfile_tmp.mem[k]);
}
}
fprintf(stderr, "*******i(h): %d*******\n", i);
output_stripe(pvHStore.mem, segWidth, segLen, i);
int16_t *t = (int16_t*)pvHStore.mem;
score = t[0];
for (i = 0; i < band_length; i++)
{
if (t[i] > score)
{
score = t[i];
}
}
free_16bit_aligned(&vProfile);
free_16bit_aligned(&vProfile_tmp);
free_16bit_aligned(&pvHStore);
free_16bit_aligned(&pvHLoad);
free_16bit_aligned(&pvE);
free_16bit_aligned(&pvE_tmp);
qry->score = score;
return score;
}
///qlen = tlen + 2*w
int ksw_semi_global_sse_debug(int qlen, const char *query, int tlen, const char *target, int m, const int8_t *mat,
int gapo, int gape, int w, kswr_t* qry, int *n_cigar_, uint32_t **cigar_)
{
int beg, end;
int i, j, k = MINUS_INF_SSE, gapoe = gapo + gape, score;
///可以把band_length扩展到能被4整除的数
int band_length = 2*w+1;
///单个sse element大小为16 bit, 所以sse vector中共有8个元素
const int32_t segWidth = 8; /* number of values in vector unit */
///sse vector的个数
const int32_t segLen = (band_length + segWidth - 1) / segWidth;
aligned_sse vProfile, vProfile_tmp, pvHStore, pvHLoad, pvE, pvE_tmp;
malloc_16bit_aligned(&vProfile, m*segLen);
malloc_16bit_aligned(&vProfile_tmp, m);
malloc_16bit_aligned(&pvHStore, m*segLen);
malloc_16bit_aligned(&pvHLoad, m*segLen);
malloc_16bit_aligned(&pvE, m*segLen);
malloc_16bit_aligned(&pvE_tmp, m*segLen);
__m128i vNegLimit = _mm_set1_epi16(MINUS_INF_SSE);
const __m128i vGapOE = _mm_set1_epi16((short)gapoe);
const __m128i vGapE = _mm_set1_epi16((short)gape);
int segNum = 0;
int index = 0;
///n是字符集大小
///band_length可以向上扩展到4的倍数
for (k=0; k<m; ++k)
{
const int8_t *p = &mat[k * m];
///sse vector个数
for (i=0; i<segLen; ++i)
{
///是个union
__m128i_16_t t;
j = i;
for (segNum=0; segNum<segWidth; ++segNum)
{
///注意这里是qlen, 而不是band_length
t.v[segNum] = j >= qlen ? 0 : p[seq_nt4_table[(int)query[j]]];
j += segLen;
}
_mm_store_si128(&vProfile.mem[index], t.m);
++index;
}
}
/* initialize H and E */
__m128i_16_t h;
__m128i_16_t e;
for (segNum=0; segNum<segWidth; ++segNum) {
h.v[segNum] = 0;
e.v[segNum] = h.v[segNum] - gapoe;
///e.v[segNum] = h.v[segNum] - gapo;
}
index = 0;
for (i=0; i<segLen; ++i) {
_mm_store_si128(&pvHStore.mem[index], h.m);
_mm_store_si128(&pvE.mem[index], e.m);
++index;
}
__m128i tmp_E;
int8_t tmp_score;
for (i = 0; LIKELY(i < tlen); ++i) // target sequence is in the outer loop
{
__m128i vE;
/* Initialize F value to -inf. Any errors to vH values will be
* corrected in the Lazy_F loop. */
__m128i vF = vNegLimit;
__m128i vH = pvHStore.mem[0];
__m128i vHm;
/* Correct part of the vProfile */
//const __m128i* vP = vProfile.mem + seq_nt4_table[(int)target[i]] * segLen;
__m128i* vP = vProfile.mem + seq_nt4_table[(int)target[i]] * segLen;
/* Swap the 2 H buffers. */
__m128i* pv = pvHLoad.mem;
pvHLoad.mem = pvHStore.mem;
pvHStore.mem = pv;
// if (i == 149)
// {
// fprintf(stderr, "*******i(h): %d*******\n", i);
// output_stripe(pvHLoad.mem, segWidth, segLen, i);
// fprintf(stderr, "*******i(p): %d*******\n", i);
// output_stripe(vP, segWidth, segLen, i);
// fprintf(stderr, "*******i(e): %d*******\n", i);
// output_stripe(pvE.mem, segWidth, segLen, i);
// fprintf(stderr, "segLen: %d, segWidth: %d\n", segLen, segWidth);
// }
memcpy(pvE_tmp.mem, pvE.mem, segLen*sizeof(__m128i));
/* inner loop to process the query sequence */
for (j=0; j<segLen; ++j)
{
// At the beginning of the loop: eh[j] = { H(i-1,j-1), E(i,j) }, f = F(i,j) and h1 = H(i,j-1)
// Cells are computed in the following order:
// M(i,j) = H(i-1,j-1) + S(i,j)
// H(i,j) = max{M(i,j), E(i,j), F(i,j)}
// E(i+1,j) = max{M(i,j)-gapo, E(i,j)} - gape
// F(i,j+1) = max{M(i,j)-gapo, F(i,j)} - gape
// We have to separate M(i,j); otherwise the direction may not be recorded correctly.
// However, a CIGAR like "10M3I3D10M" allowed by local() is disallowed by global().
// Such a CIGAR may occur, in theory, if mismatch_penalty > 2*gap_ext_penalty + 2*gap_open_penalty/k.
// In practice, this should happen very rarely given a reasonable scoring system.
///M(i,j) = H(i-1,j-1) + S(i,j)
vHm = _mm_add_epi16(vH, _mm_load_si128(vP + j));
///E(i, j)
vE = _mm_load_si128(pvE.mem + j);
///H(i,j) = max{M(i,j), E(i,j), F(i,j)}
vH = _mm_max_epi16(vHm, vE);
vH = _mm_max_epi16(vH, vF);
_mm_store_si128(pvHStore.mem + j, vH);
///M(i,j)-gapo-gape
vHm = _mm_sub_epi16(vHm, vGapOE);
///E(i,j)-gape
vE = _mm_sub_epi16(vE, vGapE);
///E(i+1,j) = max{M(i,j)-gapo, E(i,j)} - gape
vE = _mm_max_epi16(vE, vHm);
_mm_store_si128(pvE.mem + j, vE);
// if (i == 2)
// {
// fprintf(stderr, "*******-i(f): %d, j: %d*******\n", i, j);
// output_stripe_single_word(vF, segLen, i, j);
// }
///F(i,j) - gape
vF = _mm_sub_epi16(vF, vGapE);
///F(i,j+1) = max{M(i,j)-gapo, F(i,j)} - gape
vF = _mm_max_epi16(vF, vHm);
// if (i == 2)
// {
// fprintf(stderr, "*******+i(f): %d, j: %d*******\n", i, j+1);
// output_stripe_single_word(vF, segLen, i, j+1);
// }
/* Load the next vH. */
vH = _mm_load_si128(pvHLoad.mem + j);
}
///下面correct步不涉及到vP和vE, 所以应该没关系
/* Lazy_F loop: has been revised to disallow adjecent insertion and
* then deletion, so don't update E(i, i), learn from SWPS3 */
// M(i,j) = H(i-1,j-1) + S(i,j)
// H(i,j) = max{M(i,j), E(i,j), F(i,j)}
// E(i+1,j) = max{M(i,j)-gapo, E(i,j)} - gape
// F(i,j+1) = max{M(i,j)-gapo, F(i,j)} - gape
///外循环是单个sse vector的长度
///不更新E会导致E偏小
/**
fprintf(stderr, "*******i(h)+: %d*******\n", i);
output_stripe(pvHStore.mem, segWidth, segLen, i);
**/
for (k=0; k<segWidth; ++k)
{
///此时的vF是最后一个vF sse vector
///现在要用这个vF sse vector来矫正第一个vF sse vector
vF = _mm_slli_si128(vF, 2);
vF = _mm_insert_epi16(vF, MINUS_INF_SSE, 0);
///刚开始的时候是vF[0, j]
///内循环是sse vector的个数
for (j=0; j<segLen; ++j)
{
// if (i == 2)
// {
// fprintf(stderr, "*******new vF, i: %d, j: %d*******\n", i, j);
// output_stripe_single_word(vF, segLen, i, j);
// }
/**
/// H(i,j) = max{M(i,j), E(i,j), F(i,j)}
///此时H(i,j)至少是max{M(i,j), E(i,j)}
///所以只要和F(i,j)}比大小
vH = _mm_load_si128(pvHStore.mem + j);
vH = _mm_max_epi16(vH,vF);
_mm_store_si128(pvHStore.mem + j, vH);
**/
///M(i,j) = H(i-1,j-1) + S(i,j)
vHm = _mm_load_si128(pvHLoad.mem + j);
vH = _mm_add_epi16(vHm, _mm_load_si128(vP + j));
vE = _mm_load_si128(pvE_tmp.mem + j);
/// H(i,j) = max{M(i,j), E(i,j), F(i,j)}
vHm = _mm_max_epi16(vH,vF);
vHm = _mm_max_epi16(vHm,vE);
_mm_store_si128(pvHStore.mem + j, vHm);
/// F(i,j+1) = max{M(i,j)-gapo, F(i,j)} - gape
vH = _mm_sub_epi16(vH, vGapOE);
vF = _mm_sub_epi16(vF, vGapE);
///_mm_cmpgt_epi16(vF, vH): 如果vF > vH, 则返回结果全是1
//_mm_movemask_epi8等于就是提取元素了呗
///只要vH里所有元素都>=vF, 就跳出循环
if (! _mm_movemask_epi8(_mm_cmpgt_epi16(vF, vH)))
{
goto end;
///fprintf(stderr, "haha\n");
}
vF = _mm_max_epi16(vF, vH);
}
}
end:
/**
fprintf(stderr, "*******i(h)-: %d*******\n", i);
output_stripe(pvHStore.mem, segWidth, segLen, i);
**/
///pvE和vP最好都要动一下
///pvE好办,但是vP难办啊, 因为vP有m个
for (k=0; k<m; ++k)
{
_mm_store_si128(vProfile_tmp.mem+k, _mm_srli_si128(vProfile.mem[k * segLen], 2));
j = segWidth*segLen+i;
const int8_t *p = &mat[k * m];
tmp_score = j >= qlen ? 0 : p[seq_nt4_table[(int)query[j]]];
vProfile_tmp.mem[k] = _mm_insert_epi16(vProfile_tmp.mem[k], tmp_score, 7);
}
tmp_E = _mm_srli_si128(pvE.mem[0], 2);
tmp_E = _mm_insert_epi16(tmp_E, MINUS_INF_SSE, 7);
__m128i* tmp_vP;
for (j=1; j<segLen; ++j)
{
_mm_store_si128(pvE.mem + j - 1, pvE.mem[j]);
for (k=0; k<m; ++k)
{
tmp_vP = vProfile.mem + k * segLen;
_mm_store_si128(tmp_vP + j - 1, tmp_vP[j]);
}
}
_mm_store_si128(pvE.mem + segLen-1, tmp_E);
for (k=0; k<m; ++k)
{
tmp_vP = vProfile.mem + k * segLen;
_mm_store_si128(tmp_vP+segLen-1, vProfile_tmp.mem[k]);
}