Debian Codegraph @

hmmer/src/impl_neon/ssvfilter.c

raw symbols SCIP
1 //#define ARMDEBUG 1
2 /* The SSV filter implementation; NEON version.
3 *
4 * Contents:
5 * 1. Introduction
6 * 2. p7_SSVFilter() implementation
7 *
8 * Bjarne Knudsen, CLC Bio
9 */
10
11 /*****************************************************************
12 * 1. Introduction
13 *****************************************************************/
14
15 /* Here is a description of the major ideas going into this
16 * implementation of the SSV filter.
17 *
18 *
19 * REMOVING THE J STATE
20 * ====================
21 *
22 * The original MSV filter allows use of the J state to chain together
23 * multiple matches in different diagonals. Thus, a full match can
24 * consist of diagonal match followed by the J state and then another
25 * diagonal match later in the sequence. Going through the J state
26 * has a certain price so for the full match to contain two different
27 * diagonal matches connected by the J state, each of the individual
28 * diagonal matches must score higher than the cost of going through
29 * the J state.
30 *
31 * It turns out that even the best match in a model-sequence
32 * comparison rarely scores higher than the cost of going through the
33 * J state. This is the basis of the idea used here, which is to
34 * completely ignore the J state. To avoid this leading to false
35 * negatives, we check the resulting maximum score against the cost of
36 * the going through the J state. In the rare cases where the J state
37 * may in fact have been used, we return eslNORESULT. This indicates
38 * to the original J state that it should recalculate the score.
39 *
40 * Since removing the J state allows significant improvements in
41 * speed, the extra overhead of having to go through the original MSV
42 * filter in about 1% of the cases is not a problem.
43 *
44 * Note that for the score to actually be different, we need two
45 * diagonals to have a high scoring match, but we cannot easily check
46 * for that. Thus, oftentimes the re-calculated score in the original
47 * MSV filter will be the same as without the J state.
48 *
49 * The code governing the use of the J state in the original filter is:
50 *
51 * xEv = _mm_subs_epu8(xEv, tecv);
52 * xJv = _mm_max_epu8(xJv,xEv);
53 * xBv = _mm_max_epu8(basev, xJv);
54 *
55 * So for an xE value to be high enough to affect xJ, the following
56 * inequality must be true:
57 *
58 * xJ = xE - om->tec_b > om->base_b
59 *
60 * We defer this check until the final maximal xE value has been
61 * calculated. If the above holds true, we return eslNORESULT.
62 *
63 * Since the J state is removed, the xBv vector is constant, so we can
64 * set it once an for all to a vector where all entries are:
65 *
66 * om->base_b - om->tjb_b - om->tbm_b
67 *
68 * But see the following section for why this is changed for other
69 * reasons.
70 *
71 *
72 * INTERNAL LOOP ADJUSTMENT AND IMPLICATIONS
73 * =========================================
74 *
75 * The following assumes that we have already gotten rid of the J
76 * state.
77 *
78 * Here is an analysis of what is going on in the central loop. The
79 * original code is:
80 *
81 * 1: sv = _mm_max_epu8(sv, xBv);
82 * 2: sv = _mm_adds_epu8(sv, biasv);
83 * 3: sv = _mm_subs_epu8(sv, *rsc); rsc++;
84 * 4: xEv = _mm_max_epu8(xEv, sv);
85 *
86 * Here is a line by line description:
87 *
88 * 1: If sv is below xBv, it is set to xBv. xBv is the begin score,
89 * which is om->base_b - om->tjb_b - om->tbm_b.
90 *
91 * 2: The bias (om->bias_b) is added. This is done since we are
92 * using unsigned numbers and the score can be both positive and
93 * negative. The bias is the negative of the highest value the
94 * real match scores may have.
95 *
96 * 3: The match score (and bias) is subtracted. The subtracted score
97 * must be positive since we using are unsigned bytes, thus the
98 * score subtracted here is the one adjusted for bias. We also
99 * progress to the next match score (rsc++).
100 *
101 * 4: The global maximum is updated.
102 *
103 * When the everything has been traversed, xEv is checked for a number
104 * of conditions. First, the maximum value is extracted to xE, though.
105 *
106 * if xE is greater than or equal to 255 - om->bias_b, there may have
107 * been an overflow, and the result is reported as infinite.
108 *
109 * Since we ignored the J state, we have to check whether it could
110 * potentially have been used, possibly resulting in a higher
111 * score. This is the case if (xE - om->tec_b) > om->base_b. The left
112 * side of the check is the highest score that xJ could have
113 * attained. In the original MSV filter this score would only have
114 * affected the begin scores if this xJ value exceeded
115 * om->base_b. This explains the check.
116 *
117 * Now, we optimize this internal loop by using two ideas:
118 *
119 * A: Get rid of line 1 by using saturation. This can be done
120 * because xBv is a constant vector after getting rid of the J
121 * state.
122 *
123 * B: Combine lines 2 and 3 by using a single signed subtraction
124 * instead of an unsigned addition followed by an unsigned
125 * subtraction.
126 *
127 * (This comment from the original implementation, doesn't apply to NEON)
128 * It is a challenge that SSE2 does not have a signed byte max
129 * operation, yet we need to subtract a signed byte in idea B. First
130 * the new code, then the explanation:
131 *
132 * sv = _mm_subs_epi8(sv, *rsc); rsc++;
133 * xEv = _mm_max_epu8(xEv, sv);
134 *
135 * The last line is unchanged, i.e. the overall max is still done as
136 * an unsigned maximum. The subtraction is saturated to satisfy idea A
137 * and it is signed to satisfy idea B.
138 *
139 * To make the saturation work in the lower end of the scale, the
140 * begin scores have to equal signed -128 which is the same as
141 * unsigned 128, or a bit value of 10000000. Thus, we basically shift
142 * the calculation with a (signed) value of -(om->base_b - om->tjb_b -
143 * om->tbm_b + 128), which takes the original begin value to -128.
144 *
145 * Since we are using an unsigned maximum, the signed saturation at
146 * +127 will not work. Thus, if the score gets high enough, we are
147 * going to pass from signed negative values to non-negative values
148 * without any saturation kicking in. In the unsigned domain this
149 * basically constitutes an overflow from 255 to 0. This means that we
150 * may miss a high score of it crosses this boundary.
151 *
152 * The highest positive effect that the subtraction can have is to add
153 * om->bias_b, since this is the highest real match score. So only
154 * scores strictly higher than 255 - om->bias_b in the unsigned domain
155 * may cause an overflow. In the signed domain this corresponds to -1
156 * - om->bias_b.
157 *
158 * When the calculation is all done, we may check xE against this
159 * boundary to determine if an overflow might have occurred. The other
160 * thing to consider is the check for whether the J state may have
161 * been used. This happens when:
162 *
163 * (xE + (om->base_b - om->tjb_b - om->tbm_b - 128) - om->tec_b)
164 * > om->base_b.
165 *
166 * <=> xE > om->tjb_b + om->tbm_b + om->tec_b + 128
167 *
168 * Thus, we have these two checks:
169 *
170 * xE >= 255 - om->bias_b (possible overflow)
171 *
172 * xE > om->tjb_b + om->tbm_b + om->tec_b - 128 (possible J state)
173 *
174 * To avoid having to call too many false positives, we do not want
175 * the overflow to occur before the J state becomes possible. This
176 * mean that we want:
177 *
178 * (Overflow => J state)
179 *
180 * <=> 255 - om->bias_b > om->tjb_b + om->tbm_b + om->tec_b + 128
181 *
182 * <=> om->tjb_b + om->tbm_b + om->tec_b + om->bias_b < 127
183 *
184 * The worst case bias is 19, om->tec_B is 3 for a sequence length of
185 * L and a model length of M, we have:
186 *
187 * om->tjb_b = 3 * logf(3 / (L + 3))
188 * om->tbm_b = 3 * logf(2 / (M * (M + 1)))
189 *
190 * So if the sequence length is L = 1,000,000, the longest possible
191 * model where the above holds true is M = 482. If the model size is M
192 * = 2,295 (the largest in Pfam 23.0), the longest sequence length
193 * where the condition is true is L = 43,786. So, the condition is not
194 * always true, but typically, it is. And, importantly, it can be
195 * checked.
196 *
197 * A final thing to consider is what to do on an overflow. Since we
198 * shifted the baseline for the calculation, the question is if an
199 * overflow is necessarily going to happen in the original MSV
200 * filter. This is true when our baseline as no higher than the
201 * original MSV filter baseline. Thus, when the following holds we
202 * know that an overflow will occur for the original filter:
203 *
204 * om->base_b - om->tjb_b - om->tbm_b >= 128
205 *
206 * If it does not hold, we are not sure what the true result is and we
207 * have to indicate that in the return value.
208 *
209 * Since we perform a single signed subtraction instead of an unsigned
210 * addition followed by in unsigned subtraction, a new set of match
211 * scores have been introduced in the P7_OPROFILE structure. These are
212 * called sb where the originals are rb.
213 *
214 *
215 * EXPLANATION OF THE CODE
216 * =======================
217 *
218 * The basic idea is to traverse the sequence while analyzing only
219 * enough diagonals that they may residue in registers rather than
220 * memory. This may require several traversals of the sequence, but
221 * this is still worth it due to reduced memory access.
222 *
223 * So we have a basic calculation concept where we fill out some
224 * number of adjacent striped diagonal vectors throughout the whole
225 * sequence. Consider a simple case where we have two registers, A and
226 * B and they each have only two fields instead of 16. In one sweep of
227 * a sequence we calculate the following matrix cells:
228 *
229 * | BA BA BA BA BA BA BA
230 * | BA BA BA BA BA BA BA
231 * |BA BA BA BA BA BA BA
232 * H |A BA BA BA BA BA BA B
233 * M | BA BA BA BA BA BA BA
234 * M | BA BA BA BA BA BA BA
235 * |BA BA BA BA BA BA BA
236 * |A BA BA BA BA BA BA B
237 * ----------------------------
238 * Sequence
239 *
240 * When the top entry in one of the vectors hits the top, the vector
241 * must be left shifted to be ready for the next column. This first
242 * happens to the last vector (B), then in the following round to the
243 * first vector (A).
244 *
245 * This means that the sweep contains two different phases: one where
246 * vectors are being moved without shifting and then a phase where the
247 * vectors are being shifted one by one until the have all been
248 * shifted. If we have Q sets of 16 diagonal and we have w registers
249 * in use, the first phase takes Q - w rounds and the second phase
250 * takes w rounds and we are back where we started. This is done until
251 * the sequence ends.
252 *
253 * After having done this, we do another sweep, where we calculate the
254 * remaining cells:
255 *
256 * |BA BA BA BA BA BA BA
257 * |A BA BA BA BA BA BA B
258 * | BA BA BA BA BA BA BA
259 * H | BA BA BA BA BA BA BA
260 * M |BA BA BA BA BA BA BA
261 * M |A BA BA BA BA BA BA B
262 * | BA BA BA BA BA BA BA
263 * | BA BA BA BA BA BA BA
264 * ----------------------------
265 * Sequence
266 *
267 * This sweep is identical to the first, except there is an offset to
268 * the starting point. We call this offset q which is 2 in this case
269 * and 0 above).
270 *
271 * Apart from the model, sequence, etc., the core calculation has two
272 * parameters: w and q. If we have three registers and Q = 8, we do
273 * three sweeps:
274 *
275 * sweep 1: q = 0, w = 3
276 * sweep 2: q = 3, w = 3
277 * sweep 3: q = 6, w = 2
278 *
279 * This covers all diagonals and we are done.
280 *
281 * To make the compiler use registers as much as possible, we have to
282 * be quite specific about what is going on, so we have to make a
283 * function for each value of w. Since 64 bit machines have 16 xmm
284 * registers, we need quite a few of these functions. It is also
285 * possible that some of the diagonals actually end up in memory while
286 * retaining high performance since a few scattered memory accesses
287 * are not going to slow things down.
288 *
289 * To make the code maintainable, we cannot write out all these
290 * functions. Instead the are defined via macros. So a function
291 * definition may look like this:
292 *
293 * __m128i calc_band_6(ESL_DSQ *dsq, int L, P7_OPROFILE *om, int q, __m128i beginv, __m128i xEv)
294 * {
295 * CALC(RESET_6, STEP_BANDS_6, CONVERT_6, 6)
296 * }
297 *
298 * The parameters are the sequence, its length, the model, the q
299 * value, a begin vector and the max vector. The return value is the
300 * updated max vector. The whole body of the function is defined as a
301 * macro with parameters that are themselves expanded macros (apart
302 * from the last parameter).
303 *
304 * The RESET macro defines and resets the xmm variables in the
305 * function. It is defined recursively:
306 *
307 * #define RESET_1()
308 * register __m128i sv00 = beginv;
309 *
310 * #define RESET_2()
311 * RESET_1()
312 * register __m128i sv01 = beginv;
313 *
314 * #define RESET_3()
315 * RESET_2()
316 * register __m128i sv02 = beginv;
317 *
318 * So the variables holding the scores for the diagonals are called
319 * sv00, sv01, etc.
320 *
321 * The next macro is STEP_BANDS, which moves the diagonals. Again,
322 * this is a recursively defined macro:
323 *
324 * #define STEP_BANDS_1()
325 * STEP_SINGLE(sv00)
326 *
327 * #define STEP_BANDS_2()
328 * STEP_BANDS_1()
329 * STEP_SINGLE(sv01)
330 *
331 * #define STEP_BANDS_3()
332 * STEP_BANDS_2()
333 * STEP_SINGLE(sv02)
334 *
335 * So we end up using STEP_SINGLE on each vector. This is where the
336 * central calculation is done as described above:
337 *
338 * #define STEP_SINGLE(sv)
339 * sv = _mm_subs_epi8(sv, *rsc); rsc++;
340 * xEv = _mm_max_epu8(xEv, sv);
341 *
342 * The CONVERT macro handles the second phase mentioned above where
343 * the vectors have to be shifted. This is yet another recursive
344 * macro:
345 *
346 * #define CONVERT_1(step, LENGTH_CHECK, label)
347 * CONVERT_STEP(step, LENGTH_CHECK, label, sv00, Q - 1)
348 *
349 * #define CONVERT_2(step, LENGTH_CHECK, label)
350 * CONVERT_STEP(step, LENGTH_CHECK, label, sv01, Q - 2)
351 * CONVERT_1(step, LENGTH_CHECK, label)
352 *
353 * #define CONVERT_3(step, LENGTH_CHECK, label)
354 * CONVERT_STEP(step, LENGTH_CHECK, label, sv02, Q - 3)
355 * CONVERT_2(step, LENGTH_CHECK, label)
356 *
357 * Here, CONVERT_STEP ends up being called on each vector in reverse
358 * order. It does the following:
359 *
360 * #define CONVERT_STEP(step, LENGTH_CHECK, label, sv, pos)
361 * length_check(label)
362 * rsc = om->sbv[dsq[i]] + pos;
363 * step()
364 * sv = _mm_slli_si128(sv, 1);
365 * sv = _mm_or_si128(sv, beginv);
366 * i++;
367 *
368 * First a check is made. This is sometimes used to check whether the
369 * sequence is done. Then the match score pointer is set. After this,
370 * STEP_BANDS is called using the step parameter of this
371 * macro. Finally one vector is shifted and or'ed with the begin
372 * vector of (128, 128, ... ). This ensures that the zero that was
373 * shifted in is converted to the needed base line of 128. Other
374 * entries are not significantly affected by this since either their
375 * most significant bit is already set or we already had an overflow
376 * and it does not matter.
377 *
378 * Notice that the CONVERT macro ends up stepping the diagonals w
379 * times, so it handles the whole of phase two. Note also that the
380 * macro may let rsc overflow since it does not reset rsc after a
381 * shift operation. This is handled by extending the match score array
382 * in the P7_OPROFILE by MAX_BANDS - 1 = 17 as defined by the p7O_EXTRA_SB
383 * constant in that file.
384 *
385 * The only macro remaining is the CALC macro which just contains the
386 * overall function for going through the various phases. Due to the
387 * starting offset (q), the first Q - q sequence positions have to be
388 * handled separately. After this follows a number of blocks of length
389 * Q where we can be efficient and not do a check of whether the
390 * sequence stops (the NO_CHECK macro indicates this). Finally, at the
391 * end of the sequence we have to be careful and stop at the right
392 * time, again using LENGTH_CHECK.
393 *
394 * Even though the code is only around 500 lines, it expands to a
395 * fairly large file when the macros are parsed. For example,
396 * _mm_subs_epi8() is called 6,840 times even though it is only
397 * present once in this file. The object file is still not
398 * ridiculously large.
399 *
400 * To better see what is going on, run the preprocessor on this file:
401 *
402 * gcc -E ssvfilter.c | sed 's/[;:]/&\n/g' | less
403 *
404 * Ignore the warnings and go look for the calc_band_2 function.
405 *
406 */
407
408 #include <p7_config.h>
409
410 #include <math.h>
411
412 #include <arm_neon.h> /* NEON */
413
414 #include "easel.h"
415 #include "esl_neon.h"
416
417 #include "hmmer.h"
418 #include "impl_neon.h"
419
420 /* NEON defines 32 vector registers, so in theory could handle up to 30-width bands before hitting register
421 pressure. Worry about that optimization later */
422 #define MAX_BANDS 18
423
424 #define STEP_SINGLE(sv, xEv) \
425 sv = vqsubq_s8(sv, vreinterpretq_s8_u8(*rsc)); \
426 rsc++; \
427 xEv = vreinterpretq_s8_u8(vmaxq_u8(vreinterpretq_u8_s8(xEv), vreinterpretq_u8_s8(sv)));
428
429 #define LENGTH_CHECK(label) \
430 if (i >= L) goto label;
431
432
433 #define NO_CHECK(label)
434
435
436 #define STEP_BANDS_1() \
437 STEP_SINGLE(sv00, xEv0)
438
439 #define STEP_BANDS_2() \
440 STEP_BANDS_1() \
441 STEP_SINGLE(sv01, xEv1)
442
443 #define STEP_BANDS_3() \
444 STEP_BANDS_2() \
445 STEP_SINGLE(sv02, xEv2)
446
447 #define STEP_BANDS_4() \
448 STEP_BANDS_3() \
449 STEP_SINGLE(sv03, xEv3)
450
451 #define STEP_BANDS_5() \
452 STEP_BANDS_4() \
453 STEP_SINGLE(sv04, xEv4)
454
455 #define STEP_BANDS_6() \
456 STEP_BANDS_5() \
457 STEP_SINGLE(sv05, xEv5)
458
459 #define STEP_BANDS_7() \
460 STEP_BANDS_6() \
461 STEP_SINGLE(sv06, xEv0)
462
463 #define STEP_BANDS_8() \
464 STEP_BANDS_7() \
465 STEP_SINGLE(sv07, xEv1)
466
467 #define STEP_BANDS_9() \
468 STEP_BANDS_8() \
469 STEP_SINGLE(sv08, xEv2)
470
471 #define STEP_BANDS_10() \
472 STEP_BANDS_9() \
473 STEP_SINGLE(sv09, xEv3)
474
475 #define STEP_BANDS_11() \
476 STEP_BANDS_10() \
477 STEP_SINGLE(sv10, xEv4)
478
479 #define STEP_BANDS_12() \
480 STEP_BANDS_11() \
481 STEP_SINGLE(sv11, xEv5)
482
483 #define STEP_BANDS_13() \
484 STEP_BANDS_12() \
485 STEP_SINGLE(sv12, xEv0)
486
487 #define STEP_BANDS_14() \
488 STEP_BANDS_13() \
489 STEP_SINGLE(sv13, xEv1)
490
491 #define STEP_BANDS_15() \
492 STEP_BANDS_14() \
493 STEP_SINGLE(sv14, xEv2)
494
495 #define STEP_BANDS_16() \
496 STEP_BANDS_15() \
497 STEP_SINGLE(sv15, xEv3)
498
499 #define STEP_BANDS_17() \
500 STEP_BANDS_16() \
501 STEP_SINGLE(sv16, xEv4)
502
503 #define STEP_BANDS_18() \
504 STEP_BANDS_17() \
505 STEP_SINGLE(sv17, xEv5)
506
507 #define CONVERT_STEP(step, length_check, label, sv, pos) \
508 length_check(label) \
509 rsc = om->sbv[dsq[i]] + pos; \
510 step() \
511 sv = vextq_s8(beginv, sv, 15); \
512 i++;
513
514 #define CONVERT_1(step, LENGTH_CHECK, label) \
515 CONVERT_STEP(step, LENGTH_CHECK, label, sv00, Q - 1)
516
517 #define CONVERT_2(step, LENGTH_CHECK, label) \
518 CONVERT_STEP(step, LENGTH_CHECK, label, sv01, Q - 2) \
519 CONVERT_1(step, LENGTH_CHECK, label)
520
521 #define CONVERT_3(step, LENGTH_CHECK, label) \
522 CONVERT_STEP(step, LENGTH_CHECK, label, sv02, Q - 3) \
523 CONVERT_2(step, LENGTH_CHECK, label)
524
525 #define CONVERT_4(step, LENGTH_CHECK, label) \
526 CONVERT_STEP(step, LENGTH_CHECK, label, sv03, Q - 4) \
527 CONVERT_3(step, LENGTH_CHECK, label)
528
529 #define CONVERT_5(step, LENGTH_CHECK, label) \
530 CONVERT_STEP(step, LENGTH_CHECK, label, sv04, Q - 5) \
531 CONVERT_4(step, LENGTH_CHECK, label)
532
533 #define CONVERT_6(step, LENGTH_CHECK, label) \
534 CONVERT_STEP(step, LENGTH_CHECK, label, sv05, Q - 6) \
535 CONVERT_5(step, LENGTH_CHECK, label)
536
537 #define CONVERT_7(step, LENGTH_CHECK, label) \
538 CONVERT_STEP(step, LENGTH_CHECK, label, sv06, Q - 7) \
539 CONVERT_6(step, LENGTH_CHECK, label)
540
541 #define CONVERT_8(step, LENGTH_CHECK, label) \
542 CONVERT_STEP(step, LENGTH_CHECK, label, sv07, Q - 8) \
543 CONVERT_7(step, LENGTH_CHECK, label)
544
545 #define CONVERT_9(step, LENGTH_CHECK, label) \
546 CONVERT_STEP(step, LENGTH_CHECK, label, sv08, Q - 9) \
547 CONVERT_8(step, LENGTH_CHECK, label)
548
549 #define CONVERT_10(step, LENGTH_CHECK, label) \
550 CONVERT_STEP(step, LENGTH_CHECK, label, sv09, Q - 10) \
551 CONVERT_9(step, LENGTH_CHECK, label)
552
553 #define CONVERT_11(step, LENGTH_CHECK, label) \
554 CONVERT_STEP(step, LENGTH_CHECK, label, sv10, Q - 11) \
555 CONVERT_10(step, LENGTH_CHECK, label)
556
557 #define CONVERT_12(step, LENGTH_CHECK, label) \
558 CONVERT_STEP(step, LENGTH_CHECK, label, sv11, Q - 12) \
559 CONVERT_11(step, LENGTH_CHECK, label)
560
561 #define CONVERT_13(step, LENGTH_CHECK, label) \
562 CONVERT_STEP(step, LENGTH_CHECK, label, sv12, Q - 13) \
563 CONVERT_12(step, LENGTH_CHECK, label)
564
565 #define CONVERT_14(step, LENGTH_CHECK, label) \
566 CONVERT_STEP(step, LENGTH_CHECK, label, sv13, Q - 14) \
567 CONVERT_13(step, LENGTH_CHECK, label)
568
569 #define CONVERT_15(step, LENGTH_CHECK, label) \
570 CONVERT_STEP(step, LENGTH_CHECK, label, sv14, Q - 15) \
571 CONVERT_14(step, LENGTH_CHECK, label)
572
573 #define CONVERT_16(step, LENGTH_CHECK, label) \
574 CONVERT_STEP(step, LENGTH_CHECK, label, sv15, Q - 16) \
575 CONVERT_15(step, LENGTH_CHECK, label)
576
577 #define CONVERT_17(step, LENGTH_CHECK, label) \
578 CONVERT_STEP(step, LENGTH_CHECK, label, sv16, Q - 17) \
579 CONVERT_16(step, LENGTH_CHECK, label)
580
581 #define CONVERT_18(step, LENGTH_CHECK, label) \
582 CONVERT_STEP(step, LENGTH_CHECK, label, sv17, Q - 18) \
583 CONVERT_17(step, LENGTH_CHECK, label)
584
585
586 #define RESET_1() \
587 register int8x16_t sv00 = beginv;
588
589 #define RESET_2() \
590 RESET_1() \
591 register int8x16_t sv01 = beginv;
592
593 #define RESET_3() \
594 RESET_2() \
595 register int8x16_t sv02 = beginv;
596
597 #define RESET_4() \
598 RESET_3() \
599 register int8x16_t sv03 = beginv;
600
601 #define RESET_5() \
602 RESET_4() \
603 register int8x16_t sv04 = beginv;
604
605 #define RESET_6() \
606 RESET_5() \
607 register int8x16_t sv05 = beginv;
608
609 #define RESET_7() \
610 RESET_6() \
611 register int8x16_t sv06 = beginv;
612
613 #define RESET_8() \
614 RESET_7() \
615 register int8x16_t sv07 = beginv;
616
617 #define RESET_9() \
618 RESET_8() \
619 register int8x16_t sv08 = beginv;
620
621 #define RESET_10() \
622 RESET_9() \
623 register int8x16_t sv09 = beginv;
624
625 #define RESET_11() \
626 RESET_10() \
627 register int8x16_t sv10 = beginv;
628
629 #define RESET_12() \
630 RESET_11() \
631 register int8x16_t sv11 = beginv;
632
633 #define RESET_13() \
634 RESET_12() \
635 register int8x16_t sv12 = beginv;
636
637 #define RESET_14() \
638 RESET_13() \
639 register int8x16_t sv13 = beginv;
640
641 #define RESET_15() \
642 RESET_14() \
643 register int8x16_t sv14 = beginv;
644
645 #define RESET_16() \
646 RESET_15() \
647 register int8x16_t sv15 = beginv;
648
649 #define RESET_17() \
650 RESET_16() \
651 register int8x16_t sv16 = beginv;
652
653 #define RESET_18() \
654 RESET_17() \
655 register int8x16_t sv17 = beginv;
656
657 #define CALC(reset, step, convert, width) \
658 int i2; \
659 int i; \
660 int Q = p7O_NQB(om->M); \
661 uint8x16_t *rsc; \
662 \
663 int w = width; \
664 \
665 dsq++; \
666 \
667 reset() \
668 \
669 for (i = 0; i < L && i < Q - q - w; i++) \
670 { \
671 rsc = om->sbv[dsq[i]] + i + q; \
672 step() \
673 } \
674 \
675 i = Q - q - w; \
676 convert(step, LENGTH_CHECK, done1) \
677 done1 : \
678 \
679 for (i2 = Q - q; i2 < L - Q; i2 += Q) \
680 { \
681 for (i = 0; i < Q - w; i++) \
682 { \
683 rsc = om->sbv[dsq[i2 + i]] + i; \
684 step() \
685 } \
686 \
687 i += i2; \
688 convert(step, NO_CHECK, ) \
689 } \
690 \
691 for (i = 0; i2 + i < L && i < Q - w; i++) \
692 { \
693 rsc = om->sbv[dsq[i2 + i]] + i; \
694 step() \
695 } \
696 \
697 i += i2; \
698 convert(step, LENGTH_CHECK, done2) \
699 done2 : \
700 \
701 xEv0 = vreinterpretq_s8_u8(vmaxq_u8(vreinterpretq_u8_s8(xEv0), vreinterpretq_u8_s8(xEv1)));\
702 xEv2 = vreinterpretq_s8_u8(vmaxq_u8(vreinterpretq_u8_s8(xEv2), vreinterpretq_u8_s8(xEv3)));\
703 xEv4 = vreinterpretq_s8_u8(vmaxq_u8(vreinterpretq_u8_s8(xEv4), vreinterpretq_u8_s8(xEv5)));\
704 xEv0 = vreinterpretq_s8_u8(vmaxq_u8(vreinterpretq_u8_s8(xEv0), vreinterpretq_u8_s8(xEv2)));\
705 xEv0 = vreinterpretq_s8_u8(vmaxq_u8(vreinterpretq_u8_s8(xEv0), vreinterpretq_u8_s8(xEv4)));\
706 return xEv0;
707
708 int8x16_t
709 calc_band_1(const ESL_DSQ *dsq, int L, const P7_OPROFILE *om, int q, register int8x16_t beginv, register int8x16_t xEv0, register int8x16_t xEv1, register int8x16_t xEv2, register int8x16_t xEv3, register int8x16_t xEv4, register int8x16_t xEv5){
710 CALC(RESET_1, STEP_BANDS_1, CONVERT_1, 1)}
711
712 int8x16_t
713 calc_band_2(const ESL_DSQ *dsq, int L, const P7_OPROFILE *om, int q, register int8x16_t beginv, register int8x16_t xEv0, register int8x16_t xEv1, register int8x16_t xEv2, register int8x16_t xEv3, register int8x16_t xEv4, register int8x16_t xEv5){
714 CALC(RESET_2, STEP_BANDS_2, CONVERT_2, 2)}
715
716 int8x16_t
717 calc_band_3(const ESL_DSQ *dsq, int L, const P7_OPROFILE *om, int q, register int8x16_t beginv, register int8x16_t xEv0, register int8x16_t xEv1, register int8x16_t xEv2, register int8x16_t xEv3, register int8x16_t xEv4, register int8x16_t xEv5){
718 CALC(RESET_3, STEP_BANDS_3, CONVERT_3, 3)}
719
720 int8x16_t
721 calc_band_4(const ESL_DSQ *dsq, int L, const P7_OPROFILE *om, int q, register int8x16_t beginv, register int8x16_t xEv0, register int8x16_t xEv1, register int8x16_t xEv2, register int8x16_t xEv3, register int8x16_t xEv4, register int8x16_t xEv5){
722 CALC(RESET_4, STEP_BANDS_4, CONVERT_4, 4)}
723
724 int8x16_t
725 calc_band_5(const ESL_DSQ *dsq, int L, const P7_OPROFILE *om, int q, register int8x16_t beginv, register int8x16_t xEv0, register int8x16_t xEv1, register int8x16_t xEv2, register int8x16_t xEv3, register int8x16_t xEv4, register int8x16_t xEv5){
726 CALC(RESET_5, STEP_BANDS_5, CONVERT_5, 5)}
727
728 int8x16_t
729 calc_band_6(const ESL_DSQ *dsq, int L, const P7_OPROFILE *om, int q, register int8x16_t beginv, register int8x16_t xEv0, register int8x16_t xEv1, register int8x16_t xEv2, register int8x16_t xEv3, register int8x16_t xEv4, register int8x16_t xEv5)
730 {
731 CALC(RESET_6, STEP_BANDS_6, CONVERT_6, 6)
732 }
733
734 #if MAX_BANDS > 6 /* Only include needed functions to limit object file size */
735 int8x16_t
736 calc_band_7(const ESL_DSQ *dsq, int L, const P7_OPROFILE *om, int q, register int8x16_t beginv, register int8x16_t xEv0, register int8x16_t xEv1, register int8x16_t xEv2, register int8x16_t xEv3, register int8x16_t xEv4, register int8x16_t xEv5){
737 CALC(RESET_7, STEP_BANDS_7, CONVERT_7, 7)}
738
739 int8x16_t
740 calc_band_8(const ESL_DSQ *dsq, int L, const P7_OPROFILE *om, int q, register int8x16_t beginv, register int8x16_t xEv0, register int8x16_t xEv1, register int8x16_t xEv2, register int8x16_t xEv3, register int8x16_t xEv4, register int8x16_t xEv5){
741 CALC(RESET_8, STEP_BANDS_8, CONVERT_8, 8)}
742
743 int8x16_t
744 calc_band_9(const ESL_DSQ *dsq, int L, const P7_OPROFILE *om, int q, register int8x16_t beginv, register int8x16_t xEv0, register int8x16_t xEv1, register int8x16_t xEv2, register int8x16_t xEv3, register int8x16_t xEv4, register int8x16_t xEv5){
745 CALC(RESET_9, STEP_BANDS_9, CONVERT_9, 9)}
746
747 int8x16_t
748 calc_band_10(const ESL_DSQ *dsq, int L, const P7_OPROFILE *om, int q, register int8x16_t beginv, register int8x16_t xEv0, register int8x16_t xEv1, register int8x16_t xEv2, register int8x16_t xEv3, register int8x16_t xEv4, register int8x16_t xEv5){
749 CALC(RESET_10, STEP_BANDS_10, CONVERT_10, 10)}
750
751 int8x16_t
752 calc_band_11(const ESL_DSQ *dsq, int L, const P7_OPROFILE *om, int q, register int8x16_t beginv, register int8x16_t xEv0, register int8x16_t xEv1, register int8x16_t xEv2, register int8x16_t xEv3, register int8x16_t xEv4, register int8x16_t xEv5){
753 CALC(RESET_11, STEP_BANDS_11, CONVERT_11, 11)}
754
755 int8x16_t
756 calc_band_12(const ESL_DSQ *dsq, int L, const P7_OPROFILE *om, int q, register int8x16_t beginv, register int8x16_t xEv0, register int8x16_t xEv1, register int8x16_t xEv2, register int8x16_t xEv3, register int8x16_t xEv4, register int8x16_t xEv5){
757 CALC(RESET_12, STEP_BANDS_12, CONVERT_12, 12)}
758
759 int8x16_t
760 calc_band_13(const ESL_DSQ *dsq, int L, const P7_OPROFILE *om, int q, register int8x16_t beginv, register int8x16_t xEv0, register int8x16_t xEv1, register int8x16_t xEv2, register int8x16_t xEv3, register int8x16_t xEv4, register int8x16_t xEv5){
761 CALC(RESET_13, STEP_BANDS_13, CONVERT_13, 13)}
762
763 int8x16_t
764 calc_band_14(const ESL_DSQ *dsq, int L, const P7_OPROFILE *om, int q, register int8x16_t beginv, register int8x16_t xEv0, register int8x16_t xEv1, register int8x16_t xEv2, register int8x16_t xEv3, register int8x16_t xEv4, register int8x16_t xEv5)
765 {
766 CALC(RESET_14, STEP_BANDS_14, CONVERT_14, 14)
767 }
768 #endif /* MAX_BANDS > 6 */
769 #if MAX_BANDS > 14
770 int8x16_t
771 calc_band_15(const ESL_DSQ *dsq, int L, const P7_OPROFILE *om, int q, register int8x16_t beginv, register int8x16_t xEv0, register int8x16_t xEv1, register int8x16_t xEv2, register int8x16_t xEv3, register int8x16_t xEv4, register int8x16_t xEv5){
772 CALC(RESET_15, STEP_BANDS_15, CONVERT_15, 15)}
773
774 int8x16_t
775 calc_band_16(const ESL_DSQ *dsq, int L, const P7_OPROFILE *om, int q, register int8x16_t beginv, register int8x16_t xEv0, register int8x16_t xEv1, register int8x16_t xEv2, register int8x16_t xEv3, register int8x16_t xEv4, register int8x16_t xEv5){
776 CALC(RESET_16, STEP_BANDS_16, CONVERT_16, 16)}
777
778 int8x16_t
779 calc_band_17(const ESL_DSQ *dsq, int L, const P7_OPROFILE *om, int q, register int8x16_t beginv, register int8x16_t xEv0, register int8x16_t xEv1, register int8x16_t xEv2, register int8x16_t xEv3, register int8x16_t xEv4, register int8x16_t xEv5){
780 CALC(RESET_17, STEP_BANDS_17, CONVERT_17, 17)}
781
782 int8x16_t
783 calc_band_18(const ESL_DSQ *dsq, int L, const P7_OPROFILE *om, int q, register int8x16_t beginv, register int8x16_t xEv0, register int8x16_t xEv1, register int8x16_t xEv2, register int8x16_t xEv3, register int8x16_t xEv4, register int8x16_t xEv5)
784 {
785 CALC(RESET_18, STEP_BANDS_18, CONVERT_18, 18)
786 }
787 #endif /* MAX_BANDS > 14 */
788
789
790 /*****************************************************************
791 * 2. p7_SSVFilter() implementation
792 *****************************************************************/
793
794 uint8_t
795 get_xE(const ESL_DSQ *dsq, int L, const P7_OPROFILE *om)
796 {
797 register int8x16_t xEv0, xEv1, xEv2, xEv3, xEv4, xEv5; /* E state: keeps max for Mk->E as we go */
798 register int8x16_t beginv; /* begin scores */
799
800 int q; /* counter over vectors 0..nq-1 */
801 int Q = p7O_NQB(om->M); /* segment length: # of vectors */
802
803 int bands; /* the number of bands (rounds) to use */
804
805 int last_q = 0; /* for saving the last q value to find band width */
806 int i; /* counter for bands */
807
808 /* function pointers for the various number of vectors to use */
809 int8x16_t (*fs[MAX_BANDS + 1])(const ESL_DSQ *, int, const P7_OPROFILE *, int, register int8x16_t, register int8x16_t, register int8x16_t, register int8x16_t, register int8x16_t, register int8x16_t, register int8x16_t) = { NULL,
810 calc_band_1,
811 calc_band_2,
812 calc_band_3,
813 calc_band_4,
814 calc_band_5,
815 calc_band_6
816 #if MAX_BANDS > 6
817 ,
818 calc_band_7,
819 calc_band_8,
820 calc_band_9,
821 calc_band_10,
822 calc_band_11,
823 calc_band_12,
824 calc_band_13,
825 calc_band_14
826 #endif
827 #if MAX_BANDS > 14
828 ,
829 calc_band_15,
830 calc_band_16,
831 calc_band_17,
832 calc_band_18
833 #endif
834 };
835
836 beginv = vmovq_n_s8(-128);
837 xEv0 = beginv;
838 xEv1 = beginv;
839 xEv2 = beginv;
840 xEv3 = beginv;
841 xEv4 = beginv;
842 xEv5 = beginv;
843 /* Use the highest number of bands but no more than MAX_BANDS */
844 bands = (Q + MAX_BANDS - 1) / MAX_BANDS;
845
846 for (i = 0; i < bands; i++) {
847 q = (Q * (i + 1)) / bands;
848
849 xEv0 = fs[q - last_q](dsq, L, om, last_q, beginv, xEv0, xEv1, xEv2, xEv3, xEv4, xEv5);
850
851 last_q = q;
852 }
853 return esl_neon_hmax_u8((esl_neon_128i_t) xEv0);
854 }
855
856
857 int
858 p7_SSVFilter(const ESL_DSQ *dsq, int L, const P7_OPROFILE *om, float *ret_sc)
859 {
860 /* Use 16 bit values to avoid overflow due to moved baseline */
861 uint16_t xE;
862 uint16_t xJ;
863
864 if (om->tjb_b + om->tbm_b + om->tec_b + om->bias_b >= 127) {
865 /* the optimizations are not guaranteed to work under these
866 conditions (see comments at start of file) */
867 return eslENORESULT;
868 }
869
870 xE = get_xE(dsq, L, om);
871
872 if (xE >= 255 - om->bias_b)
873 {
874 /* We have an overflow. */
875 *ret_sc = eslINFINITY;
876 #ifdef ARMDEBUG
877 printf(" SSV1=%f\n", *ret_sc);
878 #endif
879 if (om->base_b - om->tjb_b - om->tbm_b < 128)
880 {
881 /* The original MSV filter may not overflow, so we are not sure our result is correct */
882 return eslENORESULT;
883 }
884
885 /* We know that the overflow will also occur in the original MSV filter */
886 return eslERANGE;
887 }
888
889 xE += om->base_b - om->tjb_b - om->tbm_b;
890 xE -= 128;
891
892 if (xE >= 255 - om->bias_b)
893 {
894 /* We know that the result will overflow in the original MSV filter */
895 *ret_sc = eslINFINITY;
896 #ifdef ARMDEBUG
897 printf(" SSV2=%f\n", *ret_sc);
898 #endif
899 return eslERANGE;
900 }
901
902 xJ = xE - om->tec_b;
903
904 if (xJ > om->base_b) return eslENORESULT; /* The J state could have been used, so doubt about score */
905
906 /* finally C->T, and add our missing precision on the NN,CC,JJ back */
907 *ret_sc = ((float) (xJ - om->tjb_b) - (float) om->base_b);
908 *ret_sc /= om->scale_b;
909 *ret_sc -= 3.0; /* that's ~ L \log \frac{L}{L+3}, for our NN,CC,JJ */
910 #ifdef ARMDEBUG
911 printf(" SSV3=%f\n", *ret_sc);
912 #endif
913 return eslOK;
914 }
915 /*****************************************************************
916 * 2. Benchmark driver.
917 *****************************************************************/
918
919 #ifdef p7SSVFILTER_BENCHMARK
920
921 #include <p7_config.h>
922
923 #include "easel.h"
924 #include "esl_alphabet.h"
925 #include "esl_getopts.h"
926 #include "esl_random.h"
927 #include "esl_randomseq.h"
928 #include "esl_stopwatch.h"
929
930 #include "hmmer.h"
931 #include "impl_neon.h"
932
933 static ESL_OPTIONS options[] = {
934 /* name type default env range toggles reqs incomp help docgroup*/
935 {"-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0},
936 {"-s", eslARG_INT, "42", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0},
937 {"-L", eslARG_INT, "400", NULL, "n>0", NULL, NULL, NULL, "length of random target seqs", 0},
938 {"-N", eslARG_INT, "50000", NULL, "n>0", NULL, NULL, NULL, "number of random target seqs", 0},
939 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
940 };
941 static char usage[] = "[-options] <hmmfile>";
942 static char banner[] = "benchmark driver for MSVFilter() implementation";
943
944 int main(int argc, char **argv)
945 {
946 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 1, argc, argv, banner, usage);
947 char *hmmfile = esl_opt_GetArg(go, 1);
948 ESL_STOPWATCH *w = esl_stopwatch_Create();
949 ESL_RANDOMNESS *r = esl_randomness_CreateFast(esl_opt_GetInteger(go, "-s"));
950 ESL_ALPHABET *abc = NULL;
951 P7_HMMFILE *hfp = NULL;
952 P7_HMM *hmm = NULL;
953 P7_BG *bg = NULL;
954 P7_PROFILE *gm = NULL;
955 P7_OPROFILE *om = NULL;
956 P7_OMX *ox = NULL;
957 P7_GMX *gx = NULL;
958 int L = esl_opt_GetInteger(go, "-L");
959 int N = esl_opt_GetInteger(go, "-N");
960 ESL_DSQ *dsq = malloc(sizeof(ESL_DSQ) * (L + 2));
961 int i;
962 float sc1, sc2, total_sc;
963 double base_time, bench_time, Mcs;
964
965 if (p7_hmmfile_Open(hmmfile, NULL, &hfp, NULL) != eslOK)
966 p7_Fail("Failed to open HMM file %s", hmmfile);
967 if (p7_hmmfile_Read(hfp, &abc, &hmm) != eslOK)
968 p7_Fail("Failed to read HMM");
969 total_sc = 0.0;
970 bg = p7_bg_Create(abc);
971 p7_bg_SetLength(bg, L);
972 gm = p7_profile_Create(hmm->M, abc);
973 p7_ProfileConfig(hmm, bg, gm, L, p7_LOCAL);
974 om = p7_oprofile_Create(gm->M, abc);
975 p7_oprofile_Convert(gm, om);
976 p7_oprofile_ReconfigLength(om, L);
977
978 gx = p7_gmx_Create(gm->M, L);
979
980 /* Get a baseline time: how long it takes just to generate the sequences */
981
982 base_time = w->user;
983 esl_rsq_xfIID(r, bg->f, abc->K, L, dsq); // Only need one sequence because SSV time independent of sequence data
984 esl_stopwatch_Start(w);
985 for (i = 0; i < N; i++)
986 {
987 p7_SSVFilter(dsq, L, om, &sc1);
988 total_sc += sc1; //keep from optimizing out call to ssvfilter
989 }
990 esl_stopwatch_Stop(w);
991 bench_time = w->user;
992 Mcs = (double)N * (double)L * (double)gm->M * 1e-6 / (double)bench_time;
993 printf("%lf, %lf, %lf, %lf\n", (double)N, (double)L, (double)gm->M, bench_time);
994 esl_stopwatch_Display(stdout, w, "# CPU time: ");
995 printf("# M = %d\n", gm->M);
996 printf("# %.1f Mc/s\n", Mcs);
997 printf("Ignore this: %f", total_sc);
998 free(dsq);
999 p7_gmx_Destroy(gx);
1000 p7_oprofile_Destroy(om);
1001 p7_profile_Destroy(gm);
1002 p7_bg_Destroy(bg);
1003 p7_hmm_Destroy(hmm);
1004 p7_hmmfile_Close(hfp);
1005 esl_alphabet_Destroy(abc);
1006 esl_stopwatch_Destroy(w);
1007 esl_randomness_Destroy(r);
1008 esl_getopts_Destroy(go);
1009 return 0;
1010 }
1011 #endif /*p7SSVFILTER_BENCHMARK*/
1012 /*------------------ end, benchmark driver ----------------------*/