Debian Codegraph @

hmmer/src/impl_sse/vitfilter.c

raw symbols SCIP
1 /* Viterbi filter implementation; SSE version.
2 *
3 * This is a SIMD vectorized, striped, interleaved, one-row, reduced
4 * precision (epi16) implementation of the Viterbi algorithm.
5 *
6 * It calculates a close approximation of the Viterbi score, in
7 * limited precision (signed words: 16 bits) and range. It may overflow on
8 * high scoring sequences, but this indicates that the sequence is a
9 * high-scoring hit worth examining more closely anyway. It will not
10 * underflow, in local alignment mode.
11 *
12 * Contents:
13 * 1. Viterbi filter implementation.
14 * 2. Benchmark driver.
15 * 3. Unit tests.
16 * 4. Test driver.
17 * 5. Example.
18 */
19 #include <p7_config.h>
20
21 #include <stdio.h>
22 #include <math.h>
23
24 #include <xmmintrin.h> /* SSE */
25 #include <emmintrin.h> /* SSE2 */
26
27 #include "easel.h"
28 #include "esl_sse.h"
29 #include "esl_gumbel.h"
30
31 #include "hmmer.h"
32 #include "impl_sse.h"
33
34
35 /*****************************************************************
36 * 1. Viterbi filter implementation.
37 *****************************************************************/
38
39 /* Function: p7_ViterbiFilter()
40 * Synopsis: Calculates Viterbi score, vewy vewy fast, in limited precision.
41 * Incept: SRE, Tue Nov 27 09:15:24 2007 [Janelia]
42 *
43 * Purpose: Calculates an approximation of the Viterbi score for sequence
44 * <dsq> of length <L> residues, using optimized profile <om>,
45 * and a preallocated one-row DP matrix <ox>. Return the
46 * estimated Viterbi score (in nats) in <ret_sc>.
47 *
48 * Score may overflow (and will, on high-scoring
49 * sequences), but will not underflow.
50 *
51 * The model must be in a local alignment mode; other modes
52 * cannot provide the necessary guarantee of no underflow.
53 *
54 * This is a striped SIMD Viterbi implementation using Intel
55 * SSE/SSE2 integer intrinsics \citep{Farrar07}, in reduced
56 * precision (signed words, 16 bits).
57 *
58 * Args: dsq - digital target sequence, 1..L
59 * L - length of dsq in residues
60 * om - optimized profile
61 * ox - DP matrix
62 * ret_sc - RETURN: Viterbi score (in nats)
63 *
64 * Returns: <eslOK> on success;
65 * <eslERANGE> if the score overflows; in this case
66 * <*ret_sc> is <eslINFINITY>, and the sequence can
67 * be treated as a high-scoring hit.
68 *
69 * Throws: <eslEINVAL> if <ox> allocation is too small, or if
70 * profile isn't in a local alignment mode. (Must be in local
71 * alignment mode because that's what helps us guarantee
72 * limited dynamic range.)
73 *
74 * Xref: [Farrar07] for ideas behind striped SIMD DP.
75 * J2/46-47 for layout of HMMER's striped SIMD DP.
76 * J2/50 for single row DP.
77 * J2/60 for reduced precision (epu8)
78 * J2/65 for initial benchmarking
79 * J2/66 for precision maximization
80 * J4/138-140 for reimplementation in 16-bit precision
81 */
82 int
83 p7_ViterbiFilter(const ESL_DSQ *dsq, int L, const P7_OPROFILE *om, P7_OMX *ox, float *ret_sc)
84 {
85 register __m128i mpv, dpv, ipv; /* previous row values */
86 register __m128i sv; /* temp storage of 1 curr row value in progress */
87 register __m128i dcv; /* delayed storage of D(i,q+1) */
88 register __m128i xEv; /* E state: keeps max for Mk->E as we go */
89 register __m128i xBv; /* B state: splatted vector of B[i-1] for B->Mk calculations */
90 register __m128i Dmaxv; /* keeps track of maximum D cell on row */
91 int16_t xE, xB, xC, xJ, xN; /* special states' scores */
92 int16_t Dmax; /* maximum D cell score on row */
93 int i; /* counter over sequence positions 1..L */
94 int q; /* counter over vectors 0..nq-1 */
95 int Q = p7O_NQW(om->M); /* segment length: # of vectors */
96 __m128i *dp = ox->dpw[0]; /* using {MDI}MX(q) macro requires initialization of <dp> */
97 __m128i *rsc; /* will point at om->ru[x] for residue x[i] */
98 __m128i *tsc; /* will point into (and step thru) om->tu */
99
100 __m128i negInfv;
101
102 /* Check that the DP matrix is ok for us. */
103 if (Q > ox->allocQ8) ESL_EXCEPTION(eslEINVAL, "DP matrix allocated too small");
104 if (om->mode != p7_LOCAL && om->mode != p7_UNILOCAL) ESL_EXCEPTION(eslEINVAL, "Fast filter only works for local alignment");
105 ox->M = om->M;
106
107 /* -infinity is -32768 */
108 negInfv = _mm_set1_epi16(-32768);
109 negInfv = _mm_srli_si128(negInfv, 14); /* negInfv = 16-byte vector, 14 0 bytes + 2-byte value=-32768, for an OR operation. */
110
111 /* Initialization. In unsigned arithmetic, -infinity is -32768
112 */
113 for (q = 0; q < Q; q++)
114 MMXo(q) = IMXo(q) = DMXo(q) = _mm_set1_epi16(-32768);
115 xN = om->base_w;
116 xB = xN + om->xw[p7O_N][p7O_MOVE];
117 xJ = -32768;
118 xC = -32768;
119 xE = -32768;
120
121 #if eslDEBUGLEVEL > 0
122 if (ox->debugging) p7_omx_DumpVFRow(ox, 0, xE, 0, xJ, xB, xC); /* first 0 is <rowi>: do header. second 0 is xN: always 0 here. */
123 #endif
124
125 for (i = 1; i <= L; i++)
126 {
127 rsc = om->rwv[dsq[i]];
128 tsc = om->twv;
129 dcv = _mm_set1_epi16(-32768); /* "-infinity" */
130 xEv = _mm_set1_epi16(-32768);
131 Dmaxv = _mm_set1_epi16(-32768);
132 xBv = _mm_set1_epi16(xB);
133
134 /* Right shifts by 1 value (2 bytes). 4,8,12,x becomes x,4,8,12.
135 * Because ia32 is littlendian, this means a left bit shift.
136 * Zeros shift on automatically; replace it with -32768.
137 */
138 mpv = MMXo(Q-1); mpv = _mm_slli_si128(mpv, 2); mpv = _mm_or_si128(mpv, negInfv);
139 dpv = DMXo(Q-1); dpv = _mm_slli_si128(dpv, 2); dpv = _mm_or_si128(dpv, negInfv);
140 ipv = IMXo(Q-1); ipv = _mm_slli_si128(ipv, 2); ipv = _mm_or_si128(ipv, negInfv);
141
142 for (q = 0; q < Q; q++)
143 {
144 /* Calculate new MMXo(i,q); don't store it yet, hold it in sv. */
145 sv = _mm_adds_epi16(xBv, *tsc); tsc++;
146 sv = _mm_max_epi16 (sv, _mm_adds_epi16(mpv, *tsc)); tsc++;
147 sv = _mm_max_epi16 (sv, _mm_adds_epi16(ipv, *tsc)); tsc++;
148 sv = _mm_max_epi16 (sv, _mm_adds_epi16(dpv, *tsc)); tsc++;
149 sv = _mm_adds_epi16(sv, *rsc); rsc++;
150 xEv = _mm_max_epi16(xEv, sv);
151
152 /* Load {MDI}(i-1,q) into mpv, dpv, ipv;
153 * {MDI}MX(q) is then the current, not the prev row
154 */
155 mpv = MMXo(q);
156 dpv = DMXo(q);
157 ipv = IMXo(q);
158
159 /* Do the delayed stores of {MD}(i,q) now that memory is usable */
160 MMXo(q) = sv;
161 DMXo(q) = dcv;
162
163 /* Calculate the next D(i,q+1) partially: M->D only;
164 * delay storage, holding it in dcv
165 */
166 dcv = _mm_adds_epi16(sv, *tsc); tsc++;
167 Dmaxv = _mm_max_epi16(dcv, Dmaxv);
168
169 /* Calculate and store I(i,q) */
170 sv = _mm_adds_epi16(mpv, *tsc); tsc++;
171 IMXo(q)= _mm_max_epi16 (sv, _mm_adds_epi16(ipv, *tsc)); tsc++;
172 }
173
174 /* Now the "special" states, which start from Mk->E (->C, ->J->B) */
175 xE = esl_sse_hmax_epi16(xEv);
176 if (xE >= 32767) { *ret_sc = eslINFINITY; return eslERANGE; } /* immediately detect overflow */
177 xN = xN + om->xw[p7O_N][p7O_LOOP];
178 xC = ESL_MAX(xC + om->xw[p7O_C][p7O_LOOP], xE + om->xw[p7O_E][p7O_MOVE]);
179 xJ = ESL_MAX(xJ + om->xw[p7O_J][p7O_LOOP], xE + om->xw[p7O_E][p7O_LOOP]);
180 xB = ESL_MAX(xJ + om->xw[p7O_J][p7O_MOVE], xN + om->xw[p7O_N][p7O_MOVE]);
181 /* and now xB will carry over into next i, and xC carries over after i=L */
182
183 /* Finally the "lazy F" loop (sensu [Farrar07]). We can often
184 * prove that we don't need to evaluate any D->D paths at all.
185 *
186 * The observation is that if we can show that on the next row,
187 * B->M(i+1,k) paths always dominate M->D->...->D->M(i+1,k) paths
188 * for all k, then we don't need any D->D calculations.
189 *
190 * The test condition is:
191 * max_k D(i,k) + max_k ( TDD(k-2) + TDM(k-1) - TBM(k) ) < xB(i)
192 * So:
193 * max_k (TDD(k-2) + TDM(k-1) - TBM(k)) is precalc'ed in om->dd_bound;
194 * max_k D(i,k) is why we tracked Dmaxv;
195 * xB(i) was just calculated above.
196 */
197 Dmax = esl_sse_hmax_epi16(Dmaxv);
198 if (Dmax + om->ddbound_w > xB)
199 {
200 /* Now we're obligated to do at least one complete DD path to be sure. */
201 /* dcv has carried through from end of q loop above */
202 dcv = _mm_slli_si128(dcv, 2);
203 dcv = _mm_or_si128(dcv, negInfv);
204 tsc = om->twv + 7*Q; /* set tsc to start of the DD's */
205 for (q = 0; q < Q; q++)
206 {
207 DMXo(q) = _mm_max_epi16(dcv, DMXo(q));
208 dcv = _mm_adds_epi16(DMXo(q), *tsc); tsc++;
209 }
210
211 /* We may have to do up to three more passes; the check
212 * is for whether crossing a segment boundary can improve
213 * our score.
214 */
215 do {
216 dcv = _mm_slli_si128(dcv, 2);
217 dcv = _mm_or_si128(dcv, negInfv);
218 tsc = om->twv + 7*Q; /* set tsc to start of the DD's */
219 for (q = 0; q < Q; q++)
220 {
221 if (! esl_sse_any_gt_epi16(dcv, DMXo(q))) break;
222 DMXo(q) = _mm_max_epi16(dcv, DMXo(q));
223 dcv = _mm_adds_epi16(DMXo(q), *tsc); tsc++;
224 }
225 } while (q == Q);
226 }
227 else /* not calculating DD? then just store the last M->D vector calc'ed.*/
228 {
229 dcv = _mm_slli_si128(dcv, 2);
230 DMXo(0) = _mm_or_si128(dcv, negInfv);
231 }
232
233 #if eslDEBUGLEVEL > 0
234 if (ox->debugging) p7_omx_DumpVFRow(ox, i, xE, 0, xJ, xB, xC);
235 #endif
236 } /* end loop over sequence residues 1..L */
237
238 /* finally C->T */
239 if (xC > -32768)
240 {
241 *ret_sc = (float) xC + (float) om->xw[p7O_C][p7O_MOVE] - (float) om->base_w;
242 /* *ret_sc += L * om->ncj_roundoff; see J4/150 for rationale: superceded by -3.0nat approximation*/
243 *ret_sc /= om->scale_w;
244 *ret_sc -= 3.0; /* the NN/CC/JJ=0,-3nat approximation: see J5/36. That's ~ L \log \frac{L}{L+3}, for our NN,CC,JJ contrib */
245 }
246 else *ret_sc = -eslINFINITY;
247 return eslOK;
248 }
249 /*---------------- end, p7_ViterbiFilter() ----------------------*/
250
251
252
253 /* Function: p7_ViterbiFilter_longtarget()
254 * Synopsis: Finds windows within potentially long sequence blocks with Viterbi
255 * scores above threshold (vewy vewy fast, in limited precision)
256 *
257 * Purpose: Calculates an approximation of the Viterbi score for regions
258 * of sequence <dsq>, using optimized profile <om>, and a pre-
259 * allocated one-row DP matrix <ox>, and captures the positions
260 * at which such regions exceed the score required to be
261 * significant in the eyes of the calling function (usually
262 * p=0.001).
263 *
264 * The resulting landmarks are converted to subsequence
265 * windows by the calling function
266 *
267 * The model must be in a local alignment mode; other modes
268 * cannot provide the necessary guarantee of no underflow.
269 *
270 * This is a striped SIMD Viterbi implementation using Intel
271 * SSE/SSE2 integer intrinsics \citep{Farrar07}, in reduced
272 * precision (signed words, 16 bits).
273 *
274 * Args: dsq - digital target sequence, 1..L
275 * L - length of dsq in residues
276 * om - optimized profile
277 * ox - DP matrix
278 * filtersc - null or bias correction, required for translating a P-value threshold into a score threshold
279 * P - p-value below which a region is captured as being above threshold
280 * windowlist - RETURN: preallocated array of hit windows (start and end of diagonal) for the above-threshold areas
281 *
282 * Returns: <eslOK> on success;
283 *
284 * Throws: <eslEINVAL> if <ox> allocation is too small, or if
285 * profile isn't in a local alignment mode. (Must be in local
286 * alignment mode because that's what helps us guarantee
287 * limited dynamic range.)
288 *
289 * Xref: See p7_ViterbiFilter()
290 */
291 int
292 p7_ViterbiFilter_longtarget(const ESL_DSQ *dsq, int L, const P7_OPROFILE *om, P7_OMX *ox,
293 float filtersc, double P, P7_HMM_WINDOWLIST *windowlist)
294 {
295 register __m128i mpv, dpv, ipv; /* previous row values */
296 register __m128i sv; /* temp storage of 1 curr row value in progress */
297 register __m128i dcv; /* delayed storage of D(i,q+1) */
298 register __m128i xEv; /* E state: keeps max for Mk->E as we go */
299 register __m128i xBv; /* B state: splatted vector of B[i-1] for B->Mk calculations */
300 register __m128i Dmaxv; /* keeps track of maximum D cell on row */
301 int16_t xE, xB, xC, xJ, xN; /* special states' scores */
302 int16_t Dmax; /* maximum D cell score on row */
303 int i; /* counter over sequence positions 1..L */
304 int q; /* counter over vectors 0..nq-1 */
305 int Q = p7O_NQW(om->M); /* segment length: # of vectors */
306 __m128i *dp = ox->dpw[0]; /* using {MDI}MX(q) macro requires initialization of <dp> */
307 __m128i *rsc; /* will point at om->ru[x] for residue x[i] */
308 __m128i *tsc; /* will point into (and step thru) om->tu */
309
310 __m128i negInfv;
311
312 int16_t sc_thresh;
313 float invP;
314
315 int z;
316 union { __m128i v; int16_t i[8]; } tmp;
317 windowlist->count = 0;
318
319 /*
320 * In p7_ViterbiFilter, converting from a scaled int Viterbi score
321 * S (aka xE the score getting to state E) to a probability
322 * goes like this:
323 * vsc = S + om->xw[p7O_E][p7O_MOVE] + om->xw[p7O_C][p7O_MOVE] - om->base_w
324 * ret_sc /= om->scale_w;
325 * vsc -= 3.0;
326 * P = esl_gumbel_surv((vfsc - filtersc) / eslCONST_LOG2 , om->evparam[p7_VMU], om->evparam[p7_VLAMBDA]);
327 * and we're computing the threshold vsc, so invert it:
328 * (vsc - filtersc) / eslCONST_LOG2 = esl_gumbel_invsurv( P, om->evparam[p7_VMU], om->evparam[p7_VLAMBDA])
329 * vsc = filtersc + eslCONST_LOG2 * esl_gumbel_invsurv( P, om->evparam[p7_VMU], om->evparam[p7_VLAMBDA])
330 * vsc += 3.0
331 * vsc *= om->scale_w
332 * S = vsc - (float)om->xw[p7O_E][p7O_MOVE] - (float)om->xw[p7O_C][p7O_MOVE] + (float)om->base_w
333 */
334 invP = esl_gumbel_invsurv(P, om->evparam[p7_VMU], om->evparam[p7_VLAMBDA]);
335 sc_thresh = (int) ceil ( ( (filtersc + (eslCONST_LOG2 * invP) + 3.0) * om->scale_w )
336 - (float)om->xw[p7O_E][p7O_MOVE] - (float)om->xw[p7O_C][p7O_MOVE] + (float)om->base_w );
337
338 /* Check that the DP matrix is ok for us. */
339 if (Q > ox->allocQ8) ESL_EXCEPTION(eslEINVAL, "DP matrix allocated too small");
340 if (om->mode != p7_LOCAL && om->mode != p7_UNILOCAL) ESL_EXCEPTION(eslEINVAL, "Fast filter only works for local alignment");
341 ox->M = om->M;
342
343 /* -infinity is -32768 */
344 negInfv = _mm_set1_epi16(-32768);
345 negInfv = _mm_srli_si128(negInfv, 14); /* negInfv = 16-byte vector, 14 0 bytes + 2-byte value=-32768, for an OR operation. */
346
347 /* Initialization. In unsigned arithmetic, -infinity is -32768
348 */
349 for (q = 0; q < Q; q++)
350 MMXo(q) = IMXo(q) = DMXo(q) = _mm_set1_epi16(-32768);
351 xN = om->base_w;
352 xB = xN + om->xw[p7O_N][p7O_MOVE];
353 xJ = -32768;
354 xC = -32768;
355 xE = -32768;
356
357 #if eslDEBUGLEVEL > 0
358 if (ox->debugging) p7_omx_DumpVFRow(ox, 0, xE, 0, xJ, xB, xC); /* first 0 is <rowi>: do header. second 0 is xN: always 0 here. */
359 #endif
360
361
362 for (i = 1; i <= L; i++)
363 {
364 rsc = om->rwv[dsq[i]];
365 tsc = om->twv;
366 dcv = _mm_set1_epi16(-32768); /* "-infinity" */
367 xEv = _mm_set1_epi16(-32768);
368 Dmaxv = _mm_set1_epi16(-32768);
369 xBv = _mm_set1_epi16(xB);
370
371 /* Right shifts by 1 value (2 bytes). 4,8,12,x becomes x,4,8,12.
372 * Because ia32 is littlendian, this means a left bit shift.
373 * Zeros shift on automatically; replace it with -32768.
374 */
375 mpv = MMXo(Q-1); mpv = _mm_slli_si128(mpv, 2); mpv = _mm_or_si128(mpv, negInfv);
376 dpv = DMXo(Q-1); dpv = _mm_slli_si128(dpv, 2); dpv = _mm_or_si128(dpv, negInfv);
377 ipv = IMXo(Q-1); ipv = _mm_slli_si128(ipv, 2); ipv = _mm_or_si128(ipv, negInfv);
378
379 for (q = 0; q < Q; q++)
380 {
381 /* Calculate new MMXo(i,q); don't store it yet, hold it in sv. */
382 sv = _mm_adds_epi16(xBv, *tsc); tsc++;
383 sv = _mm_max_epi16 (sv, _mm_adds_epi16(mpv, *tsc)); tsc++;
384 sv = _mm_max_epi16 (sv, _mm_adds_epi16(ipv, *tsc)); tsc++;
385 sv = _mm_max_epi16 (sv, _mm_adds_epi16(dpv, *tsc)); tsc++;
386 sv = _mm_adds_epi16(sv, *rsc); rsc++;
387 xEv = _mm_max_epi16(xEv, sv);
388
389 /* Load {MDI}(i-1,q) into mpv, dpv, ipv;
390 * {MDI}MX(q) is then the current, not the prev row
391 */
392 mpv = MMXo(q);
393 dpv = DMXo(q);
394 ipv = IMXo(q);
395
396 /* Do the delayed stores of {MD}(i,q) now that memory is usable */
397 MMXo(q) = sv;
398 DMXo(q) = dcv;
399
400 /* Calculate the next D(i,q+1) partially: M->D only;
401 * delay storage, holding it in dcv
402 */
403 dcv = _mm_adds_epi16(sv, *tsc); tsc++;
404 Dmaxv = _mm_max_epi16(dcv, Dmaxv);
405
406 /* Calculate and store I(i,q) */
407 sv = _mm_adds_epi16(mpv, *tsc); tsc++;
408 IMXo(q)= _mm_max_epi16 (sv, _mm_adds_epi16(ipv, *tsc)); tsc++;
409 }
410
411 /* Now the "special" states, which start from Mk->E (->C, ->J->B) */
412 xE = esl_sse_hmax_epi16(xEv);
413
414 if (xE >= sc_thresh) {
415 //hit score threshold. Add a window to the list, then reset scores.
416
417 /* Unpack and unstripe, then find the position responsible for the hit */
418 for (q = 0; q < Q; q++) {
419 tmp.v = MMXo(q);
420 for (z = 0; z < 8; z++) { // unstripe
421 if ( tmp.i[z] == xE && (q+Q*z+1) <= om->M) {
422 // (q+Q*z+1) is the model position k at which the xE score is found
423 p7_hmmwindow_new(windowlist, 0, i, i-1, (q+Q*z+1), 1, 0.0, p7_NOCOMPLEMENT, L );
424 }
425 }
426 MMXo(q) = IMXo(q) = DMXo(q) = _mm_set1_epi16(-32768); //reset score to start search for next vit window.
427 }
428
429 } else {
430
431
432 xN = xN + om->xw[p7O_N][p7O_LOOP];
433 xC = ESL_MAX(xC + om->xw[p7O_C][p7O_LOOP], xE + om->xw[p7O_E][p7O_MOVE]);
434 xJ = ESL_MAX(xJ + om->xw[p7O_J][p7O_LOOP], xE + om->xw[p7O_E][p7O_LOOP]);
435 xB = ESL_MAX(xJ + om->xw[p7O_J][p7O_MOVE], xN + om->xw[p7O_N][p7O_MOVE]);
436 /* and now xB will carry over into next i, and xC carries over after i=L */
437
438 /* Finally the "lazy F" loop (sensu [Farrar07]). We can often
439 * prove that we don't need to evaluate any D->D paths at all.
440 *
441 * The observation is that if we can show that on the next row,
442 * B->M(i+1,k) paths always dominate M->D->...->D->M(i+1,k) paths
443 * for all k, then we don't need any D->D calculations.
444 *
445 * The test condition is:
446 * max_k D(i,k) + max_k ( TDD(k-2) + TDM(k-1) - TBM(k) ) < xB(i)
447 * So:
448 * max_k (TDD(k-2) + TDM(k-1) - TBM(k)) is precalc'ed in om->dd_bound;
449 * max_k D(i,k) is why we tracked Dmaxv;
450 * xB(i) was just calculated above.
451 */
452 Dmax = esl_sse_hmax_epi16(Dmaxv);
453 if (Dmax + om->ddbound_w > xB)
454 {
455 /* Now we're obligated to do at least one complete DD path to be sure. */
456 /* dcv has carried through from end of q loop above */
457 dcv = _mm_slli_si128(dcv, 2);
458 dcv = _mm_or_si128(dcv, negInfv);
459 tsc = om->twv + 7*Q; /* set tsc to start of the DD's */
460 for (q = 0; q < Q; q++)
461 {
462 DMXo(q) = _mm_max_epi16(dcv, DMXo(q));
463 dcv = _mm_adds_epi16(DMXo(q), *tsc); tsc++;
464 }
465
466 /* We may have to do up to three more passes; the check
467 * is for whether crossing a segment boundary can improve
468 * our score.
469 */
470 do {
471 dcv = _mm_slli_si128(dcv, 2);
472 dcv = _mm_or_si128(dcv, negInfv);
473 tsc = om->twv + 7*Q; /* set tsc to start of the DD's */
474 for (q = 0; q < Q; q++)
475 {
476 if (! esl_sse_any_gt_epi16(dcv, DMXo(q))) break;
477 DMXo(q) = _mm_max_epi16(dcv, DMXo(q));
478 dcv = _mm_adds_epi16(DMXo(q), *tsc); tsc++;
479 }
480 } while (q == Q);
481 }
482 else /* not calculating DD? then just store the last M->D vector calc'ed.*/
483 {
484 dcv = _mm_slli_si128(dcv, 2);
485 DMXo(0) = _mm_or_si128(dcv, negInfv);
486 }
487 }
488 #if eslDEBUGLEVEL > 0
489 if (ox->debugging) p7_omx_DumpVFRow(ox, i, xE, 0, xJ, xB, xC);
490 #endif
491 } /* end loop over sequence residues 1..L */
492
493
494 return eslOK;
495
496 }
497 /*---------------- end, p7_ViterbiFilter_longtarget() ----------------------*/
498
499
500
501 /*****************************************************************
502 * 2. Benchmark driver.
503 *****************************************************************/
504 #ifdef p7VITFILTER_BENCHMARK
505 /* -c, -x are used for debugging, testing; see msvfilter.c for explanation */
506
507 /*
508 gcc -o vitfilter_benchmark -std=gnu99 -g -Wall -msse2 -I.. -L.. -I../../easel -L../../easel -Dp7VITFILTER_BENCHMARK vitfilter.c -lhmmer -leasel -lm
509 icc -o vitfilter_benchmark -O3 -static -I.. -L.. -I../../easel -L../../easel -Dp7VITFILTER_BENCHMARK vitfilter.c -lhmmer -leasel -lm
510
511 ./benchmark-vitfilter <hmmfile> runs benchmark
512 ./benchmark-vitfilter -N100 -c <hmmfile> compare scores to generic impl
513 ./benchmark-vitfilter -N100 -x <hmmfile> compare scores to exact emulation
514 */
515 #include <p7_config.h>
516
517 #include "easel.h"
518 #include "esl_alphabet.h"
519 #include "esl_getopts.h"
520 #include "esl_random.h"
521 #include "esl_randomseq.h"
522 #include "esl_stopwatch.h"
523
524 #include "hmmer.h"
525 #include "impl_sse.h"
526
527 static ESL_OPTIONS options[] = {
528 /* name type default env range toggles reqs incomp help docgroup*/
529 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
530 { "-c", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "-x", "compare scores to generic implementation (debug)", 0 },
531 { "-s", eslARG_INT, "42", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 },
532 { "-x", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "-c", "equate scores to trusted implementation (debug)", 0 },
533 { "-L", eslARG_INT, "400", NULL, "n>0", NULL, NULL, NULL, "length of random target seqs", 0 },
534 { "-N", eslARG_INT, "50000", NULL, "n>0", NULL, NULL, NULL, "number of random target seqs", 0 },
535 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
536 };
537 static char usage[] = "[-options] <hmmfile>";
538 static char banner[] = "benchmark driver for Viterbi filter";
539
540
541 int
542 main(int argc, char **argv)
543 {
544 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 1, argc, argv, banner, usage);
545 char *hmmfile = esl_opt_GetArg(go, 1);
546 ESL_STOPWATCH *w = esl_stopwatch_Create();
547 ESL_RANDOMNESS *r = esl_randomness_CreateFast(esl_opt_GetInteger(go, "-s"));
548 ESL_ALPHABET *abc = NULL;
549 P7_HMMFILE *hfp = NULL;
550 P7_HMM *hmm = NULL;
551 P7_BG *bg = NULL;
552 P7_PROFILE *gm = NULL;
553 P7_OPROFILE *om = NULL;
554 P7_OMX *ox = NULL;
555 P7_GMX *gx = NULL;
556 int L = esl_opt_GetInteger(go, "-L");
557 int N = esl_opt_GetInteger(go, "-N");
558 ESL_DSQ *dsq = malloc(sizeof(ESL_DSQ) * (L+2));
559 int i;
560 float sc1, sc2;
561 double base_time, bench_time, Mcs;
562
563 if (p7_hmmfile_Open(hmmfile, NULL, &hfp, NULL) != eslOK) p7_Fail("Failed to open HMM file %s", hmmfile);
564 if (p7_hmmfile_Read(hfp, &abc, &hmm) != eslOK) p7_Fail("Failed to read HMM");
565
566 bg = p7_bg_Create(abc);
567 p7_bg_SetLength(bg, L);
568 gm = p7_profile_Create(hmm->M, abc);
569 p7_ProfileConfig(hmm, bg, gm, L, p7_LOCAL);
570 om = p7_oprofile_Create(gm->M, abc);
571 p7_oprofile_Convert(gm, om);
572 p7_oprofile_ReconfigLength(om, L);
573
574 if (esl_opt_GetBoolean(go, "-x")) p7_profile_SameAsVF(om, gm);
575
576 ox = p7_omx_Create(gm->M, 0, 0);
577 gx = p7_gmx_Create(gm->M, L);
578
579 /* Get a baseline time: how long it takes just to generate the sequences */
580 esl_stopwatch_Start(w);
581 for (i = 0; i < N; i++)
582 esl_rsq_xfIID(r, bg->f, abc->K, L, dsq);
583 esl_stopwatch_Stop(w);
584 base_time = w->user;
585
586 /* Run the benchmark */
587 esl_stopwatch_Start(w);
588 for (i = 0; i < N; i++)
589 {
590 esl_rsq_xfIID(r, bg->f, abc->K, L, dsq);
591 p7_ViterbiFilter(dsq, L, om, ox, &sc1);
592
593 if (esl_opt_GetBoolean(go, "-c"))
594 {
595 p7_GViterbi(dsq, L, gm, gx, &sc2);
596 printf("%.4f %.4f\n", sc1, sc2);
597 }
598
599 if (esl_opt_GetBoolean(go, "-x"))
600 {
601 p7_GViterbi(dsq, L, gm, gx, &sc2);
602 sc2 /= om->scale_w;
603 if (om->mode == p7_UNILOCAL) sc2 -= 2.0; /* that's ~ L \log \frac{L}{L+2}, for our NN,CC,JJ */
604 else if (om->mode == p7_LOCAL) sc2 -= 3.0; /* that's ~ L \log \frac{L}{L+3}, for our NN,CC,JJ */
605 printf("%.4f %.4f\n", sc1, sc2);
606 }
607 }
608 esl_stopwatch_Stop(w);
609 bench_time = w->user - base_time;
610 Mcs = (double) N * (double) L * (double) gm->M * 1e-6 / (double) bench_time;
611 esl_stopwatch_Display(stdout, w, "# CPU time: ");
612 printf("# M = %d\n", gm->M);
613 printf("# %.1f Mc/s\n", Mcs);
614
615 free(dsq);
616 p7_omx_Destroy(ox);
617 p7_gmx_Destroy(gx);
618 p7_oprofile_Destroy(om);
619 p7_profile_Destroy(gm);
620 p7_bg_Destroy(bg);
621 p7_hmm_Destroy(hmm);
622 p7_hmmfile_Close(hfp);
623 esl_alphabet_Destroy(abc);
624 esl_stopwatch_Destroy(w);
625 esl_randomness_Destroy(r);
626 esl_getopts_Destroy(go);
627 return 0;
628 }
629 #endif /*p7VITFILTER_BENCHMARK*/
630 /*---------------- end, benchmark driver ------------------------*/
631
632
633
634
635 /*****************************************************************
636 * 3. Unit tests.
637 *****************************************************************/
638 #ifdef p7VITFILTER_TESTDRIVE
639 #include "esl_random.h"
640 #include "esl_randomseq.h"
641
642 /* ViterbiFilter() unit test
643 *
644 * We can check that scores are identical (within machine error) to
645 * scores of generic DP with scores rounded the same way. Do this for
646 * a random model of length <M>, for <N> test sequences of length <L>.
647 *
648 * We assume that we don't accidentally generate a high-scoring random
649 * sequence that overflows ViterbiFilter()'s limited range.
650 *
651 */
652 static void
653 utest_viterbi_filter(ESL_RANDOMNESS *r, ESL_ALPHABET *abc, P7_BG *bg, int M, int L, int N)
654 {
655 P7_HMM *hmm = NULL;
656 P7_PROFILE *gm = NULL;
657 P7_OPROFILE *om = NULL;
658 ESL_DSQ *dsq = malloc(sizeof(ESL_DSQ) * (L+2));
659 P7_OMX *ox = p7_omx_Create(M, 0, 0);
660 P7_GMX *gx = p7_gmx_Create(M, L);
661 float sc1, sc2;
662
663 p7_oprofile_Sample(r, abc, bg, M, L, &hmm, &gm, &om);
664 p7_profile_SameAsVF(om, gm); /* round and scale the scores in <gm> the same as in <om> */
665
666 #if 0
667 p7_oprofile_Dump(stdout, om); // dumps the optimized profile
668 p7_omx_SetDumpMode(stdout, ox, TRUE); // makes the fast DP algorithms dump their matrices
669 #endif
670
671 while (N--)
672 {
673 esl_rsq_xfIID(r, bg->f, abc->K, L, dsq);
674
675 p7_ViterbiFilter(dsq, L, om, ox, &sc1);
676 p7_GViterbi (dsq, L, gm, gx, &sc2);
677
678 #if 0
679 p7_gmx_Dump(stdout, gx, p7_DEFAULT); // dumps a generic DP matrix
680 #endif
681
682 sc2 /= om->scale_w;
683 sc2 -= 3.0;
684
685 if (fabs(sc1-sc2) > 0.001) esl_fatal("viterbi filter unit test failed: scores differ (%.2f, %.2f)", sc1, sc2);
686 }
687
688 free(dsq);
689 p7_hmm_Destroy(hmm);
690 p7_omx_Destroy(ox);
691 p7_gmx_Destroy(gx);
692 p7_profile_Destroy(gm);
693 p7_oprofile_Destroy(om);
694 }
695 #endif /*p7VITFILTER_TESTDRIVE*/
696
697
698 /*****************************************************************
699 * 4. Test driver
700 *****************************************************************/
701 #ifdef p7VITFILTER_TESTDRIVE
702 /*
703 gcc -g -Wall -msse2 -std=gnu99 -I.. -L.. -I../../easel -L../../easel -o vitfilter_utest -Dp7VITFILTER_TESTDRIVE vitfilter.c -lhmmer -leasel -lm
704 ./vitfilter_utest
705 */
706 #include <p7_config.h>
707
708 #include "easel.h"
709 #include "esl_alphabet.h"
710 #include "esl_getopts.h"
711 #include "esl_random.h"
712 #include "esl_randomseq.h"
713
714 #include "hmmer.h"
715 #include "impl_sse.h"
716
717 static ESL_OPTIONS options[] = {
718 /* name type default env range toggles reqs incomp help docgroup*/
719 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
720 { "-s", eslARG_INT, "42", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 },
721 { "-v", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "be verbose", 0 },
722 { "-L", eslARG_INT, "200", NULL, NULL, NULL, NULL, NULL, "size of random sequences to sample", 0 },
723 { "-M", eslARG_INT, "145", NULL, NULL, NULL, NULL, NULL, "size of random models to sample", 0 },
724 { "-N", eslARG_INT, "100", NULL, NULL, NULL, NULL, NULL, "number of random sequences to sample", 0 },
725 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
726 };
727 static char usage[] = "[-options]";
728 static char banner[] = "test driver for the SSE implementation";
729
730 int
731 main(int argc, char **argv)
732 {
733 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 0, argc, argv, banner, usage);
734 ESL_RANDOMNESS *r = esl_randomness_CreateFast(esl_opt_GetInteger(go, "-s"));
735 ESL_ALPHABET *abc = NULL;
736 P7_BG *bg = NULL;
737 int M = esl_opt_GetInteger(go, "-M");
738 int L = esl_opt_GetInteger(go, "-L");
739 int N = esl_opt_GetInteger(go, "-N");
740
741 /* First round of tests for DNA alphabets. */
742 if ((abc = esl_alphabet_Create(eslDNA)) == NULL) esl_fatal("failed to create alphabet");
743 if ((bg = p7_bg_Create(abc)) == NULL) esl_fatal("failed to create null model");
744
745 if (esl_opt_GetBoolean(go, "-v")) printf("ViterbiFilter() tests, DNA\n");
746 utest_viterbi_filter(r, abc, bg, M, L, N);
747 utest_viterbi_filter(r, abc, bg, 1, L, 10);
748 utest_viterbi_filter(r, abc, bg, M, 1, 10);
749
750 esl_alphabet_Destroy(abc);
751 p7_bg_Destroy(bg);
752
753 /* Second round of tests for amino alphabets. */
754 if ((abc = esl_alphabet_Create(eslAMINO)) == NULL) esl_fatal("failed to create alphabet");
755 if ((bg = p7_bg_Create(abc)) == NULL) esl_fatal("failed to create null model");
756
757 if (esl_opt_GetBoolean(go, "-v")) printf("ViterbiFilter() tests, protein\n");
758 utest_viterbi_filter(r, abc, bg, M, L, N);
759 utest_viterbi_filter(r, abc, bg, 1, L, 10);
760 utest_viterbi_filter(r, abc, bg, M, 1, 10);
761
762 esl_alphabet_Destroy(abc);
763 p7_bg_Destroy(bg);
764
765 esl_getopts_Destroy(go);
766 esl_randomness_Destroy(r);
767 return eslOK;
768 }
769 #endif /*VITFILTER_TESTDRIVE*/
770 /*---------------- end, test driver -----------------------------*/
771
772
773
774 /*****************************************************************
775 * 5. Example
776 *****************************************************************/
777 #ifdef p7VITFILTER_EXAMPLE
778 /* A minimal example.
779 Also useful for debugging on small HMMs and sequences.
780
781 gcc -g -Wall -msse2 -std=gnu99 -I.. -L.. -I../../easel -L../../easel -o vitfilter_example -Dp7VITFILTER_EXAMPLE vitfilter.c -lhmmer -leasel -lm
782 ./vitfilter_example <hmmfile> <seqfile>
783 */
784 #include <p7_config.h>
785
786 #include "easel.h"
787 #include "esl_alphabet.h"
788 #include "esl_getopts.h"
789 #include "esl_gumbel.h"
790 #include "esl_sq.h"
791 #include "esl_sqio.h"
792
793 #include "hmmer.h"
794 #include "impl_sse.h"
795
796 static ESL_OPTIONS options[] = {
797 /* name type default env range toggles reqs incomp help docgroup*/
798 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
799 { "-1", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "output in one line awkable format", 0 },
800 { "-P", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "output in profmark format", 0 },
801 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
802 };
803 static char usage[] = "[-options] <hmmfile> <seqfile>";
804 static char banner[] = "example of Viterbi filter algorithm";
805
806 int
807 main(int argc, char **argv)
808 {
809 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 2, argc, argv, banner, usage);
810 char *hmmfile = esl_opt_GetArg(go, 1);
811 char *seqfile = esl_opt_GetArg(go, 2);
812 ESL_ALPHABET *abc = NULL;
813 P7_HMMFILE *hfp = NULL;
814 P7_HMM *hmm = NULL;
815 P7_BG *bg = NULL;
816 P7_PROFILE *gm = NULL;
817 P7_OPROFILE *om = NULL;
818 P7_OMX *ox = NULL;
819 P7_GMX *gx = NULL;
820 ESL_SQ *sq = NULL;
821 ESL_SQFILE *sqfp = NULL;
822 int format = eslSQFILE_UNKNOWN;
823 float vfraw, nullsc, vfscore;
824 float graw, gscore;
825 double P, gP;
826 int status;
827
828 /* Read in one HMM */
829 if (p7_hmmfile_Open(hmmfile, NULL, &hfp, NULL) != eslOK) p7_Fail("Failed to open HMM file %s", hmmfile);
830 if (p7_hmmfile_Read(hfp, &abc, &hmm) != eslOK) p7_Fail("Failed to read HMM");
831
832 /* Read in one sequence */
833 sq = esl_sq_CreateDigital(abc);
834 status = esl_sqfile_Open(seqfile, format, NULL, &sqfp);
835 if (status == eslENOTFOUND) p7_Fail("No such file.");
836 else if (status == eslEFORMAT) p7_Fail("Format unrecognized.");
837 else if (status == eslEINVAL) p7_Fail("Can't autodetect stdin or .gz.");
838 else if (status != eslOK) p7_Fail("Open failed, code %d.", status);
839
840 /* create default null model, then create and optimize profile */
841 bg = p7_bg_Create(abc);
842 p7_bg_SetLength(bg, sq->n);
843 gm = p7_profile_Create(hmm->M, abc);
844 p7_ProfileConfig(hmm, bg, gm, sq->n, p7_LOCAL);
845 om = p7_oprofile_Create(gm->M, abc);
846 p7_oprofile_Convert(gm, om);
847
848 /* allocate DP matrices, both a generic and an optimized one */
849 ox = p7_omx_Create(gm->M, 0, sq->n);
850 gx = p7_gmx_Create(gm->M, sq->n);
851
852 /* Useful to place and compile in for debugging:
853 p7_oprofile_Dump(stdout, om); dumps the optimized profile
854 p7_omx_SetDumpMode(ox, TRUE); makes the fast DP algorithms dump their matrices
855 p7_gmx_Dump(stdout, gx, p7_DEFAULT); dumps a generic DP matrix
856 */
857
858 while ((status = esl_sqio_Read(sqfp, sq)) == eslOK)
859 {
860 p7_oprofile_ReconfigLength(om, sq->n);
861 p7_ReconfigLength(gm, sq->n);
862 p7_bg_SetLength(bg, sq->n);
863 p7_omx_GrowTo(ox, om->M, 0, sq->n);
864 p7_gmx_GrowTo(gx, gm->M, sq->n);
865
866 p7_ViterbiFilter (sq->dsq, sq->n, om, ox, &vfraw);
867 p7_bg_NullOne (bg, sq->dsq, sq->n, &nullsc);
868 vfscore = (vfraw - nullsc) / eslCONST_LOG2;
869 P = esl_gumbel_surv(vfscore, om->evparam[p7_VMU], om->evparam[p7_VLAMBDA]);
870
871 p7_GViterbi (sq->dsq, sq->n, gm, gx, &graw);
872 gscore = (graw - nullsc) / eslCONST_LOG2;
873 gP = esl_gumbel_surv(gscore, gm->evparam[p7_VMU], gm->evparam[p7_VLAMBDA]);
874
875 if (esl_opt_GetBoolean(go, "-1"))
876 {
877 printf("%-30s\t%-20s\t%9.2g\t%7.2f\t%9.2g\t%7.2f\n", sq->name, hmm->name, P, vfscore, gP, gscore);
878 }
879 else if (esl_opt_GetBoolean(go, "-P"))
880 { /* output suitable for direct use in profmark benchmark postprocessors: */
881 printf("%g\t%.2f\t%s\t%s\n", P, vfscore, sq->name, hmm->name);
882 }
883 else
884 {
885 printf("target sequence: %s\n", sq->name);
886 printf("vit filter raw score: %.2f nats\n", vfraw);
887 printf("null score: %.2f nats\n", nullsc);
888 printf("per-seq score: %.2f bits\n", vfscore);
889 printf("P-value: %g\n", P);
890 printf("GViterbi raw score: %.2f nats\n", graw);
891 printf("GViterbi seq score: %.2f bits\n", gscore);
892 printf("GViterbi P-value: %g\n", gP);
893 }
894
895 esl_sq_Reuse(sq);
896 }
897
898 /* cleanup */
899 esl_sq_Destroy(sq);
900 esl_sqfile_Close(sqfp);
901 p7_omx_Destroy(ox);
902 p7_gmx_Destroy(gx);
903 p7_oprofile_Destroy(om);
904 p7_profile_Destroy(gm);
905 p7_bg_Destroy(bg);
906 p7_hmm_Destroy(hmm);
907 p7_hmmfile_Close(hfp);
908 esl_alphabet_Destroy(abc);
909 esl_getopts_Destroy(go);
910 return 0;
911 }
912 #endif /*p7VITFILTER_EXAMPLE*/
913 /*-------------------- end, example -----------------------------*/
914
915