-
Notifications
You must be signed in to change notification settings - Fork 4
/
align_vec.h
379 lines (275 loc) · 12 KB
/
align_vec.h
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
/*
* Copyright (C) 2009-2012 Simon A. Berger
*
* This file is part of papara.
*
* papara 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.
*
* papara 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 papara. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __align_vec_h
#define __align_vec_h
#include "vec_unit.h"
#include "ivymike/aligned_buffer.h"
#include "ivymike/fasta.h"
using ivy_mike::aligned_buffer;
using ivy_mike::scoring_matrix;
template <class score_t>
struct persistent_state {
aligned_buffer<score_t> out;
aligned_buffer<score_t> s;
aligned_buffer<score_t> si;
};
//#define LIKELY(x) __builtin_expect((x),1)
// #define LIKELY(x) (x)
template <class score_t, class sscore_t, size_t W>
void align_vec( persistent_state<score_t> &ps, size_t asize, const std::vector<uint8_t> &b, const scoring_matrix &m, aligned_buffer<sscore_t> &qprofile, const sscore_t gap_open, const sscore_t gap_extend, std::vector<int> &out ) {
typedef vector_unit<score_t,W> vu;
typedef typename vu::vec_t vec_t;
// assert( a.size() % W == 0 );
// const size_t asize = a.size() / W;
// std::vector<score_t> s( a.size());
// std::vector<score_t> si( a.size());
aligned_buffer<score_t> &s = ps.s;
aligned_buffer<score_t> &si = ps.si;
aligned_buffer<score_t> pb(W);
if( s.size() < asize * W ) {
s.resize( asize * W );
si.resize( asize * W );
}
const score_t bias = vu::BIAS;
std::fill( s.begin(), s.end(), bias );
std::fill( si.begin(), si.end(), bias );
const score_t SMALL = vu::SMALL_VALUE;
vec_t max_vec = vu::set1(SMALL);
const vec_t GAP_EXT_vec = vu::set1(score_t(-gap_extend)); // these values are _subtracted_ from the score. so use negative of user parameters!
const vec_t GAP_OPEN_vec = vu::set1(score_t(-gap_open));
const vec_t BIAS_vec = vu::set1( vu::BIAS );
// vec_t len_vec = vu::load( len.m_ptr );
#define LOCAL_ALIGN
for( size_t ib = 0; ib < b.size(); ib++ ) {
#ifndef LOCAL_ALIGN
const bool lastrow = ib == (b.size() - 1);
#endif
char bc = b[ib];
// std::cout << "bc: " << bc << std::endl;
assert( bc < char(qprofile.size() / (W * asize)));
vec_t last_sl = vu::set1(SMALL);
vec_t last_s = vu::set1(vu::BIAS);
vec_t last_sdiag = vu::set1(vu::BIAS);
// std::cout << "sbm: " << m.state_backmap(bc) << " " << (W * asize) << std::endl;
// sscore_t *qpp_iter = qprofile(m.state_backmap(bc) * W * asize);
sscore_t *qpp_iter = qprofile( bc * W * asize);
score_t * __restrict s_iter = s.base();
score_t * __restrict su_iter = si.base();
score_t * __restrict s_end = s_iter + (asize * W);
for( ; s_iter != s_end; s_iter += W, su_iter += W, qpp_iter += W ) {
const vec_t match = vu::load( (score_t*) qpp_iter );
// getchar();
const vec_t sm = vu::add( last_sdiag, match );
last_sdiag = vu::load( s_iter );
#ifdef LOCAL_ALIGN
const vec_t sm_zero = vu::max( sm, BIAS_vec);
#else
const vec_t sm_zero = sm_vec;
#endif
const vec_t sl_open = vu::sub( last_s, GAP_OPEN_vec );
const vec_t sl_ext = vu::sub( last_sl, GAP_EXT_vec );
const vec_t sl = vu::max( sl_ext, sl_open );
last_sl = sl;
const vec_t su_ext = vu::sub( vu::load(su_iter), GAP_EXT_vec );
const vec_t su_open = vu::sub( last_sdiag, GAP_OPEN_vec );
const vec_t su = vu::max( su_ext, su_open );
vu::store( su, su_iter );
const vec_t s = vu::max( sm_zero, vu::max( sl, su ));
// vu::println( max_vec, pb.m_ptr );
last_s = s;
vu::store( s, s_iter );
#ifdef LOCAL_ALIGN
max_vec = vu::max( s, max_vec );
#else
if( s_iter == s_end - W || lastrow ) {
max_vec = vu::max( sc_vec, max_vec );
}
#endif
}
}
ps.out.resize(W);
vu::store( max_vec, ps.out.base() );
out.resize(0);
out.reserve(W);
for( size_t i = 0; i < W; i++ ) {
out.push_back(int(ps.out[i]) - vu::BIAS );
// std::cout << "x: " << int(ps.out.m_ptr[i]) << "\n";
}
//return max;
}
template<typename score_t, typename sscore_t>
struct block_cont {
score_t * __restrict s_iter;
score_t * __restrict su_iter;
score_t * __restrict s_end;
sscore_t * __restrict qpp_iter;
};
template <class score_t, class sscore_t>
struct persistent_state_blocked {
aligned_buffer<score_t> out;
aligned_buffer<score_t> s;
aligned_buffer<score_t> si;
aligned_buffer<score_t> bcv_store;
std::vector<block_cont<score_t, sscore_t> > bc_array;
};
template <class score_t, class sscore_t, size_t W, size_t BW>
void align_vec_blocked( persistent_state_blocked<score_t,sscore_t> &ps, size_t asize, const std::vector<uint8_t> &b, const scoring_matrix &m, aligned_buffer<sscore_t> &qprofile, const sscore_t gap_open, const sscore_t gap_extend, std::vector<int> &out ) {
typedef vector_unit<score_t,W> vu;
typedef typename vu::vec_t vec_t;
// assert( a.size() % W == 0 );
// const size_t asize = a.size() / W;
// std::vector<score_t> s( a.size());
// std::vector<score_t> si( a.size());
aligned_buffer<score_t> &s = ps.s;
aligned_buffer<score_t> &si = ps.si;
// aligned_buffer<score_t> pb(W);
if( s.size() < asize * W ) {
s.resize( asize * W );
si.resize( asize * W );
}
const score_t bias = vu::BIAS;
std::fill( s.begin(), s.end(), bias );
std::fill( si.begin(), si.end(), bias );
const score_t SMALL = vu::SMALL_VALUE;
vec_t max_vec = vu::set1(SMALL);
const vec_t GAP_EXT_vec = vu::set1(score_t(-gap_extend)); // these values are _subtracted_ from the score. so use negative of user parameters!
const vec_t GAP_OPEN_vec = vu::set1(score_t(-gap_open));
const vec_t BIAS_vec = vu::set1( vu::BIAS );
struct block_cont_vec {
vec_t last_sl;
vec_t last_s;
vec_t last_sdiag;
void init() {
last_sl = vu::set1(vu::SMALL_VALUE);
last_s = vu::set1(vu::BIAS);
last_sdiag = vu::set1(vu::BIAS);
}
void load( score_t *ptr ) {
last_sl = vu::load( ptr );
last_s = vu::load( ptr + W );
last_sdiag = vu::load( ptr + W + W );
}
void store( score_t *ptr ) {
vu::store( last_sl, ptr );
vu::store( last_s, ptr + W );
vu::store( last_sdiag, ptr + W + W );
}
};
size_t num_blocks = asize / BW;
if( asize % BW != 0 ) {
num_blocks++;
}
//aligned_buffer<score_t> bcv_store( b.size() * W * 3 );
aligned_buffer<score_t> &bcv_store = ps.bcv_store;
std::vector<block_cont<score_t, sscore_t> > &bc_array = ps.bc_array;
if( bcv_store.size() < b.size() * W * 3 ) {
bcv_store.resize(b.size() * W * 3);
bc_array.resize( b.size() );
}
//std::vector<block_cont> bc_array(b.size());
// vec_t len_vec = vu::load( len.m_ptr );
for( size_t iblock = 0; iblock < num_blocks; ++iblock ) {
block_cont_vec bcv_;
#define LOCAL_ALIGN
for( size_t ib = 0; ib < b.size(); ib++ ) {
#ifndef LOCAL_ALIGN
const bool lastrow = ib == (b.size() - 1);
#endif
block_cont<score_t, sscore_t> &bc_ = bc_array[ib];
if( iblock == 0 ) {
const char bchar = b[ib];
assert( bchar < char(qprofile.size() / (W * asize)));
bcv_.init();
bc_.s_iter = s.base();
bc_.su_iter = si.base();
bc_.s_end = bc_.s_iter + (asize * W);
bc_.qpp_iter = qprofile( bchar * W * asize );
} else {
bcv_.load( bcv_store( ib * W * 3 ) );
}
// std::cout << "bc: " << bc << std::endl;
// vec_t last_sl = vu::set1(SMALL);
// vec_t last_s = vu::set1(vu::BIAS);
// vec_t last_sdiag = vu::set1(vu::BIAS);
// std::cout << "sbm: " << m.state_backmap(bc) << " " << (W * asize) << std::endl;
sscore_t *qpp_iter = bc_.qpp_iter;
_mm_prefetch( (const char *) qpp_iter,_MM_HINT_T0);
score_t * __restrict s_iter = bc_.s_iter;
score_t * __restrict su_iter = bc_.su_iter;
score_t * __restrict s_end = bc_.s_end;
vec_t last_sdiag = bcv_.last_sdiag;
vec_t last_sl = bcv_.last_sl;
vec_t last_s = bcv_.last_s;
score_t * __restrict s_block_end;// = std::min( bc.s_iter + BW * W, bc.s_end );
if( s_iter + BW * W < s_end ) {
s_block_end = s_iter + BW * W;
} else {
s_block_end = s_end;
}
assert( s_block_end != s_iter );
for( ; s_iter != s_block_end; s_iter += W, su_iter += W, qpp_iter += W ) {
const vec_t match = vu::load( (score_t*) qpp_iter );
// getchar();
const vec_t sm = vu::add( last_sdiag, match );
last_sdiag = vu::load( s_iter );
#ifdef LOCAL_ALIGN
const vec_t sm_zero = vu::max( sm, BIAS_vec);
#else
const vec_t sm_zero = sm_vec;
#endif
const vec_t sl_open = vu::sub( last_s, GAP_OPEN_vec );
const vec_t sl_ext = vu::sub( last_sl, GAP_EXT_vec );
const vec_t sl = vu::max( sl_ext, sl_open );
last_sl = sl;
const vec_t su_ext = vu::sub( vu::load(su_iter), GAP_EXT_vec );
const vec_t su_open = vu::sub( last_sdiag, GAP_OPEN_vec );
const vec_t su = vu::max( su_ext, su_open );
vu::store( su, su_iter );
const vec_t s = vu::max( sm_zero, vu::max( sl, su ));
// vu::println( max_vec, pb.m_ptr );
last_s = s;
vu::store( s, s_iter );
#ifdef LOCAL_ALIGN
max_vec = vu::max( s, max_vec );
#else
if( s_iter == s_end - W || lastrow ) {
max_vec = vu::max( sc_vec, max_vec );
}
#endif
}
bcv_.last_sdiag = last_sdiag;
bcv_.last_sl = last_sl;
bcv_.last_s = last_s;
bcv_.store( bcv_store( ib * W * 3 ) );
bc_.qpp_iter = qpp_iter;
bc_.s_iter = s_iter;
bc_.su_iter = su_iter;
}
}
ps.out.resize(W);
vu::store( max_vec, ps.out.base() );
out.resize(0);
out.reserve(W);
for( size_t i = 0; i < W; i++ ) {
out.push_back(int(ps.out[i]) - vu::BIAS );
// std::cout << "x: " << int(ps.out.m_ptr[i]) << "\n";
}
//return max;
}
#endif