-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscalar.cc
176 lines (150 loc) · 4.49 KB
/
scalar.cc
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
/**
* @file scalar.cc
*
* @brief scalar parallelization of the standard banded matrix
*/
#include <string.h>
#include "sse.h"
#include "util.h"
#include "log.h"
#define MIN ( 0 )
#define OFS ( 32768 )
#define roundup(a, bound) ( (((a) + (bound) - 1) / (bound)) * (bound) )
/**
* @fn scalar_affine
*/
int
scalar_affine(
void *work,
char const *a,
uint64_t alen,
char const *b,
uint64_t blen,
int8_t *score_matrix, int8_t gi, int8_t ge, int16_t xt, uint32_t bw)
{
if(alen == 0 || blen == 0) { return(0); }
debug("%s, %s", a, b);
/* s: score vector, e: horizontal gap, f: scalarical gap */
#define _s(_p, _i) ( (_p)[ (_i)] )
#define _e(_p, _i) ( (_p)[2 * bw + (_i)] )
#define _f(_p, _i) ( (_p)[4 * bw + (_i)] )
#define _vlen() ( 6 * bw )
uint8_t c[2 * bw + vec::LEN];
/* init the leftmost vector (scalarically placed) */
uint16_t *base = (uint16_t *)((uint8_t *)work + sizeof(maxpos_t)), *curr = base, *prev = base;
#define _gap(_i) ( ((_i) > 0 ? gi : 0) + (_i) * ge )
for(uint64_t i = 0; i < 2 * bw; i++) {
if(i < bw) {
_s(curr, i) = _e(curr, i) = _f(curr, i) = 0;
c[i + 1] = 0;
} else {
_s(curr, i) = _f(curr, i) = OFS + _gap(i - bw);
_e(curr, i) = 0;
c[i + 1] = (i - bw) < blen ? encode_b(b[i - bw]) : encode_n();
}
}
_e(curr, bw) = OFS + gi; /* fix gap cells at (0, 0) */
_f(curr, bw) = OFS + gi;
int32_t max = OFS;
uint64_t amax = 0, bmax = 0; /* max score and its position */
for(uint64_t apos = 0; apos < alen; apos++) {
debug("apos(%llu)", apos);
int8_t ach = encode_a(a[apos]);
prev = curr; curr += _vlen();
/* fetch the next base */
c[2 * bw] = (apos + bw - 1) < blen ? encode_b(b[apos + bw - 1]) : encode_n();
/* init f */
int32_t pf = 0, pv = 0, pd = _s(prev, 0);
/* bpos = apos + bofs - bw/2 */
for(uint64_t bofs = 0; bofs < 2 * bw; bofs++) {
/* load prev vectors */
int32_t ph = bofs < (2 * bw - 1) ? _s(prev, bofs + 1) : 0;
int32_t pe = bofs < (2 * bw - 1) ? _e(prev, bofs + 1) : 0;
int32_t score = score_matrix[ach | c[bofs + 1]];
debug("(%c, %c), pd(%d), ph(%d), pe(%d), score(%d)", "ACGT"[ach], "ACGT"[c[bofs + 1]>>2], pd - OFS, ph - OFS, pe - OFS, score - OFS);
c[bofs] = c[bofs + 1]; /* shift by one */
pe = MAX3(0, ph + gi, pe) + ge;
pf = MAX3(0, pv + gi, pf) + ge;
pv = MAX4(0, pd + score, pe, pf);
_s(curr, bofs) = pv < 0 ? 0 : pv;
_e(curr, bofs) = pe < 0 ? 0 : pe;
_f(curr, bofs) = pf < 0 ? 0 : pf;
if(pv > max) { max = pv; amax = apos + 1; bmax = bofs - bw + apos + 1; }
debug("apos(%llu), bofs(%llu), e(%d), f(%d), s(%d), max(%d)", apos, bofs, pe - OFS, pf - OFS, pv - OFS, max - OFS);
pd = ph;
}
/* update maxpos */
debug("max(%d)", max - OFS);
}
/* save the maxpos */
maxpos_t *r = (maxpos_t *)work;
r->alen = alen;
r->blen = blen;
r->apos = amax;
r->bpos = bmax;
#ifdef debug
r->ccnt = alen * 2 * bw;
r->fcnt = 0;
#endif
return(max - OFS);
}
#ifdef MAIN
#include <assert.h>
#include <stdlib.h>
int main_ext(int argc, char *argv[])
{
uint64_t alen = strlen(argv[2]);
uint64_t blen = strlen(argv[3]);
char *a = (char *)malloc(alen + vec::LEN + 1);
char *b = (char *)malloc(blen + vec::LEN + 1);
memcpy(a, argv[2], alen);
memset(a + alen, 0, vec::LEN + 1);
memcpy(b, argv[3], blen);
memset(b + blen, 0x80, vec::LEN + 1);
int8_t score_matrix[16] __attribute__(( aligned(16) ));
build_score_matrix(score_matrix, atoi(argv[4]), atoi(argv[5]));
void *work = aligned_malloc(128 * 1024 * 1024, 16);
if(0) {
printf("./a.out AAA AAA 2 -3 -5 -1 30\n");
}
int score = scalar_affine(
work,
a, alen, b, blen,
score_matrix,
atoi(argv[6]),
atoi(argv[7]),
atoi(argv[8]),
32);
printf("%d\n", score);
free(a); free(b); free(work);
return(0);
}
int main(int argc, char *argv[])
{
if(argc > 1) { return(main_ext(argc, argv)); }
int8_t score_matrix[16] __attribute__(( aligned(16) ));
build_score_matrix(score_matrix, 1, -1);
void *work = aligned_malloc(128 * 1024 * 1024, 16);
#define a(s, p, q) { \
assert(scalar_affine(work, p, strlen(p), q, strlen(q), score_matrix, -1, -1, 10, 32) == (s)); \
}
a( 0, "", "");
a( 0, "A", "");
a( 1, "A", "A");
a( 3, "AAA", "AAA");
a( 0, "AAA", "TTT");
a( 3, "AAAGGG", "AAATTTTTT");
a( 3, "TTTGGGGGAAAA", "TTTCCCCCCCCAAAA");
a( 4, "TTTAAAA", "TTTCCAAAA");
a( 4, "GGGCCCC", "GGGAACCCC");
a( 6, "AAACAAAAAGGG", "AAAAAAAATTTTTTT");
a( 6, "AAAAAAAATTTTTTT", "AAACAAAAAGGG");
a( 5, "AAACCAAAAAGGG", "AAAAAAAATTTTTTT");
a( 5, "AAAAAAAATTTTTTT", "AAACCAAAAAGGG");
free(work);
return(0);
}
#endif
/**
* end of scalar.cc
*/