Debian Codegraph @

hmmer/src/impl_neon/fwdback.c

raw symbols SCIP
1
2 /* NEON implementation of Forward and Backward algorithms.
3 *
4 * Both profile and DP matrix are striped and interleaved, for fast
5 * SIMD vector operations. Calculations are in probability space
6 * (scaled odds ratios, actually) rather than log probabilities,
7 * allowing fast multiply/add operations rather than slow Logsum()
8 * calculations. Sparse rescaling is used to achieve full dynamic
9 * range of scores.
10 *
11 * The Forward and Backward implementations may be used either in a
12 * full O(ML) mode that keeps an entire DP matrix, or in a O(M+L)
13 * linear memory "parsing" mode that only keeps one row of memory for
14 * the main MDI states and one column 0..L for the special states
15 * B,E,N,C,J. Keeping a full matrix allows subsequent stochastic
16 * traceback or posterior decoding of any state. In parsing mode, we
17 * can do posterior decoding on the special states and determine
18 * regions of the target sequence that are generated by the
19 * nonhomology states (NCJ) versus not -- thus, identifying where
20 * high-probability "regions" are, the first step of identifying the
21 * domain structure of a target sequence.
22 *
23 * Contents:
24 * 1. Forward/Backward wrapper API
25 * 2. Forward and Backward engine implementations
26 * 4. Benchmark driver.
27 * 5. Unit tests.
28 * 6. Test driver.
29 * 7. Example.
30 *
31 * SRE, Thu Jul 31 08:43:20 2008 [Janelia]
32 */
33 #include <p7_config.h>
34
35 #include <stdio.h>
36 #include <math.h>
37
38 #include <arm_neon.h> /* NEON */
39
40 #include "easel.h"
41 #include "esl_neon.h"
42
43 #include "hmmer.h"
44 #include "impl_neon.h"
45
46 static int forward_engine (int do_full, const ESL_DSQ *dsq, int L, const P7_OPROFILE *om, P7_OMX *fwd, float *opt_sc);
47 static int backward_engine(int do_full, const ESL_DSQ *dsq, int L, const P7_OPROFILE *om, const P7_OMX *fwd, P7_OMX *bck, float *opt_sc);
48
49
50 /*****************************************************************
51 * 1. Forward/Backward API.
52 *****************************************************************/
53
54 /* Function: p7_Forward()
55 * Synopsis: The Forward algorithm, full matrix fill version.
56 * Incept: SRE, Fri Aug 15 18:59:43 2008 [Casa de Gatos]
57 *
58 * Purpose: Calculates the Forward algorithm for sequence <dsq> of
59 * length <L> residues, using optimized profile <om>, and a
60 * preallocated DP matrix <ox>. Upon successful return, <ox>
61 * contains the filled Forward matrix, and <*opt_sc>
62 * optionally contains the raw Forward score in nats.
63 *
64 * This calculation requires $O(ML)$ memory and time.
65 * The caller must provide a suitably allocated full <ox>
66 * by calling <ox = p7_omx_Create(M, L, L)> or
67 * <p7_omx_GrowTo(ox, M, L, L)>.
68 *
69 * The model <om> must be configured in local alignment
70 * mode. The sparse rescaling method used to keep
71 * probability values within single-precision floating
72 * point dynamic range cannot be safely applied to models in
73 * glocal or global mode.
74 *
75 * Args: dsq - digital target sequence, 1..L
76 * L - length of dsq in residues
77 * om - optimized profile
78 * ox - RETURN: Forward DP matrix
79 * opt_sc - RETURN: Forward score (in nats)
80 *
81 * Returns: <eslOK> on success.
82 *
83 * Throws: <eslEINVAL> if <ox> allocation is too small, or if the profile
84 * isn't in local alignment mode.
85 * <eslERANGE> if the score exceeds the limited range of
86 * a probability-space odds ratio.
87 * In either case, <*opt_sc> is undefined.
88 */
89 int
90 p7_Forward(const ESL_DSQ *dsq, int L, const P7_OPROFILE *om, P7_OMX *ox, float *opt_sc)
91 {
92 #if eslDEBUGLEVEL > 0
93 if (om->M > ox->allocQ4*4) ESL_EXCEPTION(eslEINVAL, "DP matrix allocated too small (too few columns)");
94 if (L >= ox->validR) ESL_EXCEPTION(eslEINVAL, "DP matrix allocated too small (too few MDI rows)");
95 if (L >= ox->allocXR) ESL_EXCEPTION(eslEINVAL, "DP matrix allocated too small (too few X rows)");
96 if (! p7_oprofile_IsLocal(om)) ESL_EXCEPTION(eslEINVAL, "Forward implementation makes assumptions that only work for local alignment");
97 #endif
98
99 return forward_engine(TRUE, dsq, L, om, ox, opt_sc);
100 }
101
102 /* Function: p7_ForwardParser()
103 * Synopsis: The Forward algorithm, linear memory parsing version.
104 * Incept: SRE, Fri Aug 15 19:05:26 2008 [Casa de Gatos]
105 *
106 * Purpose: Same as <p7_Forward() except that the full matrix isn't
107 * kept. Instead, a linear $O(M+L)$ memory algorithm is
108 * used, keeping only the DP matrix values for the special
109 * (BENCJ) states. These are sufficient to do posterior
110 * decoding to identify high-probability regions where
111 * domains are.
112 *
113 * The caller must provide a suitably allocated "parsing"
114 * <ox> by calling <ox = p7_omx_Create(M, 0, L)> or
115 * <p7_omx_GrowTo(ox, M, 0, L)>.
116 *
117 * Args: dsq - digital target sequence, 1..L
118 * L - length of dsq in residues
119 * om - optimized profile
120 * ox - RETURN: Forward DP matrix
121 * ret_sc - RETURN: Forward score (in nats)
122 *
123 * The caller must provide a suitably allocated "parsing"
124 * <ox> by calling <ox = p7_omx_Create(M, 0, L)> or
125 * <p7_omx_GrowTo(ox, M, 0, L)>.
126 *
127 * Args: dsq - digital target sequence, 1..L
128 * L - length of dsq in residues
129 * om - optimized profile
130 * ox - RETURN: Forward DP matrix
131 * ret_sc - RETURN: Forward score (in nats)
132 *
133 * Returns: <eslOK> on success.
134 *
135 * Throws: <eslEINVAL> if <ox> allocation is too small, or if the profile
136 * isn't in local alignment mode.
137 * <eslERANGE> if the score exceeds the limited range of
138 * a probability-space odds ratio.
139 * In either case, <*opt_sc> is undefined.
140 */
141 int
142 p7_ForwardParser(const ESL_DSQ *dsq, int L, const P7_OPROFILE *om, P7_OMX *ox, float *opt_sc)
143 {
144 #if eslDEBUGLEVEL > 0
145 if (om->M > ox->allocQ4*4) ESL_EXCEPTION(eslEINVAL, "DP matrix allocated too small (too few columns)");
146 if (ox->validR < 1) ESL_EXCEPTION(eslEINVAL, "DP matrix allocated too small (too few MDI rows)");
147 if (L >= ox->allocXR) ESL_EXCEPTION(eslEINVAL, "DP matrix allocated too small (too few X rows)");
148 if (! p7_oprofile_IsLocal(om)) ESL_EXCEPTION(eslEINVAL, "Forward implementation makes assumptions that only work for local alignment");
149 #endif
150
151 return forward_engine(FALSE, dsq, L, om, ox, opt_sc);
152 }
153
154
155
156 /* Function: p7_Backward()
157 * Synopsis: The Backward algorithm; full matrix fill version.
158 * Incept: SRE, Sat Aug 16 08:34:22 2008 [Janelia]
159 *
160 * Purpose: Calculates the Backward algorithm for sequence <dsq> of
161 * length <L> residues, using optimized profile <om>, and a
162 * preallocated DP matrix <bck>. A filled Forward matrix
163 * must also be provided in <fwd>, because we need to use
164 * the same sparse scaling factors that Forward used. The
165 * <bck> matrix is filled in, and the Backward score (in
166 * nats) is optionally returned in <opt_sc>.
167 *
168 * This calculation requires $O(ML)$ memory and time. The
169 * caller must provide a suitably allocated full <bck> by
170 * calling <bck = p7_omx_Create(M, L, L)> or
171 * <p7_omx_GrowTo(bck, M, L, L)>.
172 *
173 * Because only the sparse scaling factors are needed from
174 * the <fwd> matrix, the caller may have this matrix
175 * calculated either in full or parsing mode.
176 *
177 * The model <om> must be configured in local alignment
178 * mode. The sparse rescaling method used to keep
179 * probability values within single-precision floating
180 * point dynamic range cannot be safely applied to models in
181 * glocal or global mode.
182 *
183 * Args: dsq - digital target sequence, 1..L
184 * L - length of dsq in residues
185 * om - optimized profile
186 * fwd - filled Forward DP matrix, for scale factors
187 * do_full - TRUE=full matrix; FALSE=linear memory parse mode
188 * bck - RETURN: filled Backward matrix
189 * opt_sc - optRETURN: Backward score (in nats)
190 *
191 * Returns: <eslOK> on success.
192 *
193 * Throws: <eslEINVAL> if <ox> allocation is too small, or if the profile
194 * isn't in local alignment mode.
195 * <eslERANGE> if the score exceeds the limited range of
196 * a probability-space odds ratio.
197 * In either case, <*opt_sc> is undefined.
198 */
199 int
200 p7_Backward(const ESL_DSQ *dsq, int L, const P7_OPROFILE *om, const P7_OMX *fwd, P7_OMX *bck, float *opt_sc)
201 {
202 #if eslDEBUGLEVEL > 0
203 if (om->M > bck->allocQ4*4) ESL_EXCEPTION(eslEINVAL, "DP matrix allocated too small (too few columns)");
204 if (L >= bck->validR) ESL_EXCEPTION(eslEINVAL, "DP matrix allocated too small (too few MDI rows)");
205 if (L >= bck->allocXR) ESL_EXCEPTION(eslEINVAL, "DP matrix allocated too small (too few X rows)");
206 if (L != fwd->L) ESL_EXCEPTION(eslEINVAL, "fwd matrix size doesn't agree with length L");
207 if (! p7_oprofile_IsLocal(om)) ESL_EXCEPTION(eslEINVAL, "Forward implementation makes assumptions that only work for local alignment");
208 #endif
209
210 return backward_engine(TRUE, dsq, L, om, fwd, bck, opt_sc);
211 }
212
213
214
215 /* Function: p7_BackwardParser()
216 * Synopsis: The Backward algorithm, linear memory parsing version.
217 * Incept: SRE, Sat Aug 16 08:34:13 2008 [Janelia]
218 *
219 * Purpose: Same as <p7_Backward()> except that the full matrix isn't
220 * kept. Instead, a linear $O(M+L)$ memory algorithm is
221 * used, keeping only the DP matrix values for the special
222 * (BENCJ) states. These are sufficient to do posterior
223 * decoding to identify high-probability regions where
224 * domains are.
225 *
226 * The caller must provide a suitably allocated "parsing"
227 * <bck> by calling <bck = p7_omx_Create(M, 0, L)> or
228 * <p7_omx_GrowTo(bck, M, 0, L)>.
229 *
230 * Args: dsq - digital target sequence, 1..L
231 * L - length of dsq in residues
232 * om - optimized profile
233 * fwd - filled Forward DP matrix, for scale factors
234 * bck - RETURN: filled Backward matrix
235 * opt_sc - optRETURN: Backward score (in nats)
236 *
237 * Returns: <eslOK> on success.
238 *
239 * Throws: <eslEINVAL> if <ox> allocation is too small, or if the profile
240 * isn't in local alignment mode.
241 * <eslERANGE> if the score exceeds the limited range of
242 * a probability-space odds ratio.
243 * In either case, <*opt_sc> is undefined.
244 */
245 int
246 p7_BackwardParser(const ESL_DSQ *dsq, int L, const P7_OPROFILE *om, const P7_OMX *fwd, P7_OMX *bck, float *opt_sc)
247 {
248 #if eslDEBUGLEVEL > 0
249 if (om->M > bck->allocQ4*4) ESL_EXCEPTION(eslEINVAL, "DP matrix allocated too small (too few columns)");
250 if (bck->validR < 1) ESL_EXCEPTION(eslEINVAL, "DP matrix allocated too small (too few MDI rows)");
251 if (L >= bck->allocXR) ESL_EXCEPTION(eslEINVAL, "DP matrix allocated too small (too few X rows)");
252 if (L != fwd->L) ESL_EXCEPTION(eslEINVAL, "fwd matrix size doesn't agree with length L");
253 if (! p7_oprofile_IsLocal(om)) ESL_EXCEPTION(eslEINVAL, "Forward implementation makes assumptions that only work for local alignment");
254 #endif
255
256 return backward_engine(FALSE, dsq, L, om, fwd, bck, opt_sc);
257 }
258
259
260
261 /*****************************************************************
262 * 2. Forward/Backward engine implementations (called thru API)
263 *****************************************************************/
264
265 static int
266 forward_engine(int do_full, const ESL_DSQ *dsq, int L, const P7_OPROFILE *om, P7_OMX *ox, float *opt_sc)
267 {
268 register float32x4_t mpv, dpv, ipv; /* previous row values */
269 register float32x4_t sv; /* temp storage of 1 curr row value in progress */
270 register float32x4_t dcv; /* delayed storage of D(i,q+1) */
271 register float32x4_t xEv; /* E state: keeps max for Mk->E as we go */
272 register float32x4_t xBv; /* B state: splatted vector of B[i-1] for B->Mk calculations */
273 register float32x4_t zerov; /* zero vector: used for emulating logical shifts with VEXT */
274 float xN, xE, xB, xC, xJ; /* special states' scores */
275 int i; /* counter over sequence positions 1..L */
276 int q; /* counter over quads 0..nq-1 */
277 int j; /* counter over DD iterations (4 is full serialization) */
278 int Q = p7O_NQF(om->M); /* segment length: # of vectors */
279 float32x4_t *dpc = ox->dpf[0]; /* current row, for use in {MDI}MO(dpp,q) access macro */
280 float32x4_t *dpp; /* previous row, for use in {MDI}MO(dpp,q) access macro */
281 float32x4_t *rp; /* will point at om->rfv[x] for residue x[i] */
282 float32x4_t *tp; /* will point into (and step thru) om->tfv */
283
284 /* Initialization. */
285 zerov = vmovq_n_f32(0.0);
286 ox->M = om->M;
287 ox->L = L;
288 ox->has_own_scales = TRUE; /* all forward matrices control their own scalefactors */
289 for (q = 0; q < Q; q++)
290 MMO(dpc,q) = IMO(dpc,q) = DMO(dpc,q) = zerov;
291 xE = ox->xmx[p7X_E] = 0.;
292 xN = ox->xmx[p7X_N] = 1.;
293 xJ = ox->xmx[p7X_J] = 0.;
294 xB = ox->xmx[p7X_B] = om->xf[p7O_N][p7O_MOVE];
295 xC = ox->xmx[p7X_C] = 0.;
296
297 ox->xmx[p7X_SCALE] = 1.0;
298 ox->totscale = 0.0;
299
300 #if eslDEBUGLEVEL > 0
301 if (ox->debugging) p7_omx_DumpFBRow(ox, TRUE, 0, 9, 5, xE, xN, xJ, xB, xC); /* logify=TRUE, <rowi>=0, width=8, precision=5*/
302 #endif
303
304 for (i = 1; i <= L; i++)
305 {
306 dpp = dpc;
307 dpc = ox->dpf[do_full * i]; /* avoid conditional, use do_full as kronecker delta */
308 rp = om->rfv[dsq[i]];
309 tp = om->tfv;
310 dcv = vmovq_n_f32(0.0f);
311 xEv = vmovq_n_f32(0.0f);
312 xBv = vmovq_n_f32(xB);
313
314 /* Right shifts by 4 bytes. 4,8,12,x becomes x,4,8,12. Shift zeros on. */
315 mpv = vextq_f32(zerov, MMO(dpp,Q-1), 3);
316 dpv = vextq_f32(zerov, DMO(dpp,Q-1), 3);
317 ipv = vextq_f32(zerov, IMO(dpp,Q-1), 3);
318
319 for (q = 0; q < Q; q++) {
320 /* Calculate new MMO(i,q); don't store it yet, hold it in sv. */
321 sv = vmulq_f32(xBv, *tp); tp++;
322 #ifdef ARMDEBUG
323 printf("i= %d, q=%d, step=1 ", i, q);
324 esl_neon_dump_float(stdout, (esl_neon_128f_t)sv);
325 printf("\n");
326 #endif
327 sv = vaddq_f32(sv, vmulq_f32(mpv, *tp)); tp++;
328 #ifdef ARMDEBUG
329 printf("i= %d, q=%d, step=2 ", i, q);
330 esl_neon_dump_float(stdout, (esl_neon_128f_t)sv);
331 printf("\n");
332 #endif
333 sv = vaddq_f32(sv, vmulq_f32(ipv, *tp)); tp++;
334 #ifdef ARMDEBUG
335 printf("i= %d, q=%d, step=3 ", i, q);
336 esl_neon_dump_float(stdout, (esl_neon_128f_t)sv);
337 printf("\n");
338 #endif
339 sv = vaddq_f32(sv, vmulq_f32(dpv, *tp)); tp++;
340 #ifdef ARMDEBUG
341 printf("i= %d, q=%d, step=4 ", i, q);
342 esl_neon_dump_float(stdout, (esl_neon_128f_t)sv);
343 printf("\n");
344 #endif
345 #ifdef ARMDEBUG
346 printf("i= %d, q=%d, rp=", i, q);
347 esl_neon_dump_float(stdout, (esl_neon_128f_t)*rp);
348 printf("\n");
349 #endif
350 sv = vmulq_f32(sv, *rp); rp++;
351 #ifdef ARMDEBUG
352 printf("i= %d, q=%d, step=5 ", i, q);
353 esl_neon_dump_float(stdout, (esl_neon_128f_t)sv);
354 printf("\n");
355 #endif
356 xEv = vaddq_f32(xEv, sv);
357 #ifdef ARMDEBUG
358 printf("i= %d, q=%d, ", i, q);
359 esl_neon_dump_float(stdout, (esl_neon_128f_t)xEv);
360 printf("\n");
361 #endif
362 /* Load {MDI}(i-1,q) into mpv, dpv, ipv;
363 * {MDI}MX(q) is then the current, not the prev row
364 */
365 mpv = MMO(dpp,q);
366 dpv = DMO(dpp,q);
367 ipv = IMO(dpp,q);
368
369 /* Do the delayed stores of {MD}(i,q) now that memory is usable */
370 MMO(dpc,q) = sv;
371 DMO(dpc,q) = dcv;
372
373 /* Calculate the next D(i,q+1) partially: M->D only;
374 * delay storage, holding it in dcv
375 */
376 dcv = vmulq_f32(sv, *tp); tp++;
377
378 /* Calculate and store I(i,q); assumes odds ratio for emission is 1.0 */
379 sv = vmulq_f32(mpv, *tp); tp++;
380 IMO(dpc,q) = vaddq_f32(sv, vmulq_f32(ipv, *tp)); tp++;
381 }
382 #ifdef ARMDEBUG
383 printf("i= %d, step = 1, ", i);
384 esl_neon_dump_float(stdout, (esl_neon_128f_t) xEv);
385 printf("\n");
386 #endif
387 /* Now the DD paths. We would rather not serialize them but
388 * in an accurate Forward calculation, we have few options.
389 */
390 /* dcv has carried through from end of q loop above; store it
391 * in first pass, we add M->D and D->D path into DMX
392 */
393 /* We're almost certainly're obligated to do at least one complete
394 * DD path to be sure:
395 */
396 dcv = vextq_f32(zerov, dcv, 3);
397 DMO(dpc,0) = vmovq_n_f32(0.0);
398 tp = om->tfv + 7*Q; /* set tp to start of the DD's */
399 for (q = 0; q < Q; q++) {
400 DMO(dpc,q) = vaddq_f32(dcv, DMO(dpc,q));
401 dcv = vmulq_f32(DMO(dpc,q), *tp); tp++; /* extend DMO(q), so we include M->D and D->D paths */
402 }
403
404 /* now. on small models, it seems best (empirically) to just go
405 * ahead and serialize. on large models, we can do a bit better,
406 * by testing for when dcv (DD path) accrued to DMO(q) is below
407 * machine epsilon for all q, in which case we know DMO(q) are all
408 * at their final values. The tradeoff point is (empirically) somewhere around M=100,
409 * at least on my desktop. We don't worry about the conditional here;
410 * it's outside any inner loops.
411 */
412 if (om->M < 100){ /* Fully serialized version */
413 for (j = 1; j < 4; j++){
414 dcv = vextq_f32(zerov, dcv, 3);
415 tp = om->tfv + 7*Q; /* set tp to start of the DD's */
416 for (q = 0; q < Q; q++){ /* note, extend dcv, not DMO(q); only adding DD paths now */
417 DMO(dpc,q) = vaddq_f32(dcv, DMO(dpc,q));
418 dcv = vmulq_f32(dcv, *tp); tp++;
419 }
420 }
421 }
422 else { /* Slightly parallelized version, but which incurs some overhead */
423 for (j = 1; j < 4; j++) {
424 register uint32x4_t cv; /* keeps track of whether any DD's change DMO(q) */
425
426 dcv = vextq_f32(zerov, dcv, 3);
427 tp = om->tfv + 7*Q; /* set tp to start of the DD's */
428 cv = vmovq_n_u32(0);
429 for (q = 0; q < Q; q++){ /* using cmpgt below tests if DD changed any DMO(q) *without* conditional branch */
430 sv = vaddq_f32(dcv, DMO(dpc,q));
431 cv = vorrq_u32(cv, vcgtq_f32(sv, DMO(dpc,q)));
432 DMO(dpc,q) = sv; /* store new DMO(q) */
433 dcv = vmulq_f32(dcv, *tp); tp++; /* note, extend dcv, not DMO(q) */
434 }
435 if (esl_neon_hmax_u8((esl_neon_128i_t) cv) == 0) break; /* DD's didn't change any DMO(q)? Then done, break out. */
436 }
437 }
438 #ifdef ARMDEBUG
439 printf("i= %d, step = 2, ", i);
440 esl_neon_dump_float(stdout, (esl_neon_128f_t) xEv);
441 printf("\n");
442 #endif
443 /* Add D's to xEv */
444 for (q = 0; q < Q; q++) xEv = vaddq_f32(DMO(dpc,q), xEv);
445
446 /* Finally the "special" states, which start from Mk->E (->C, ->J->B) */
447 /* The following incantation is a horizontal sum of xEv's elements */
448 /* These must follow DD calculations, because D's contribute to E in Forward
449 * (as opposed to Viterbi)
450 */
451 esl_neon_hsum_float((esl_neon_128f_t) xEv, &xE);
452 xN = xN * om->xf[p7O_N][p7O_LOOP];
453 xC = (xC * om->xf[p7O_C][p7O_LOOP]) + (xE * om->xf[p7O_E][p7O_MOVE]);
454 xJ = (xJ * om->xf[p7O_J][p7O_LOOP]) + (xE * om->xf[p7O_E][p7O_LOOP]);
455 xB = (xJ * om->xf[p7O_J][p7O_MOVE]) + (xN * om->xf[p7O_N][p7O_MOVE]);
456 /* and now xB will carry over into next i, and xC carries over after i=L */
457
458 /* Sparse rescaling. xE above threshold? trigger a rescaling event. */
459 if (xE > 1.0e4) /* that's a little less than e^10, ~10% of our dynamic range */{
460 xN = xN / xE;
461 xC = xC / xE;
462 xJ = xJ / xE;
463 xB = xB / xE;
464
465 xEv = vmovq_n_f32(1.0 / xE);
466 for (q = 0; q < Q; q++){
467 MMO(dpc,q) = vmulq_f32(MMO(dpc,q), xEv);
468 DMO(dpc,q) = vmulq_f32(DMO(dpc,q), xEv);
469 IMO(dpc,q) = vmulq_f32(IMO(dpc,q), xEv);
470 }
471 ox->xmx[i*p7X_NXCELLS+p7X_SCALE] = xE;
472 ox->totscale += log(xE);
473 xE = 1.0;
474 }
475 else ox->xmx[i*p7X_NXCELLS+p7X_SCALE] = 1.0;
476
477 /* Storage of the specials. We could've stored these already
478 * but using xE, etc. variables makes it easy to convert this
479 * code to O(M) memory versions just by deleting storage steps.
480 */
481 ox->xmx[i*p7X_NXCELLS+p7X_E] = xE;
482 ox->xmx[i*p7X_NXCELLS+p7X_N] = xN;
483 ox->xmx[i*p7X_NXCELLS+p7X_J] = xJ;
484 ox->xmx[i*p7X_NXCELLS+p7X_B] = xB;
485 ox->xmx[i*p7X_NXCELLS+p7X_C] = xC;
486
487 #if eslDEBUGLEVEL > 0
488 if (ox->debugging) p7_omx_DumpFBRow(ox, TRUE, i, 9, 5, xE, xN, xJ, xB, xC); /* logify=TRUE, <rowi>=i, width=8, precision=5*/
489 #endif
490 #ifdef ARMDEBUG
491 printf("i= %d, xE = %f\n", i, xE);
492 #endif
493 } /* end loop over sequence residues 1..L */
494
495 /* finally C->T, and flip total score back to log space (nats) */
496 /* On overflow, xC is inf or nan (nan arises because inf*0 = nan). */
497 /* On an underflow (which shouldn't happen), we counterintuitively return infinity:
498 * the effect of this is to force the caller to rescore us with full range.
499 */
500 if (isnan(xC)) ESL_EXCEPTION(eslERANGE, "forward score is NaN");
501 else if (L>0 && xC == 0.0) ESL_EXCEPTION(eslERANGE, "forward score underflow (is 0.0)"); /* if L==0, xC *should* be 0.0; J5/118 */
502 else if (isinf(xC) == 1) ESL_EXCEPTION(eslERANGE, "forward score overflow (is infinity)");
503
504 if (opt_sc != NULL) *opt_sc = ox->totscale + log(xC * om->xf[p7O_C][p7O_MOVE]);
505 return eslOK;
506 }
507
508
509
510 static int
511 backward_engine(int do_full, const ESL_DSQ *dsq, int L, const P7_OPROFILE *om, const P7_OMX *fwd, P7_OMX *bck, float *opt_sc)
512 {
513 register float32x4_t mpv, ipv, dpv; /* previous row values */
514 register float32x4_t mcv, dcv; /* current row values */
515 register float32x4_t tmmv, timv, tdmv; /* tmp vars for accessing rotated transition scores */
516 register float32x4_t xBv; /* collects B->Mk components of B(i) */
517 register float32x4_t xEv; /* splatted E(i) */
518 register float32x4_t zerov; /* zero vector: used for emulating logical shifts with VEXT */
519 float xN, xE, xB, xC, xJ; /* special states' scores */
520 int i; /* counter over sequence positions 0,1..L */
521 int q; /* counter over quads 0..Q-1 */
522 int Q = p7O_NQF(om->M); /* segment length: # of vectors */
523 int j; /* DD segment iteration counter (4 = full serialization) */
524 float32x4_t *dpc; /* current DP row */
525 float32x4_t *dpp; /* next ("previous") DP row */
526 float32x4_t *rp; /* will point into om->rfv[x] for residue x[i+1] */
527 float32x4_t *tp; /* will point into (and step thru) om->tfv transition scores */
528
529 /* initialize the L row. */
530 zerov = vmovq_n_f32(0.0);
531 bck->M = om->M;
532 bck->L = L;
533 bck->has_own_scales = FALSE; /* backwards scale factors are *usually* given by <fwd> */
534 dpc = bck->dpf[L * do_full];
535 xJ = 0.0;
536 xB = 0.0;
537 xN = 0.0;
538 xC = om->xf[p7O_C][p7O_MOVE]; /* C<-T */
539 xE = xC * om->xf[p7O_E][p7O_MOVE]; /* E<-C, no tail */
540 xEv = vmovq_n_f32(xE);
541 dcv = vmovq_n_f32(0.0); /* solely to silence a compiler warning */
542 for (q = 0; q < Q; q++) MMO(dpc,q) = DMO(dpc,q) = xEv;
543 for (q = 0; q < Q; q++) IMO(dpc,q) = zerov;
544
545 /* init row L's DD paths, 1) first segment includes xE, from DMO(q) */
546 tp = om->tfv + 8*Q - 1; /* <*tp> now the [4 8 12 x] TDD quad */
547 dpv = vextq_f32(DMO(dpc,Q-1), zerov, 1); /* leftshift:[x 5 9 13] -> [5 9 13 x] */
548 for (q = Q-1; q >= 0; q--)
549 {
550 dcv = vmulq_f32(dpv, *tp); tp--;
551 DMO(dpc,q) = vaddq_f32(DMO(dpc,q), dcv);
552 dpv = DMO(dpc,q);
553 }
554 /* 2) three more passes, only extending DD component (dcv only; no xE contrib from DMO(q)) */
555 for (j = 1; j < 4; j++)
556 {
557 tp = om->tfv + 8*Q - 1; /* <*tp> now the [4 8 12 x] TDD quad */
558 dcv = vextq_f32(dcv, zerov, 1); /* leftshift:[x 5 9 13] -> [5 9 13 x] */
559 for (q = Q-1; q >= 0; q--)
560 {
561 dcv = vmulq_f32(dcv, *tp); tp--;
562 DMO(dpc,q) = vaddq_f32(DMO(dpc,q), dcv);
563 }
564 }
565 /* now MD init */
566 tp = om->tfv + 7*Q - 3; /* <*tp> now the [4 8 12 x] Mk->Dk+1 quad */
567 dcv = vextq_f32(DMO(dpc, 0), zerov, 1); /* leftshift:[x 5 9 13] -> [5 9 13 x] */
568 for (q = Q-1; q >= 0; q--)
569 {
570 MMO(dpc,q) = vaddq_f32(MMO(dpc,q), vmulq_f32(dcv, *tp)); tp -= 7;
571 dcv = DMO(dpc,q);
572 }
573
574 /* Sparse rescaling: same scale factors as fwd matrix */
575 if (fwd->xmx[L*p7X_NXCELLS+p7X_SCALE] > 1.0)
576 {
577 xE = xE / fwd->xmx[L*p7X_NXCELLS+p7X_SCALE];
578 xN = xN / fwd->xmx[L*p7X_NXCELLS+p7X_SCALE];
579 xC = xC / fwd->xmx[L*p7X_NXCELLS+p7X_SCALE];
580 xJ = xJ / fwd->xmx[L*p7X_NXCELLS+p7X_SCALE];
581 xB = xB / fwd->xmx[L*p7X_NXCELLS+p7X_SCALE];
582 xEv = vmovq_n_f32(1.0 / fwd->xmx[L*p7X_NXCELLS+p7X_SCALE]);
583 for (q = 0; q < Q; q++) {
584 MMO(dpc,q) = vmulq_f32(MMO(dpc,q), xEv);
585 DMO(dpc,q) = vmulq_f32(DMO(dpc,q), xEv);
586 IMO(dpc,q) = vmulq_f32(IMO(dpc,q), xEv);
587 }
588 }
589 bck->xmx[L*p7X_NXCELLS+p7X_SCALE] = fwd->xmx[L*p7X_NXCELLS+p7X_SCALE];
590 bck->totscale = log(bck->xmx[L*p7X_NXCELLS+p7X_SCALE]);
591
592 /* Stores */
593 bck->xmx[L*p7X_NXCELLS+p7X_E] = xE;
594 bck->xmx[L*p7X_NXCELLS+p7X_N] = xN;
595 bck->xmx[L*p7X_NXCELLS+p7X_J] = xJ;
596 bck->xmx[L*p7X_NXCELLS+p7X_B] = xB;
597 bck->xmx[L*p7X_NXCELLS+p7X_C] = xC;
598
599 #if eslDEBUGLEVEL > 0
600 if (bck->debugging) p7_omx_DumpFBRow(bck, TRUE, L, 9, 4, xE, xN, xJ, xB, xC); /* logify=TRUE, <rowi>=L, width=9, precision=4*/
601 #endif
602
603 /* main recursion */
604 for (i = L-1; i >= 1; i--) /* backwards stride */
605 {
606 /* phase 1. B(i) collected. Old row destroyed, new row contains
607 * complete I(i,k), partial {MD}(i,k) w/ no {MD}->{DE} paths yet.
608 */
609 dpc = bck->dpf[i * do_full];
610 dpp = bck->dpf[(i+1) * do_full];
611 rp = om->rfv[dsq[i+1]] + Q-1; /* <*rp> is now the [4 8 12 x] match emission quad */
612 tp = om->tfv + 7*Q - 1; /* <*tp> is now the [4 8 12 x] TII transition quad */
613
614 /* leftshift the first transition quads */
615 tmmv = vextq_f32(om->tfv[1], zerov, 1);
616 timv = vextq_f32(om->tfv[2], zerov, 1);
617 tdmv = vextq_f32(om->tfv[3], zerov, 1);
618
619 mpv = vmulq_f32(MMO(dpp,0), om->rfv[dsq[i+1]][0]); /* precalc M(i+1,k+1) * e(M_k+1, x_{i+1}) */
620 mpv = vextq_f32(mpv, zerov, 1);
621
622 xBv = vmovq_n_f32(0.0);
623 for (q = Q-1; q >= 0; q--) /* backwards stride */
624 {
625 ipv = IMO(dpp,q); /* assumes emission odds ratio of 1.0; i+1's IMO(q) now free */
626 IMO(dpc,q) = vaddq_f32(vmulq_f32(ipv, *tp), vmulq_f32(mpv, timv)); tp--;
627 DMO(dpc,q) = vmulq_f32(mpv, tdmv);
628 mcv = vaddq_f32(vmulq_f32(ipv, *tp), vmulq_f32(mpv, tmmv)); tp-= 2;
629
630 mpv = vmulq_f32(MMO(dpp,q), *rp); rp--; /* obtain mpv for next q. i+1's MMO(q) is freed */
631 MMO(dpc,q) = mcv;
632
633 tdmv = *tp; tp--;
634 timv = *tp; tp--;
635 tmmv = *tp; tp--;
636
637 xBv = vaddq_f32(xBv, vmulq_f32(mpv, *tp)); tp--;
638 }
639
640 /* phase 2: now that we have accumulated the B->Mk transitions in xBv, we can do the specials */
641 esl_neon_hsum_float((esl_neon_128f_t) xBv, &xB);
642 xBv = vmovq_n_f32(xB);
643
644 xC = xC * om->xf[p7O_C][p7O_LOOP];
645 xJ = (xB * om->xf[p7O_J][p7O_MOVE]) + (xJ * om->xf[p7O_J][p7O_LOOP]); /* must come after xB */
646 xN = (xB * om->xf[p7O_N][p7O_MOVE]) + (xN * om->xf[p7O_N][p7O_LOOP]); /* must come after xB */
647 xE = (xC * om->xf[p7O_E][p7O_MOVE]) + (xJ * om->xf[p7O_E][p7O_LOOP]); /* must come after xJ, xC */
648 xEv = vmovq_n_f32(xE); /* splat */
649
650
651 /* phase 3: {MD}->E paths and one step of the D->D paths */
652 tp = om->tfv + 8*Q - 1; /* <*tp> now the [4 8 12 x] TDD quad */
653 dpv = vaddq_f32(DMO(dpc,0), xEv);
654 dpv = vextq_f32(dpv, zerov, 1);
655 for (q = Q-1; q >= 0; q--)
656 {
657 dcv = vmulq_f32(dpv, *tp); tp--;
658 DMO(dpc,q) = vaddq_f32(DMO(dpc,q), vaddq_f32(dcv, xEv));
659 dpv = DMO(dpc,q);
660 MMO(dpc,q) = vaddq_f32(MMO(dpc,q), xEv);
661 }
662
663 /* phase 4: finish extending the DD paths */
664 /* fully serialized for now */
665 for (j = 1; j < 4; j++) /* three passes: we've already done 1 segment, we need 4 total */
666 {
667 dcv = vextq_f32(dcv, zerov, 1);
668 tp = om->tfv + 8*Q - 1; /* <*tp> now the [4 8 12 x] TDD quad */
669 for (q = Q-1; q >= 0; q--)
670 {
671 dcv = vmulq_f32(dcv, *tp); tp--;
672 DMO(dpc,q) = vaddq_f32(DMO(dpc,q), dcv);
673 }
674 }
675
676 /* phase 5: add M->D paths */
677 dcv = vextq_f32(DMO(dpc,0), zerov, 1);
678 tp = om->tfv + 7*Q - 3; /* <*tp> is now the [4 8 12 x] Mk->Dk+1 quad */
679 for (q = Q-1; q >= 0; q--)
680 {
681 MMO(dpc,q) = vaddq_f32(MMO(dpc,q), vmulq_f32(dcv, *tp)); tp -= 7;
682 dcv = DMO(dpc,q);
683 }
684
685 /* Sparse rescaling */
686
687 /* In rare cases [J3/119] scale factors from <fwd> are
688 * insufficient and backwards will overflow. In this case, we
689 * switch on the fly to using our own scale factors, different
690 * from those in <fwd>. This will complicate subsequent
691 * posterior decoding routines.
692 */
693 if (xB > 1.0e16) bck->has_own_scales = TRUE;
694
695 if (bck->has_own_scales) bck->xmx[i*p7X_NXCELLS+p7X_SCALE] = (xB > 1.0e4) ? xB : 1.0;
696 else bck->xmx[i*p7X_NXCELLS+p7X_SCALE] = fwd->xmx[i*p7X_NXCELLS+p7X_SCALE];
697
698 if (bck->xmx[i*p7X_NXCELLS+p7X_SCALE] > 1.0)
699 {
700 xE /= bck->xmx[i*p7X_NXCELLS+p7X_SCALE];
701 xN /= bck->xmx[i*p7X_NXCELLS+p7X_SCALE];
702 xJ /= bck->xmx[i*p7X_NXCELLS+p7X_SCALE];
703 xB /= bck->xmx[i*p7X_NXCELLS+p7X_SCALE];
704 xC /= bck->xmx[i*p7X_NXCELLS+p7X_SCALE];
705 xBv = vmovq_n_f32(1.0 / bck->xmx[i*p7X_NXCELLS+p7X_SCALE]);
706 for (q = 0; q < Q; q++) {
707 MMO(dpc,q) = vmulq_f32(MMO(dpc,q), xBv);
708 DMO(dpc,q) = vmulq_f32(DMO(dpc,q), xBv);
709 IMO(dpc,q) = vmulq_f32(IMO(dpc,q), xBv);
710 }
711 bck->totscale += log(bck->xmx[i*p7X_NXCELLS+p7X_SCALE]);
712 }
713
714 /* Stores are separate only for pedagogical reasons: easy to
715 * turn this into a more memory efficient version just by
716 * deleting the stores.
717 */
718 bck->xmx[i*p7X_NXCELLS+p7X_E] = xE;
719 bck->xmx[i*p7X_NXCELLS+p7X_N] = xN;
720 bck->xmx[i*p7X_NXCELLS+p7X_J] = xJ;
721 bck->xmx[i*p7X_NXCELLS+p7X_B] = xB;
722 bck->xmx[i*p7X_NXCELLS+p7X_C] = xC;
723
724 #if eslDEBUGLEVEL > 0
725 if (bck->debugging) p7_omx_DumpFBRow(bck, TRUE, i, 9, 4, xE, xN, xJ, xB, xC); /* logify=TRUE, <rowi>=i, width=9, precision=4*/
726 #endif
727 } /* thus ends the loop over sequence positions i */
728
729 /* Termination at i=0, where we can only reach N,B states. */
730 dpp = bck->dpf[1 * do_full];
731 tp = om->tfv; /* <*tp> is now the [1 5 9 13] TBMk transition quad */
732 rp = om->rfv[dsq[1]]; /* <*rp> is now the [1 5 9 13] match emission quad */
733 xBv = vmovq_n_f32(0.0);
734 for (q = 0; q < Q; q++)
735 {
736 mpv = vmulq_f32(MMO(dpp,q), *rp); rp++;
737 mpv = vmulq_f32(mpv, *tp); tp += 7;
738 xBv = vaddq_f32(xBv, mpv);
739 }
740 /* horizontal sum of xBv */
741 esl_neon_hsum_float((esl_neon_128f_t) xBv, &xB);
742
743 xN = (xB * om->xf[p7O_N][p7O_MOVE]) + (xN * om->xf[p7O_N][p7O_LOOP]);
744
745 bck->xmx[p7X_B] = xB;
746 bck->xmx[p7X_C] = 0.0;
747 bck->xmx[p7X_J] = 0.0;
748 bck->xmx[p7X_N] = xN;
749 bck->xmx[p7X_E] = 0.0;
750 bck->xmx[p7X_SCALE] = 1.0;
751
752 #if eslDEBUGLEVEL > 0
753 dpc = bck->dpf[0];
754 for (q = 0; q < Q; q++) /* Not strictly necessary, but if someone's looking at DP matrices, this is nice to do: */
755 MMO(dpc,q) = DMO(dpc,q) = IMO(dpc,q) = vmovq_n_f32(0.0);
756 if (bck->debugging) p7_omx_DumpFBRow(bck, TRUE, 0, 9, 4, bck->xmx[p7X_E], bck->xmx[p7X_N], bck->xmx[p7X_J], bck->xmx[p7X_B], bck->xmx[p7X_C]); /* logify=TRUE, <rowi>=0, width=9, precision=4*/
757 #endif
758
759 if (isnan(xN)) ESL_EXCEPTION(eslERANGE, "backward score is NaN");
760 else if (L>0 && xN == 0.0) ESL_EXCEPTION(eslERANGE, "backward score underflow (is 0.0)"); /* if L==0, xN *should* be 0.0 [J5/118]*/
761 else if (isinf(xN) == 1) ESL_EXCEPTION(eslERANGE, "backward score overflow (is infinity)");
762
763 if (opt_sc != NULL) *opt_sc = bck->totscale + log(xN);
764 return eslOK;
765 }
766 /*-------------- end, forward/backward engines -----------------*/
767
768
769
770
771
772 /*****************************************************************
773 * 4. Benchmark driver.
774 *****************************************************************/
775 #ifdef p7FWDBACK_BENCHMARK
776 /* -c, -x options are for debugging and testing: see fwdfilter.c for explanation */
777 /*
778 icc -O3 -static -o fwdback_benchmark -I.. -L.. -I../../easel -L../../easel -Dp7FWDBACK_BENCHMARK fwdback.c -lhmmer -leasel -lm
779
780 ./fwdback_benchmark <hmmfile> runs benchmark on both Forward and Backward parser
781 ./fwdback_benchmark -c -N100 <hmmfile> compare scores of NEON to generic impl
782 ./fwdback_benchmark -x -N100 <hmmfile> test that scores match trusted implementation.
783 */
784 #include <p7_config.h>
785
786 #include "easel.h"
787 #include "esl_alphabet.h"
788 #include "esl_getopts.h"
789 #include "esl_random.h"
790 #include "esl_randomseq.h"
791 #include "esl_stopwatch.h"
792
793 #include "hmmer.h"
794 #include "impl_neon.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 { "-c", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "-x", "compare scores to generic implementation (debug)", 0 },
800 { "-s", eslARG_INT, "42", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 },
801 { "-x", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "-c", "equate scores to trusted implementation (debug)", 0 },
802 { "-L", eslARG_INT, "400", NULL, "n>0", NULL, NULL, NULL, "length of random target seqs", 0 },
803 { "-N", eslARG_INT, "50000", NULL, "n>0", NULL, NULL, NULL, "number of random target seqs", 0 },
804 { "-F", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "-B", "only benchmark Forward", 0 },
805 { "-B", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "-F", "only benchmark Backward", 0 },
806 { "-P", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "benchmark parsing version, not full version", 0 },
807 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
808 };
809 static char usage[] = "[-options] <hmmfile>";
810 static char banner[] = "benchmark driver for Forward, Backward implementations";
811
812 int
813 main(int argc, char **argv)
814 {
815 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 1, argc, argv, banner, usage);
816 char *hmmfile = esl_opt_GetArg(go, 1);
817 ESL_STOPWATCH *w = esl_stopwatch_Create();
818 ESL_RANDOMNESS *r = esl_randomness_CreateFast(esl_opt_GetInteger(go, "-s"));
819 ESL_ALPHABET *abc = NULL;
820 P7_HMMFILE *hfp = NULL;
821 P7_HMM *hmm = NULL;
822 P7_BG *bg = NULL;
823 P7_PROFILE *gm = NULL;
824 P7_OPROFILE *om = NULL;
825 P7_GMX *gx = NULL;
826 P7_OMX *fwd = NULL;
827 P7_OMX *bck = NULL;
828 int L = esl_opt_GetInteger(go, "-L");
829 int N = esl_opt_GetInteger(go, "-N");
830 ESL_DSQ *dsq = malloc(sizeof(ESL_DSQ) * (L+2));
831 int i;
832 float fsc, bsc;
833 float fsc2, bsc2;
834 double base_time, bench_time, Mcs;
835
836 p7_FLogsumInit();
837
838 if (p7_hmmfile_Open(hmmfile, NULL, &hfp, NULL) != eslOK) p7_Fail("Failed to open HMM file %s", hmmfile);
839 if (p7_hmmfile_Read(hfp, &abc, &hmm) != eslOK) p7_Fail("Failed to read HMM");
840
841 bg = p7_bg_Create(abc);
842 p7_bg_SetLength(bg, L);
843 gm = p7_profile_Create(hmm->M, abc);
844 p7_ProfileConfig(hmm, bg, gm, L, p7_LOCAL);
845 om = p7_oprofile_Create(gm->M, abc);
846 p7_oprofile_Convert(gm, om);
847 p7_oprofile_ReconfigLength(om, L);
848
849 if (esl_opt_GetBoolean(go, "-x") && p7_FLogsumError(-0.4, -0.5) > 0.0001)
850 p7_Fail("-x here requires p7_Logsum() recompiled in slow exact mode");
851
852 if (esl_opt_GetBoolean(go, "-P")) {
853 fwd = p7_omx_Create(gm->M, 0, L);
854 bck = p7_omx_Create(gm->M, 0, L);
855 } else {
856 fwd = p7_omx_Create(gm->M, L, L);
857 bck = p7_omx_Create(gm->M, L, L);
858 }
859 gx = p7_gmx_Create(gm->M, L);
860
861 /* Get a baseline time: how long it takes just to generate the sequences */
862 esl_stopwatch_Start(w);
863 for (i = 0; i < N; i++) esl_rsq_xfIID(r, bg->f, abc->K, L, dsq);
864 esl_stopwatch_Stop(w);
865 base_time = w->user;
866
867 esl_stopwatch_Start(w);
868 for (i = 0; i < N; i++)
869 {
870 esl_rsq_xfIID(r, bg->f, abc->K, L, dsq);
871 if (esl_opt_GetBoolean(go, "-P")) {
872 if (! esl_opt_GetBoolean(go, "-B")) p7_ForwardParser (dsq, L, om, fwd, &fsc);
873 if (! esl_opt_GetBoolean(go, "-F")) p7_BackwardParser(dsq, L, om, fwd, bck, &bsc);
874 } else {
875 if (! esl_opt_GetBoolean(go, "-B")) p7_Forward (dsq, L, om, fwd, &fsc);
876 if (! esl_opt_GetBoolean(go, "-F")) p7_Backward(dsq, L, om, fwd, bck, &bsc);
877 }
878
879 if (esl_opt_GetBoolean(go, "-c") || esl_opt_GetBoolean(go, "-x"))
880 {
881 p7_GForward (dsq, L, gm, gx, &fsc2);
882 p7_GBackward(dsq, L, gm, gx, &bsc2);
883 printf("%.4f %.4f %.4f %.4f\n", fsc, bsc, fsc2, bsc2);
884 }
885 }
886 esl_stopwatch_Stop(w);
887 bench_time = w->user - base_time;
888 Mcs = (double) N * (double) L * (double) gm->M * 1e-6 / (double) bench_time;
889 esl_stopwatch_Display(stdout, w, "# CPU time: ");
890 printf("# M = %d\n", gm->M);
891 printf("# %.1f Mc/s\n", Mcs);
892
893 free(dsq);
894 p7_omx_Destroy(bck);
895 p7_omx_Destroy(fwd);
896 p7_gmx_Destroy(gx);
897 p7_oprofile_Destroy(om);
898 p7_profile_Destroy(gm);
899 p7_bg_Destroy(bg);
900 p7_hmm_Destroy(hmm);
901 p7_hmmfile_Close(hfp);
902 esl_alphabet_Destroy(abc);
903 esl_stopwatch_Destroy(w);
904 esl_randomness_Destroy(r);
905 esl_getopts_Destroy(go);
906 return 0;
907 }
908 #endif /*p7FWDBACK_BENCHMARK*/
909 /*------------------- end, benchmark driver ---------------------*/
910
911
912
913
914
915 /*****************************************************************
916 * 5. Unit tests.
917 *****************************************************************/
918 #ifdef p7FWDBACK_TESTDRIVE
919 #include "esl_random.h"
920 #include "esl_randomseq.h"
921
922 /*
923 * compare to GForward() scores.
924 */
925 static void
926 utest_fwdback(ESL_RANDOMNESS *r, ESL_ALPHABET *abc, P7_BG *bg, int M, int L, int N)
927 {
928 char *msg = "forward/backward unit test failed";
929 P7_HMM *hmm = NULL;
930 P7_PROFILE *gm = NULL;
931 P7_OPROFILE *om = NULL;
932 ESL_DSQ *dsq = malloc(sizeof(ESL_DSQ) * (L+2));
933 P7_OMX *fwd = p7_omx_Create(M, 0, L);
934 P7_OMX *bck = p7_omx_Create(M, 0, L);
935 P7_OMX *oxf = p7_omx_Create(M, L, L);
936 P7_OMX *oxb = p7_omx_Create(M, L, L);
937 P7_GMX *gx = p7_gmx_Create(M, L);
938 float tolerance;
939 float fsc1, fsc2;
940 float bsc1, bsc2;
941 float generic_sc;
942
943 p7_FLogsumInit();
944 if (p7_FLogsumError(-0.4, -0.5) > 0.0001) tolerance = 1.0; /* weaker test against GForward() */
945 else tolerance = 0.0001; /* stronger test: FLogsum() is in slow exact mode. */
946
947 p7_oprofile_Sample(r, abc, bg, M, L, &hmm, &gm, &om);
948 while (N--)
949 {
950 esl_rsq_xfIID(r, bg->f, abc->K, L, dsq);
951
952 p7_Forward (dsq, L, om, oxf, &fsc1);
953 p7_Backward (dsq, L, om, oxf, oxb, &bsc1);
954 p7_ForwardParser (dsq, L, om, fwd, &fsc2);
955 p7_BackwardParser(dsq, L, om, fwd, bck, &bsc2);
956 p7_GForward (dsq, L, gm, gx, &generic_sc);
957
958 /* Forward and Backward scores should agree with high tolerance */
959 if (fabs(fsc1-bsc1) > 0.0001) esl_fatal(msg);
960 if (fabs(fsc2-bsc2) > 0.0001) esl_fatal(msg);
961 if (fabs(fsc1-fsc2) > 0.0001) esl_fatal(msg);
962
963 /* GForward scores should approximate Forward scores,
964 * with tolerance that depends on how logsum.c was compiled
965 */
966 if (fabs(fsc1-generic_sc) > tolerance) esl_fatal(msg);
967 }
968
969 free(dsq);
970 p7_hmm_Destroy(hmm);
971 p7_omx_Destroy(oxb);
972 p7_omx_Destroy(oxf);
973 p7_omx_Destroy(bck);
974 p7_omx_Destroy(fwd);
975 p7_gmx_Destroy(gx);
976 p7_profile_Destroy(gm);
977 p7_oprofile_Destroy(om);
978 }
979 #endif /*p7FWDBACK_TESTDRIVE*/
980 /*---------------------- end, unit tests ------------------------*/
981
982
983
984
985 /*****************************************************************
986 * 6. Test driver
987 *****************************************************************/
988 #ifdef p7FWDBACK_TESTDRIVE
989 /*
990 ./fwdback_utest
991 */
992 #include <p7_config.h>
993
994 #include "easel.h"
995 #include "esl_alphabet.h"
996 #include "esl_getopts.h"
997 #include "esl_random.h"
998
999 #include "hmmer.h"
1000 #include "impl_neon.h"
1001
1002 static ESL_OPTIONS options[] = {
1003 /* name type default env range toggles reqs incomp help docgroup*/
1004 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
1005 { "-s", eslARG_INT, "42", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 },
1006 { "-L", eslARG_INT, "200", NULL, NULL, NULL, NULL, NULL, "size of random sequences to sample", 0 },
1007 { "-M", eslARG_INT, "145", NULL, NULL, NULL, NULL, NULL, "size of random models to sample", 0 },
1008 { "-N", eslARG_INT, "100", NULL, NULL, NULL, NULL, NULL, "number of random sequences to sample", 0 },
1009 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
1010 };
1011 static char usage[] = "[-options]";
1012 static char banner[] = "test driver for NEON Forward, Backward implementations";
1013
1014 int
1015 main(int argc, char **argv)
1016 {
1017 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 0, argc, argv, banner, usage);
1018 ESL_RANDOMNESS *r = esl_randomness_CreateFast(esl_opt_GetInteger(go, "-s"));
1019 ESL_ALPHABET *abc = NULL;
1020 P7_BG *bg = NULL;
1021 int M = esl_opt_GetInteger(go, "-M");
1022 int L = esl_opt_GetInteger(go, "-L");
1023 int N = esl_opt_GetInteger(go, "-N");
1024
1025 /* First round of tests for DNA alphabets. */
1026 if ((abc = esl_alphabet_Create(eslDNA)) == NULL) esl_fatal("failed to create alphabet");
1027 if ((bg = p7_bg_Create(abc)) == NULL) esl_fatal("failed to create null model");
1028
1029 utest_fwdback(r, abc, bg, M, L, N); /* normal sized models */
1030 utest_fwdback(r, abc, bg, 1, L, 10); /* size 1 models */
1031 utest_fwdback(r, abc, bg, M, 1, 10); /* size 1 sequences */
1032
1033 esl_alphabet_Destroy(abc);
1034 p7_bg_Destroy(bg);
1035
1036 /* Second round of tests for amino alphabets. */
1037 if ((abc = esl_alphabet_Create(eslAMINO)) == NULL) esl_fatal("failed to create alphabet");
1038 if ((bg = p7_bg_Create(abc)) == NULL) esl_fatal("failed to create null model");
1039
1040 utest_fwdback(r, abc, bg, M, L, N);
1041 utest_fwdback(r, abc, bg, 1, L, 10);
1042 utest_fwdback(r, abc, bg, M, 1, 10);
1043
1044 esl_alphabet_Destroy(abc);
1045 p7_bg_Destroy(bg);
1046
1047 esl_getopts_Destroy(go);
1048 esl_randomness_Destroy(r);
1049 return eslOK;
1050 }
1051 #endif /*p7FWDBACK_TESTDRIVE*/
1052 /*--------------------- end, test driver ------------------------*/
1053
1054
1055
1056 /*****************************************************************
1057 * 7. Example
1058 *****************************************************************/
1059 #ifdef p7FWDBACK_EXAMPLE
1060 /* Useful for debugging on small HMMs and sequences.
1061 *
1062 * Compares to GForward().
1063 *
1064 ./fwdback_example <hmmfile> <seqfile>
1065 */
1066 #include <p7_config.h>
1067
1068 #include "easel.h"
1069 #include "esl_alphabet.h"
1070 #include "esl_exponential.h"
1071 #include "esl_getopts.h"
1072 #include "esl_sq.h"
1073 #include "esl_sqio.h"
1074
1075 #include "hmmer.h"
1076 #include "impl_neon.h"
1077
1078 #define STYLES "--fs,--sw,--ls,--s" /* Exclusive choice for alignment mode */
1079
1080 static ESL_OPTIONS options[] = {
1081 /* name type default env range toggles reqs incomp help docgroup*/
1082 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
1083 { "-1", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "output in one line awkable format", 0 },
1084 { "-P", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "output in profmark format", 0 },
1085 { "--fs", eslARG_NONE,"default",NULL, NULL, STYLES, NULL, NULL, "multihit local alignment", 0 },
1086 { "--sw", eslARG_NONE, FALSE, NULL, NULL, STYLES, NULL, NULL, "unihit local alignment", 0 },
1087 { "--ls", eslARG_NONE, FALSE, NULL, NULL, STYLES, NULL, NULL, "multihit glocal alignment", 0 },
1088 { "--s", eslARG_NONE, FALSE, NULL, NULL, STYLES, NULL, NULL, "unihit glocal alignment", 0 },
1089 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
1090 };
1091 static char usage[] = "[-options] <hmmfile> <seqfile>";
1092 static char banner[] = "example of Forward/Backward (NEON versions)";
1093
1094 int
1095 main(int argc, char **argv)
1096 {
1097 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 2, argc, argv, banner, usage);
1098 char *hmmfile = esl_opt_GetArg(go, 1);
1099 char *seqfile = esl_opt_GetArg(go, 2);
1100 ESL_ALPHABET *abc = NULL;
1101 P7_HMMFILE *hfp = NULL;
1102 P7_HMM *hmm = NULL;
1103 P7_BG *bg = NULL;
1104 P7_PROFILE *gm = NULL;
1105 P7_OPROFILE *om = NULL;
1106 P7_GMX *gx = NULL;
1107 P7_OMX *fwd = NULL;
1108 P7_OMX *bck = NULL;
1109 ESL_SQ *sq = NULL;
1110 ESL_SQFILE *sqfp = NULL;
1111 int format = eslSQFILE_UNKNOWN;
1112 float fraw, braw, nullsc, fsc;
1113 float gfraw, gbraw, gfsc;
1114 double P, gP;
1115 int status;
1116
1117 /* Read in one HMM */
1118 if (p7_hmmfile_Open(hmmfile, NULL, &hfp, NULL) != eslOK) p7_Fail("Failed to open HMM file %s", hmmfile);
1119 if (p7_hmmfile_Read(hfp, &abc, &hmm) != eslOK) p7_Fail("Failed to read HMM");
1120
1121 /* Open sequence file for reading */
1122 sq = esl_sq_CreateDigital(abc);
1123 status = esl_sqfile_Open(seqfile, format, NULL, &sqfp);
1124 if (status == eslENOTFOUND) p7_Fail("No such file.");
1125 else if (status == eslEFORMAT) p7_Fail("Format unrecognized.");
1126 else if (status == eslEINVAL) p7_Fail("Can't autodetect stdin or .gz.");
1127 else if (status != eslOK) p7_Fail("Open failed, code %d.", status);
1128
1129 /* create default null model, then create and optimize profile */
1130 bg = p7_bg_Create(abc);
1131 p7_bg_SetLength(bg, sq->n);
1132 gm = p7_profile_Create(hmm->M, abc);
1133
1134 /* Now reconfig the models however we were asked to */
1135 if (esl_opt_GetBoolean(go, "--fs")) p7_ProfileConfig(hmm, bg, gm, sq->n, p7_LOCAL);
1136 else if (esl_opt_GetBoolean(go, "--sw")) p7_ProfileConfig(hmm, bg, gm, sq->n, p7_UNILOCAL);
1137 else if (esl_opt_GetBoolean(go, "--ls")) p7_ProfileConfig(hmm, bg, gm, sq->n, p7_GLOCAL);
1138 else if (esl_opt_GetBoolean(go, "--s")) p7_ProfileConfig(hmm, bg, gm, sq->n, p7_UNIGLOCAL);
1139
1140 /* now the optimized profile */
1141 om = p7_oprofile_Create(gm->M, abc);
1142 p7_oprofile_Convert(gm, om);
1143 /* p7_oprofile_Dump(stdout, om); */
1144
1145 /* allocate DP matrices for O(M+L) parsers */
1146 fwd = p7_omx_Create(gm->M, 0, sq->n);
1147 bck = p7_omx_Create(gm->M, 0, sq->n);
1148 gx = p7_gmx_Create(gm->M, sq->n);
1149
1150 /* allocate DP matrices for O(ML) fills */
1151 /* fwd = p7_omx_Create(gm->M, sq->n, sq->n); */
1152 /* bck = p7_omx_Create(gm->M, sq->n, sq->n); */
1153
1154 /* p7_omx_SetDumpMode(stdout, fwd, TRUE); */ /* makes the fast DP algorithms dump their matrices */
1155 /* p7_omx_SetDumpMode(stdout, bck, TRUE); */
1156
1157 while ((status = esl_sqio_Read(sqfp, sq)) == eslOK)
1158 {
1159 p7_oprofile_ReconfigLength(om, sq->n);
1160 p7_ReconfigLength(gm, sq->n);
1161 p7_bg_SetLength(bg, sq->n);
1162 p7_omx_GrowTo(fwd, om->M, 0, sq->n);
1163 p7_omx_GrowTo(bck, om->M, 0, sq->n);
1164 p7_gmx_GrowTo(gx, gm->M, sq->n);
1165
1166 p7_bg_NullOne (bg, sq->dsq, sq->n, &nullsc);
1167
1168 p7_ForwardParser (sq->dsq, sq->n, om, fwd, &fraw);
1169 p7_BackwardParser(sq->dsq, sq->n, om, fwd, bck, &braw);
1170
1171 /* p7_Forward (sq->dsq, sq->n, om, fwd, &fsc); printf("forward: %.2f nats\n", fsc); */
1172 /* p7_Backward(sq->dsq, sq->n, om, fwd, bck, &bsc); printf("backward: %.2f nats\n", bsc); */
1173
1174 /* Comparison to other F/B implementations */
1175 p7_GForward (sq->dsq, sq->n, gm, gx, &gfraw);
1176 p7_GBackward (sq->dsq, sq->n, gm, gx, &gbraw);
1177
1178 /* p7_gmx_Dump(stdout, gx, p7_DEFAULT); */
1179
1180 fsc = (fraw-nullsc) / eslCONST_LOG2;
1181 gfsc = (gfraw-nullsc) / eslCONST_LOG2;
1182 P = esl_exp_surv(fsc, om->evparam[p7_FTAU], om->evparam[p7_FLAMBDA]);
1183 gP = esl_exp_surv(gfsc, gm->evparam[p7_FTAU], gm->evparam[p7_FLAMBDA]);
1184
1185 if (esl_opt_GetBoolean(go, "-1"))
1186 {
1187 printf("%-30s\t%-20s\t%9.2g\t%6.1f\t%9.2g\t%6.1f\n", sq->name, hmm->name, P, fsc, gP, gfsc);
1188 }
1189 else if (esl_opt_GetBoolean(go, "-P"))
1190 { /* output suitable for direct use in profmark benchmark postprocessors: */
1191 printf("%g\t%.2f\t%s\t%s\n", P, fsc, sq->name, hmm->name);
1192 }
1193 else
1194 {
1195 printf("target sequence: %s\n", sq->name);
1196 printf("fwd filter raw score: %.2f nats\n", fraw);
1197 printf("bck filter raw score: %.2f nats\n", braw);
1198 printf("null score: %.2f nats\n", nullsc);
1199 printf("per-seq score: %.2f bits\n", fsc);
1200 printf("P-value: %g\n", P);
1201 printf("GForward raw score: %.2f nats\n", gfraw);
1202 printf("GBackward raw score: %.2f nats\n", gbraw);
1203 printf("GForward seq score: %.2f bits\n", gfsc);
1204 printf("GForward P-value: %g\n", gP);
1205 }
1206
1207 esl_sq_Reuse(sq);
1208 }
1209
1210 /* cleanup */
1211 esl_sq_Destroy(sq);
1212 esl_sqfile_Close(sqfp);
1213 p7_omx_Destroy(bck);
1214 p7_omx_Destroy(fwd);
1215 p7_gmx_Destroy(gx);
1216 p7_oprofile_Destroy(om);
1217 p7_profile_Destroy(gm);
1218 p7_bg_Destroy(bg);
1219 p7_hmm_Destroy(hmm);
1220 p7_hmmfile_Close(hfp);
1221 esl_alphabet_Destroy(abc);
1222 esl_getopts_Destroy(go);
1223 return 0;
1224 }
1225 #endif /*p7FWDBACK_EXAMPLE*/