Debian Codegraph @

src/impl_sse/sse_cm_dpsmall.c

raw symbols SCIP
1 /* Beginning modifications for vectorized implementation ... */
2
3 /* cm_dpsmall.c (formerly smallcyk.c)
4 * SRE, Wed Aug 2 08:42:49 2000 [St. Louis]
5 *
6 * Alignment of a CM to a target (sub)sequence.
7 *
8 * Implementation of the CM divide and conquer alignment algorithm
9 * described in [Eddy02]. Also implements standard CYK/Inside
10 * optimal alignment by dynamic programming [Durbin98].
11 *
12 * These algorithms align to the entire target (sub)sequence
13 * (e.g. global alignment). For sequence-local alignment, see
14 * scancyk.c.
15 *
16 */
17
18 /*################################################################
19 * smallcyk's external API:
20 *
21 * CYKDivideAndConquer() - The divide and conquer algorithm. Align
22 * a model to a (sub)sequence.
23 * CYKInside() - Align model to (sub)sequence, using normal
24 * CYK/Inside algorithm.
25 * CYKInsideScore() - Calculate the CYK/Inside score of optimal
26 * alignment, without recovering the alignment;
27 * allows timing CYK/Inside without blowing
28 * out memory, for large target RNAs.
29 *
30 * CYKDemands() - Print a bunch of info comparing predicted d&c
31 * time/memory requirements to standard CYK/inside
32 * time/memory requirements.
33 *
34 *################################################################
35 */
36
37 // FIXME: Assuming 'int' is 32-bit in the context of SSE hardware...
38 // not sure how well that actually holds
39
40 #include <esl_config.h>
41 #include "config.h"
42
43 #include <stdio.h>
44 #include <stdlib.h>
45
46 #include <xmmintrin.h> /* SSE */
47 #include <emmintrin.h> /* SSE2 */
48
49 #include "easel.h"
50 #include "esl_stack.h"
51 #include "esl_vectorops.h"
52 #include "esl_sse.h"
53
54 #include "hmmer.h"
55
56 #include "infernal.h"
57
58 #include "impl_sse.h"
59
60 #define WORDRSHIFTX(a,b,x) (_mm_or_si128(_mm_slli_si128(a,2*x),_mm_srli_si128(b,(8-x)*2)))
61
62 typedef struct sse_deck_s {
63 __m128 *mem;
64 __m128 **vec;
65 __m128i **ivec; /* points to same place as vec, but typed as __m128i */
66 } sse_deck_t;
67
68 struct sse_deckpool_s {
69 sse_deck_t **pool;
70 int n;
71 int nalloc;
72 int block;
73 };
74
75 /* The dividers and conquerors.
76 */
77 static float sse_generic_splitter(CM_t *cm, ESL_DSQ *dsq, int L, Parsetree_t *tr,
78 int r, int vend, int i0, int j0);
79 static float sse_wedge_splitter(CM_t *cm, ESL_DSQ *dsq, int L, Parsetree_t *tr,
80 int r, int z, int i0, int j0);
81 static void sse_v_splitter(CM_t *cm, ESL_DSQ *dsq, int L, Parsetree_t *tr,
82 int r, int z, int i0, int i1, int j1, int j0, int useEL);
83
84 /* The alignment engines.
85 */
86 static float sse_inside(CM_t *cm, ESL_DSQ *dsq, int L,
87 int r, int z, int i0, int j0, int do_full,
88 sse_deck_t **alpha, sse_deck_t ***ret_alpha,
89 struct sse_deckpool_s *dpool, struct sse_deckpool_s **ret_dpool,
90 sse_deck_t ***ret_shadow, int allow_begin, int *ret_b, float *ret_bsc);
91 static void sse_outside(CM_t *cm, ESL_DSQ *dsq, int L, int vroot, int vend, int i0, int j0,
92 int do_full, sse_deck_t **beta, sse_deck_t ***ret_beta,
93 struct sse_deckpool_s *dpool, struct sse_deckpool_s **ret_dpool);
94 static float sse_vinside(CM_t *cm, ESL_DSQ *dsq, int L,
95 int r, int z, int i0, int i1, int j1, int j0, int useEL,
96 int do_full, sse_deck_t **a, sse_deck_t ***ret_a,
97 struct sse_deckpool_s *dpool, struct sse_deckpool_s **ret_dpool,
98 sse_deck_t ***ret_shadow,
99 int allow_begin, int *ret_b, float *ret_bsc);
100 static void sse_voutside(CM_t *cm, ESL_DSQ *dsq, int L,
101 int r, int z, int i0, int i1, int j1, int j0, int useEL,
102 int do_full, sse_deck_t **beta, sse_deck_t ***ret_beta,
103 struct sse_deckpool_s *dpool, struct sse_deckpool_s **ret_dpool);
104
105 /* The traceback routines.
106 */
107 static float sse_insideT(CM_t *cm, ESL_DSQ *dsq, int L, Parsetree_t *tr,
108 int r, int z, int i0, int j0, int allow_begin);
109 static float sse_vinsideT(CM_t *cm, ESL_DSQ *dsq, int L, Parsetree_t *tr,
110 int r, int z, int i0, int i1, int j1, int j0, int useEL,
111 int allow_begin);
112
113 /* The size calculators.
114 */
115 float sse_insideT_size(CM_t *cm, int L, int r, int z, int i0, int j0, int x);
116 float sse_vinsideT_size(CM_t *cm, int r, int z, int i0, int i1, int j1, int j0, int x);
117 static int cyk_deck_count(CM_t *cm, int r, int z);
118 static int cyk_extra_decks(CM_t *cm);
119
120 /* The memory management routines
121 */
122 struct sse_deckpool_s *sse_deckpool_create(void);
123 void sse_deckpool_push(struct sse_deckpool_s *dpool, sse_deck_t *deck);
124 int sse_deckpool_pop(struct sse_deckpool_s *d, sse_deck_t **ret_deck);
125 void sse_deckpool_free(struct sse_deckpool_s *d);
126 float sse_size_vjd_deck(int L, int i, int j, int x);
127 sse_deck_t* sse_alloc_vjd_deck(int L, int i, int j, int x);
128 void sse_free_vjd_deck(sse_deck_t *a);
129 void sse_free_vjd_matrix(sse_deck_t **a, int M);
130 float sse_size_vji_deck(int i0, int i1, int j1, int j0, int x);
131 sse_deck_t* sse_alloc_vji_deck(int i0, int i1, int j1, int j0, int x);
132 void sse_free_vji_deck(sse_deck_t *a);
133 void sse_free_vji_matrix(sse_deck_t **a, int M);
134
135 /* BE_EFFICIENT and BE_PARANOID are alternative (exclusive) settings
136 * for the do_full? argument to the alignment engines.
137 */
138 #define BE_EFFICIENT 0 /* setting for do_full: small memory mode */
139 #define BE_PARANOID 1 /* setting for do_full: keep whole matrix, perhaps for debugging */
140
141 /* Special flags for use in shadow (traceback) matrices, instead of
142 * offsets to connected states. When yshad[0][][] is USED_LOCAL_BEGIN,
143 * the b value returned by inside() is the best connected state (a 0->b
144 * local entry). When yshad[v][][] is USED_EL, there is a v->EL transition
145 * and the remaining subsequence is aligned to the EL state.
146 */
147 #define USED_LOCAL_BEGIN 101
148 #define USED_EL 102
149
150 #ifdef _EPI16_DEBUG_OUTPUT
151 static void vecprint_epi16(CM_OPTIMIZED *ocm, __m128i a)
152 {
153 int z;
154 union {
155 __m128i vec;
156 int16_t i[8];
157 } x;
158 float f[8];
159
160 x.vec = a;
161 for (z = 0; z<8; z++) f[z] = (float) x.i[z]/ocm->scale_w;
162 fprintf(stderr,"%6.2f %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f",f[0],f[1],f[2],f[3],f[4],f[5],f[6],f[7]);
163
164 return;
165 }
166 #endif
167
168 /* Function: CYKDivideAndConquer()
169 * Date: SRE, Sun Jun 3 19:32:14 2001 [St. Louis]
170 *
171 * Purpose: Align a CM to a (sub)sequence using the divide and conquer
172 * algorithm. Return the score (in bits) and a traceback
173 * structure.
174 *
175 * The simplest call to this, for a model cm and a sequence
176 * dsq of length L and no bands on d:
177 * CYKDivideAndConquer(cm, dsq, L, 0, 1, &tr, NULL, NULL);
178 * which will align the model to the entire sequence. (The alignment
179 * will be global w.r.t the sequence.)
180 *
181 * Sometimes we already know the second state in the traceback:
182 * a CYKScan() will tell us r, for a 0->r local begin transition.
183 * (It also tells us i0, j0: the bounds of a high-scoring subsequence
184 * hit in the target sequence.) We take all this information in
185 * as a shortcut. The 0->r transition is still counted
186 * towards the score. That is, CYKDivideAndConquer() always
187 * gives a parsetree rooted at state 0, the root, and the sc
188 * we return is the score for that complete parse tree.
189 *
190 * Args: cm - the covariance model
191 * dsq - the digitized sequence, 1..L
192 * L - length of sequence
193 * r - root of subgraph to align to target subseq (usually 0, the model's root)
194 * i0 - start of target subsequence (often 1, beginning of sq)
195 * j0 - end of target subsequence (often L, end of sq)
196 * ret_tr - RETURN: traceback (pass NULL if trace isn't wanted)
197 *
198 * Returns: score of the alignment in bits.
199 */
200 float
201 SSE_CYKDivideAndConquer(CM_t *cm, ESL_DSQ *dsq, int L, int r, int i0, int j0, Parsetree_t **ret_tr)
202 {
203 Parsetree_t *tr;
204 float sc;
205 int z;
206
207 /*printf("alignment strategy:CYKDivideAndConquer:nb:small\n");*/
208 /* Trust, but verify.
209 * Check out input parameters.
210 */
211 if (cm->stid[r] != ROOT_S) {
212 if (! (cm->flags & CMH_LOCAL_BEGIN)) cm_Fail("internal error: we're not in local mode, but r is not root");
213 if (cm->stid[r] != MATP_MP && cm->stid[r] != MATL_ML &&
214 cm->stid[r] != MATR_MR && cm->stid[r] != BIF_B)
215 cm_Fail("internal error: trying to do a local begin at a non-mainline start");
216 }
217
218 /* Create a parse tree structure.
219 * The traceback machinery expects to build on a start state already
220 * in the parsetree, so initialize by adding the root state.
221 */
222 tr = CreateParsetree(100);
223 InsertTraceNode(tr, -1, TRACE_LEFT_CHILD, i0, j0, 0); /* init: attach the root S */
224 z = cm->M-1;
225 sc = 0.;
226
227 /* If r != 0, we already know we're starting with a local entry transition 0->r;
228 * add that node too, and count the begin transition towards the score. We have
229 * just done our one allowed local begin, so allow_begin becomes FALSE.
230 */
231 if (r != 0)
232 {
233 InsertTraceNode(tr, 0, TRACE_LEFT_CHILD, i0, j0, r);
234 z = CMSubtreeFindEnd(cm, r);
235 sc = cm->beginsc[r];
236 }
237
238 /* Start the divide and conquer recursion: call the generic_splitter()
239 */
240 sc += sse_generic_splitter(cm, dsq, L, tr, r, z, i0, j0);
241
242 /* Free memory and return
243 */
244 if (ret_tr != NULL) *ret_tr = tr; else FreeParsetree(tr);
245 ESL_DPRINTF1(("returning from CYKDivideAndConquer() sc : %f\n", sc));
246 return sc;
247 }
248
249 /* Function: CYKInside()
250 * Date: SRE, Sun Jun 3 19:48:33 2001 [St. Louis]
251 *
252 * Purpose: Wrapper for the insideT() routine - solve
253 * a full alignment problem, return the traceback
254 * and the score, without dividing & conquering.
255 *
256 * Analogous to CYKDivideAndConquer() in many respects;
257 * see the more extensive comments in that function for
258 * more details on shared aspects.
259 *
260 * Args: cm - the covariance model
261 * sq - the sequence, 1..L
262 * r - root of subgraph to align to target subseq (usually 0, the model's root)
263 * i0 - start of target subsequence (often 1, beginning of sq)
264 * j0 - end of target subsequence (often L, end of sq)
265 * ret_tr - RETURN: traceback (pass NULL if trace isn't wanted)
266 *
267 * Returns: score of the alignment in bits.
268 */
269 float
270 SSE_CYKInside(CM_t *cm, ESL_DSQ *dsq, int L, int r, int i0, int j0, Parsetree_t **ret_tr)
271 {
272 Parsetree_t *tr;
273 int z;
274 float sc;
275
276 /* Trust, but verify.
277 * Check out input parameters.
278 */
279 if (cm->stid[r] != ROOT_S) {
280 if (! (cm->flags & CMH_LOCAL_BEGIN)) cm_Fail("internal error: we're not in local mode, but r is not root");
281 if (cm->stid[r] != MATP_MP && cm->stid[r] != MATL_ML &&
282 cm->stid[r] != MATR_MR && cm->stid[r] != BIF_B)
283 cm_Fail("internal error: trying to do a local begin at a non-mainline start");
284 }
285
286 /* Create the parse tree, and initialize.
287 */
288 tr = CreateParsetree(100);
289 InsertTraceNode(tr, -1, TRACE_LEFT_CHILD, 1, L, 0); /* init: attach the root S */
290 z = cm->M-1;
291 sc = 0.;
292
293 /* Deal with case where we already know a local entry transition 0->r
294 */
295 if (r != 0)
296 {
297 InsertTraceNode(tr, 0, TRACE_LEFT_CHILD, i0, j0, r);
298 z = CMSubtreeFindEnd(cm, r);
299 sc = cm->beginsc[r];
300 }
301
302 /* Solve the whole thing with one call to insideT.
303 */
304 sc += sse_insideT(cm, dsq, L, tr, r, z, i0, j0, (r==0));
305
306 if (ret_tr != NULL) *ret_tr = tr; else FreeParsetree(tr);
307 return sc;
308 }
309
310 /* Function: CYKInsideScore()
311 * Date: SRE, Tue Apr 9 05:21:22 2002 [St. Louis]
312 *
313 * Purpose: Wrapper for the inside() routine. Solve
314 * a full alignment problem in one pass of inside,
315 * in memory-saving mode, returning only the score.
316 *
317 * Fairly useless. Written just to obtain timings
318 * for SSU and LSU alignments, for comparison to
319 * divide and conquer.
320 *
321 * Analogous to CYKDivideAndConquer() in many respects;
322 * see the more extensive comments in that function for
323 * more details on shared aspects.
324 *
325 * Args: cm - the covariance model
326 * dsq - the sequence, 1..L
327 * L - length of sequence
328 * r - root of subgraph to align to target subseq (usually 0, the model's root)
329 * i0 - start of target subsequence (often 1, beginning of sq)
330 * j0 - end of target subsequence (often L, end of sq)
331 *
332 * Returns: score of the alignment in bits.
333 */
334 float
335 SSE_CYKInsideScore(CM_t *cm, ESL_DSQ *dsq, int L, int r, int i0, int j0)
336 {
337 int z;
338 float sc;
339 int b;
340 float bsc;
341
342 z = cm->M-1;
343 sc = 0.;
344
345 if (r != 0)
346 {
347 z = CMSubtreeFindEnd(cm, r);
348 sc = cm->beginsc[r];
349 }
350
351 sc += sse_inside(cm, dsq, L, r, z, i0, j0, FALSE,
352 NULL, NULL, NULL, NULL, NULL,
353 (r==0), &b, &bsc);
354 if (bsc > sc) sc = bsc;
355
356 return sc;
357 }
358
359
360 /* Function: CYKDemands()
361 * Date: SRE, Sun Jun 3 20:00:54 2001 [St. Louis]
362 *
363 * Purpose: Print out information on the computational
364 * complexity of an alignment problem for divide
365 * and conquer versus the full CYK.
366 *
367 * Args: cm - the model
368 * L - length of sequence.
369 * be_quiet - TRUE to not print info, just return number of DP calcs
370 *
371 * Returns: (float) the total number of DP calculations
372 */
373 float
374 SSE_CYKDemands(CM_t *cm, int L, int be_quiet)
375 {
376 float Mb_per_deck; /* megabytes per deck */
377 int bif_decks; /* bifurcation decks */
378 int nends; /* end decks (only need 1, even for multiple E's */
379 int maxdecks; /* maximum # of decks needed by CYKInside() */
380 int extradecks; /* max # of extra decks needed for bifurcs */
381 float smallmemory; /* how much memory small version of CYKInside() needs */
382 float bigmemory; /* how much memory a full CYKInside() would take */
383 float dpcells; /* # of dp cells */
384 float bifcalcs; /* # of inner loops executed for bifurcation calculations */
385 float dpcalcs; /* # of inner loops executed for non-bif calculations */
386 int j;
387 /*float avg_Mb_per_banded_deck;*/ /* average megabytes per deck in mem efficient big mode */
388 const int vecwidth = 4;
389
390 Mb_per_deck = sse_size_vjd_deck(L, 1, L, vecwidth);
391 bif_decks = CMCountStatetype(cm, B_st);
392 nends = CMCountStatetype(cm, E_st);
393 maxdecks = cyk_deck_count(cm, 0, cm->M-1);
394 extradecks = cyk_extra_decks(cm);
395 smallmemory = (float) maxdecks * Mb_per_deck;
396 bifcalcs = 0.;
397 for (j = 0; j <= L; j++)
398 bifcalcs += (float)(j+1)*(float)(j+2)/2.;
399 bifcalcs *= (float) bif_decks;
400 dpcalcs = (float) (L+2)*(float)(L+1)*0.5*(float) (cm->M - bif_decks - nends +1);
401 bigmemory = (float) (cm->M - nends +1) * Mb_per_deck;
402 dpcells = (float) (L+2)*(float)(L+1)*0.5*(float) (cm->M - nends +1);
403 /*avg_Mb_per_banded_deck = 0.; */ /* irrelevant */
404
405 if(!be_quiet)
406 {
407 printf("CYK cpu/memory demand estimates:\n");
408 printf("Mb per cyk deck: %.4f\n", Mb_per_deck);
409 printf("# of decks (M): %d\n", cm->M);
410 printf("# of decks needed in small CYK: %d\n", maxdecks);
411 printf("# of extra decks needed: %d\n", extradecks);
412 printf("RAM needed for full CYK, Mb: %.2f\n", bigmemory);
413 printf("RAM needed for small CYK, Mb: %.2f\n", smallmemory);
414 printf("# of dp cells, total: %.3g\n", dpcells);
415 printf("# of non-bifurc dp cells: %.3g\n", dpcalcs);
416 printf("# of bifurcations: %d\n", bif_decks);
417 printf("# of bifurc dp inner loop calcs: %.3g\n", bifcalcs);
418 printf("# of dp inner loops: %.3g\n", dpcalcs+bifcalcs);
419 }
420 return (dpcalcs + bifcalcs);
421 }
422
423 /*################################################################
424 * The dividers and conquerors.
425 *################################################################*/
426
427 /* Function: generic_splitter()
428 * Date: SRE, Sat May 12 15:08:38 2001 [CSHL]
429 *
430 * Purpose: Solve a "generic problem": best parse of
431 * a possibly bifurcated subgraph cm^r_z to
432 * a substring sq->sq[i0..j0]. r is usually a start
433 * state (S_st) but may be any non-end state type in
434 * the case of local alignment begins (ROOT 0->r).
435 * z is always an end state (E_st).
436 *
437 * Given: a cm subgraph from r..z
438 * a subsequence from i0..j0
439 * Attaches the optimal trace T{r..z}, exclusive of r
440 * and inclusive of z, to tr.
441 *
442 * A full divide & conquer never terminates
443 * in generic_splitter; the recursion must
444 * terminate in v_splitter and wedge_splitter;
445 * so we don't test an end-of-recursion boundary.
446 *
447 * Args: cm - model
448 * sq - sequence, digitized, 1..L
449 * tr - the traceback we're adding on to.
450 * r - index of the root state of this problem in the model
451 * z - index of an end state (E_st) in the model
452 * i0 - start in the sequence (1..L)
453 * j0 - end in the sequence (1..L)
454 *
455 * Returns: score of the optimal parse of sq(i0..j0) with cm^r_z
456 */
457 static float
458 sse_generic_splitter(CM_t *cm, ESL_DSQ *dsq, int L, Parsetree_t *tr,
459 int r, int z, int i0, int j0)
460 {
461 sse_deck_t **alpha;
462 sse_deck_t **beta;
463 struct sse_deckpool_s *pool;
464 int v,w,y; /* state indices */
465 int wend, yend; /* indices for end of subgraphs rooted at w,y */
466 int jp, dp, kp; /* j': relative position in subseq, 0..W */
467 int W, sW; /* length of subseq i0..j0 */
468 float sc; /* tmp variable for a score */
469 int j,k; /* sequence indices */
470 float best_sc; /* optimal score at the optimal split point */
471 int best_k; /* optimal k for the optimal split */
472 int best_d; /* optimal d for the optimal split */
473 int best_j; /* optimal j for the optimal split */
474 __m128 vb_sc, vb_k, vb_d, vb_j; /*vectors of optimal sc, k, d, j */
475 __m128 tmpv, mask;
476 __m128i doffset;
477 __m128 vec_k, vec_d, vec_j;
478 __m128 begr_v;
479 int tv; /* remember the position of a bifurc in the trace. */
480 int b1,b2; /* argmax_v for 0->v local begin transitions */
481 float b1_sc, b2_sc; /* max_v scores for 0->v local begin transitions */
482 float *vec_access;
483 const int vecwidth = 4;
484 __m128 neginfv;
485
486 neginfv = _mm_set1_ps(-eslINFINITY);
487
488 /* 1. If the generic problem is small enough, solve it with inside^T,
489 * and append the trace to tr.
490 */
491 if (sse_insideT_size(cm, L, r, z, i0, j0, vecwidth) < RAMLIMIT) {
492 ESL_DPRINTF2(("Solving a generic w/ insideT - G%d[%s]..%d[%s], %d..%d\n",
493 r, UniqueStatetype(cm->stid[r]),
494 z, UniqueStatetype(cm->stid[z]),
495 i0, j0));
496 sc = sse_insideT(cm, dsq, L, tr, r, z, i0, j0, (r==0));
497
498 return sc;
499 }
500
501 /* 2. Traverse down from r, find first bifurc.
502 * The lowest a bifurc could be: B-S-E/S-IL-E = vend-5
503 *
504 */
505 for (v = r; v <= z-5; v++)
506 if (cm->sttype[v] == B_st) break; /* found the first bifurcation, now v */
507
508 /* 3. If there was no bifurcation, this is a wedge problem; solve it
509 * with wedge_splitter.
510 */
511 if (v > z-5) { /* no bifurc? it's a wedge problem */
512 if (cm->sttype[z] != E_st) cm_Fail("inconceivable.");
513 sc = sse_wedge_splitter(cm, dsq, L, tr, r, z, i0, j0);
514 return sc;
515 }
516
517 /* Set up the state quartet r,v,w,y for a divide and conquer
518 * solution of the generic problem.
519 */
520 w = cm->cfirst[v]; /* index of left S */
521 y = cm->cnum[v]; /* index right S */
522 if (w < y) { wend = y-1; yend = z; }
523 else { yend = w-1; wend = z; }
524
525 /* Calculate alpha[w] deck and alpha[y] deck.
526 * We also get b1: best choice for 0->b local begin. b1_sc is the score if we do this.
527 * Analogous for b2, b2_sc on the other side.
528 */
529 sse_inside(cm, dsq, L, w, wend, i0, j0, BE_EFFICIENT, NULL, &alpha, NULL, &pool, NULL,
530 (r==0), &b1, &b1_sc);
531 sse_inside(cm, dsq, L, y, yend, i0, j0, BE_EFFICIENT, alpha, &alpha, pool, &pool, NULL,
532 (r==0), &b2, &b2_sc);
533
534 /* Calculate beta[v] deck (stick it in alpha). Let the pool get free'd.
535 * (If we're doing local alignment, deck M is the beta[EL] deck.)
536 */
537 sse_outside(cm, dsq, L, r, v, i0, j0, BE_EFFICIENT, alpha, &beta, pool, NULL);
538
539 /* Find the optimal split at the B.
540 W = j0-i0+1;
541 best_sc = IMPOSSIBLE;
542 for (jp = 0; jp <= W; jp++)
543 {
544 j = i0-1+jp;
545 for (d = 0; d <= jp; d++)
546 for (k = 0; k <= d; k++)
547 if ((sc = alpha[w][j-k][d-k] + alpha[y][j][k] + beta[v][j][d]) > best_sc)
548 {
549 best_sc = sc;
550 best_k = k;
551 best_j = j;
552 best_d = d;
553 }
554 }
555 */
556
557 W = j0-i0+1;
558 doffset = _mm_setr_epi32(0, 1, 2, 3);
559 vb_sc = _mm_set1_ps(-eslINFINITY);
560 for (jp = 0; jp <= W; jp++) {
561 j = i0-1+jp;
562 vec_j = (__m128) _mm_set1_epi32(j);
563 sW = jp/vecwidth;
564
565 /* case: k = 0 */
566 vec_access = (float *) (&alpha[y]->vec[j][0]);
567 begr_v = _mm_set1_ps(*vec_access);
568 vec_k = (__m128) _mm_set1_epi32(0);
569 for (dp = 0; dp <= sW; dp++)
570 {
571 vec_d = (__m128) _mm_add_epi32(doffset, _mm_set1_epi32(dp*vecwidth));
572 tmpv = _mm_add_ps(alpha[w]->vec[j][dp], begr_v);
573 tmpv = _mm_add_ps(tmpv, beta[v]->vec[j][dp]);
574 mask = _mm_cmpgt_ps(tmpv, vb_sc);
575 vb_sc = _mm_max_ps(tmpv, vb_sc);
576 vb_k = esl_sse_select_ps(vb_k, vec_k, mask);
577 vb_j = esl_sse_select_ps(vb_j, vec_j, mask);
578 vb_d = esl_sse_select_ps(vb_d, vec_d, mask);
579 }
580 /* case: k = 4x */
581 for (k = 4; k <= jp; k+=4)
582 {
583 vec_k = (__m128) _mm_set1_epi32(k);
584 kp = k/vecwidth;
585 vec_access = (float *) (&alpha[y]->vec[j][kp]);
586 begr_v = _mm_set1_ps(*vec_access);
587
588 for (dp = kp; dp <= sW; dp++)
589 {
590 vec_d = (__m128) _mm_add_epi32(doffset, _mm_set1_epi32(dp*vecwidth));
591 tmpv = _mm_add_ps(alpha[w]->vec[j-k][dp-kp], begr_v);
592 tmpv = _mm_add_ps(tmpv, beta[v]->vec[j][dp]);
593 mask = _mm_cmpgt_ps(tmpv, vb_sc);
594 vb_sc = _mm_max_ps(tmpv, vb_sc);
595 vb_k = esl_sse_select_ps(vb_k, vec_k, mask);
596 vb_j = esl_sse_select_ps(vb_j, vec_j, mask);
597 vb_d = esl_sse_select_ps(vb_d, vec_d, mask);
598 }
599 }
600 /* case k = 4x+1 */
601 for (k = 1; k <= jp; k+=4)
602 {
603 vec_k = (__m128) _mm_set1_epi32(k);
604 dp = kp = k/vecwidth;
605 vec_access = (float *) (&alpha[y]->vec[j][kp]) + 1;
606 begr_v = _mm_set1_ps(*vec_access);
607
608 vec_d = (__m128) _mm_add_epi32(doffset, _mm_set1_epi32(dp*vecwidth));
609 tmpv = esl_sse_rightshift_ps(alpha[w]->vec[j-k][0], neginfv);
610 tmpv = _mm_add_ps(tmpv, begr_v);
611 tmpv = _mm_add_ps(tmpv, beta[v]->vec[j][dp]);
612 mask = _mm_cmpgt_ps(tmpv, vb_sc);
613 vb_sc = _mm_max_ps(tmpv, vb_sc);
614 vb_k = esl_sse_select_ps(vb_k, vec_k, mask);
615 vb_j = esl_sse_select_ps(vb_j, vec_j, mask);
616 vb_d = esl_sse_select_ps(vb_d, vec_d, mask);
617
618 for (dp = kp+1; dp <= sW; dp++)
619 {
620 vec_d = (__m128) _mm_add_epi32(doffset, _mm_set1_epi32(dp*vecwidth));
621 tmpv = alt_rightshift_ps(alpha[w]->vec[j-k][dp-kp], alpha[w]->vec[j-k][dp-kp-1]);
622 tmpv = _mm_add_ps(tmpv, begr_v);
623 tmpv = _mm_add_ps(tmpv, beta[v]->vec[j][dp]);
624 mask = _mm_cmpgt_ps(tmpv, vb_sc);
625 vb_sc = _mm_max_ps(tmpv, vb_sc);
626 vb_k = esl_sse_select_ps(vb_k, vec_k, mask);
627 vb_j = esl_sse_select_ps(vb_j, vec_j, mask);
628 vb_d = esl_sse_select_ps(vb_d, vec_d, mask);
629 }
630 }
631 /* case k = 4x+2 */
632 for (k = 2; k <= jp; k+=4)
633 {
634 vec_k = (__m128) _mm_set1_epi32(k);
635 dp = kp = k/vecwidth;
636 vec_access = (float *) (&alpha[y]->vec[j][kp]) + 2;
637 begr_v = _mm_set1_ps(*vec_access);
638
639 vec_d = (__m128) _mm_add_epi32(doffset, _mm_set1_epi32(dp*vecwidth));
640 tmpv = _mm_movelh_ps(neginfv, alpha[w]->vec[j-k][0]);
641 tmpv = _mm_add_ps(tmpv, begr_v);
642 tmpv = _mm_add_ps(tmpv, beta[v]->vec[j][dp]);
643 mask = _mm_cmpgt_ps(tmpv, vb_sc);
644 vb_sc = _mm_max_ps(tmpv, vb_sc);
645 vb_k = esl_sse_select_ps(vb_k, vec_k, mask);
646 vb_j = esl_sse_select_ps(vb_j, vec_j, mask);
647 vb_d = esl_sse_select_ps(vb_d, vec_d, mask);
648
649 for (dp = kp+1; dp <= sW; dp++)
650 {
651 vec_d = (__m128) _mm_add_epi32(doffset, _mm_set1_epi32(dp*vecwidth));
652 tmpv = _mm_movelh_ps(neginfv, alpha[w]->vec[j-k][dp-kp]);
653 tmpv = _mm_movehl_ps(tmpv, alpha[w]->vec[j-k][dp-kp-1]);
654 tmpv = _mm_add_ps(tmpv, begr_v);
655 tmpv = _mm_add_ps(tmpv, beta[v]->vec[j][dp]);
656 mask = _mm_cmpgt_ps(tmpv, vb_sc);
657 vb_sc = _mm_max_ps(tmpv, vb_sc);
658 vb_k = esl_sse_select_ps(vb_k, vec_k, mask);
659 vb_j = esl_sse_select_ps(vb_j, vec_j, mask);
660 vb_d = esl_sse_select_ps(vb_d, vec_d, mask);
661 }
662 }
663 /* case k = 4x+3 */
664 for (k = 3; k <= jp; k+= 4)
665 {
666 vec_k = (__m128) _mm_set1_epi32(k);
667 dp = kp = k/vecwidth;
668 vec_access = (float *) (&alpha[y]->vec[j][kp]) + 3;
669 begr_v = _mm_set1_ps(*vec_access);
670
671 vec_d = (__m128) _mm_add_epi32(doffset, _mm_set1_epi32(dp*vecwidth));
672 tmpv = esl_sse_leftshift_ps(neginfv, alpha[w]->vec[j-k][0]);
673 tmpv = _mm_add_ps(tmpv, begr_v);
674 tmpv = _mm_add_ps(tmpv, beta[v]->vec[j][dp]);
675 mask = _mm_cmpgt_ps(tmpv, vb_sc);
676 vb_sc = _mm_max_ps(tmpv, vb_sc);
677 vb_k = esl_sse_select_ps(vb_k, vec_k, mask);
678 vb_j = esl_sse_select_ps(vb_j, vec_j, mask);
679 vb_d = esl_sse_select_ps(vb_d, vec_d, mask);
680
681 for (dp = kp+1; dp <= sW; dp++)
682 {
683 vec_d = (__m128) _mm_add_epi32(doffset, _mm_set1_epi32(dp*vecwidth));
684 tmpv = esl_sse_leftshift_ps(alpha[w]->vec[j-k][dp-kp-1], alpha[w]->vec[j-k][dp-kp]);
685 tmpv = _mm_add_ps(tmpv, begr_v);
686 tmpv = _mm_add_ps(tmpv, beta[v]->vec[j][dp]);
687 mask = _mm_cmpgt_ps(tmpv, vb_sc);
688 vb_sc = _mm_max_ps(tmpv, vb_sc);
689 vb_k = esl_sse_select_ps(vb_k, vec_k, mask);
690 vb_j = esl_sse_select_ps(vb_j, vec_j, mask);
691 vb_d = esl_sse_select_ps(vb_d, vec_d, mask);
692 }
693 }
694 }
695
696 /* Local alignment only: maybe we're better off in EL?
697 */
698 if (cm->flags & CMH_LOCAL_END) {
699 vec_k = (__m128) _mm_set1_epi32(-1); /* flag for using EL above v */
700 for (jp = 0; jp <= W; jp++)
701 {
702 j = i0-1+jp;
703 vec_j = (__m128) _mm_set1_epi32(j);
704 sW = jp/vecwidth;
705 /*for (d = jp; d >= 0; d--)
706 if ((sc = beta[cm->M][j][d]) > best_sc) {
707 best_sc = sc;
708 best_k = -1;
709 best_j = j;
710 best_d = d;
711 } */
712 for (dp = 0; dp <= sW; dp++) {
713 vec_d = (__m128) _mm_add_epi32(doffset, _mm_set1_epi32(dp*vecwidth));
714 mask = _mm_cmpgt_ps(beta[cm->M]->vec[j][dp], vb_sc);
715 vb_sc = _mm_max_ps(beta[cm->M]->vec[j][dp], vb_sc);
716 vb_k = esl_sse_select_ps(vb_k, vec_k, mask);
717 vb_j = esl_sse_select_ps(vb_j, vec_j, mask);
718 vb_d = esl_sse_select_ps(vb_d, vec_d, mask);
719 }
720 }
721 }
722
723 /* Local alignment only: maybe we're better off in ROOT?
724 */
725 if (r == 0 && cm->flags & CMH_LOCAL_BEGIN) {
726 vec_j = (__m128) _mm_set1_epi32(j0);
727 vec_d = (__m128) _mm_set1_epi32(W);
728
729 tmpv = _mm_set1_ps(b1_sc);
730 vec_k = (__m128) _mm_set1_epi32(-2); /* flag for using local begin into left wedge w..wend */
731 mask = _mm_cmpgt_ps(tmpv, vb_sc);
732 vb_sc = _mm_max_ps(tmpv, vb_sc);
733 vb_k = esl_sse_select_ps(vb_k, vec_k, mask);
734 vb_j = esl_sse_select_ps(vb_j, vec_j, mask);
735 vb_d = esl_sse_select_ps(vb_d, vec_d, mask);
736
737 tmpv = _mm_set1_ps(b2_sc);
738 vec_k = (__m128) _mm_set1_epi32(-3); /* flag for using local begin into right wedge y..yend */
739 mask = _mm_cmpgt_ps(tmpv, vb_sc);
740 vb_sc = _mm_max_ps(tmpv, vb_sc);
741 vb_k = esl_sse_select_ps(vb_k, vec_k, mask);
742 vb_j = esl_sse_select_ps(vb_j, vec_j, mask);
743 vb_d = esl_sse_select_ps(vb_d, vec_d, mask);
744 }
745
746 /* Free now, before recursing.
747 * The two alpha matrices and the beta matrix
748 * actually all point to the same memory, since no
749 * decks in Inside and Outside needed to overlap.
750 * Free 'em all in one call.
751 */
752 sse_free_vjd_matrix(alpha, cm->M);
753
754 /* determine values corresponding to best score out of our 4x vector */
755 /* like esl_sse_hmax(), but re-using the mask from the scores */
756 tmpv = _mm_shuffle_ps(vb_sc, vb_sc, _MM_SHUFFLE(0, 3, 2, 1));
757 mask = _mm_cmpgt_ps(tmpv, vb_sc);
758 vb_sc = _mm_max_ps(tmpv, vb_sc);
759 tmpv = _mm_shuffle_ps(vb_k, vb_k, _MM_SHUFFLE(0, 3, 2, 1));
760 vb_k = esl_sse_select_ps(vb_k, tmpv, mask);
761 tmpv = _mm_shuffle_ps(vb_j, vb_j, _MM_SHUFFLE(0, 3, 2, 1));
762 vb_j = esl_sse_select_ps(vb_j, tmpv, mask);
763 tmpv = _mm_shuffle_ps(vb_d, vb_d, _MM_SHUFFLE(0, 3, 2, 1));
764 vb_d = esl_sse_select_ps(vb_d, tmpv, mask);
765
766 tmpv = _mm_shuffle_ps(vb_sc, vb_sc, _MM_SHUFFLE(1, 0, 3, 2));
767 mask = _mm_cmpgt_ps(tmpv, vb_sc);
768 vb_sc = _mm_max_ps(tmpv, vb_sc);
769 tmpv = _mm_shuffle_ps(vb_k, vb_k, _MM_SHUFFLE(1, 0, 3, 2));
770 vb_k = esl_sse_select_ps(vb_k, tmpv, mask);
771 tmpv = _mm_shuffle_ps(vb_j, vb_j, _MM_SHUFFLE(1, 0, 3, 2));
772 vb_j = esl_sse_select_ps(vb_j, tmpv, mask);
773 tmpv = _mm_shuffle_ps(vb_d, vb_d, _MM_SHUFFLE(1, 0, 3, 2));
774 vb_d = esl_sse_select_ps(vb_d, tmpv, mask);
775
776 union {
777 int i;
778 float f;
779 } u_tmp;
780
781 best_sc = *((float *) &vb_sc);
782 u_tmp.f = *((float *) &vb_k); best_k = u_tmp.i;
783 u_tmp.f = *((float *) &vb_j); best_j = u_tmp.i;
784 u_tmp.f = *((float *) &vb_d); best_d = u_tmp.i;
785
786 /*
787 for (k = 1; k < vecwidth; k++) {
788 if (*((float *) &vb_sc + k) > best_sc) {
789 best_sc = *((float *) &vb_sc + k);
790 best_k = *((int *) &vb_k + k);
791 best_j = *((int *) &vb_j + k);
792 best_d = *((int *) &vb_d + k);
793 }
794 }
795 */
796 /* If we're in EL, instead of B, the optimal alignment is entirely
797 * in a V problem that's still above us. The TRUE flag sets useEL.
798 */
799 if (best_k == -1) {
800 sse_v_splitter(cm, dsq, L, tr, r, v, i0, best_j-best_d+1, best_j, j0, TRUE);
801 return best_sc;
802 }
803
804 /* Else: if we're in the root 0, we know which r we did our local begin into.
805 * We have a generic problem rooted there. The FALSE flag disallows
806 * any further local begins.
807 */
808 if (best_k == -2) {
809 InsertTraceNode(tr, tr->n-1, TRACE_LEFT_CHILD, i0, j0, b1);
810 z = CMSubtreeFindEnd(cm, b1);
811 sse_generic_splitter(cm, dsq, L, tr, b1, z, i0, j0);
812 return best_sc;
813 }
814 if (best_k == -3) {
815 InsertTraceNode(tr, tr->n-1, TRACE_LEFT_CHILD, i0, j0, b2);
816 z = CMSubtreeFindEnd(cm, b2);
817 sse_generic_splitter(cm, dsq, L, tr, b2, z, i0, j0);
818 return best_sc;
819 }
820
821 /* Else (the usual case), ok, we did use B in the optimal split.
822 * Split now into a V problem and two generic problems, and recurse
823 * left fragment: i1 = j-d+1, j1 = j-k, vroot = w, vend = wend
824 * right frag: i2 = j-k+1, j2 = j, vroot = y, vend = yend
825 *
826 * The problems must be solved in a particular order, since we're
827 * constructing the trace in a postorder traversal.
828 */
829 ESL_DPRINTF2(("Generic splitter:\n"));
830 ESL_DPRINTF2((" V: G%d[%s]..%d[%s], %d..%d//%d..%d\n",
831 r, UniqueStatetype(cm->stid[r]),
832 v, UniqueStatetype(cm->stid[v]),
833 i0, best_j-best_d+1, best_j, j0));
834 ESL_DPRINTF2((" generic: G%d[%s]..%d[%s], %d..%d\n",
835 w, UniqueStatetype(cm->stid[w]),
836 wend, UniqueStatetype(cm->stid[wend]),
837 best_j-best_d+1, best_j-best_k));
838 ESL_DPRINTF2((" generic: G%d[%s]..%d[%s], %d..%d\n",
839 y, UniqueStatetype(cm->stid[y]),
840 yend, UniqueStatetype(cm->stid[yend]),
841 best_j-best_k+1, best_j));
842
843 sse_v_splitter(cm, dsq, L, tr, r, v, i0, best_j-best_d+1, best_j, j0, FALSE);
844 tv = tr->n-1;
845
846 InsertTraceNode(tr, tv, TRACE_LEFT_CHILD, best_j-best_d+1, best_j-best_k, w);
847 sse_generic_splitter(cm, dsq, L, tr, w, wend, best_j-best_d+1, best_j-best_k);
848 InsertTraceNode(tr, tv, TRACE_RIGHT_CHILD, best_j-best_k+1, best_j, y);
849 sse_generic_splitter(cm, dsq, L, tr, y, yend, best_j-best_k+1, best_j);
850
851 return best_sc;
852 }
853
854 /* Function: wedge_splitter()
855 * Date: SRE, Sun May 13 08:44:15 2001 [CSHL genome mtg]
856 *
857 * Purpose: Solve a "wedge problem": best parse of an
858 * unbifurcated subgraph cm^r..z to a substring
859 * sq->sq[i0..j0]. r may be a start state (when
860 * the wedge problem comes from being a special case
861 * of a generic problem) or a non-insert state
862 * (D, MP, ML, MR) (when the wedge comes from a
863 * previous wedge_splitter), or indeed, any non-end
864 * state (when wedge comes from a local begin).
865 * z, however, is always an end state.
866 *
867 * Attaches the optimal trace T(r..z), exclusive
868 * of r and inclusive of z, to the growing trace tr.
869 *
870 * Deal with a divide and conquer boundary condition:
871 * the next non-insert state after r is the end state z.
872 * All remaining sequence of i0..j0 that r doesn't emit
873 * must be dealt with by insert states.
874 *
875 * Args: cm - model
876 * sq - digitized sequence 1..L
877 * tr - the traceback we're adding on to.
878 * r - index of the first state in the subgraph
879 * z - index of an end state (E_st) in the model
880 * i0 - start in the sequence (1..L)
881 * j0 - end in the sequence (1..L)
882 *
883 * Returns: The score of the best parse in bits.
884 */
885 static float
886 sse_wedge_splitter(CM_t *cm, ESL_DSQ *dsq, int L, Parsetree_t *tr, int r, int z, int i0, int j0)
887 {
888 sse_deck_t **alpha;
889 sse_deck_t **beta;
890 struct sse_deckpool_s *pool;
891 float sc;
892 float best_sc;
893 int v,w,y;
894 int W, sW;
895 int dp, jp, j;
896 int best_v, best_d, best_j;
897 int midnode;
898 int b; /* optimal local begin: b = argmax_v alpha_v(i0,j0) + t_0(v) */
899 float bsc; /* score for optimal local begin */
900 __m128 tmpv, mask;
901 __m128 vb_sc, vb_v, vb_j, vb_d;
902 __m128 vec_v, vec_j, vec_d;
903 __m128i doffset;
904 const int vecwidth = 4;
905
906 doffset = _mm_setr_epi32(0, 1, 2, 3);
907
908 /* 1. If the wedge problem is either a boundary condition,
909 * or small enough, solve it with inside^T and append
910 * the trace to tr.
911 * It's formally possible that someone could set RAMLIMIT
912 * to something so small that even the boundary condition
913 * couldn't be done with inside^T - but that'd be a silly
914 * thing to do, so we ignore RAMLIMIT in that case.
915 */
916 if (cm->ndidx[z] == cm->ndidx[r] + 1 ||
917 sse_insideT_size(cm, L, r, z, i0, j0, vecwidth) < RAMLIMIT)
918 {
919 ESL_DPRINTF2(("Solving a wedge: G%d[%s]..%d[%s], %d..%d\n",
920 r, UniqueStatetype(cm->stid[r]),
921 z, UniqueStatetype(cm->stid[z]),
922 i0,j0));
923 sc = sse_insideT(cm, dsq, L, tr, r, z, i0, j0, (r==0));
924
925 return sc;
926 }
927
928 /* 2. Find our split set, w..y
929 * We choose the node in the middle.
930 * This can't be a BIF_nd (we're a wedge), or an END_nd (midnode
931 * can't be z) but it could be any other node including
932 * begin nodes (i.e. it might be that w==y).
933 */
934 midnode = cm->ndidx[r] + ((cm->ndidx[z] - cm->ndidx[r]) / 2);
935 w = cm->nodemap[midnode];
936 y = cm->cfirst[w]-1;
937 //fprintf(stderr,"\tw %d y %d\n",w,y);
938
939 /* 3. Calculate inside up to w, and outside down to y.
940 * We rely on a side effect of how deallocation works
941 * in these routines; the w..y decks are guaranteed
942 * to be retained.
943 * b will contain the optimal 0->v state for a local begin, and bsc
944 * is the score for using it.
945 * beta[cm->M] will contain the EL deck, if needed for local ends.
946 */
947 sse_inside(cm, dsq, L, w, z, i0, j0, BE_EFFICIENT,
948 NULL, &alpha, NULL, &pool, NULL,
949 (r==0), &b, &bsc);
950 sse_outside(cm, dsq, L, r, y, i0, j0, BE_EFFICIENT, NULL, &beta, pool, NULL);
951
952 /* 4. Find the optimal split at the split set: best_v, best_d, best_j
953 */
954 W = j0-i0+1;
955 best_sc = IMPOSSIBLE;
956 vb_sc = _mm_set1_ps(-eslINFINITY);
957 vb_v = vb_j = vb_d = (__m128) _mm_set1_epi32(-3);
958
959 for (v = w; v <= y; v++) {
960 vec_v = (__m128) _mm_set1_epi32(v);
961 for (jp = 0; jp <= W; jp++)
962 {
963 j = i0-1+jp;
964 vec_j = (__m128) _mm_set1_epi32(j);
965 sW = jp/vecwidth;
966 /*for (d = 0; d <= jp; d++)
967 if ((sc = alpha[v][j][d] + beta[v][j][d]) > best_sc)
968 {
969 best_sc = sc;
970 best_v = v;
971 best_d = d;
972 best_j = j;
973 } */
974 for (dp = 0; dp <= sW; dp++)
975 {
976 vec_d = (__m128) _mm_add_epi32(doffset, _mm_set1_epi32(dp*vecwidth));
977 tmpv = _mm_add_ps(alpha[v]->vec[j][dp], beta[v]->vec[j][dp]);
978 mask = _mm_cmpgt_ps(tmpv, vb_sc);
979 vb_sc = _mm_max_ps(tmpv, vb_sc);
980 vb_v = esl_sse_select_ps(vb_v, vec_v, mask);
981 vb_j = esl_sse_select_ps(vb_j, vec_j, mask);
982 vb_d = esl_sse_select_ps(vb_d, vec_d, mask);
983 }
984 }
985 }
986
987 /* Local alignment ends only: maybe we're better off in EL,
988 * not in the split set?
989 */
990 if (cm->flags & CMH_LOCAL_END) {
991 vec_v = (__m128) _mm_set1_epi32(-1); /* flag for local alignment */
992 for (jp = 0; jp <= W; jp++)
993 {
994 j = i0-1+jp;
995 vec_j = (__m128) _mm_set1_epi32(j);
996 sW = jp/vecwidth;
997 /*for (d = 0; d <= jp; d++)
998 if ((sc = beta[cm->M][j][d]) > best_sc) {
999 best_sc = sc;
1000 best_v = -1;
1001 best_j = j;
1002 best_d = d;
1003 } */
1004 for (dp = 0; dp <= sW; dp++)
1005 {
1006 vec_d = (__m128) _mm_add_epi32(doffset, _mm_set1_epi32(dp*vecwidth));
1007 tmpv = beta[cm->M]->vec[j][dp];
1008 mask = _mm_cmpgt_ps(tmpv, vb_sc);
1009 vb_sc = _mm_max_ps(tmpv, vb_sc);
1010 vb_v = esl_sse_select_ps(vb_v, vec_v, mask);
1011 vb_j = esl_sse_select_ps(vb_j, vec_j, mask);
1012 vb_d = esl_sse_select_ps(vb_d, vec_d, mask);
1013 }
1014 }
1015 }
1016
1017 /* Local alignment begins only: maybe we're better off in the root.
1018 */
1019 if (r==0 && (cm->flags & CMH_LOCAL_BEGIN)) {
1020 /*if (bsc > best_sc) {
1021 best_sc = bsc;
1022 best_v = -2;
1023 best_j = j0;
1024 best_d = W;
1025 } */
1026 vec_v = (__m128) _mm_set1_epi32(-2); /* flag for local alignment */
1027 vec_j = (__m128) _mm_set1_epi32(j0);
1028 vec_d = (__m128) _mm_set1_epi32(W);
1029 tmpv = _mm_set1_ps(bsc);
1030 mask = _mm_cmpgt_ps(tmpv, vb_sc);
1031 vb_sc = _mm_max_ps(tmpv, vb_sc);
1032 vb_v = esl_sse_select_ps(vb_v, vec_v, mask);
1033 vb_j = esl_sse_select_ps(vb_j, vec_j, mask);
1034 vb_d = esl_sse_select_ps(vb_d, vec_d, mask);
1035 }
1036
1037 /* free now, before recursing!
1038 */
1039 sse_free_vjd_matrix(alpha, cm->M);
1040 sse_free_vjd_matrix(beta, cm->M);
1041
1042 /*
1043 fprintf(stderr,"%f %f %f %f\t",*((float *) &vb_sc),*((float *) &vb_sc+1),*((float *) &vb_sc+2),*((float *) &vb_sc+3));
1044 fprintf(stderr,"%d %d %d %d\t",*((int *) &vb_v),*((int *) &vb_v+1),*((int *) &vb_v+2),*((int *) &vb_v+3));
1045 fprintf(stderr,"%d %d %d %d\t",*((int *) &vb_j),*((int *) &vb_j+1),*((int *) &vb_j+2),*((int *) &vb_j+3));
1046 fprintf(stderr,"%d %d %d %d\n",*((int *) &vb_d),*((int *) &vb_d+1),*((int *) &vb_d+2),*((int *) &vb_d+3));
1047 */
1048
1049 /* determine values corresponding to best score out of our 4x vector */
1050 /* like esl_sse_hmax(), but re-using the mask from the scores */
1051 tmpv = _mm_shuffle_ps(vb_sc, vb_sc, _MM_SHUFFLE(0, 3, 2, 1));
1052 mask = _mm_cmpgt_ps(tmpv, vb_sc);
1053 vb_sc = _mm_max_ps(tmpv, vb_sc);
1054 tmpv = _mm_shuffle_ps(vb_v, vb_v, _MM_SHUFFLE(0, 3, 2, 1));
1055 vb_v = esl_sse_select_ps(vb_v, tmpv, mask);
1056 tmpv = _mm_shuffle_ps(vb_j, vb_j, _MM_SHUFFLE(0, 3, 2, 1));
1057 vb_j = esl_sse_select_ps(vb_j, tmpv, mask);
1058 tmpv = _mm_shuffle_ps(vb_d, vb_d, _MM_SHUFFLE(0, 3, 2, 1));
1059 vb_d = esl_sse_select_ps(vb_d, tmpv, mask);
1060
1061 tmpv = _mm_shuffle_ps(vb_sc, vb_sc, _MM_SHUFFLE(1, 0, 3, 2));
1062 mask = _mm_cmpgt_ps(tmpv, vb_sc);
1063 vb_sc = _mm_max_ps(tmpv, vb_sc);
1064 tmpv = _mm_shuffle_ps(vb_v, vb_v, _MM_SHUFFLE(1, 0, 3, 2));
1065 vb_v = esl_sse_select_ps(vb_v, tmpv, mask);
1066 tmpv = _mm_shuffle_ps(vb_j, vb_j, _MM_SHUFFLE(1, 0, 3, 2));
1067 vb_j = esl_sse_select_ps(vb_j, tmpv, mask);
1068 tmpv = _mm_shuffle_ps(vb_d, vb_d, _MM_SHUFFLE(1, 0, 3, 2));
1069 vb_d = esl_sse_select_ps(vb_d, tmpv, mask);
1070
1071 union {
1072 int i;
1073 float f;
1074 } u_tmp;
1075
1076 best_sc = *((float *) &vb_sc);
1077 u_tmp.f = *((float *) &vb_v); best_v = u_tmp.i;
1078 u_tmp.f = *((float *) &vb_j); best_j = u_tmp.i;
1079 u_tmp.f = *((float *) &vb_d); best_d = u_tmp.i;
1080
1081 /*
1082 int k;
1083 for (k = 1; k < vecwidth; k++) {
1084 if (*((float *) &vb_sc + k) > best_sc) {
1085 best_sc = *((float *) &vb_sc + k);
1086 best_v = *((int *) &vb_v + k);
1087 best_j = *((int *) &vb_j + k);
1088 best_d = *((int *) &vb_d + k);
1089 }
1090 }
1091 */
1092
1093 if (best_v == -3) {
1094 /*`
1095 fprintf(stderr,"%f %f %f %f\t",*((float *) &vb_sc),*((float *) &vb_sc+1),*((float *) &vb_sc+2),*((float *) &vb_sc+3));
1096 fprintf(stderr,"%d %d %d %d\t",*((int *) &vb_v),*((int *) &vb_v+1),*((int *) &vb_v+2),*((int *) &vb_v+3));
1097 fprintf(stderr,"%d %d %d %d\t",*((int *) &vb_j),*((int *) &vb_j+1),*((int *) &vb_j+2),*((int *) &vb_j+3));
1098 fprintf(stderr,"%d %d %d %d\n",*((int *) &vb_d),*((int *) &vb_d+1),*((int *) &vb_d+2),*((int *) &vb_d+3));
1099 */
1100
1101 fprintf(stderr,"%f %d %d %d\n",best_sc, best_v, best_j, best_d);
1102 cm_Fail("vb_v never got set to anything?\n");
1103 }
1104 /* If we're in EL, instead of the split set, the optimal alignment
1105 * is entirely in a V problem that's still above us. The TRUE
1106 * flag sets useEL. It doesn't matter which state in the split
1107 * set w..y we use as the end of the graph; vinside() will have to
1108 * initialize the whole thing to IMPOSSIBLE anyway.
1109 */
1110 if (best_v == -1) {
1111 sse_v_splitter(cm, dsq, L, tr, r, w, i0, best_j-best_d+1, best_j, j0, TRUE);
1112 return best_sc;
1113 }
1114
1115 /* If we're in the root because of a local begin, the local alignment
1116 * is entirely in a wedge problem that's still below us, rooted at b.
1117 * The FALSE flag prohibits any more local begins in this and subsequent
1118 * problems.
1119 */
1120 if (best_v == -2) {
1121 InsertTraceNode(tr, tr->n-1, TRACE_LEFT_CHILD, i0, j0, b);
1122 sse_wedge_splitter(cm, dsq, L, tr, b, z, i0, j0);
1123 return best_sc;
1124 }
1125
1126 /* Else (usual case): the optimal split into a V problem and a wedge problem:
1127 * i1 = best_j-best_d+1, j1 = best_j
1128 * the V problem: r..v, i0..i1, j1..j0
1129 * the wedge problem: v..z, i1..j1
1130 *
1131 * These have to solved in the order given because we're
1132 * constructing the trace in postorder traversal.
1133 */
1134 ESL_DPRINTF2(("Wedge splitter:\n"));
1135 ESL_DPRINTF2((" V: G%d[%s]..%d[%s], %d..%d//%d..%d\n",
1136 r, UniqueStatetype(cm->stid[r]),
1137 best_v, UniqueStatetype(cm->stid[best_v]),
1138 i0, best_j-best_d+1, best_j, j0));
1139 ESL_DPRINTF2((" wedge: G%d[%s]..%d[%s], %d..%d\n",
1140 best_v, UniqueStatetype(cm->stid[best_v]),
1141 z, UniqueStatetype(cm->stid[z]),
1142 best_j-best_d+1, best_j));
1143
1144 sse_v_splitter(cm, dsq, L, tr, r, best_v, i0, best_j-best_d+1, best_j, j0, FALSE);
1145 sse_wedge_splitter(cm, dsq, L, tr, best_v, z, best_j-best_d+1, best_j);
1146 return best_sc;
1147 }
1148
1149 /* Function: v_splitter()
1150 * Date: SRE, Thu May 31 19:47:57 2001 [Kaldi's]
1151 *
1152 * Purpose: Solve a "V problem": best parse of an unbifurcated
1153 * subgraph cm^r..z to a one-hole subsequence
1154 * i0..i1 // j1..j0.
1155 *
1156 * Attaches the optimal trace T(r..z), exclusive of
1157 * r, inclusive of z, to the growing trace tr.
1158 *
1159 * r and z can be any non-insert state.
1160 *
1161 * Args: cm - model
1162 * sq - digitized sequence 1..L
1163 * tr - the traceback we're adding on to.
1164 * r - index of the first state in the subgraph
1165 * z - index of the last state in the subgraph
1166 * i0,i1 - first part of the subsequence (1..L)
1167 * j1,j0 - second part of the subsequence (1..L)
1168 * useEL - TRUE if i1,j1 aligned to EL, not z
1169 *
1170 * Returns: (void)
1171 */
1172 static void
1173 sse_v_splitter(CM_t *cm, ESL_DSQ *dsq, int L, Parsetree_t *tr,
1174 int r, int z, int i0, int i1, int j1, int j0,
1175 int useEL)
1176 {
1177 sse_deck_t **alpha, **beta; /* inside and outside matrices */
1178 struct sse_deckpool_s *pool; /* pool for holding alloced decks */
1179 int v,w,y; /* state indexes */
1180 int ip,jp;
1181 int best_v;
1182 int best_i, best_j; /* optimal i', j' split point */
1183 float best_sc; /* score at optimal split point */
1184 int midnode;
1185 int b; /* optimal choice for a 0->b local begin */
1186 float bsc; /* score if we use the local begin */
1187 __m128 vb_sc, tmpv, mask;
1188 __m128 vb_v, vb_j, vb_i;
1189 __m128 vec_v, vec_j, vec_i;
1190 __m128i ioffset;
1191 int sW;
1192 const int vecwidth = 4;
1193
1194 alpha = NULL; beta = NULL;
1195 ioffset = _mm_setr_epi32(0, 1, 2, 3);
1196
1197 /* 1. If the V problem is either a boundary condition, or small
1198 * enough, solve it with v_inside^T and append the trace to tr.
1199 * (With local alignment, we might even see a lone B state
1200 * get handed to v_splitter(); hence the r==z case.)
1201 */
1202 if (cm->ndidx[z] == cm->ndidx[r] + 1 || r == z ||
1203 vinsideT_size(cm, r, z, i0, i1, j1, j0) < RAMLIMIT)
1204 {
1205 ESL_DPRINTF2(("Solving a V: G%d[%s]..%d[%s], %d..%d//%d..%d\n",
1206 r, UniqueStatetype(cm->stid[r]),
1207 z, UniqueStatetype(cm->stid[z]),
1208 i0,j1,j1,j0));
1209 sse_vinsideT(cm, dsq, L, tr, r, z, i0, i1, j1, j0, useEL, (r==0));
1210 return;
1211 }
1212
1213 /* 2. Find our split set, w..y.
1214 * Choose the node in the middle.
1215 */
1216 midnode = cm->ndidx[r] + ((cm->ndidx[z] - cm->ndidx[r]) / 2);
1217 w = cm->nodemap[midnode];
1218 y = cm->cfirst[w]-1;
1219
1220 /* 3. Calculate v_inside up to w, and v_outside down to y.
1221 * As with wedge_splitter(), we rely on a side effect of how
1222 * deallocation works, so the w..y decks are retained
1223 * in alpha and beta even though we're in small memory mode.
1224 * beta[cm->M] is the EL deck, needed for local ends.
1225 */
1226 sse_vinside (cm, dsq, L, w, z, i0, i1, j1, j0, useEL, BE_EFFICIENT,
1227 NULL, &alpha, NULL, &pool, NULL, (r==0), &b, &bsc);
1228 sse_voutside(cm, dsq, L, r, y, i0, i1, j1, j0, useEL, BE_EFFICIENT,
1229 NULL, &beta, pool, NULL);
1230
1231 /* 4. Find the optimal split: v, ip, jp.
1232 */
1233 best_sc = IMPOSSIBLE;
1234 vb_sc = _mm_set1_ps(-eslINFINITY);
1235 vb_v = vb_j = vb_i = (__m128) _mm_set1_epi32(-3);
1236 for (v = w; v <= y; v++) {
1237 vec_v = (__m128) _mm_set1_epi32(v);
1238 for (jp = 0; jp <= j0-j1; jp++) {
1239 vec_j = (__m128) _mm_set1_epi32(jp+j1);
1240 sW = (i1-i0)/vecwidth;
1241 //for (ip = 0; ip <= i1-i0; ip++) {
1242 for (ip = 0; ip <= sW; ip++) {
1243 /* if ((sc = alpha[v][jp][ip] + beta[v][jp][ip]) > best_sc)
1244 {
1245 best_sc = sc;
1246 best_v = v;
1247 best_i = ip + i0;
1248 best_j = jp + j1;
1249 } */
1250 vec_i = (__m128) _mm_add_epi32(_mm_set1_epi32(ip*vecwidth), _mm_set1_epi32(i0));
1251 vec_i = (__m128) _mm_add_epi32((__m128i) vec_i, ioffset);
1252 tmpv = _mm_add_ps(alpha[v]->vec[jp][ip], beta[v]->vec[jp][ip]);
1253 mask = _mm_cmpgt_ps(tmpv, vb_sc);
1254 vb_sc = _mm_max_ps(tmpv, vb_sc);
1255 vb_v = esl_sse_select_ps(vb_v, vec_v, mask);
1256 vb_j = esl_sse_select_ps(vb_j, vec_j, mask);
1257 vb_i = esl_sse_select_ps(vb_i, vec_i, mask);
1258 }
1259 }
1260 }
1261
1262 /* Local alignment ends: maybe we're better off in EL, not
1263 * the split set?
1264 */
1265 if (useEL && (cm->flags & CMH_LOCAL_END)) {
1266 vec_v = (__m128) _mm_set1_epi32(-1);
1267 for (jp = 0; jp <= j0-j1; jp++) {
1268 vec_j = (__m128) _mm_set1_epi32(jp+j1);
1269 sW = (i1-i0)/vecwidth;
1270 //for (ip = 0; ip <= i1-i0; ip++) {
1271 for (ip = 0; ip <= sW; ip++) {
1272 /* if ((sc = beta[cm->M][jp][ip]) > best_sc) {
1273 best_sc = sc;
1274 best_v = -1;
1275 best_i = ip + i0;
1276 best_j = jp + j1;
1277 } */
1278 vec_i = (__m128) _mm_add_epi32(_mm_set1_epi32(ip*vecwidth), _mm_set1_epi32(i0));
1279 vec_i = (__m128) _mm_add_epi32((__m128i) vec_i, ioffset);
1280 tmpv = beta[cm->M]->vec[jp][ip];
1281 mask = _mm_cmpgt_ps(tmpv, vb_sc);
1282 vb_sc = _mm_max_ps(tmpv, vb_sc);
1283 vb_v = esl_sse_select_ps(vb_v, vec_v, mask);
1284 vb_j = esl_sse_select_ps(vb_j, vec_j, mask);
1285 vb_i = esl_sse_select_ps(vb_i, vec_i, mask);
1286 }
1287 }
1288 }
1289
1290 /* Local alignment begins: maybe we're better off in root...
1291 */
1292 if (r==0 && (cm->flags & CMH_LOCAL_BEGIN)) {
1293 /* if (bsc > best_sc) {
1294 best_sc = bsc;
1295 best_v = -2;
1296 best_i = i0;
1297 best_j = j0;
1298 } */
1299 vec_v = (__m128) _mm_set1_epi32(-2);
1300 vec_j = (__m128) _mm_set1_epi32(j0);
1301 vec_i = (__m128) _mm_set1_epi32(i0);
1302 tmpv = _mm_set1_ps(bsc);
1303 mask = _mm_cmpgt_ps(tmpv, vb_sc);
1304 vb_sc = _mm_max_ps(tmpv, vb_sc);
1305 vb_v = esl_sse_select_ps(vb_v, vec_v, mask);
1306 vb_j = esl_sse_select_ps(vb_j, vec_j, mask);
1307 vb_i = esl_sse_select_ps(vb_i, vec_i, mask);
1308 }
1309
1310 /* Free now, before recursing!
1311 */
1312 sse_free_vji_matrix(alpha, cm->M);
1313 sse_free_vji_matrix(beta, cm->M);
1314
1315 /* determine values corresponding to best score out of our 4x vector */
1316 /* like esl_sse_hmax(), but re-using the mask from the scores */
1317 tmpv = _mm_shuffle_ps(vb_sc, vb_sc, _MM_SHUFFLE(0, 3, 2, 1));
1318 mask = _mm_cmpgt_ps(tmpv, vb_sc);
1319 vb_sc = _mm_max_ps(tmpv, vb_sc);
1320 tmpv = _mm_shuffle_ps(vb_v, vb_v, _MM_SHUFFLE(0, 3, 2, 1));
1321 vb_v = esl_sse_select_ps(vb_v, tmpv, mask);
1322 tmpv = _mm_shuffle_ps(vb_j, vb_j, _MM_SHUFFLE(0, 3, 2, 1));
1323 vb_j = esl_sse_select_ps(vb_j, tmpv, mask);
1324 tmpv = _mm_shuffle_ps(vb_i, vb_i, _MM_SHUFFLE(0, 3, 2, 1));
1325 vb_i = esl_sse_select_ps(vb_i, tmpv, mask);
1326
1327 tmpv = _mm_shuffle_ps(vb_sc, vb_sc, _MM_SHUFFLE(1, 0, 3, 2));
1328 mask = _mm_cmpgt_ps(tmpv, vb_sc);
1329 vb_sc = _mm_max_ps(tmpv, vb_sc);
1330 tmpv = _mm_shuffle_ps(vb_v, vb_v, _MM_SHUFFLE(1, 0, 3, 2));
1331 vb_v = esl_sse_select_ps(vb_v, tmpv, mask);
1332 tmpv = _mm_shuffle_ps(vb_j, vb_j, _MM_SHUFFLE(1, 0, 3, 2));
1333 vb_j = esl_sse_select_ps(vb_j, tmpv, mask);
1334 tmpv = _mm_shuffle_ps(vb_i, vb_i, _MM_SHUFFLE(1, 0, 3, 2));
1335 vb_i = esl_sse_select_ps(vb_i, tmpv, mask);
1336
1337 union {
1338 int i;
1339 float f;
1340 } u_tmp;
1341
1342 best_sc = *((float *) &vb_sc);
1343 u_tmp.f = *((float *) &vb_v); best_v = u_tmp.i;
1344 u_tmp.f = *((float *) &vb_j); best_j = u_tmp.i;
1345 u_tmp.f = *((float *) &vb_i); best_i = u_tmp.i;
1346
1347 /*
1348 int k;
1349 for (k = 1; k < vecwidth; k++) {
1350 if (*((float *) &vb_sc + k) > best_sc) {
1351 best_sc = *((float *) &vb_sc + k);
1352 best_v = *((int *) &vb_v + k);
1353 best_j = *((int *) &vb_j + k);
1354 best_i = *((int *) &vb_i + k);
1355 }
1356 }
1357 */
1358
1359 if (best_v == -3) {
1360 /*
1361 fprintf(stderr,"%f %f %f %f\t",*((float *) &vb_sc),*((float *) &vb_sc+1),*((float *) &vb_sc+2),*((float *) &vb_sc+3));
1362 fprintf(stderr,"%d %d %d %d\t",*((int *) &vb_v),*((int *) &vb_v+1),*((int *) &vb_v+2),*((int *) &vb_v+3));
1363 fprintf(stderr,"%d %d %d %d\t",*((int *) &vb_j),*((int *) &vb_j+1),*((int *) &vb_j+2),*((int *) &vb_j+3));
1364 fprintf(stderr,"%d %d %d %d\n",*((int *) &vb_i),*((int *) &vb_i+1),*((int *) &vb_i+2),*((int *) &vb_i+3));
1365 */
1366
1367 fprintf(stderr,"%f %d %d %d\n",best_sc, best_v, best_j, best_i);
1368 cm_Fail("vb_v never got set to anything?\n");
1369 }
1370 /* If we're in EL, instead of the split set, the optimal
1371 * alignment is entirely in a V problem that's still above us.
1372 * The TRUE flag sets useEL; we propagate allow_begin.
1373 */
1374 if (best_v == -1) {
1375 sse_v_splitter(cm, dsq, L, tr, r, w, i0, best_i, best_j, j0, TRUE);
1376 return;
1377 }
1378
1379 /* If we used a local begin, the optimal alignment is
1380 * entirely in a V problem that's still below us, rooted
1381 * at b, for the entire one-hole sequence. The FALSE
1382 * flag prohibits more local begin transitions; we propagate
1383 * useEL.
1384 */
1385 if (best_v == -2) {
1386 if (b != z)
1387 {
1388 InsertTraceNode(tr, tr->n-1, TRACE_LEFT_CHILD, i0, j0, b);
1389 }
1390 sse_v_splitter(cm, dsq, L, tr, b, z, i0, i1, j1, j0, useEL);
1391 return;
1392 }
1393
1394 /* The optimal split into two V problems:
1395 * V: r..v, i0..i', j'..j0
1396 * V: v..z, i'..i1, j1..j'
1397 * Solve in this order, because we're constructing the
1398 * trace in postorder traversal.
1399 */
1400 ESL_DPRINTF2(("V splitter:\n"));
1401 ESL_DPRINTF2((" V: G%d[%s]..%d[%s], %d..%d//%d..%d\n",
1402 r, UniqueStatetype(cm->stid[r]),
1403 best_v, UniqueStatetype(cm->stid[best_v]),
1404 i0, best_i, best_j, j0));
1405 ESL_DPRINTF2((" V: G%d[%s]..%d[%s], %d..%d//%d..%d\n",
1406 best_v, UniqueStatetype(cm->stid[best_v]),
1407 z, UniqueStatetype(cm->stid[z]),
1408 best_i, i1, j1, best_j));
1409
1410 sse_v_splitter(cm, dsq, L, tr, r, best_v, i0, best_i, best_j, j0, FALSE);
1411 sse_v_splitter(cm, dsq, L, tr, best_v, z, best_i, i1, j1, best_j, useEL);
1412 return;
1413 }
1414
1415
1416 /*****************************************************************
1417 * The alignment engines:
1418 * inside - given generic or wedge problem G^r_z to i0..j0, return score and matrix
1419 * outside - given unbifurcated G^r_z to i0..j0, return matrix
1420 *
1421 * vinside - given V problem G^r_z to i0..i1//j1..j0, return score and matrix
1422 * voutside - given unbifurcated G^r_z to i0..i1//j1..j0, return matrix
1423 ******************************************************************/
1424
1425 /* Function: inside()
1426 * Date: SRE, Mon Aug 7 13:15:37 2000 [St. Louis]
1427 *
1428 * Purpose: Run the inside phase of a CYK alignment algorithm, on a
1429 * subsequence from i0..j0, using a subtree of a model
1430 * anchored at a start state vroot, and ending at an end
1431 * state vend. (It is a feature of the model layout in
1432 * a CM structure that all subtrees are contiguous in the
1433 * model.)
1434 *
1435 * A note on the loop conventions. We're going to keep the
1436 * sequence (sq) and the matrix (alpha) in the full coordinate
1437 * system: [0..v..M-1][0..j..L][0..d..j]. However, we're
1438 * only calculating a part of that matrix: only vroot..vend
1439 * in the decks, i0-1..j in the rows, and up to j0-i0+1 in
1440 * the columns (d dimension). Where this is handled the most
1441 * is in two variables: W, which is the length of the subsequence
1442 * (j0-i0+1), and is oft used in place of L in the usual CYK;
1443 * and jp (read: j'), which is the *relative* j w.r.t. the
1444 * subsequence, ranging from 0..W, and then d ranges from
1445 * 0 to jp, and j is calculated from jp (i0-1+jp).
1446 *
1447 * The caller is allowed to provide us with a preexisting
1448 * matrix and/or deckpool (thru "alpha" and "dpool"), or
1449 * have them newly created by passing NULL. If we pass in an
1450 * alpha, we expect that alpha[vroot..vend] are all NULL
1451 * decks already; any other decks <vroot and >vend will
1452 * be preserved. If we pass in a dpool, the decks *must* be
1453 * sized for the same subsequence i0,j0.
1454 *
1455 * Note that the (alpha, ret_alpha) calling idiom allows the
1456 * caller to provide an existing matrix or not, and to
1457 * retrieve the calculated matrix or not, in any combination.
1458 *
1459 * We also deal with local begins, by keeping track of the optimal
1460 * state that we could enter and account for the whole target
1461 * sequence: b = argmax_v alpha_v(i0,j0) + log t_0(v),
1462 * and bsc is the score for that.
1463 *
1464 * If vroot==0, i0==1, and j0==L (e.g. a complete alignment),
1465 * the optimal alignment might use a local begin transition, 0->b,
1466 * and we'd have to be able to trace that back. For any
1467 * problem where the caller sets allow_begin, we return a valid b
1468 * (the optimal 0->b choice) and bsc (the score if 0->b is used).
1469 * If a local begin is part of the optimal parse tree, the optimal
1470 * alignment score returned by inside() will be bsc and yshad[0][L][L]
1471 * will be USE_LOCAL_BEGIN, telling insideT() to check b and
1472 * start with a local 0->b entry transition. When inside()
1473 * is called on smaller subproblems (v != 0 || i0 > 1 || j0
1474 * < L), we're using inside() as an engine in divide &
1475 * conquer, and we don't use the overall return score nor
1476 * shadow matrices, but we do need allow_begin, b, and bsc for
1477 * divide&conquer to sort out where a local begin might be used.
1478 *
1479 * Args: cm - the model [0..M-1]
1480 * sq - the sequence [1..L]
1481 * vroot - first start state of subtree (0, for whole model)
1482 * vend - last end state of subtree (cm->M-1, for whole model)
1483 * i0 - first position in subseq to align (1, for whole seq)
1484 * j0 - last position in subseq to align (L, for whole seq)
1485 * do_full - if TRUE, we save all the decks in alpha, instead of
1486 * working in our default memory-efficient mode where
1487 * we reuse decks and only the uppermost deck (vroot) is valid
1488 * at the end.
1489 * alpha - if non-NULL, this is an existing matrix, with NULL
1490 * decks for vroot..vend, and we'll fill in those decks
1491 * appropriately instead of creating a new matrix
1492 * ret_alpha - if non-NULL, return the matrix with one or more
1493 * decks available for examination (see "do_full")
1494 * dpool - if non-NULL, this is an existing deck pool, possibly empty,
1495 * but usually containing one or more allocated decks sized
1496 * for this subsequence i0..j0.
1497 * ret_dpool - if non-NULL, return the deck pool for reuse -- these will
1498 * *only* be valid on exactly the same i0..j0 subseq,
1499 * because of the size of the subseq decks.
1500 * ret_shadow- if non-NULL, the caller wants a shadow matrix, because
1501 * he intends to do a traceback.
1502 * allow_begin- TRUE to allow 0->b local alignment begin transitions.
1503 * ret_b - best local begin state, or NULL if unwanted
1504 * ret_bsc - score for using ret_b, or NULL if unwanted
1505 *
1506 *
1507 * Returns: Score of the optimal alignment.
1508 */
1509 static float
1510 sse_inside(CM_t *cm, ESL_DSQ *dsq, int L, int vroot, int vend, int i0, int j0, int do_full,
1511 sse_deck_t **alpha, sse_deck_t ***ret_alpha,
1512 struct sse_deckpool_s *dpool, struct sse_deckpool_s **ret_dpool,
1513 sse_deck_t ***ret_shadow,
1514 int allow_begin, int *ret_b, float *ret_bsc)
1515 {
1516 int status;
1517 int nends; /* counter that tracks when we can release end deck to the pool */
1518 int *touch; /* keeps track of how many higher decks still need this deck */
1519 int v,y,z; /* indices for states */
1520 int j,d,k; /* indices in sequence dimensions */
1521 float sc; /* a temporary variable holding a score */
1522 int yoffset; /* y=base+offset -- counter in child states that v can transit to */
1523 int W; /* subsequence length */
1524 int jp; /* j': relative position in the subsequence */
1525 int b; /* best local begin state */
1526 float bsc; /* score for using the best local begin state */
1527
1528 const int vecwidth = 4;
1529 int sW;
1530 int dp, kp;
1531 float *vec_access;
1532 float tmp;
1533 __m128 zerov;
1534 __m128 neginfv;
1535 __m128 el_self_v;
1536 __m128 tscv;
1537 __m128 escv;
1538 __m128 *mem_Lesc;
1539 __m128 *vec_Lesc;
1540 __m128 *mem_Pesc;
1541 __m128 **vec_Pesc;
1542 int delta, x;
1543 int *esc_stale;
1544 __m128 doffset;
1545 __m128 tmpv;
1546 __m128 tmpshad;
1547 __m128 mask;
1548 sse_deck_t *end = NULL;
1549 sse_deck_t **shadow = NULL; /* shadow matrix for tracebacks */
1550
1551 /* Allocations and initializations
1552 */
1553 zerov = _mm_setzero_ps();
1554 neginfv = _mm_set1_ps(-eslINFINITY);
1555 el_self_v = _mm_set1_ps(cm->el_selfsc);
1556 doffset = _mm_setr_ps(0.0, 1.0, 2.0, 3.0);
1557 b = -1;
1558 bsc = IMPOSSIBLE;
1559 W = j0-i0+1; /* the length of the subsequence -- used in many loops */
1560 sW = W/vecwidth;
1561
1562 /* Set up memory for pre-vectorized emission score */
1563 ESL_ALLOC(mem_Lesc, sizeof(__m128 ) * (sW+1) + 15);
1564 ESL_ALLOC(mem_Pesc, sizeof(__m128 ) * cm->abc->Kp * (sW+1) + 15);
1565 ESL_ALLOC(vec_Pesc, sizeof(__m128 *) * cm->abc->Kp);
1566 ESL_ALLOC(esc_stale,sizeof(int *) * cm->abc->Kp);
1567
1568 vec_Lesc = (__m128 *) (((unsigned long int) mem_Lesc + 15) & (~0xf));
1569 vec_Pesc[0] = (__m128 *) (((unsigned long int) mem_Pesc + 15) & (~0xf));
1570 for (j = 1; j < cm->abc->Kp; j++)
1571 {
1572 vec_Pesc[j] = vec_Pesc[0] + j*(sW+1);
1573 }
1574
1575 /* if caller didn't give us a deck pool, make one */
1576 if (dpool == NULL) dpool = sse_deckpool_create();
1577 if (! sse_deckpool_pop(dpool, &end))
1578 end = sse_alloc_vjd_deck(L, i0, j0, vecwidth);
1579 nends = CMSubtreeCountStatetype(cm, vroot, E_st);
1580 for (jp = 0; jp <= W; jp++) {
1581 j = i0+jp-1; /* e.g. j runs from 0..L on whole seq */
1582 end->vec[j][0] = esl_sse_rightshift_ps(neginfv, zerov);
1583 for (d = 1; d <= jp/vecwidth; d++) end->vec[j][d] = neginfv;
1584 }
1585
1586 /* if caller didn't give us a matrix, make one.
1587 * It's important to allocate for M+1 decks (deck M is for EL, local
1588 * alignment) - even though Inside doesn't need EL, Outside does,
1589 * and we might reuse this memory in a call to Outside.
1590 */
1591 if (alpha == NULL) {
1592 ESL_ALLOC(alpha, sizeof(sse_deck_t *) * (cm->M+1));
1593 for (v = 0; v <= cm->M; v++) alpha[v] = NULL;
1594 }
1595
1596 ESL_ALLOC(touch, sizeof(int) * (cm->M+1));
1597 for (v = 0; v < vroot; v++) touch[v] = 0;
1598 for (v = vroot; v <= vend; v++) touch[v] = cm->pnum[v];
1599 for (v = vend+1;v < cm->M; v++) touch[v] = 0;
1600
1601 /* The shadow matrix, if caller wants a traceback.
1602 * DEPRECATED COMMENT
1603 * We do some pointer tricks here to save memory. The shadow matrix
1604 * is a void ***. Decks may either be char ** (usually) or
1605 * int ** (for bifurcation decks). Watch out for the casts.
1606 * For most states we only need
1607 * to keep y as traceback info, and y <= 6. For bifurcations,
1608 * we need to keep k, and k <= L, and L might be fairly big.
1609 * (We could probably limit k to an unsigned short ... anyone
1610 * aligning an RNA > 65536 would need a big computer... but
1611 * we'll hold off on that for now. We could also pack more
1612 * traceback pointers into a smaller space since we only really
1613 * need 3 bits, not 8.)
1614 * NEW COMMENT
1615 * The vectorized implementation uses much larger memory types
1616 * for the shadow matrices, so that they fit in the same vector
1617 * width as the main alpha matrix. In 4x vectors, they'll be
1618 * 32-bit wide integer values, stored in the same type of
1619 * __m128 decks as the floats.
1620 */
1621 if (ret_shadow != NULL) {
1622 ESL_ALLOC(shadow, sizeof(sse_deck_t *) * cm->M);
1623 for (v = 0; v < cm->M; v++) shadow[v] = NULL;
1624 }
1625
1626 /* Main recursion
1627 */
1628 for (v = vend; v >= vroot; v--)
1629 {
1630 /* First we need a deck to fill in.
1631 * 1. if we're an E, reuse the end deck (and it's already calculated)
1632 * 2. else, see if we can take something from the pool
1633 * 3. else, allocate a new deck.
1634 */
1635 if (cm->sttype[v] == E_st) {
1636 alpha[v] = end; continue;
1637 }
1638 if (! sse_deckpool_pop(dpool, &(alpha[v])))
1639 alpha[v] = sse_alloc_vjd_deck(L, i0, j0, vecwidth);
1640
1641 if (ret_shadow != NULL) {
1642 shadow[v] = sse_alloc_vjd_deck(L, i0, j0, vecwidth);
1643 }
1644
1645 if (cm->sttype[v] == D_st || cm->sttype[v] == S_st)
1646 {
1647 for (jp = 0; jp <= W; jp++) {
1648 j = i0-1+jp;
1649 sW = jp/vecwidth;
1650 for (dp = 0; dp <= sW; dp++)
1651 {
1652 y = cm->cfirst[v];
1653 // alpha[v][j][d] = cm->endsc[v] + (cm->el_selfsc * (d-StateDelta(cm->sttype[v])));
1654 tmpv = _mm_mul_ps(el_self_v, _mm_add_ps(_mm_set1_ps((float) dp*vecwidth), doffset));
1655 alpha[v]->vec[j][dp] = _mm_add_ps(_mm_set1_ps(cm->endsc[v]), tmpv);
1656 /* treat EL as emitting only on self transition */
1657 if (ret_shadow != NULL) shadow[v]->vec[j][dp] = (__m128) _mm_set1_epi32(USED_EL);
1658 for (yoffset = 0; yoffset < cm->cnum[v]; yoffset++) {
1659 tscv = _mm_set1_ps(cm->tsc[v][yoffset]);
1660 tmpv = _mm_add_ps(alpha[y+yoffset]->vec[j][dp], tscv);
1661 mask = _mm_cmpgt_ps(tmpv, alpha[v]->vec[j][dp]);
1662 alpha[v]->vec[j][dp] = _mm_max_ps(alpha[v]->vec[j][dp], tmpv);
1663 if (ret_shadow != NULL) {
1664 shadow[v]->vec[j][dp] = esl_sse_select_ps(shadow[v]->vec[j][dp], (__m128) _mm_set1_epi32(yoffset), mask);
1665 }
1666 }
1667 //FIXME: SSE conversion is kind of ignoring the possibilty of underflow... this is bad.
1668 //if (alpha[v][j][d] < IMPOSSIBLE) alpha[v][j][d] = IMPOSSIBLE;
1669 }
1670 }
1671 }
1672 else if (cm->sttype[v] == B_st)
1673 {
1674 __m128 begr_v;
1675 for (jp = 0; jp <= W; jp++) {
1676 j = i0-1+jp;
1677 sW = jp/vecwidth;
1678 y = cm->cfirst[v];
1679 z = cm->cnum[v];
1680
1681 /* IMPORTANT NOTE!
1682 * while this function tries to be robust to variations in vecwidth
1683 * the section below is distinctly not, as I have enumerated the cases
1684 * of how d-k hits the vector boundaries
1685 */
1686 vec_access = (float *) (&alpha[z]->vec[j][0]);
1687 begr_v = _mm_set1_ps(*vec_access);
1688 tmpshad = (__m128) _mm_set1_epi32(0);
1689 for (dp = 0; dp <= sW; dp++)
1690 {
1691 alpha[v]->vec[j][dp] = _mm_add_ps(alpha[y]->vec[j][dp], begr_v);
1692 if (ret_shadow != NULL) shadow[v]->vec[j][dp] = tmpshad;
1693 }
1694 for (k = 4; k <=jp; k+=4)
1695 {
1696 tmpshad = (__m128) _mm_set1_epi32(k);
1697 kp = k/vecwidth;
1698 vec_access = (float *) (&alpha[z]->vec[j][kp]);
1699 begr_v = _mm_set1_ps(*vec_access);
1700
1701 for (dp = kp; dp <= sW; dp++)
1702 {
1703 tmpv = _mm_add_ps(alpha[y]->vec[j-k][dp-kp], begr_v);
1704 mask = _mm_cmpgt_ps(tmpv, alpha[v]->vec[j][dp]);
1705 alpha[v]->vec[j][dp] = _mm_max_ps(alpha[v]->vec[j][dp], tmpv);
1706 if (ret_shadow != NULL) {
1707 shadow[v]->vec[j][dp] = esl_sse_select_ps(shadow[v]->vec[j][dp], tmpshad, mask);
1708 }
1709 }
1710 }
1711 for (k = 1; k <= jp; k+= 4)
1712 {
1713 tmpshad = (__m128) _mm_set1_epi32(k);
1714 kp = k/vecwidth;
1715 vec_access = (float *) (&alpha[z]->vec[j][kp]) + 1;
1716 begr_v = _mm_set1_ps(*vec_access);
1717
1718 tmpv = esl_sse_rightshift_ps(alpha[y]->vec[j-k][0], neginfv);
1719 tmpv = _mm_add_ps(tmpv, begr_v);
1720 mask = _mm_cmpgt_ps(tmpv, alpha[v]->vec[j][kp]);
1721 alpha[v]->vec[j][kp] = _mm_max_ps(alpha[v]->vec[j][kp], tmpv);
1722 if (ret_shadow != NULL) {
1723 shadow[v]->vec[j][kp] = esl_sse_select_ps(shadow[v]->vec[j][kp], tmpshad, mask);
1724 }
1725 for (dp = kp+1; dp <= sW; dp++)
1726 {
1727 tmpv = alt_rightshift_ps(alpha[y]->vec[j-k][dp-kp], alpha[y]->vec[j-k][dp-kp-1]);
1728 tmpv = _mm_add_ps(tmpv, begr_v);
1729 mask = _mm_cmpgt_ps(tmpv, alpha[v]->vec[j][dp]);
1730 alpha[v]->vec[j][dp] = _mm_max_ps(alpha[v]->vec[j][dp], tmpv);
1731 if (ret_shadow != NULL) {
1732 shadow[v]->vec[j][dp] = esl_sse_select_ps(shadow[v]->vec[j][dp], tmpshad, mask);
1733 }
1734 }
1735 }
1736 for (k = 2; k <= jp; k+= 4)
1737 {
1738 tmpshad = (__m128) _mm_set1_epi32(k);
1739 kp = k/vecwidth;
1740 vec_access = (float *) (&alpha[z]->vec[j][kp]) + 2;
1741 begr_v = _mm_set1_ps(*vec_access);
1742
1743 tmpv = _mm_movelh_ps(neginfv, alpha[y]->vec[j-k][0]);
1744 tmpv = _mm_add_ps(tmpv, begr_v);
1745 mask = _mm_cmpgt_ps(tmpv, alpha[v]->vec[j][kp]);
1746 alpha[v]->vec[j][kp] = _mm_max_ps(alpha[v]->vec[j][kp], tmpv);
1747 if (ret_shadow != NULL) {
1748 shadow[v]->vec[j][kp] = esl_sse_select_ps(shadow[v]->vec[j][kp], tmpshad, mask);
1749 }
1750 for (dp = kp+1; dp <= sW; dp++)
1751 {
1752 tmpv = _mm_movelh_ps(neginfv, alpha[y]->vec[j-k][dp-kp]);
1753 tmpv = _mm_movehl_ps(tmpv, alpha[y]->vec[j-k][dp-kp-1]);
1754 tmpv = _mm_add_ps(tmpv, begr_v);
1755 mask = _mm_cmpgt_ps(tmpv, alpha[v]->vec[j][dp]);
1756 alpha[v]->vec[j][dp] = _mm_max_ps(alpha[v]->vec[j][dp], tmpv);
1757 if (ret_shadow != NULL) {
1758 shadow[v]->vec[j][dp] = esl_sse_select_ps(shadow[v]->vec[j][dp], tmpshad, mask);
1759 }
1760 }
1761 }
1762 for (k = 3; k <= jp; k+= 4)
1763 {
1764 tmpshad = (__m128) _mm_set1_epi32(k);
1765 kp = k/vecwidth;
1766 vec_access = (float *) (&alpha[z]->vec[j][kp]) + 3;
1767 begr_v = _mm_set1_ps(*vec_access);
1768
1769 tmpv = esl_sse_leftshift_ps(neginfv, alpha[y]->vec[j-k][0]);
1770 tmpv = _mm_add_ps(tmpv, begr_v);
1771 mask = _mm_cmpgt_ps(tmpv, alpha[v]->vec[j][kp]);
1772 alpha[v]->vec[j][kp] = _mm_max_ps(alpha[v]->vec[j][kp], tmpv);
1773 if (ret_shadow != NULL) {
1774 shadow[v]->vec[j][kp] = esl_sse_select_ps(shadow[v]->vec[j][kp], tmpshad, mask);
1775 }
1776 for (dp = kp+1; dp <= sW; dp++)
1777 {
1778 tmpv = esl_sse_leftshift_ps(alpha[y]->vec[j-k][dp-kp-1], alpha[y]->vec[j-k][dp-kp]);
1779 tmpv = _mm_add_ps(tmpv, begr_v);
1780 mask = _mm_cmpgt_ps(tmpv, alpha[v]->vec[j][dp]);
1781 alpha[v]->vec[j][dp] = _mm_max_ps(alpha[v]->vec[j][dp], tmpv);
1782 if (ret_shadow != NULL) {
1783 shadow[v]->vec[j][dp] = esl_sse_select_ps(shadow[v]->vec[j][dp], tmpshad, mask);
1784 }
1785 }
1786 }
1787 /*
1788 for (d = 0; d <= sW; d++)
1789 {
1790 for (k = 1; k < (d+1)*vecwidth; k++)
1791 if ((sc = alpha[y][j-k][d-k] + alpha[z][j][k]) > alpha[v][j][d]) {
1792 alpha[v][j][d] = sc;
1793 if (ret_shadow != NULL) kshad[j][d] = k;
1794 }
1795 if (alpha[v][j][d] < IMPOSSIBLE) alpha[v][j][d] = IMPOSSIBLE;
1796 }
1797 */
1798 }
1799 }
1800 else if (cm->sttype[v] == MP_st)
1801 {
1802 for (x = 0; x < cm->abc->Kp; x++)
1803 {
1804 esc_stale[x] = i0-1;
1805 for (dp = 0; dp <= W/vecwidth; dp ++) { vec_Pesc[x][dp] = neginfv; }
1806 vec_Pesc[x][0] = esl_sse_rightshift_ps(vec_Pesc[x][0], _mm_set1_ps(cm->oesc[v][dsq[i0]*cm->abc->Kp+x]));
1807 }
1808 alpha[v]->vec[i0-1][0] = neginfv; /* jp = 0 */
1809 for (jp = 1; jp <= W; jp++) {
1810 j = i0-1+jp;
1811 /* slide esc vec array over */
1812 sW = jp/vecwidth;
1813 delta = j>0 ? j - esc_stale[dsq[j]] : 0;
1814 if (delta == 1) {
1815 for (dp = sW; dp > 0; dp--) { vec_Pesc[dsq[j]][dp] = alt_rightshift_ps(vec_Pesc[dsq[j]][dp], vec_Pesc[dsq[j]][dp-1]); }
1816 vec_Pesc[dsq[j]][0] = alt_rightshift_ps(vec_Pesc[dsq[j]][0], (jp<W) ? _mm_set1_ps(cm->oesc[v][dsq[j+1]*cm->abc->Kp+dsq[j]]) : neginfv);
1817 }
1818 else if (delta == 2) {
1819 for (dp = sW; dp > 0; dp--) {
1820 tmpv = _mm_movelh_ps(neginfv, vec_Pesc[dsq[j]][dp]);
1821 vec_Pesc[dsq[j]][dp] = _mm_movehl_ps(tmpv, vec_Pesc[dsq[j]][dp-1]);
1822 }
1823 tmpv = _mm_setr_ps(jp<W ? cm->oesc[v][dsq[j+1]*cm->abc->Kp+dsq[j]] : -eslINFINITY, cm->oesc[v][dsq[j]*cm->abc->Kp+dsq[j]], -eslINFINITY, -eslINFINITY);
1824 vec_Pesc[dsq[j]][0] = _mm_movelh_ps(tmpv, vec_Pesc[dsq[j]][0]);
1825 }
1826 else if (delta == 3) {
1827 for (dp = sW; dp > 0; dp--) { vec_Pesc[dsq[j]][dp] = esl_sse_leftshift_ps(vec_Pesc[dsq[j]][dp-1], vec_Pesc[dsq[j]][dp]); }
1828 tmpv = _mm_setr_ps(-eslINFINITY, jp<W ? cm->oesc[v][dsq[j+1]*cm->abc->Kp+dsq[j]] : -eslINFINITY,
1829 cm->oesc[v][dsq[j ]*cm->abc->Kp+dsq[j]],
1830 cm->oesc[v][dsq[j-1]*cm->abc->Kp+dsq[j]]);
1831 vec_Pesc[dsq[j]][0] = esl_sse_leftshift_ps(tmpv, vec_Pesc[dsq[j]][0]);
1832 }
1833 else if (delta == 4) {
1834 for (dp = sW; dp > 0; dp--) { vec_Pesc[dsq[j]][dp] = vec_Pesc[dsq[j]][dp-1]; }
1835 vec_Pesc[dsq[j]][0] = _mm_setr_ps(jp<W ? cm->oesc[v][dsq[j+1]*cm->abc->Kp+dsq[j]] : -eslINFINITY,
1836 cm->oesc[v][dsq[j ]*cm->abc->Kp+dsq[j]],
1837 cm->oesc[v][dsq[j-1]*cm->abc->Kp+dsq[j]],
1838 cm->oesc[v][dsq[j-2]*cm->abc->Kp+dsq[j]]);
1839 }
1840 if (j>0) esc_stale[dsq[j]] = j;
1841
1842 tmpv = _mm_movelh_ps(neginfv, _mm_mul_ps(el_self_v, doffset));
1843 alpha[v]->vec[j][0] = _mm_add_ps(_mm_set1_ps(cm->endsc[v]), tmpv);
1844 /* treat EL as emitting only on self transition */
1845 if (ret_shadow != NULL) shadow[v]->vec[j][0] = (__m128) _mm_set1_epi32(USED_EL);
1846 y = cm->cfirst[v];
1847 for (yoffset = 0; yoffset < cm->cnum[v]; yoffset++) {
1848 tscv = _mm_set1_ps(cm->tsc[v][yoffset]);
1849 if (j==0) tmpv = neginfv;
1850 else
1851 tmpv = _mm_movelh_ps(neginfv, alpha[y+yoffset]->vec[j-1][0]);
1852 tmpv = _mm_add_ps(tmpv, tscv);
1853 mask = _mm_cmpgt_ps(tmpv, alpha[v]->vec[j][0]);
1854 alpha[v]->vec[j][0] = _mm_max_ps(alpha[v]->vec[j][0], tmpv);
1855 if (ret_shadow != NULL) {
1856 shadow[v]->vec[j][0] = esl_sse_select_ps(shadow[v]->vec[j][0], (__m128) _mm_set1_epi32(yoffset), mask);
1857 }
1858 }
1859 /*escv = _mm_setr_ps(-eslINFINITY, -eslINFINITY,
1860 j>1?cm->oesc[v][dsq[j-1]*cm->abc->Kp+dsq[j]]:-eslINFINITY,
1861 j>2?cm->oesc[v][dsq[j-2]*cm->abc->Kp+dsq[j]]:-eslINFINITY); */
1862 escv = j>0 ? _mm_movehl_ps(vec_Pesc[dsq[j]][0], neginfv) : neginfv;
1863 alpha[v]->vec[j][0] = _mm_add_ps(alpha[v]->vec[j][0], escv);
1864
1865 for (dp = 1; dp <= sW; dp++)
1866 {
1867 tmpv = _mm_mul_ps(el_self_v, _mm_add_ps(_mm_set1_ps((float) dp*vecwidth-2), doffset));
1868 alpha[v]->vec[j][dp] = _mm_add_ps(_mm_set1_ps(cm->endsc[v]), tmpv);
1869 /* treat EL as emitting only on self transition */
1870 if (ret_shadow != NULL) shadow[v]->vec[j][dp] = (__m128) _mm_set1_epi32(USED_EL);
1871 for (yoffset = 0; yoffset < cm->cnum[v]; yoffset++) {
1872 tscv = _mm_set1_ps(cm->tsc[v][yoffset]);
1873 tmpv = _mm_movelh_ps(neginfv, alpha[y+yoffset]->vec[j-1][dp]);
1874 tmpv = _mm_movehl_ps(tmpv, alpha[y+yoffset]->vec[j-1][dp-1]);
1875 tmpv = _mm_add_ps(tmpv, tscv);
1876 mask = _mm_cmpgt_ps(tmpv, alpha[v]->vec[j][dp]);
1877 alpha[v]->vec[j][dp] = _mm_max_ps(alpha[v]->vec[j][dp], tmpv);
1878 if (ret_shadow != NULL) {
1879 shadow[v]->vec[j][dp] = esl_sse_select_ps(shadow[v]->vec[j][dp], (__m128) _mm_set1_epi32(yoffset), mask);
1880 }
1881 }
1882
1883 /* i = j-dp*vecwidth+1;
1884 escv = _mm_setr_ps(i>0?cm->oesc[v][dsq[i ]*cm->abc->Kp+dsq[j]]:-eslINFINITY,
1885 i>1?cm->oesc[v][dsq[i-1]*cm->abc->Kp+dsq[j]]:-eslINFINITY,
1886 i>2?cm->oesc[v][dsq[i-2]*cm->abc->Kp+dsq[j]]:-eslINFINITY,
1887 i>3?cm->oesc[v][dsq[i-3]*cm->abc->Kp+dsq[j]]:-eslINFINITY); */
1888 escv = vec_Pesc[dsq[j]][dp];
1889 alpha[v]->vec[j][dp] = _mm_add_ps(alpha[v]->vec[j][dp], escv);
1890 }
1891
1892 for (x = 0; x < cm->abc->Kp; x++)
1893 {
1894 delta = j - esc_stale[x];
1895 if (delta == vecwidth) {
1896 for (dp = sW; dp > 0; dp--) { vec_Pesc[x][dp] = vec_Pesc[x][dp-1]; }
1897 vec_Pesc[x][0] = _mm_setr_ps(jp<W ? cm->oesc[v][dsq[j+1]*cm->abc->Kp+x] : -eslINFINITY,
1898 cm->oesc[v][dsq[j ]*cm->abc->Kp+x],
1899 cm->oesc[v][dsq[j-1]*cm->abc->Kp+x],
1900 cm->oesc[v][dsq[j-2]*cm->abc->Kp+x]);
1901 esc_stale[x] = j;
1902 }
1903 }
1904 }
1905 }
1906 /* Separate out ML_st from IL_st, since only IL_st has to worry abuot self-transitions */
1907 else if (cm->sttype[v] == ML_st)
1908 {
1909 /* initialize esc vec array */
1910 vec_Lesc[0] = _mm_move_ss(neginfv, _mm_set1_ps(cm->oesc[v][dsq[i0]]));
1911 for (dp = 1; dp <= W/vecwidth; dp++) { vec_Lesc[dp] = neginfv; }
1912
1913 for (jp = 0; jp <= W; jp++) {
1914 j = i0-1+jp;
1915 tmpv = esl_sse_rightshift_ps(_mm_mul_ps(el_self_v, doffset), neginfv);
1916 alpha[v]->vec[j][0] = _mm_add_ps(_mm_set1_ps(cm->endsc[v]), tmpv);
1917 /* treat EL as emitting only on self transition */
1918 if (ret_shadow != NULL) shadow[v]->vec[j][0] = (__m128) _mm_set1_epi32(USED_EL);
1919 y = cm->cfirst[v];
1920 for (yoffset = 0; yoffset < cm->cnum[v]; yoffset++) {
1921 tscv = _mm_set1_ps(cm->tsc[v][yoffset]);
1922 tmpv = esl_sse_rightshift_ps(alpha[y+yoffset]->vec[j][0], neginfv);
1923 tmpv = _mm_add_ps(tmpv, tscv);
1924 mask = _mm_cmpgt_ps(tmpv, alpha[v]->vec[j][0]);
1925 alpha[v]->vec[j][0] = _mm_max_ps(alpha[v]->vec[j][0], tmpv);
1926 if (ret_shadow != NULL) {
1927 shadow[v]->vec[j][0] = esl_sse_select_ps(shadow[v]->vec[j][0], (__m128) _mm_set1_epi32(yoffset), mask);
1928 }
1929 }
1930 escv = _mm_move_ss(vec_Lesc[0], neginfv);
1931 alpha[v]->vec[j][0] = _mm_add_ps(alpha[v]->vec[j][0], escv);
1932
1933 sW = jp/vecwidth;
1934 for (dp = 1; dp <= sW; dp++)
1935 {
1936 tmpv = _mm_mul_ps(el_self_v, _mm_add_ps(_mm_set1_ps((float) dp*vecwidth - 1), doffset));
1937 alpha[v]->vec[j][dp] = _mm_add_ps(_mm_set1_ps(cm->endsc[v]), tmpv);
1938 /* treat EL as emitting only on self transition */
1939 if (ret_shadow != NULL) shadow[v]->vec[j][dp] = (__m128) _mm_set1_epi32(USED_EL);
1940 for (yoffset = 0; yoffset < cm->cnum[v]; yoffset++) {
1941 tscv = _mm_set1_ps(cm->tsc[v][yoffset]);
1942 tmpv = alt_rightshift_ps(alpha[y+yoffset]->vec[j][dp], alpha[y+yoffset]->vec[j][dp-1]);
1943 tmpv = _mm_add_ps(tmpv, tscv);
1944 mask = _mm_cmpgt_ps(tmpv, alpha[v]->vec[j][dp]);
1945 alpha[v]->vec[j][dp] = _mm_max_ps(alpha[v]->vec[j][dp], tmpv);
1946 if (ret_shadow != NULL) {
1947 shadow[v]->vec[j][dp] = esl_sse_select_ps(shadow[v]->vec[j][dp], (__m128) _mm_set1_epi32(yoffset), mask);
1948 }
1949 }
1950
1951 escv = vec_Lesc[dp];
1952 alpha[v]->vec[j][dp] = _mm_add_ps(alpha[v]->vec[j][dp], escv);
1953 }
1954
1955 /* slide esc vec array over by one */
1956 if (sW < W/vecwidth) sW++;
1957 for (dp = sW; dp > 0; dp--) { vec_Lesc[dp] = alt_rightshift_ps(vec_Lesc[dp], vec_Lesc[dp-1]); }
1958 vec_Lesc[0] = alt_rightshift_ps(vec_Lesc[0], (jp<W-1) ? _mm_set1_ps(cm->oesc[v][dsq[j+2]]) : neginfv);
1959 }
1960 }
1961 /* The self-transition loop on IL_st will need to be completely serialized, since
1962 v and j remain the same with only d varying in a non-striped implementation.
1963 The other possible transitions for IL_st can be treated normally, however. */
1964 else if (cm->sttype[v] == IL_st)
1965 {
1966 /* initialize esc vec array */
1967 vec_Lesc[0] = _mm_move_ss(neginfv, _mm_set1_ps(cm->oesc[v][dsq[i0]]));
1968 for (dp = 1; dp <= W/vecwidth; dp++) { vec_Lesc[dp] = neginfv; }
1969
1970 for (jp = 0; jp <= W; jp++) {
1971 j = i0-1+jp;
1972 tmpv = esl_sse_rightshift_ps(_mm_mul_ps(el_self_v, doffset), neginfv);
1973 //tmpv = _mm_mul_ps(el_self_v, esl_sse_rightshift_ps(doffset, neginfv));
1974 alpha[v]->vec[j][0] = _mm_add_ps(_mm_set1_ps(cm->endsc[v]), tmpv);
1975 /* treat EL as emitting only on self transition */
1976 if (ret_shadow != NULL) shadow[v]->vec[j][0] = (__m128) _mm_set1_epi32(USED_EL);
1977 y = cm->cfirst[v];
1978 for (yoffset = 1; yoffset < cm->cnum[v]; yoffset++) {
1979 tscv = _mm_set1_ps(cm->tsc[v][yoffset]);
1980 tmpv = esl_sse_rightshift_ps(alpha[y+yoffset]->vec[j][0], neginfv);
1981 tmpv = _mm_add_ps(tmpv, tscv);
1982 mask = _mm_cmpgt_ps(tmpv, alpha[v]->vec[j][0]);
1983 alpha[v]->vec[j][0] = _mm_max_ps(alpha[v]->vec[j][0], tmpv);
1984 if (ret_shadow != NULL) {
1985 shadow[v]->vec[j][0] = esl_sse_select_ps(shadow[v]->vec[j][0], (__m128) _mm_set1_epi32(yoffset), mask);
1986 }
1987 }
1988 escv = _mm_move_ss(vec_Lesc[0], neginfv);
1989 alpha[v]->vec[j][0] = _mm_add_ps(alpha[v]->vec[j][0], escv);
1990 /* handle yoffset = 0, the self-transition case, seaparately */
1991 tscv = _mm_set1_ps(cm->tsc[v][0]);
1992 for (k = 2; k < vecwidth; k++) {
1993 tmpv = esl_sse_rightshift_ps(alpha[y]->vec[j][0], neginfv);
1994 tmpv = _mm_add_ps(escv, _mm_add_ps(tscv, tmpv));
1995 mask = _mm_cmpgt_ps(tmpv, alpha[v]->vec[j][0]);
1996 alpha[v]->vec[j][0] = _mm_max_ps(alpha[v]->vec[j][0], tmpv);
1997 if (ret_shadow != NULL) {
1998 shadow[v]->vec[j][0] = esl_sse_select_ps(shadow[v]->vec[j][0], (__m128) _mm_set1_epi32(0), mask);
1999 }
2000 /* could make this a do-while on whether any values in mask are set */
2001 }
2002
2003 sW = jp/vecwidth;
2004 for (dp = 1; dp <= sW; dp++)
2005 {
2006 tmpv = _mm_mul_ps(el_self_v, _mm_add_ps(_mm_set1_ps((float) dp*vecwidth - 1), doffset));
2007 alpha[v]->vec[j][dp] = _mm_add_ps(_mm_set1_ps(cm->endsc[v]), tmpv);
2008 /* treat EL as emitting only on self transition */
2009 if (ret_shadow != NULL) shadow[v]->vec[j][dp] = (__m128) _mm_set1_epi32(USED_EL);
2010 for (yoffset = 1; yoffset < cm->cnum[v]; yoffset++) {
2011 tscv = _mm_set1_ps(cm->tsc[v][yoffset]);
2012 tmpv = alt_rightshift_ps(alpha[y+yoffset]->vec[j][dp], alpha[y+yoffset]->vec[j][dp-1]);
2013 tmpv = _mm_add_ps(tmpv, tscv);
2014 mask = _mm_cmpgt_ps(tmpv, alpha[v]->vec[j][dp]);
2015 alpha[v]->vec[j][dp] = _mm_max_ps(alpha[v]->vec[j][dp], tmpv);
2016 if (ret_shadow != NULL) {
2017 shadow[v]->vec[j][dp] = esl_sse_select_ps(shadow[v]->vec[j][dp], (__m128) _mm_set1_epi32(yoffset), mask);
2018 }
2019 }
2020
2021 escv = vec_Lesc[dp];
2022 alpha[v]->vec[j][dp] = _mm_add_ps(alpha[v]->vec[j][dp], escv);
2023
2024 /* handle yoffset = 0, the self-transition case, separately */
2025 tscv = _mm_set1_ps(cm->tsc[v][0]);
2026 for (k = 0; k < vecwidth; k++) {
2027 tmpv = alt_rightshift_ps(alpha[y]->vec[j][dp], alpha[y]->vec[j][dp-1]);
2028 tmpv = _mm_add_ps(escv, _mm_add_ps(tscv, tmpv));
2029 mask = _mm_cmpgt_ps(tmpv, alpha[v]->vec[j][dp]);
2030 alpha[v]->vec[j][dp] = _mm_max_ps(alpha[v]->vec[j][dp], tmpv);
2031 if (ret_shadow != NULL) {
2032 shadow[v]->vec[j][dp] = esl_sse_select_ps(shadow[v]->vec[j][dp], (__m128) _mm_set1_epi32(0), mask);
2033 }
2034 /* could make this a do-while on whether any values in mask are set */
2035 }
2036 }
2037
2038 /* slide esc vec array over by one */
2039 if (sW < W/vecwidth) sW++;
2040 for (dp = sW; dp > 0; dp--) { vec_Lesc[dp] = alt_rightshift_ps(vec_Lesc[dp], vec_Lesc[dp-1]); }
2041 vec_Lesc[0] = alt_rightshift_ps(vec_Lesc[0], (jp<W-1) ? _mm_set1_ps(cm->oesc[v][dsq[j+2]]) : neginfv);
2042 }
2043 }
2044 else if (cm->sttype[v] == IR_st || cm->sttype[v] == MR_st)
2045 {
2046 alpha[v]->vec[i0-1][0] = neginfv; /* jp = 0 */
2047 for (jp = 1; jp <= W; jp++) {
2048 j = i0-1+jp;
2049 tmpv = esl_sse_rightshift_ps(_mm_mul_ps(el_self_v, doffset), neginfv);
2050 alpha[v]->vec[j][0] = _mm_add_ps(_mm_set1_ps(cm->endsc[v]), tmpv);
2051 /* treat EL as emitting only on self transition */
2052 if (ret_shadow != NULL) shadow[v]->vec[j][0] = (__m128) _mm_set1_epi32(USED_EL);
2053 y = cm->cfirst[v];
2054 for (yoffset = 0; yoffset < cm->cnum[v]; yoffset++) {
2055 tscv = _mm_set1_ps(cm->tsc[v][yoffset]);
2056 if (j==0) tmpv = neginfv;
2057 else
2058 tmpv = esl_sse_rightshift_ps(alpha[y+yoffset]->vec[j-1][0], neginfv);
2059 tmpv = _mm_add_ps(tmpv, tscv);
2060 mask = _mm_cmpgt_ps(tmpv, alpha[v]->vec[j][0]);
2061 alpha[v]->vec[j][0] = _mm_max_ps(alpha[v]->vec[j][0], tmpv);
2062 if (ret_shadow != NULL) {
2063 shadow[v]->vec[j][0] = esl_sse_select_ps(shadow[v]->vec[j][0], (__m128) _mm_set1_epi32(yoffset), mask);
2064 }
2065 }
2066 if (j==0) escv = neginfv;
2067 else
2068 escv = _mm_setr_ps(-eslINFINITY, cm->oesc[v][dsq[j]], cm->oesc[v][dsq[j]], cm->oesc[v][dsq[j]]);
2069 alpha[v]->vec[j][0] = _mm_add_ps(alpha[v]->vec[j][0], escv);
2070
2071 sW = jp/vecwidth;
2072 if (j==0) escv = neginfv;
2073 else
2074 escv = _mm_set1_ps(cm->oesc[v][dsq[j]]);
2075 for (dp = 1; dp <= sW; dp++)
2076 {
2077 tmpv = _mm_mul_ps(el_self_v, _mm_add_ps(_mm_set1_ps((float) dp*vecwidth - 1), doffset));
2078 alpha[v]->vec[j][dp] = _mm_add_ps(_mm_set1_ps(cm->endsc[v]), tmpv);
2079 /* treat EL as emitting only on self transition */
2080 if (ret_shadow != NULL) shadow[v]->vec[j][dp] = (__m128) _mm_set1_epi32(USED_EL);
2081 for (yoffset = 0; yoffset < cm->cnum[v]; yoffset++) {
2082 tscv = _mm_set1_ps(cm->tsc[v][yoffset]);
2083 tmpv = alt_rightshift_ps(alpha[y+yoffset]->vec[j-1][dp], alpha[y+yoffset]->vec[j-1][dp-1]);
2084 tmpv = _mm_add_ps(tmpv, tscv);
2085 mask = _mm_cmpgt_ps(tmpv, alpha[v]->vec[j][dp]);
2086 alpha[v]->vec[j][dp] = _mm_max_ps(alpha[v]->vec[j][dp], tmpv);
2087 if (ret_shadow != NULL) {
2088 shadow[v]->vec[j][dp] = esl_sse_select_ps(shadow[v]->vec[j][dp], (__m128) _mm_set1_epi32(yoffset), mask);
2089 }
2090 }
2091
2092 alpha[v]->vec[j][dp] = _mm_add_ps(alpha[v]->vec[j][dp], escv);
2093 }
2094 }
2095 } /* finished calculating deck v. */
2096 /*
2097 float tmpsc;
2098 for (jp = 1; jp <= W; jp++) {
2099 j = i0-1+jp;
2100 fprintf(stderr,"v%3d j%3d ",v,j);
2101 for (d=0; d<=jp; d++) {
2102 tmpsc = *((float *) &alpha[v]->ivec[j][d/vecwidth]+d%vecwidth);
2103 if (tmpsc < IMPROBABLE) tmpsc = -eslINFINITY;
2104 fprintf(stderr,"%7.2f ",tmpsc);
2105 }
2106 fprintf(stderr,"\n");
2107 }
2108 */
2109
2110 /* Check for local begin getting us to the root.
2111 * This is "off-shadow": if/when we trace back, we'll handle this
2112 * case separately (and we'll know to do it because we'll immediately
2113 * see a USED_LOCAL_BEGIN flag in the shadow matrix, telling us
2114 * to jump right to state b; see below)
2115 */
2116 vec_access = (float *) (&alpha[v]->vec[j0][W/vecwidth]);
2117 tmp = *(vec_access + W%vecwidth);
2118 if (allow_begin && tmp + cm->beginsc[v] > bsc)
2119 {
2120 b = v;
2121 bsc = tmp + cm->beginsc[v];
2122 }
2123
2124 /* Check for whether we need to store an optimal local begin score
2125 * as the optimal overall score, and if we need to put a flag
2126 * in the shadow matrix telling insideT() to use the b we return.
2127 */
2128 if (allow_begin && v == 0 && bsc > tmp) {
2129 tmp = bsc;
2130 if (ret_shadow != NULL) {
2131 vec_access = (float *) (&shadow[v]->vec[j0][W/vecwidth]);
2132 vec_access += W%vecwidth;
2133 *vec_access = USED_LOCAL_BEGIN;
2134 }
2135 }
2136
2137 /* Now, if we're trying to reuse memory in our normal mode (e.g. ! do_full):
2138 * Look at our children; if they're fully released, take their deck
2139 * into the pool for reuse.
2140 */
2141 if (! do_full) {
2142 if (cm->sttype[v] == B_st)
2143 { /* we can definitely release the S children of a bifurc. */
2144 y = cm->cfirst[v]; sse_deckpool_push(dpool, alpha[y]); alpha[y] = NULL;
2145 z = cm->cnum[v]; sse_deckpool_push(dpool, alpha[z]); alpha[z] = NULL;
2146 }
2147 else
2148 {
2149 for (y = cm->cfirst[v]; y < cm->cfirst[v]+cm->cnum[v]; y++)
2150 {
2151 touch[y]--;
2152 if (touch[y] == 0)
2153 {
2154 if (cm->sttype[y] == E_st) {
2155 nends--;
2156 if (nends == 0) { sse_deckpool_push(dpool, end); end = NULL;}
2157 } else
2158 sse_deckpool_push(dpool, alpha[y]);
2159 alpha[y] = NULL;
2160 }
2161 }
2162 }
2163 }
2164 } /* end loop over all v */
2165
2166 /* debug_print_alpha(alpha, cm, L);*/
2167
2168 /* Now we free our memory.
2169 * if we've got do_full set, all decks vroot..vend are now valid (end is shared).
2170 * else, only vroot deck is valid now and all others vroot+1..vend are NULL,
2171 * and end is NULL.
2172 * We could check this status to be sure (and we used to) but now we trust.
2173 */
2174 vec_access = (float *) (&alpha[vroot]->vec[j0][W/vecwidth]);
2175 sc = *(vec_access + W%vecwidth);
2176 if (ret_b != NULL) *ret_b = b; /* b is -1 if allow_begin is FALSE. */
2177 if (ret_bsc != NULL) *ret_bsc = bsc; /* bsc is IMPOSSIBLE if allow_begin is FALSE */
2178
2179 /* If the caller doesn't want the matrix, free it (saving the decks in the pool!)
2180 * Else, pass it back to him.
2181 */
2182 if (ret_alpha == NULL) {
2183 for (v = vroot; v <= vend; v++) /* be careful of our reuse of the end deck -- free it only once */
2184 if (alpha[v] != NULL) {
2185 if (cm->sttype[v] != E_st) { sse_deckpool_push(dpool, alpha[v]); alpha[v] = NULL; }
2186 else end = alpha[v];
2187 }
2188 if (end != NULL) { sse_deckpool_push(dpool, end); end = NULL; }
2189 free(alpha);
2190 } else *ret_alpha = alpha;
2191
2192 /* If the caller doesn't want the deck pool, free it.
2193 * Else, pass it back to him.
2194 */
2195 if (ret_dpool == NULL) {
2196 while (sse_deckpool_pop(dpool, &end)) sse_free_vjd_deck(end);
2197 sse_deckpool_free(dpool);
2198 } else {
2199 *ret_dpool = dpool;
2200 }
2201
2202 free(esc_stale);
2203 free(vec_Pesc);
2204 free(mem_Pesc);
2205 free(mem_Lesc);
2206
2207 free(touch);
2208 if (ret_shadow != NULL) *ret_shadow = shadow;
2209 return sc;
2210
2211 ERROR:
2212 cm_Fail("Memory allocation error.\n");
2213 return 0.; /* never reached */
2214 }
2215
2216
2217 /* Function: outside()
2218 * Date: SRE, Tue Aug 8 10:42:52 2000 [St. Louis]
2219 *
2220 * Purpose: Run the outside version of a CYK alignment algorithm,
2221 * on a subsequence i0..j0 of a digitized sequence sq [1..L],
2222 * using a linear segment of a model anchored at a start state
2223 * (possibly the absolute root, 0) or (MP,ML,MR,D) and ending at an end
2224 * state, bifurcation state, or (MP|ML|MR|D) vend. There must be no
2225 * start, end, or bifurcation states in the path other than
2226 * these termini: this is not a full Outside implementation,
2227 * it is only the bit that's necessary in the divide
2228 * and conquer alignment algorithm.
2229 *
2230 * Much of the behavior in calling conventions, etc., is
2231 * analogous to the cyk_inside_engine(); see its preface
2232 * for more info.
2233 *
2234 * At the end of the routine, the bottom deck (vend) is valid.
2235 *
2236 * Args: cm - the model [0..M-1]
2237 * dsq - the sequence [1..L]
2238 * vroot - first state of linear model segment (S; MP|ML|MR|D)
2239 * vend - last state of linear model segment (B; E; MP|ML|MR|D)
2240 * i0 - first position in subseq to align (1, for whole seq)
2241 * j0 - last position in subseq to align (L, for whole seq)
2242 * do_full - if TRUE, we save all the decks in beta, instead of
2243 * working in our default memory-efficient mode where
2244 * we reuse decks and only the lowermost deck (vend) is valid
2245 * at the end.
2246 * beta - if non-NULL, this is an existing matrix, with NULL
2247 * decks for vroot..vend, and we'll fill in those decks
2248 * appropriately instead of creating a new matrix
2249 * ret_beta - if non-NULL, return the matrix with one or more
2250 * decks available for examination (see "do_full")
2251 * dpool - if non-NULL, this is an existing deck pool, possibly empty,
2252 * but usually containing one or more allocated decks sized
2253 * for this subsequence i0..j0.
2254 * ret_dpool - if non-NULL, return the deck pool for reuse -- these will
2255 * *only* be valid on exactly the same i0..j0 subseq,
2256 * because of the size of the subseq decks.
2257 */
2258 static void
2259 sse_outside(CM_t *cm, ESL_DSQ *dsq, int L, int vroot, int vend, int i0, int j0,
2260 int do_full, sse_deck_t **beta, sse_deck_t ***ret_beta,
2261 struct sse_deckpool_s *dpool, struct sse_deckpool_s **ret_dpool)
2262 {
2263 int status;
2264 int v,y; /* indices for states */
2265 int j,i; /* indices in sequence dimensions */
2266 int *touch; /* keeps track of how many lower decks still need this deck */
2267 float escore; /* an emission score, tmp variable */
2268 int W, sW; /* subsequence length */
2269 int jp, dp; /* j': relative position in the subsequence, 0..W */
2270 int voffset; /* index of v in t_v(y) transition scores */
2271 int w1,w2; /* bounds of split set */
2272 int k;
2273 __m128 neginfv;
2274 __m128 tmpv;
2275 __m128 escv, tscv;
2276 __m128 el_self_v, loop_v, doffset;
2277 float *vec_access;
2278 const int vecwidth = 4;
2279
2280 doffset = _mm_setr_ps(0.0, 1.0, 2.0, 3.0);
2281 el_self_v = _mm_set1_ps(cm->el_selfsc);
2282
2283 /* Allocations and initializations
2284 */
2285 neginfv = _mm_set1_ps(-eslINFINITY);
2286 W = j0-i0+1; /* the length of the subsequence: used in many loops */
2287
2288 /* if caller didn't give us a deck pool, make one */
2289 if (dpool == NULL) dpool = sse_deckpool_create();
2290
2291 /* if caller didn't give us a matrix, make one.
2292 * Allocate room for M+1 decks because we might need the EL deck (M)
2293 * if we're doing local alignment.
2294 */
2295 if (beta == NULL) {
2296 ESL_ALLOC(beta, sizeof(sse_deck_t *) * (cm->M+1));
2297 for (v = 0; v < cm->M+1; v++) beta[v] = NULL;
2298 }
2299
2300 /* Initialize the root deck.
2301 * If the root is in a split set, initialize the whole split set.
2302 */
2303 w1 = cm->nodemap[cm->ndidx[vroot]]; /* first state in split set */
2304 if (cm->sttype[vroot] == B_st) { /* special boundary case of Outside on a single B state. */
2305 w2 = w1;
2306 if (vend != vroot) cm_Fail("oh no. not again.");
2307 } else
2308 w2 = cm->cfirst[w1]-1; /* last state in split set w1<=vroot<=w2 */
2309
2310 for (v = w1; v <= w2; v++) {
2311 if (! sse_deckpool_pop(dpool, &(beta[v])))
2312 beta[v] = sse_alloc_vjd_deck(L, i0, j0, vecwidth);
2313 for (jp = 0; jp <= W; jp++) {
2314 j = i0-1+jp;
2315 sW = jp/vecwidth;
2316 for (dp = 0; dp <= sW; dp++)
2317 beta[v]->vec[j][dp] = neginfv;
2318 }
2319 }
2320 vec_access = (float *) &(beta[vroot]->vec[j0][W/vecwidth]) + W%vecwidth;
2321 *vec_access = 0.0;
2322
2323 /* Initialize the EL deck at M, if we're doing local alignment w.r.t. ends.
2324 */
2325 if (cm->flags & CMH_LOCAL_END) {
2326 if (! sse_deckpool_pop(dpool, &(beta[cm->M])))
2327 beta[cm->M] = sse_alloc_vjd_deck(L, i0, j0, vecwidth);
2328 for (jp = 0; jp <= W; jp++) {
2329 j = i0-1+jp;
2330 sW = jp/vecwidth;
2331 for (dp = 0; dp <= sW; dp++)
2332 beta[cm->M]->vec[j][dp] = neginfv;
2333 }
2334
2335 // FIXME: messy scalar access block
2336 /* We have to worry about vroot -> EL transitions.
2337 * since we start the main recursion at w2+1. This requires a
2338 * laborious partial unroll of the main recursion, grabbing
2339 * the stuff relevant to a beta[EL] calculation for just the
2340 * vroot->EL transition.
2341 */
2342 if (NOT_IMPOSSIBLE(cm->endsc[vroot])) {
2343 switch (cm->sttype[vroot]) {
2344 case MP_st:
2345 if (W < 2) break;
2346 escore = cm->oesc[vroot][dsq[i0]*cm->abc->Kp+dsq[j0]];
2347 vec_access = (float *) &(beta[cm->M]->vec[j0-1][(W-2)/vecwidth]) + (W-2)%vecwidth;
2348 *vec_access = cm->endsc[vroot] + (cm->el_selfsc * (W-2)) + escore;
2349 //beta[cm->M][j0-1][W-2] = cm->endsc[vroot] + (cm->el_selfsc * (W-2)) + escore;
2350
2351 // FIXME: the underflow issue again - probably fix with vec = max(vec, impossible_vec)
2352 //if (beta[cm->M][j0-1][W-2] < IMPOSSIBLE) beta[cm->M][j0-1][W-2] = IMPOSSIBLE;
2353 break;
2354 case ML_st:
2355 case IL_st:
2356 if (W < 1) break;
2357 escore = cm->oesc[vroot][dsq[i0]];
2358 vec_access = (float *) &(beta[cm->M]->vec[j0][(W-1)/vecwidth]) + (W-1)%vecwidth;
2359 *vec_access = cm->endsc[vroot] + (cm->el_selfsc * (W-1)) + escore;
2360 //beta[cm->M][j0][W-1] = cm->endsc[vroot] + (cm->el_selfsc * (W-1)) + escore;
2361
2362 //if (beta[cm->M][j0][W-1] < IMPOSSIBLE) beta[cm->M][j0][W-1] = IMPOSSIBLE;
2363 break;
2364 case MR_st:
2365 case IR_st:
2366 if (W < 1) break;
2367 escore = cm->oesc[vroot][dsq[j0]];
2368 vec_access = (float *) &(beta[cm->M]->vec[j0-1][(W-1)/vecwidth]) + (W-1)%vecwidth;
2369 *vec_access = cm->endsc[vroot] + (cm->el_selfsc * (W-1)) + escore;
2370 //beta[cm->M][j0-1][W-1] = cm->endsc[vroot] + (cm->el_selfsc * (W-1)) + escore;
2371
2372 //if (beta[cm->M][j0-1][W-1] < IMPOSSIBLE) beta[cm->M][j0-1][W-1] = IMPOSSIBLE;
2373 break;
2374 case S_st:
2375 case D_st:
2376 vec_access = (float *) &(beta[cm->M]->vec[j0][W/vecwidth]) + W%vecwidth;
2377 *vec_access = cm->endsc[vroot] + (cm->el_selfsc * W);
2378 //beta[cm->M][j0][W] = cm->endsc[vroot] + (cm->el_selfsc * W);
2379 //if (beta[cm->M][j0][W] < IMPOSSIBLE) beta[cm->M][j0][W] = IMPOSSIBLE;
2380 break;
2381 case B_st: /* can't start w/ bifurcation at vroot. */
2382 default: cm_Fail("bogus parent state %d\n", cm->sttype[vroot]);
2383 }
2384 }
2385 }
2386
2387 ESL_ALLOC(touch, sizeof(int) * cm->M);
2388 for (v = 0; v < w1; v++) touch[v] = 0; /* note: top of split set w1, not vroot */
2389
2390 for (v = vend+1; v < cm->M; v++) touch[v] = 0;
2391 for (v = w1; v <= vend; v++) {
2392 if (cm->sttype[v] == B_st) touch[v] = 2; /* well, we'll never use this, but set it anyway. */
2393 else touch[v] = cm->cnum[v];
2394 }
2395
2396
2397
2398 /* Main loop down through the decks
2399 */
2400 for (v = w2+1; v <= vend; v++)
2401 {
2402 /* First we need to fetch a deck of memory to fill in;
2403 * we try to reuse a deck but if one's not available we allocate
2404 * a fresh one.
2405 */
2406 if (! sse_deckpool_pop(dpool, &(beta[v])))
2407 beta[v] = sse_alloc_vjd_deck(L, i0, j0, vecwidth);
2408
2409 /* Init the whole deck to IMPOSSIBLE
2410 */
2411 for (jp = W; jp >= 0; jp--) {
2412 j = i0-1+jp;
2413 sW = jp/vecwidth;
2414 /* for (d = jp; d >= 0; d--)
2415 beta[v][j][d] = IMPOSSIBLE; */
2416 for (dp = sW; dp >= 0; dp--) beta[v]->vec[j][dp] = neginfv;
2417 }
2418
2419 /* If we can do a local begin into v, also init with that.
2420 * By definition, beta[0][j0][W] == 0.
2421 */
2422 if (vroot == 0 && i0 == 1 && j0 == L && (cm->flags & CMH_LOCAL_BEGIN)) {
2423 /* beta[v][j0][W] = cm->beginsc[v]; */
2424 vec_access = (float *) &(beta[v]->vec[j0][W/vecwidth]) + W%vecwidth;
2425 *vec_access = cm->beginsc[v];
2426 }
2427
2428 // FIXME: probably want to pull dp to the very inside of the loop, since
2429 // dp == sW needs to be handled differently anyway. Might also help
2430 // when switching to pre-calc'd esc vectors, too.
2431 /* main recursion:
2432 */
2433 for (jp = W; jp >= 0; jp--) {
2434 j = i0-1+jp;
2435 sW = jp/vecwidth;
2436 for (dp = sW; dp >= 0; dp--)
2437 {
2438 i = j-dp*vecwidth+1;
2439 //for (y = cm->plast[v]; y > cm->plast[v]-cm->pnum[v]; y--) {
2440 /* Change loop order to make sure IL (if present) is last */
2441 for (y = cm->plast[v]-cm->pnum[v]+1; y <= cm->plast[v]; y++) {
2442 if (y < vroot) continue; /* deal with split sets */
2443 voffset = v - cm->cfirst[y]; /* gotta calculate the transition score index for t_y(v) */
2444 tscv = _mm_set1_ps(cm->tsc[y][voffset]);
2445
2446 switch(cm->sttype[y]) {
2447 case MP_st:
2448 //if (j == j0 || d == jp) continue; /* boundary condition */
2449 if (j == j0) continue; /* boundary condition */
2450 if (dp == (jp+1)/vecwidth) {
2451 tmpv = _mm_movehl_ps(neginfv, beta[y]->vec[j+1][dp]);
2452 }
2453 else {
2454 tmpv = _mm_movelh_ps(neginfv, beta[y]->vec[j+1][dp+1]);
2455 tmpv = _mm_movehl_ps(tmpv, beta[y]->vec[j+1][dp]);
2456 }
2457
2458 //escore = cm->oesc[y][dsq[i-1]*cm->abc->Kp+dsq[j+1]];
2459 escv = j==j0 ? neginfv :
2460 _mm_setr_ps(i>1?cm->oesc[y][dsq[i-1]*cm->abc->Kp+dsq[j+1]]:-eslINFINITY,
2461 i>2?cm->oesc[y][dsq[i-2]*cm->abc->Kp+dsq[j+1]]:-eslINFINITY,
2462 i>3?cm->oesc[y][dsq[i-3]*cm->abc->Kp+dsq[j+1]]:-eslINFINITY,
2463 i>4?cm->oesc[y][dsq[i-4]*cm->abc->Kp+dsq[j+1]]:-eslINFINITY);
2464
2465 tmpv = _mm_add_ps(tmpv, tscv);
2466 tmpv = _mm_add_ps(tmpv, escv);
2467 beta[v]->vec[j][dp] = _mm_max_ps(beta[v]->vec[j][dp], tmpv);
2468 /*if ((sc = beta[y][j+1][d+2] + cm->tsc[y][voffset] + escore) > beta[v][j][d])
2469 beta[v][j][d] = sc; */
2470 break;
2471
2472 case ML_st:
2473 //escore = cm->oesc[y][dsq[i-1]];
2474 escv = _mm_setr_ps(i>1?cm->oesc[y][dsq[i-1]]:-eslINFINITY,
2475 i>2?cm->oesc[y][dsq[i-2]]:-eslINFINITY,
2476 i>3?cm->oesc[y][dsq[i-3]]:-eslINFINITY,
2477 i>4?cm->oesc[y][dsq[i-4]]:-eslINFINITY);
2478
2479 //if (d == jp) continue; /* boundary condition (note when j=0, d=0*/
2480 if (dp == sW)
2481 tmpv = esl_sse_leftshift_ps(beta[y]->vec[j][dp], neginfv);
2482 else
2483 tmpv = esl_sse_leftshift_ps(beta[y]->vec[j][dp], beta[y]->vec[j][dp+1]);
2484
2485 tmpv = _mm_add_ps(tmpv, tscv);
2486 tmpv = _mm_add_ps(tmpv, escv);
2487 beta[v]->vec[j][dp] = _mm_max_ps(beta[v]->vec[j][dp], tmpv);
2488 /*if ((sc = beta[y][j][d+1] + cm->tsc[y][voffset] + escore) > beta[v][j][d])
2489 beta[v][j][d] = sc; */
2490 break;
2491
2492 case IL_st:
2493 //escore = cm->oesc[y][dsq[i-1]];
2494 escv = _mm_setr_ps(i>1?cm->oesc[y][dsq[i-1]]:-eslINFINITY,
2495 i>2?cm->oesc[y][dsq[i-2]]:-eslINFINITY,
2496 i>3?cm->oesc[y][dsq[i-3]]:-eslINFINITY,
2497 i>4?cm->oesc[y][dsq[i-4]]:-eslINFINITY);
2498
2499 //if (d == jp) continue; /* boundary condition (note when j=0, d=0*/
2500 if (y == v) {
2501 for (k = 0; k < vecwidth; k++) {
2502 if (dp == sW)
2503 tmpv = esl_sse_leftshift_ps(beta[y]->vec[j][dp], neginfv);
2504 else
2505 tmpv = esl_sse_leftshift_ps(beta[y]->vec[j][dp], beta[y]->vec[j][dp+1]);
2506
2507 tmpv = _mm_add_ps(tmpv, tscv);
2508 tmpv = _mm_add_ps(tmpv, escv);
2509 beta[v]->vec[j][dp] = _mm_max_ps(beta[v]->vec[j][dp], tmpv);
2510 }
2511 }
2512 else {
2513 if (dp == sW)
2514 tmpv = esl_sse_leftshift_ps(beta[y]->vec[j][dp], neginfv);
2515 else
2516 tmpv = esl_sse_leftshift_ps(beta[y]->vec[j][dp], beta[y]->vec[j][dp+1]);
2517
2518 tmpv = _mm_add_ps(tmpv, tscv);
2519 tmpv = _mm_add_ps(tmpv, escv);
2520 beta[v]->vec[j][dp] = _mm_max_ps(beta[v]->vec[j][dp], tmpv);
2521 }
2522 /*if ((sc = beta[y][j][d+1] + cm->tsc[y][voffset] + escore) > beta[v][j][d])
2523 beta[v][j][d] = sc; */
2524 break;
2525
2526 case MR_st:
2527 case IR_st:
2528 if (j == j0) continue;
2529 if (dp == (jp+1)/vecwidth)
2530 tmpv = esl_sse_leftshift_ps(beta[y]->vec[j+1][dp], neginfv);
2531 else
2532 tmpv = esl_sse_leftshift_ps(beta[y]->vec[j+1][dp], beta[y]->vec[j+1][dp+1]);
2533
2534 //escore = cm->oesc[y][dsq[j+1]];
2535 escv = j==j0 ? neginfv : _mm_set1_ps(cm->oesc[y][dsq[j+1]]);
2536
2537 tmpv = _mm_add_ps(tmpv, tscv);
2538 tmpv = _mm_add_ps(tmpv, escv);
2539 beta[v]->vec[j][dp] = _mm_max_ps(beta[v]->vec[j][dp], tmpv);
2540 /* if ((sc = beta[y][j+1][d+1] + cm->tsc[y][voffset] + escore) > beta[v][j][d])
2541 beta[v][j][d] = sc; */
2542 break;
2543
2544 case S_st:
2545 case E_st:
2546 case D_st:
2547 tmpv = _mm_add_ps(beta[y]->vec[j][dp], tscv);
2548 beta[v]->vec[j][dp] = _mm_max_ps(beta[v]->vec[j][dp], tmpv);
2549 /* if ((sc = beta[y][j][d] + cm->tsc[y][voffset]) > beta[v][j][d])
2550 beta[v][j][d] = sc; */
2551 break;
2552
2553 default: cm_Fail("bogus child state %d\n", cm->sttype[y]);
2554 }/* end switch over states*/
2555 } /* ends for loop over parent states. we now know beta[v][j][d] for this d */
2556 //if (beta[v][j][d] < IMPOSSIBLE) beta[v][j][d] = IMPOSSIBLE;
2557
2558
2559 } /* ends loop over d. We know all beta[v][j][d] in this row j*/
2560 }/* end loop over jp. We know the beta's for the whole deck.*/
2561
2562 /* Deal with local alignment end transitions v->EL
2563 * (EL = deck at M.)
2564 */
2565 if (NOT_IMPOSSIBLE(cm->endsc[v])) {
2566 for (jp = 0; jp <= W; jp++) {
2567 j = i0-1+jp;
2568 sW = jp/vecwidth;
2569 for (dp = 0; dp <= sW; dp++)
2570 {
2571 i = j-dp*vecwidth+1;
2572 loop_v = _mm_mul_ps(el_self_v, _mm_add_ps(_mm_set1_ps((float) dp*vecwidth), doffset));
2573 switch (cm->sttype[v]) {
2574 case MP_st:
2575 //if (j == j0 || d == jp) continue; /* boundary condition */
2576 if (dp == (jp+1)/vecwidth)
2577 tmpv = _mm_movehl_ps(neginfv, beta[v]->vec[j+1][dp]);
2578 else {
2579 tmpv = _mm_movelh_ps(neginfv, beta[v]->vec[j+1][dp+1]);
2580 tmpv = _mm_movehl_ps(tmpv, beta[v]->vec[j+1][dp]);
2581 }
2582
2583 //escore = cm->oesc[v][dsq[i-1]*cm->abc->K+dsq[j+1]];
2584 escv = j==j0 ? neginfv:
2585 _mm_setr_ps(i>1?cm->oesc[v][dsq[i-1]*cm->abc->Kp+dsq[j+1]]:-eslINFINITY,
2586 i>2?cm->oesc[v][dsq[i-2]*cm->abc->Kp+dsq[j+1]]:-eslINFINITY,
2587 i>3?cm->oesc[v][dsq[i-3]*cm->abc->Kp+dsq[j+1]]:-eslINFINITY,
2588 i>4?cm->oesc[v][dsq[i-4]*cm->abc->Kp+dsq[j+1]]:-eslINFINITY);
2589
2590 tmpv = _mm_add_ps(tmpv, _mm_set1_ps(cm->endsc[v]));
2591 tmpv = _mm_add_ps(tmpv, loop_v);
2592 tmpv = _mm_add_ps(tmpv, escv);
2593 beta[cm->M]->vec[j][dp] = _mm_max_ps(beta[cm->M]->vec[j][dp], tmpv);
2594 /*if ((sc = beta[v][j+1][d+2] + cm->endsc[v] +
2595 (cm->el_selfsc * d) + escore) > beta[cm->M][j][d])
2596 beta[cm->M][j][d] = sc; */
2597 break;
2598 case ML_st:
2599 //escore = cm->oesc[v][dsq[i-1]];
2600 escv = _mm_setr_ps(i>1?cm->oesc[v][dsq[i-1]]:-eslINFINITY,
2601 i>2?cm->oesc[v][dsq[i-2]]:-eslINFINITY,
2602 i>3?cm->oesc[v][dsq[i-3]]:-eslINFINITY,
2603 i>4?cm->oesc[v][dsq[i-4]]:-eslINFINITY);
2604
2605 //if (d == jp) continue;
2606 if (dp == sW)
2607 tmpv = esl_sse_leftshift_ps(beta[v]->vec[j][dp], neginfv);
2608 else
2609 tmpv = esl_sse_leftshift_ps(beta[v]->vec[j][dp], beta[v]->vec[j][dp+1]);
2610
2611 tmpv = _mm_add_ps(tmpv, _mm_set1_ps(cm->endsc[v]));
2612 tmpv = _mm_add_ps(tmpv, loop_v);
2613 tmpv = _mm_add_ps(tmpv, escv);
2614 beta[cm->M]->vec[j][dp] = _mm_max_ps(beta[cm->M]->vec[j][dp], tmpv);
2615 /*if ((sc = beta[v][j][d+1] + cm->endsc[v] +
2616 (cm->el_selfsc * d) + escore) > beta[cm->M][j][d])
2617 beta[cm->M][j][d] = sc; */
2618 break;
2619 case IL_st:
2620 //escore = cm->oesc[v][dsq[i-1]];
2621 escv = _mm_setr_ps(i>1?cm->oesc[v][dsq[i-1]]:-eslINFINITY,
2622 i>2?cm->oesc[v][dsq[i-2]]:-eslINFINITY,
2623 i>3?cm->oesc[v][dsq[i-3]]:-eslINFINITY,
2624 i>4?cm->oesc[v][dsq[i-4]]:-eslINFINITY);
2625
2626 //FIXME: y is undefined here
2627 //FIXME: Do we need to worry about IL->IL->EL? If so, we're not covering that case
2628 //FIXME: because EL is handled here and we don't check IL->IL again
2629 //FIXME: is IL->EL even allowed?
2630 if (y == v) {
2631 //FIXME: this k loop doesn't make sense anyway, since no value in beta[v] is changing
2632 for (k = 0; k < vecwidth; k++) {
2633 //if (d == jp) continue;
2634 if (dp == sW)
2635 tmpv = esl_sse_leftshift_ps(beta[v]->vec[j][dp], neginfv);
2636 else
2637 tmpv = esl_sse_leftshift_ps(beta[v]->vec[j][dp], beta[v]->vec[j][dp+1]);
2638
2639 tmpv = _mm_add_ps(tmpv, _mm_set1_ps(cm->endsc[v]));
2640 tmpv = _mm_add_ps(tmpv, loop_v);
2641 tmpv = _mm_add_ps(tmpv, escv);
2642 beta[cm->M]->vec[j][dp] = _mm_max_ps(beta[cm->M]->vec[j][dp], tmpv);
2643 }
2644 }
2645 else {
2646 //if (d == jp) continue;
2647 if (dp == sW)
2648 tmpv = esl_sse_leftshift_ps(beta[v]->vec[j][dp], neginfv);
2649 else
2650 tmpv = esl_sse_leftshift_ps(beta[v]->vec[j][dp], beta[v]->vec[j][dp+1]);
2651
2652 tmpv = _mm_add_ps(tmpv, _mm_set1_ps(cm->endsc[v]));
2653 tmpv = _mm_add_ps(tmpv, loop_v);
2654 tmpv = _mm_add_ps(tmpv, escv);
2655 beta[cm->M]->vec[j][dp] = _mm_max_ps(beta[cm->M]->vec[j][dp], tmpv);
2656 }
2657 /*if ((sc = beta[v][j][d+1] + cm->endsc[v] +
2658 (cm->el_selfsc * d) + escore) > beta[cm->M][j][d])
2659 beta[cm->M][j][d] = sc; */
2660 break;
2661 case MR_st:
2662 case IR_st:
2663 if (j == j0) continue;
2664 if (dp == (jp+1)/vecwidth)
2665 tmpv = esl_sse_leftshift_ps(beta[v]->vec[j+1][dp], neginfv);
2666 else
2667 tmpv = esl_sse_leftshift_ps(beta[v]->vec[j+1][dp], beta[v]->vec[j+1][dp+1]);
2668
2669 //escore = cm->oesc[v][dsq[j+1]];
2670 escv = j==j0 ? neginfv : _mm_set1_ps(cm->oesc[v][dsq[j+1]]);
2671
2672 tmpv = _mm_add_ps(tmpv, _mm_set1_ps(cm->endsc[v]));
2673 tmpv = _mm_add_ps(tmpv, loop_v);
2674 tmpv = _mm_add_ps(tmpv, escv);
2675 beta[cm->M]->vec[j][dp] = _mm_max_ps(beta[cm->M]->vec[j][dp], tmpv);
2676 /* if ((sc = beta[v][j+1][d+1] + cm->endsc[v] +
2677 (cm->el_selfsc * d) + escore) > beta[cm->M][j][d])
2678 beta[cm->M][j][d] = sc; */
2679 break;
2680 case S_st:
2681 case D_st:
2682 case E_st:
2683 tmpv = _mm_add_ps(beta[v]->vec[j][dp], _mm_set1_ps(cm->endsc[v]));
2684 tmpv = _mm_add_ps(tmpv, loop_v);
2685 beta[cm->M]->vec[j][dp] = _mm_max_ps(beta[cm->M]->vec[j][dp], tmpv);
2686 /*if ((sc = beta[v][j][d] + cm->endsc[v] +
2687 (cm->el_selfsc * d)) > beta[cm->M][j][d])
2688 beta[cm->M][j][d] = sc; */
2689 break;
2690 case B_st:
2691 default: cm_Fail("bogus parent state %d\n", cm->sttype[v]);
2692 /* note that although B is a valid vend for a segment we'd do
2693 outside on, B->EL is set to be impossible, by the local alignment
2694 config. There's no point in having a B->EL because B is a nonemitter
2695 (indeed, it would introduce an alignment ambiguity). The same
2696 alignment case is handled by the X->EL transition where X is the
2697 parent consensus state (S, MP, ML, or MR) above the B. Thus,
2698 this code is relying on the NOT_IMPOSSIBLE() test, above,
2699 to make sure the sttype[vend]=B case gets into this switch.
2700 */
2701 } /* end switch over parent state type v */
2702 } /* end inner loop over d */
2703 } /* end outer loop over jp */
2704 } /* end conditional section for dealing w/ v->EL local end transitions */
2705
2706 /* Look at v's parents; if we're reusing memory (! do_full)
2707 * push the parents that we don't need any more into the pool.
2708 */
2709 if (! do_full) {
2710 for (y = cm->plast[v]; y > cm->plast[v]-cm->pnum[v]; y--) {
2711 touch[y]--;
2712 if (touch[y] == 0) { sse_deckpool_push(dpool, beta[y]); beta[y] = NULL; }
2713 }
2714 }
2715 } /* end loop over decks v. */
2716
2717 #if 0
2718 /* SRE: this code is superfluous, yes??? */
2719 /* Deal with last step needed for local alignment
2720 * w.r.t. ends: left-emitting, zero-scoring EL->EL transitions.
2721 * (EL = deck at M.)
2722 */
2723 if (cm->flags & CMH_LOCAL_END) {
2724 for (jp = W; jp > 0; jp--) { /* careful w/ boundary here */
2725 j = i0-1+jp;
2726 for (d = jp-1; d >= 0; d--) /* careful w/ boundary here */
2727 if ((sc = beta[cm->M][j][d+1]) > beta[cm->M][j][d])
2728 beta[cm->M][j][d] = sc;
2729 }
2730 }
2731 #endif
2732
2733 /* If the caller doesn't want the matrix, free it.
2734 * (though it would be *stupid* for the caller not to want the
2735 * matrix in the current implementation...)
2736 */
2737 if (ret_beta == NULL) {
2738 for (v = w1; v <= vend; v++) /* start at w1 - top of split set - not vroot */
2739 if (beta[v] != NULL) { sse_deckpool_push(dpool, beta[v]); beta[v] = NULL; }
2740 if (cm->flags & CMH_LOCAL_END) {
2741 sse_deckpool_push(dpool, beta[cm->M]);
2742 beta[cm->M] = NULL;
2743 }
2744 free(beta);
2745 } else *ret_beta = beta;
2746
2747 /* If the caller doesn't want the deck pool, free it.
2748 * Else, pass it back to him.
2749 */
2750 if (ret_dpool == NULL) {
2751 sse_deck_t *a;
2752 while (sse_deckpool_pop(dpool, &a)) sse_free_vjd_deck(a);
2753 sse_deckpool_free(dpool);
2754 } else {
2755 *ret_dpool = dpool;
2756 }
2757 free(touch);
2758 return;
2759
2760 ERROR:
2761 cm_Fail("Memory allocation error.\n");
2762 }
2763
2764
2765 /* Function: vinside()
2766 * Date: SRE, Sat Jun 2 09:24:51 2001 [Kaldi's]
2767 *
2768 * Purpose: Run the inside phase of the CYK alignment algorithm for
2769 * a V problem: an unbifurcated CM subgraph from
2770 * r..z, aligned to a one-hole subsequence
2771 * i0..i1 // j1..j0, exclusive of z,i1,j1.
2772 *
2773 * This is done in the vji coord system, where
2774 * both our j and i coordinates are transformed.
2775 * The Platonic matrix runs [j1..j0][i0..i1].
2776 * The actual matrix runs [0..j0-j1][0..i1-i0].
2777 * To transform a sequence coord i to a transformed
2778 * coord i', subtract i0; to transform i' to i,
2779 * add i0.
2780 *
2781 * The conventions for alpha and dpool are the
2782 * same as cyk_inside_engine().
2783 *
2784 * Args: cm - the model [0..M-1]
2785 * dsq - the sequence [1..L]
2786 * L - length of the dsq
2787 * r - first start state of subtree (0, for whole model)
2788 * z - last end state of subtree (cm->M-1, for whole model)
2789 * i0,i1 - first subseq part of the V problem
2790 * j1,j0 - second subseq part
2791 * useEL - if TRUE, V problem ends at EL/i1/j1, not z/i1/j1
2792 * do_full - if TRUE, we save all the decks in alpha, instead of
2793 * working in our default memory-efficient mode where
2794 * we reuse decks and only the uppermost deck (r) is valid
2795 * at the end.
2796 * a - if non-NULL, this is an existing matrix, with NULL
2797 * decks for r..z, and we'll fill in those decks
2798 * appropriately instead of creating a new matrix
2799 * ret_a - if non-NULL, return the matrix with one or more
2800 * decks available for examination (see "do_full")
2801 * dpool - if non-NULL, this is an existing deck pool, possibly empty,
2802 * but usually containing one or more allocated vji decks sized
2803 * for this subsequence i0..i1//j0..j1.
2804 * ret_dpool - if non-NULL, return the deck pool for reuse -- these will
2805 * *only* be valid on exactly the same i0..i1//j0..j1 subseq
2806 * because of the size of the subseq decks.
2807 * ret_shadow- if non-NULL, the caller wants a shadow matrix, because
2808 * he intends to do a traceback.
2809 * allow_begin- TRUE to allow 0->b local alignment begin transitions.
2810 * ret_b - best local begin state, or NULL if unwanted
2811 * ret_bsc - score for using ret_b, or NULL if unwanted
2812
2813 *
2814 * Returns: score.
2815 */
2816 static float
2817 sse_vinside(CM_t *cm, ESL_DSQ *dsq, int L,
2818 int r, int z, int i0, int i1, int j1, int j0, int useEL,
2819 int do_full, sse_deck_t **a, sse_deck_t ***ret_a,
2820 struct sse_deckpool_s *dpool, struct sse_deckpool_s **ret_dpool,
2821 sse_deck_t ***ret_shadow,
2822 int allow_begin, int *ret_b, float *ret_bsc)
2823 {
2824 int status;
2825 sse_deck_t **shadow = NULL; /* the shadow matrix -- traceback ptrs -- memory is kept */
2826 int v,i,j;
2827 int w1,w2; /* bounds of the split set */
2828 int jp, ip; /* j' and i' -- in the matrix coords */
2829 int *touch; /* keeps track of whether we can free a deck yet or not */
2830 int y, yoffset;
2831 float sc; /* tmp variable holding a score */
2832 int b; /* best local begin state */
2833 float bsc; /* score for using the best local begin state */
2834 int k;
2835 __m128 neginfv;
2836 __m128 tmpv, mask;
2837 __m128 tscv, escv;
2838 __m128 el_self_v, loop_v, ioffset;
2839 int sW;
2840 float *vec_access;
2841 const int vecwidth = 4;
2842
2843 neginfv = _mm_set1_ps(-eslINFINITY);
2844 el_self_v = _mm_set1_ps(cm->el_selfsc);
2845 ioffset = _mm_setr_ps(0.0, -1.0, -2.0, -3.0);
2846 sW = (i1-i0)/vecwidth;
2847
2848 /*printf("***in vinside()****\n");
2849 printf("\tr : %d\n", r);
2850 printf("\tz : %d\n", z);
2851 printf("\ti0 : %d\n", i0);
2852 printf("\ti1 : %d\n", i1);
2853 printf("\tj1 : %d\n", j1);
2854 printf("\tj0 : %d\n", j0);
2855 */
2856
2857 /* Allocations, initializations.
2858 * Remember to allocate for M+1 decks, in case we reuse this
2859 * memory for a local alignment voutside() calculation.
2860 */
2861 b = -1;
2862 bsc = IMPOSSIBLE;
2863 if (dpool == NULL) dpool = sse_deckpool_create();
2864 if (a == NULL) {
2865 ESL_ALLOC(a, sizeof(sse_deck_t *) * (cm->M+1));
2866 for (v = 0; v <= cm->M; v++) a[v] = NULL;
2867 }
2868 /* the whole split set w<=z<=y must be initialized */
2869 w1 = cm->nodemap[cm->ndidx[z]];
2870 w2 = cm->cfirst[w1]-1;
2871 for (v = w1; v <= w2; v++) {
2872 if (! sse_deckpool_pop(dpool, &(a[v])))
2873 a[v] = sse_alloc_vji_deck(i0, i1, j1, j0, vecwidth);
2874 for (jp = 0; jp <= j0-j1; jp++) {
2875 /* for (ip = 0; ip <= i1-i0; ip++)
2876 a[v][jp][ip] = IMPOSSIBLE; */
2877 for (ip = 0; ip <= sW; ip++)
2878 a[v]->vec[jp][ip] = neginfv;
2879 }
2880 }
2881
2882 if (ret_shadow != NULL) {
2883 ESL_ALLOC(shadow, sizeof(sse_deck_t *) * cm->M);
2884 for (v = 0; v < cm->M; v++) shadow[v] = NULL;
2885 }
2886
2887 /* Initialize the one non-IMPOSSIBLE cell as a boundary
2888 * condition.
2889 * If local alignment (useEL=1), we must connect z to EL;
2890 * we would init a[EL][0][i1-i0] = 0. But, we're not explicitly
2891 * keeping an EL deck, we're swallowing it into the recursion.
2892 * So, we unroll a chunk of the main recursion;
2893 * we have to laboriously figure out from the statetype z
2894 * and our position where and what our initialization is.
2895 * Else, for global alignments, we simply connect to z,0,i1-i0.
2896 */
2897 ip = i1-i0;
2898 jp = 0;
2899 if (! useEL) {
2900 vec_access = (float *) &(a[z]->vec[jp][ip/vecwidth]) + ip%vecwidth;
2901 *vec_access = 0.;
2902 //a[z][jp][ip] = 0.;
2903 }
2904 else
2905 {
2906 if (ret_shadow != NULL)
2907 shadow[z] = sse_alloc_vji_deck(i0,i1,j1,j0,vecwidth);
2908
2909 // FIXME: messy scalar access block
2910 switch (cm->sttype[z]) {
2911 case D_st:
2912 case S_st:
2913 /*a[z][jp][ip] = cm->endsc[z] + (cm->el_selfsc * ((jp+j1)-(ip+i0)+1 - StateDelta(cm->sttype[z])));*/
2914 vec_access = (float *) &(a[z]->vec[jp][ip/vecwidth]) + ip%vecwidth;
2915 *vec_access = cm->endsc[z] + (cm->el_selfsc * ((jp+j1)-(ip+i0)+1));
2916 // FIXME: here and below, this is sloppy - should only set the one shadow cell
2917 if (ret_shadow != NULL) shadow[z]->vec[jp][ip/vecwidth] = (__m128) _mm_set1_epi32(USED_EL);
2918 break;
2919 case MP_st:
2920 if (i0 == i1 || j1 == j0) break;
2921 /*a[z][jp+1][ip-1] = cm->endsc[z] + (cm->el_selfsc * ((jp+j1)-(ip+i0)+1 - StateDelta(cm->sttype[z])));*/
2922 vec_access = (float *) &(a[z]->vec[jp+1][(ip-1)/vecwidth]) + (ip-1)%vecwidth;
2923 *vec_access = cm->endsc[z] + (cm->el_selfsc * ((jp+j1)-(ip+i0)+1));
2924 *vec_access += cm->oesc[z][dsq[i1-1]*cm->abc->Kp + dsq[j1+1]];
2925 if (ret_shadow != NULL) shadow[z]->vec[jp+1][(ip-1)/vecwidth] = (__m128) _mm_set1_epi32(USED_EL);
2926 //if (a[z][jp+1][ip-1] < IMPOSSIBLE) a[z][jp+1][ip-1] = IMPOSSIBLE;
2927 break;
2928 case ML_st:
2929 case IL_st:
2930 if (i0==i1) break;
2931 /*a[z][jp][ip-1] = cm->endsc[z] + (cm->el_selfsc * ((jp+j1)-(ip+i0)+1 - StateDelta(cm->sttype[z])));*/
2932 vec_access = (float *) &(a[z]->vec[jp][(ip-1)/vecwidth]) + (ip-1)%vecwidth;
2933 *vec_access = cm->endsc[z] + (cm->el_selfsc * ((jp+j1)-(ip+i0)+1));
2934 *vec_access += cm->oesc[z][dsq[i1-1]];
2935 if (ret_shadow != NULL) shadow[z]->vec[jp][(ip-1)/vecwidth] = (__m128) _mm_set1_epi32(USED_EL);
2936 //if (a[z][jp][ip-1] < IMPOSSIBLE) a[z][jp][ip-1] = IMPOSSIBLE;
2937 break;
2938 case MR_st:
2939 case IR_st:
2940 if (j1==j0) break;
2941 /*a[z][jp+1][ip] = cm->endsc[z] + (cm->el_selfsc * ((jp+j1)-(ip+i0)+1 - StateDelta(cm->sttype[z])));*/
2942 vec_access = (float *) &(a[z]->vec[jp+1][ip/vecwidth]) + ip%vecwidth;
2943 *vec_access = cm->endsc[z] + (cm->el_selfsc * ((jp+j1)-(ip+i0)+1));
2944 *vec_access += cm->oesc[z][dsq[j1+1]];
2945 if (ret_shadow != NULL) shadow[z]->vec[jp+1][ip/vecwidth] = (__m128) _mm_set1_epi32(USED_EL);
2946 //if (a[z][jp+1][ip] < IMPOSSIBLE) a[z][jp+1][ip] = IMPOSSIBLE;
2947 break;
2948 }
2949
2950 } /* done initializing the appropriate cell for useEL=TRUE */
2951
2952 ESL_ALLOC(touch, sizeof(int) * cm->M);
2953 for (v = 0; v < r; v++) touch[v] = 0;
2954 for (v = r; v <= w2; v++) touch[v] = cm->pnum[v]; /* note w2 not z: to bottom of split set */
2955 for (v = w2+1; v < cm->M; v++) touch[v] = 0;
2956
2957 /* A special case. If vinside() is called on empty sequences,
2958 * we might do a begin transition right into z.
2959 */
2960 if (allow_begin && j0-j1 == 0 && i1-i0 == 0)
2961 {
2962 b = z;
2963 vec_access = (float *) &(a[z]->vec[0][0]);
2964 bsc = *vec_access + cm->beginsc[z];
2965 if (z == 0) {
2966 *vec_access = bsc;
2967 if (ret_shadow != NULL)
2968 shadow[0]->vec[0][0] = _mm_move_ss(shadow[0]->vec[0][0], (__m128) _mm_set1_epi32(USED_LOCAL_BEGIN));
2969 }
2970 }
2971
2972 /* Main recursion
2973 */
2974 for (v = w1-1; v >= r; v--)
2975 {
2976 /* Get a deck and a shadow deck.
2977 */
2978 if (! sse_deckpool_pop(dpool, &(a[v])))
2979 a[v] = sse_alloc_vji_deck(i0,i1,j1,j0,vecwidth);
2980 if (ret_shadow != NULL)
2981 shadow[v] = sse_alloc_vji_deck(i0,i1,j1,j0,vecwidth);
2982 /* reassert our definition of a V problem */
2983 if (cm->sttype[v] == E_st || cm->sttype[v] == B_st || (cm->sttype[v] == S_st && v > r))
2984 cm_Fail("you told me you wouldn't ever do that again.");
2985
2986 if (cm->sttype[v] == D_st || cm->sttype[v] == S_st)
2987 {
2988 for (jp = 0; jp <= j0-j1; jp++) {
2989 //for (ip = i1-i0; ip >= 0; ip--) {
2990 for (ip = sW; ip >= 0; ip--) {
2991 /*printf("D S jp : %d | ip : %d\n", jp, ip);*/
2992 y = cm->cfirst[v];
2993 a[v]->vec[jp][ip] = _mm_add_ps(a[y]->vec[jp][ip], _mm_set1_ps(cm->tsc[v][0]));
2994 /*printf("set a[%d][%d][%d] to %f\n", v, jp, ip, sc);*/
2995 if (ret_shadow != NULL) shadow[v]->vec[jp][ip] = (__m128) _mm_set1_epi32(0);
2996
2997 /* if (useEL && NOT_IMPOSSIBLE(cm->endsc[v]) &&
2998 ((cm->endsc[v] + (cm->el_selfsc * (((jp+j1)-(ip+i0)+1) - StateDelta(cm->sttype[v]))))
2999 > a[v][jp][ip])) {
3000 a[v][jp][ip] = cm->endsc[v] +
3001 (cm->el_selfsc * (((jp+j1)-(ip+i0)+1) - StateDelta(cm->sttype[v])));
3002 if (ret_shadow != NULL) shadow[v][jp][ip] = USED_EL;
3003 } */
3004 if (useEL && NOT_IMPOSSIBLE(cm->endsc[v])) {
3005 loop_v = _mm_set1_ps((float) (jp+j1) - (ip*vecwidth+i0) + 1);
3006 loop_v = _mm_add_ps(loop_v, ioffset);
3007 loop_v = _mm_mul_ps(el_self_v, loop_v);
3008 tmpv = _mm_add_ps(_mm_set1_ps(cm->endsc[v]), loop_v);
3009 mask = _mm_cmpgt_ps(tmpv, a[v]->vec[jp][ip]);
3010 a[v]->vec[jp][ip] = _mm_max_ps(tmpv, a[v]->vec[jp][ip]);
3011 if (ret_shadow != NULL) {
3012 shadow[v]->vec[jp][ip] = esl_sse_select_ps(shadow[v]->vec[jp][ip], (__m128) _mm_set1_epi32(USED_EL), mask);
3013 }
3014 }
3015 for (yoffset = 1; yoffset < cm->cnum[v]; yoffset++) {
3016 /* if ((sc = a[y+yoffset][jp][ip] + cm->tsc[v][yoffset]) > a[v][jp][ip])
3017 {
3018 a[v][jp][ip] = sc;
3019 if (ret_shadow != NULL) shadow[v][jp][ip] = (char) yoffset;
3020 } */
3021 tscv = _mm_set1_ps(cm->tsc[v][yoffset]);
3022 tmpv = _mm_add_ps(a[y+yoffset]->vec[jp][ip], tscv);
3023 mask = _mm_cmpgt_ps(tmpv, a[v]->vec[jp][ip]);
3024 a[v]->vec[jp][ip] = _mm_max_ps(tmpv, a[v]->vec[jp][ip]);
3025 if (ret_shadow != NULL) {
3026 shadow[v]->vec[jp][ip] = esl_sse_select_ps(shadow[v]->vec[jp][ip], (__m128) _mm_set1_epi32(yoffset), mask);
3027 }
3028 }
3029 //FIXME: there's that underflow again...
3030 //if (a[v][jp][ip] < IMPOSSIBLE) a[v][jp][ip] = IMPOSSIBLE;
3031 }
3032 }
3033 } else if (cm->sttype[v] == MP_st) {
3034 for (ip = sW; ip >= 0; ip--) a[v]->vec[0][ip] = neginfv; /* boundary condition */
3035
3036 for (jp = 1; jp <= j0-j1; jp++) {
3037 j = jp+j1;
3038 /* ip = sW case handled separately */
3039 ip = sW;
3040 y = cm->cfirst[v];
3041 tmpv = esl_sse_leftshift_ps(a[y]->vec[jp-1][ip],neginfv);
3042 a[v]->vec[jp][ip] = _mm_add_ps(tmpv, _mm_set1_ps(cm->tsc[v][0]));
3043 if (ret_shadow != NULL) shadow[v]->vec[jp][ip] = (__m128) _mm_set1_epi32(0);
3044 if (useEL && NOT_IMPOSSIBLE(cm->endsc[v])) {
3045 loop_v = _mm_set1_ps((float) (jp+j1) - (ip*vecwidth+i0) - 1); /* j-i+1, -2 for StateDelta */
3046 loop_v = _mm_add_ps(loop_v, ioffset);
3047 loop_v = _mm_mul_ps(el_self_v, loop_v);
3048 tmpv = _mm_add_ps(_mm_set1_ps(cm->endsc[v]), loop_v);
3049 mask = _mm_cmpgt_ps(tmpv, a[v]->vec[jp][ip]);
3050 a[v]->vec[jp][ip] = _mm_max_ps(tmpv, a[v]->vec[jp][ip]);
3051 if (ret_shadow != NULL) {
3052 shadow[v]->vec[jp][ip] = esl_sse_select_ps(shadow[v]->vec[jp][ip], (__m128) _mm_set1_epi32(USED_EL), mask);
3053 }
3054 }
3055 for (yoffset = 1; yoffset < cm->cnum[v]; yoffset++) {
3056 tmpv = esl_sse_leftshift_ps(a[y+yoffset]->vec[jp-1][ip],neginfv);
3057 tmpv = _mm_add_ps(tmpv, _mm_set1_ps(cm->tsc[v][yoffset]));
3058 mask = _mm_cmpgt_ps(tmpv, a[v]->vec[jp][ip]);
3059 a[v]->vec[jp][ip] = _mm_max_ps(tmpv, a[v]->vec[jp][ip]);
3060 if (ret_shadow != NULL) {
3061 shadow[v]->vec[jp][ip] = esl_sse_select_ps(shadow[v]->vec[jp][ip], (__m128) _mm_set1_epi32(yoffset), mask);
3062 }
3063 }
3064 i = ip*vecwidth + i0;
3065 escv = _mm_setr_ps(i <i1?cm->oesc[v][dsq[i ]*cm->abc->Kp+dsq[j]]:-eslINFINITY,
3066 i+1<i1?cm->oesc[v][dsq[i+1]*cm->abc->Kp+dsq[j]]:-eslINFINITY,
3067 i+2<i1?cm->oesc[v][dsq[i+2]*cm->abc->Kp+dsq[j]]:-eslINFINITY,
3068 i+3<i1?cm->oesc[v][dsq[i+3]*cm->abc->Kp+dsq[j]]:-eslINFINITY);
3069 a[v]->vec[jp][ip] = _mm_add_ps(a[v]->vec[jp][ip], escv);
3070
3071 /* all other values for ip */
3072 for (ip = sW-1; ip >= 0; ip--) {
3073 /*printf("MP jp : %d | ip : %d\n", jp, ip);*/
3074 i = ip*vecwidth + i0;
3075 y = cm->cfirst[v];
3076 tmpv = esl_sse_leftshift_ps(a[y]->vec[jp-1][ip], a[y]->vec[jp-1][ip+1]);
3077 a[v]->vec[jp][ip] = _mm_add_ps(tmpv, _mm_set1_ps(cm->tsc[v][0]));
3078 /*printf("set a[%d][%d][%d] to %f\n", v, jp, ip, sc);*/
3079 if (ret_shadow != NULL) shadow[v]->vec[jp][ip] = (__m128) _mm_set1_epi32(0);
3080 if (useEL && NOT_IMPOSSIBLE(cm->endsc[v])) {
3081 loop_v = _mm_set1_ps((float) (jp+j1) - (ip*vecwidth+i0) - 1); /* j-i+1, -2 for StateDelta */
3082 loop_v = _mm_add_ps(loop_v, ioffset);
3083 loop_v = _mm_mul_ps(el_self_v, loop_v);
3084 tmpv = _mm_add_ps(_mm_set1_ps(cm->endsc[v]), loop_v);
3085 mask = _mm_cmpgt_ps(tmpv, a[v]->vec[jp][ip]);
3086 a[v]->vec[jp][ip] = _mm_max_ps(tmpv, a[v]->vec[jp][ip]);
3087 if (ret_shadow != NULL) {
3088 shadow[v]->vec[jp][ip] = esl_sse_select_ps(shadow[v]->vec[jp][ip], (__m128) _mm_set1_epi32(USED_EL), mask);
3089 }
3090 }
3091 for (yoffset = 1; yoffset < cm->cnum[v]; yoffset++) {
3092 tmpv = esl_sse_leftshift_ps(a[y+yoffset]->vec[jp-1][ip],a[y+yoffset]->vec[jp-1][ip+1]);
3093 tmpv = _mm_add_ps(tmpv, _mm_set1_ps(cm->tsc[v][yoffset]));
3094 mask = _mm_cmpgt_ps(tmpv, a[v]->vec[jp][ip]);
3095 a[v]->vec[jp][ip] = _mm_max_ps(tmpv, a[v]->vec[jp][ip]);
3096 if (ret_shadow != NULL) {
3097 shadow[v]->vec[jp][ip] = esl_sse_select_ps(shadow[v]->vec[jp][ip], (__m128) _mm_set1_epi32(yoffset), mask);
3098 }
3099 }
3100 i = ip*vecwidth + i0;
3101 /* Could drop the <i1 checks here, since they should never be true, but we
3102 should be switching to precalculated matrices anyway */
3103 escv = _mm_setr_ps(i <i1?cm->oesc[v][dsq[i ]*cm->abc->Kp+dsq[j]]:-eslINFINITY,
3104 i+1<i1?cm->oesc[v][dsq[i+1]*cm->abc->Kp+dsq[j]]:-eslINFINITY,
3105 i+2<i1?cm->oesc[v][dsq[i+2]*cm->abc->Kp+dsq[j]]:-eslINFINITY,
3106 i+3<i1?cm->oesc[v][dsq[i+3]*cm->abc->Kp+dsq[j]]:-eslINFINITY);
3107 a[v]->vec[jp][ip] = _mm_add_ps(a[v]->vec[jp][ip], escv);
3108 //if (a[v][jp][ip] < IMPOSSIBLE) a[v][jp][ip] = IMPOSSIBLE;
3109 }
3110 }
3111 } else if (cm->sttype[v] == ML_st || cm->sttype[v] == IL_st) {
3112
3113 for (jp = 0; jp <= j0-j1; jp++) {
3114 j = jp+j1;
3115 /* ip = sW case handled separately */
3116 ip = sW;
3117 y = cm->cfirst[v];
3118 if (v == y) tmpv = neginfv;
3119 else tmpv = esl_sse_leftshift_ps(a[y]->vec[jp][ip],neginfv);
3120 a[v]->vec[jp][ip] = _mm_add_ps(tmpv, _mm_set1_ps(cm->tsc[v][0]));
3121 if (ret_shadow != NULL) shadow[v]->vec[jp][ip] = (__m128) _mm_set1_epi32(0);
3122 if (useEL && NOT_IMPOSSIBLE(cm->endsc[v])) {
3123 loop_v = _mm_set1_ps((float) (jp+j1) - (ip*vecwidth+i0)); /* j-i+1, -1 for StateDelta */
3124 loop_v = _mm_add_ps(loop_v, ioffset);
3125 loop_v = _mm_mul_ps(el_self_v, loop_v);
3126 tmpv = _mm_add_ps(_mm_set1_ps(cm->endsc[v]), loop_v);
3127 mask = _mm_cmpgt_ps(tmpv, a[v]->vec[jp][ip]);
3128 a[v]->vec[jp][ip] = _mm_max_ps(tmpv, a[v]->vec[jp][ip]);
3129 if (ret_shadow != NULL) {
3130 shadow[v]->vec[jp][ip] = esl_sse_select_ps(shadow[v]->vec[jp][ip], (__m128) _mm_set1_epi32(USED_EL), mask);
3131 }
3132 }
3133 for (yoffset = 1; yoffset < cm->cnum[v]; yoffset++) {
3134 tmpv = esl_sse_leftshift_ps(a[y+yoffset]->vec[jp][ip],neginfv);
3135 tmpv = _mm_add_ps(tmpv, _mm_set1_ps(cm->tsc[v][yoffset]));
3136 mask = _mm_cmpgt_ps(tmpv, a[v]->vec[jp][ip]);
3137 a[v]->vec[jp][ip] = _mm_max_ps(tmpv, a[v]->vec[jp][ip]);
3138 if (ret_shadow != NULL) {
3139 shadow[v]->vec[jp][ip] = esl_sse_select_ps(shadow[v]->vec[jp][ip], (__m128) _mm_set1_epi32(yoffset), mask);
3140 }
3141 }
3142 i = ip*vecwidth + i0;
3143 escv = _mm_setr_ps(i <i1?cm->oesc[v][dsq[i ]]:-eslINFINITY,
3144 i+1<i1?cm->oesc[v][dsq[i+1]]:-eslINFINITY,
3145 i+2<i1?cm->oesc[v][dsq[i+2]]:-eslINFINITY,
3146 i+3<i1?cm->oesc[v][dsq[i+3]]:-eslINFINITY);
3147 a[v]->vec[jp][ip] = _mm_add_ps(a[v]->vec[jp][ip], escv);
3148 /* finish the serial IL->IL path */
3149 if (v == y) {
3150 for (k = 1; k < vecwidth; k++) {
3151 tmpv = esl_sse_leftshift_ps(a[y]->vec[jp][ip],neginfv);
3152 tmpv = _mm_add_ps(tmpv, _mm_set1_ps(cm->tsc[v][0]));
3153 tmpv = _mm_add_ps(tmpv, escv);
3154 mask = _mm_cmpgt_ps(tmpv, a[v]->vec[jp][ip]);
3155 a[v]->vec[jp][ip] = _mm_max_ps(tmpv, a[v]->vec[jp][ip]);
3156 if (ret_shadow != NULL) {
3157 shadow[v]->vec[jp][ip] = esl_sse_select_ps(shadow[v]->vec[jp][ip], (__m128) _mm_set1_epi32(0), mask);
3158 }
3159 }
3160 }
3161
3162 /* all other values for ip */
3163 for (ip = sW-1; ip >= 0; ip--) {
3164 /*printf("MP jp : %d | ip : %d\n", jp, ip);*/
3165 i = ip*vecwidth + i0;
3166 y = cm->cfirst[v];
3167 if (v == y) tmpv = esl_sse_leftshift_ps(neginfv, a[y]->vec[jp][ip+1]);
3168 else tmpv = esl_sse_leftshift_ps(a[y]->vec[jp][ip], a[y]->vec[jp][ip+1]);
3169 a[v]->vec[jp][ip] = _mm_add_ps(tmpv, _mm_set1_ps(cm->tsc[v][0]));
3170 /*printf("set a[%d][%d][%d] to %f\n", v, jp, ip, sc);*/
3171 if (ret_shadow != NULL) shadow[v]->vec[jp][ip] = (__m128) _mm_set1_epi32(0);
3172 if (useEL && NOT_IMPOSSIBLE(cm->endsc[v])) {
3173 loop_v = _mm_set1_ps((float) (jp+j1) - (ip*vecwidth+i0)); /* j-i+1, -1 for StateDelta */
3174 loop_v = _mm_add_ps(loop_v, ioffset);
3175 loop_v = _mm_mul_ps(el_self_v, loop_v);
3176 tmpv = _mm_add_ps(_mm_set1_ps(cm->endsc[v]), loop_v);
3177 mask = _mm_cmpgt_ps(tmpv, a[v]->vec[jp][ip]);
3178 a[v]->vec[jp][ip] = _mm_max_ps(tmpv, a[v]->vec[jp][ip]);
3179 if (ret_shadow != NULL) {
3180 shadow[v]->vec[jp][ip] = esl_sse_select_ps(shadow[v]->vec[jp][ip], (__m128) _mm_set1_epi32(USED_EL), mask);
3181 }
3182 }
3183 for (yoffset = 1; yoffset < cm->cnum[v]; yoffset++) {
3184 tmpv = esl_sse_leftshift_ps(a[y+yoffset]->vec[jp][ip],a[y+yoffset]->vec[jp][ip+1]);
3185 tmpv = _mm_add_ps(tmpv, _mm_set1_ps(cm->tsc[v][yoffset]));
3186 mask = _mm_cmpgt_ps(tmpv, a[v]->vec[jp][ip]);
3187 a[v]->vec[jp][ip] = _mm_max_ps(tmpv, a[v]->vec[jp][ip]);
3188 if (ret_shadow != NULL) {
3189 shadow[v]->vec[jp][ip] = esl_sse_select_ps(shadow[v]->vec[jp][ip], (__m128) _mm_set1_epi32(yoffset), mask);
3190 }
3191 }
3192 i = ip*vecwidth + i0;
3193 /* Could drop the <i1 checks here, since they should never be true, but we
3194 should be switching to precalculated matrices anyway */
3195 escv = _mm_setr_ps(i <i1?cm->oesc[v][dsq[i ]]:-eslINFINITY,
3196 i+1<i1?cm->oesc[v][dsq[i+1]]:-eslINFINITY,
3197 i+2<i1?cm->oesc[v][dsq[i+2]]:-eslINFINITY,
3198 i+3<i1?cm->oesc[v][dsq[i+3]]:-eslINFINITY);
3199 a[v]->vec[jp][ip] = _mm_add_ps(a[v]->vec[jp][ip], escv);
3200 /* finish the serial IL->IL path */
3201 if (v == y) {
3202 for (k = 1; k < vecwidth; k++) {
3203 tmpv = esl_sse_leftshift_ps(a[y]->vec[jp][ip], a[y]->vec[jp][ip+1]);
3204 tmpv = _mm_add_ps(tmpv, _mm_set1_ps(cm->tsc[v][0]));
3205 tmpv = _mm_add_ps(tmpv, escv);
3206 mask = _mm_cmpgt_ps(tmpv, a[v]->vec[jp][ip]);
3207 a[v]->vec[jp][ip] = _mm_max_ps(tmpv, a[v]->vec[jp][ip]);
3208 if (ret_shadow != NULL) {
3209 shadow[v]->vec[jp][ip] = esl_sse_select_ps(shadow[v]->vec[jp][ip], (__m128) _mm_set1_epi32(0), mask);
3210 }
3211 }
3212 }
3213 //if (a[v][jp][ip] < IMPOSSIBLE) a[v][jp][ip] = IMPOSSIBLE;
3214 }
3215 }
3216 } else if (cm->sttype[v] == MR_st || cm->sttype[v] == IR_st) {
3217 for (ip = sW; ip >= 0; ip--) a[v]->vec[0][ip] = neginfv; /* boundary condition */
3218
3219 for (jp = 1; jp <= j0-j1; jp++) {
3220 j = jp+j1;
3221 for (ip = sW; ip >= 0; ip--) {
3222 /*printf("MR IR jp : %d | ip : %d\n", jp, ip);*/
3223 y = cm->cfirst[v];
3224 a[v]->vec[jp][ip] = _mm_add_ps(a[y]->vec[jp-1][ip], _mm_set1_ps(cm->tsc[v][0]));
3225 /*printf("set a[%d][%d][%d] to %f\n", v, jp, ip, sc);*/
3226 if (ret_shadow != NULL) shadow[v]->vec[jp][ip] = _mm_setzero_ps();
3227 if (useEL && NOT_IMPOSSIBLE(cm->endsc[v])) {
3228 loop_v = _mm_set1_ps((float) (jp+j1) - (ip*vecwidth+i0)); /* j-i+1, -1 for StateDelta */
3229 loop_v = _mm_add_ps(loop_v, ioffset);
3230 loop_v = _mm_mul_ps(el_self_v, loop_v);
3231 tmpv = _mm_add_ps(_mm_set1_ps(cm->endsc[v]), loop_v);
3232 mask = _mm_cmpgt_ps(tmpv, a[v]->vec[jp][ip]);
3233 a[v]->vec[jp][ip] = _mm_max_ps(tmpv, a[v]->vec[jp][ip]);
3234 if (ret_shadow != NULL) {
3235 shadow[v]->vec[jp][ip] = esl_sse_select_ps(shadow[v]->vec[jp][ip], (__m128) _mm_set1_epi32(USED_EL), mask);
3236 }
3237 }
3238 for (yoffset = 1; yoffset < cm->cnum[v]; yoffset++) {
3239 tmpv = a[y+yoffset]->vec[jp-1][ip];
3240 tmpv = _mm_add_ps(tmpv, _mm_set1_ps(cm->tsc[v][yoffset]));
3241 mask = _mm_cmpgt_ps(tmpv, a[v]->vec[jp][ip]);
3242 a[v]->vec[jp][ip] = _mm_max_ps(tmpv, a[v]->vec[jp][ip]);
3243 if (ret_shadow != NULL) {
3244 shadow[v]->vec[jp][ip] = esl_sse_select_ps(shadow[v]->vec[jp][ip], (__m128) _mm_set1_epi32(yoffset), mask);
3245 }
3246 }
3247
3248 escv = _mm_set1_ps(cm->oesc[v][dsq[j]]);
3249 a[v]->vec[jp][ip] = _mm_add_ps(a[v]->vec[jp][ip], escv);
3250 //if (a[v][jp][ip] < IMPOSSIBLE) a[v][jp][ip] = IMPOSSIBLE;
3251 }
3252 }
3253 } /* finished calculating deck v */
3254
3255 /* Check for local begin getting us to the root.
3256 */
3257 vec_access = (float *) &(a[v]->vec[j0-j1][0]);
3258 if (allow_begin && *vec_access + cm->beginsc[v] > bsc)
3259 {
3260 b = v;
3261 bsc = *vec_access + cm->beginsc[v];
3262 }
3263
3264 /* Check whether we need to store the local begin score
3265 * for a possible traceback.
3266 */
3267 if (allow_begin && v == 0 && bsc > *vec_access)
3268 {
3269 *vec_access = bsc;
3270 if (ret_shadow != NULL)
3271 shadow[v]->vec[j0-j1][0] = _mm_move_ss(shadow[v]->vec[j0-j1][0], (__m128) _mm_set1_epi32(USED_LOCAL_BEGIN));
3272 }
3273
3274
3275 /* Now, try to reuse memory under v.
3276 */
3277 if (! do_full) {
3278 for (y = cm->cfirst[v]; y < cm->cfirst[v]+cm->cnum[v]; y++)
3279 {
3280 touch[y]--;
3281 if (touch[y] == 0) {
3282 sse_deckpool_push(dpool, a[y]);
3283 a[y] = NULL;
3284 }
3285 }
3286 }
3287 } /* end loop over v; we now have a complete matrix */
3288
3289 /* Keep the score.
3290 */
3291 vec_access = (float *) &(a[r]->vec[j0-j1][0]);
3292 sc = *vec_access;
3293 if (ret_b != NULL) *ret_b = b; /* b is -1 if allow_begin is FALSE. */
3294 if (ret_bsc != NULL) *ret_bsc = bsc; /* bsc is IMPOSSIBLE if allow_begin is FALSE */
3295
3296
3297 /* If the caller doesn't want the score matrix back, blow
3298 * it away (saving decks in the pool). Else, pass it back.
3299 */
3300 if (ret_a == NULL) {
3301 for (v = r; v <= w2; v++) /* note: go all the way to the bottom of the split set */
3302 if (a[v] != NULL) {
3303 sse_deckpool_push(dpool, a[v]);
3304 a[v] = NULL;
3305 }
3306 free(a);
3307 } else *ret_a = a;
3308
3309 /* If caller doesn't want the deck pool, blow it away.
3310 * Else, pass it back.
3311 */
3312 if (ret_dpool == NULL) {
3313 sse_deck_t *foo;
3314 while (sse_deckpool_pop(dpool, &foo))
3315 sse_free_vji_deck(foo);
3316 sse_deckpool_free(dpool);
3317 } else *ret_dpool = dpool;
3318
3319 free(touch);
3320 if (ret_shadow != NULL) *ret_shadow = shadow;
3321 return sc;
3322
3323 ERROR:
3324 cm_Fail("Memory allocation error.\n");
3325 return 0.; /* never reached */
3326 }
3327
3328 /* Function: voutside()
3329 * Date: SRE, Sun Jun 3 15:44:41 2001 [St. Louis]
3330 *
3331 * Purpose: Run the outside version of a CYK alignment algorithm for
3332 * a V problem: an unbifurcated CM subgraph from r..z, aligned
3333 * to a one-whole subsequence i0..i1//j1..j0, exclusive of
3334 * z, i1, j1.
3335 *
3336 * This is done in the vji coordinate system, where both
3337 * our j and i coordinates are transformed. The Platonic
3338 * ideal matrix runs [j1..j0][i0..i1]. The implemented
3339 * matrix runs [0..j0-j1][0..i1-i0].
3340 *
3341 * Much of the behavior in calling conventions, etc., is
3342 * analogous to inside() and vinside(); see their prefaces
3343 * for more info. Unlike the inside engines, we never
3344 * need to calculate a shadow matrix - outside engines are
3345 * only used for divide and conquer steps.
3346 *
3347 * Args: cm - the model [0..M-1]
3348 * dsq - the sequence [1..L]
3349 * L - length of the dsq
3350 * r - first state of linear model segment (S; MP, ML, MR, or D)
3351 * z - last state of linear model segment (B; MP, ML, MR, or D)
3352 * i0,i1 - subsequence before the hole (1..L)
3353 * j1,j0 - subsequence after the hole (1..L)
3354 * useEL - if TRUE, worry about local alignment.
3355 * do_full - if TRUE, we save all the decks in beta, instead of
3356 * working in our default memory-efficient mode where
3357 * we reuse decks and only the lowermost decks (inc. z) are valid
3358 * at the end.
3359 * beta - if non-NULL, this is an existing matrix, with NULL
3360 * decks for r..z, and we'll fill in those decks
3361 * appropriately instead of creating a new matrix
3362 * ret_beta - if non-NULL, return the matrix with one or more
3363 * decks available for examination (see "do_full")
3364 * dpool - if non-NULL, this is an existing deck pool, possibly empty,
3365 * but usually containing one or more allocated vji decks sized
3366 * for this subsequence i0..i1//j1..j0.
3367 * ret_dpool - if non-NULL, return the deck pool for reuse -- these will
3368 * *only* be valid on exactly the same i0..i1//j1..j0 subseq,
3369 * because of the size of the subseq decks.
3370 */
3371 static void
3372 sse_voutside(CM_t *cm, ESL_DSQ *dsq, int L,
3373 int r, int z, int i0, int i1, int j1, int j0, int useEL,
3374 int do_full, sse_deck_t **beta, sse_deck_t ***ret_beta,
3375 struct sse_deckpool_s *dpool, struct sse_deckpool_s **ret_dpool)
3376 {
3377 int status;
3378 int v,y; /* indices for states */
3379 int i,j; /* indices in sequence dimensions */
3380 int ip, jp; /* transformed sequence indices */
3381 int *touch; /* keeps track of how many lower decks still need this deck */
3382 float escore; /* an emission score, tmp variable */
3383 int voffset; /* index of v in t_v(y) transition scores */
3384 int sW;
3385 int k;
3386 __m128 neginfv;
3387 __m128 tmpv, tscv, escv;
3388 __m128 el_self_v, loop_v;
3389 __m128 ioffset;
3390 float *vec_access;
3391 const int vecwidth = 4;
3392
3393 neginfv = _mm_set1_ps(-eslINFINITY);
3394 el_self_v = _mm_set1_ps(cm->el_selfsc);
3395 ioffset = _mm_setr_ps(0.0, -1.0, -2.0, -3.0);
3396
3397 /* Allocations and initializations
3398 */
3399 /* if caller didn't give us a deck pool, make one */
3400 if (dpool == NULL) dpool = sse_deckpool_create();
3401
3402 /* If caller didn't give us a matrix, make one.
3403 * Remember to allow for deck M, the EL deck, for local alignments.
3404 */
3405 if (beta == NULL) {
3406 ESL_ALLOC(beta, sizeof(sse_deck_t *) * (cm->M+1));
3407 for (v = 0; v <= cm->M; v++) beta[v] = NULL;
3408 }
3409 /* Initialize the root deck. This probably isn't the most efficient way to do it.
3410 */
3411 if (! sse_deckpool_pop(dpool, &(beta[r])))
3412 beta[r] = sse_alloc_vji_deck(i0,i1,j1,j0,vecwidth);
3413 sW = (i1-i0)/vecwidth;
3414 for (jp = 0; jp <= j0-j1; jp++) {
3415 for (ip = 0; ip <= sW; ip++)
3416 beta[r]->vec[jp][ip] = neginfv;
3417 }
3418 beta[r]->vec[j0-j1][0] = _mm_move_ss(beta[r]->vec[j0-j1][0], _mm_set1_ps(0.0));
3419
3420 /* Initialize the EL deck, if we're in local mode w.r.t. ends.
3421 * Deal with the special initialization case of the root state r
3422 * immediately transitioning to EL, if we're supposed to use EL.
3423 */
3424 if (useEL && cm->flags & CMH_LOCAL_END) {
3425 if (! sse_deckpool_pop(dpool, &(beta[cm->M])))
3426 beta[cm->M] = sse_alloc_vji_deck(i0,i1,j1,j0,vecwidth);
3427 for (jp = 0; jp <= j0-j1; jp++) {
3428 for (ip = 0; ip <= sW; ip++)
3429 beta[cm->M]->vec[jp][ip] = neginfv;
3430 }
3431 }
3432 // FIXME: messy scalar access block
3433 if (useEL && NOT_IMPOSSIBLE(cm->endsc[r])) {
3434 switch(cm->sttype[r]) {
3435 case MP_st:
3436 if (i0 == i1 || j1 == j0) break;
3437 escore = cm->oesc[r][dsq[i0]*cm->abc->Kp+dsq[j0]];
3438 vec_access = (float *) &(beta[cm->M]->vec[j0-j1-1][1]);
3439 *vec_access = cm->endsc[r] +
3440 (cm->el_selfsc * ((j0-1)-(i0+1)+1)) + escore;
3441 break;
3442 case ML_st:
3443 case IL_st:
3444 if (i0 == i1) break;
3445 escore = cm->oesc[r][dsq[i0]];
3446 vec_access = (float *) &(beta[cm->M]->vec[j0-j1][1]);
3447 *vec_access = cm->endsc[r] +
3448 (cm->el_selfsc * ((j0)-(i0+1)+1)) + escore;
3449 break;
3450 case MR_st:
3451 case IR_st:
3452 if (j0==j1) break;
3453 escore = cm->oesc[r][dsq[j0]];
3454 vec_access = (float *) &(beta[cm->M]->vec[j0-j1-1][0]);
3455 *vec_access = cm->endsc[r] +
3456 (cm->el_selfsc * ((j0-1)-(i0)+1)) + escore;
3457 break;
3458 case S_st:
3459 case D_st:
3460 vec_access = (float *) &(beta[cm->M]->vec[j0-j1][0]);
3461 *vec_access = cm->endsc[r] +
3462 (cm->el_selfsc * ((j0)-(i0)+1));
3463 break;
3464 default: cm_Fail("bogus parent state %d\n", cm->sttype[r]);
3465 }
3466 }
3467
3468 /* Initialize the "touch" array, used for figuring out
3469 * when a deck is no longer touched, so it can be free'd.
3470 */
3471 ESL_ALLOC(touch, sizeof(int) * cm->M);
3472 for (v = 0; v < r; v++) touch[v] = 0;
3473 for (v = z+1; v < cm->M; v++) touch[v] = 0;
3474 for (v = r; v <= z; v++) {
3475 if (cm->sttype[v] == B_st) touch[v] = 2; /* well, we never use this, but be complete */
3476 else touch[v] = cm->cnum[v];
3477 }
3478
3479
3480 /* Main loop down through the decks
3481 */
3482 for (v = r+1; v <= z; v++)
3483 {
3484 sW = (i1-i0)/vecwidth;
3485 /* First we need to fetch a deck of memory to fill in;
3486 * we try to reuse a deck but if one's not available we allocate
3487 * a fresh one.
3488 */
3489 if (! sse_deckpool_pop(dpool, &(beta[v])))
3490 beta[v] = sse_alloc_vji_deck(i0,i1,j1,j0,vecwidth);
3491
3492 /* Init the whole deck to IMPOSSIBLE.
3493 */
3494 for (jp = j0-j1; jp >= 0; jp--)
3495 for (ip = 0; ip <= sW; ip++)
3496 beta[v]->vec[jp][ip] = neginfv;
3497
3498 /* If we can get into deck v by a local begin transition, do an init
3499 * with that.
3500 */
3501 if (r == 0 && i0 == 1 && j0 == L && (cm->flags & CMH_LOCAL_BEGIN))
3502 {
3503 vec_access = (float *) &(beta[v]->vec[j0-j1][0]);
3504 if (cm->beginsc[v] > *vec_access)
3505 *vec_access = cm->beginsc[v];
3506 }
3507
3508 /* main recursion:
3509 */
3510 for (jp = j0-j1; jp >= 0; jp--) {
3511 j = jp+j1;
3512 /* handle ip = 0 separately */
3513 ip = 0;
3514 i = i0;
3515 //for (y = cm->plast[v]; y > cm->plast[v]-cm->pnum[v]; y--) {
3516 /* Change loop order to make sure IL (if present) is last */
3517 for (y = cm->plast[v]-cm->pnum[v]+1; y <= cm->plast[v]; y++) {
3518 if (y < r) continue; /* deal with split sets */
3519 voffset = v - cm->cfirst[y]; /* gotta calculate the transition score index for t_y(v) */
3520 tscv = _mm_set1_ps(cm->tsc[y][voffset]);
3521
3522 switch(cm->sttype[y]) {
3523 case MP_st: /* i == i0 boundary condition true */
3524 if (j == j0) continue; /* boundary condition */
3525 escv = _mm_setr_ps( -eslINFINITY,
3526 i <i1?cm->oesc[y][dsq[i ]*cm->abc->Kp+dsq[j+1]]:-eslINFINITY,
3527 i+1<i1?cm->oesc[y][dsq[i+1]*cm->abc->Kp+dsq[j+1]]:-eslINFINITY,
3528 i+2<i1?cm->oesc[y][dsq[i+2]*cm->abc->Kp+dsq[j+1]]:-eslINFINITY);
3529
3530 tmpv = alt_rightshift_ps(beta[y]->vec[jp+1][ip], neginfv);
3531 tmpv = _mm_add_ps(tmpv, tscv);
3532 tmpv = _mm_add_ps(tmpv, escv);
3533 beta[v]->vec[jp][ip] = _mm_max_ps(beta[v]->vec[jp][ip], tmpv);
3534 break;
3535
3536 case ML_st:
3537 escv = _mm_setr_ps( -eslINFINITY,
3538 i <i1?cm->oesc[y][dsq[i ]]:-eslINFINITY,
3539 i+1<i1?cm->oesc[y][dsq[i+1]]:-eslINFINITY,
3540 i+2<i1?cm->oesc[y][dsq[i+2]]:-eslINFINITY);
3541
3542 tmpv = alt_rightshift_ps(beta[y]->vec[jp][ip], neginfv);
3543 tmpv = _mm_add_ps(tmpv, tscv);
3544 tmpv = _mm_add_ps(tmpv, escv);
3545 beta[v]->vec[jp][ip] = _mm_max_ps(beta[v]->vec[jp][ip], tmpv);
3546 break;
3547 case IL_st:
3548 escv = _mm_setr_ps( -eslINFINITY,
3549 i <i1?cm->oesc[y][dsq[i ]]:-eslINFINITY,
3550 i+1<i1?cm->oesc[y][dsq[i+1]]:-eslINFINITY,
3551 i+2<i1?cm->oesc[y][dsq[i+2]]:-eslINFINITY);
3552
3553 if (y == v) {
3554 for (k = 1; k < vecwidth; k++) {
3555 tmpv = alt_rightshift_ps(beta[y]->vec[jp][ip], neginfv);
3556 tmpv = _mm_add_ps(tmpv, tscv);
3557 tmpv = _mm_add_ps(tmpv, escv);
3558 beta[v]->vec[jp][ip] = _mm_max_ps(beta[v]->vec[jp][ip], tmpv);
3559 }
3560 }
3561 else {
3562 tmpv = alt_rightshift_ps(beta[y]->vec[jp][ip], neginfv);
3563 tmpv = _mm_add_ps(tmpv, tscv);
3564 tmpv = _mm_add_ps(tmpv, escv);
3565 beta[v]->vec[jp][ip] = _mm_max_ps(beta[v]->vec[jp][ip], tmpv);
3566 }
3567 break;
3568
3569 case MR_st:
3570 case IR_st:
3571 if (j == j0) continue;
3572 escv = _mm_set1_ps(cm->oesc[y][dsq[j+1]]);
3573 tmpv = _mm_add_ps(beta[y]->vec[jp+1][ip], tscv);
3574 tmpv = _mm_add_ps(tmpv, escv);
3575 beta[v]->vec[jp][ip] = _mm_max_ps(beta[v]->vec[jp][ip], tmpv);
3576 break;
3577
3578 case S_st:
3579 case E_st:
3580 case D_st:
3581 tmpv = _mm_add_ps(beta[y]->vec[jp][ip], tscv);
3582 beta[v]->vec[jp][ip] = _mm_max_ps(beta[v]->vec[jp][ip], tmpv);
3583 break;
3584
3585 default: cm_Fail("bogus parent state %d\n", cm->sttype[y]);
3586 }/* end switch over states*/
3587 } /* ends for loop over parent states. we now know beta[v][j][d] for this d */
3588
3589 /* all other values for ip */
3590 for (ip = 1; ip <= sW; ip++)
3591 {
3592 i = ip*vecwidth+i0;
3593
3594 //for (y = cm->plast[v]; y > cm->plast[v]-cm->pnum[v]; y--) {
3595 /* Change loop order to make sure IL (if present) is last */
3596 for (y = cm->plast[v]-cm->pnum[v]+1; y <= cm->plast[v]; y++) {
3597 if (y < r) continue; /* deal with split sets */
3598 voffset = v - cm->cfirst[y]; /* gotta calculate the transition score index for t_y(v) */
3599 tscv = _mm_set1_ps(cm->tsc[y][voffset]);
3600
3601 switch(cm->sttype[y]) {
3602 case MP_st:
3603 if (j == j0) continue; /* boundary condition */
3604 //escore = cm->oesc[y][dsq[i-1]*cm->abc->Kp+dsq[j+1]];
3605 escv = _mm_setr_ps(i-1<i1?cm->oesc[y][dsq[i-1]*cm->abc->Kp+dsq[j+1]]:-eslINFINITY,
3606 i <i1?cm->oesc[y][dsq[i ]*cm->abc->Kp+dsq[j+1]]:-eslINFINITY,
3607 i+1<i1?cm->oesc[y][dsq[i+1]*cm->abc->Kp+dsq[j+1]]:-eslINFINITY,
3608 i+2<i1?cm->oesc[y][dsq[i+2]*cm->abc->Kp+dsq[j+1]]:-eslINFINITY);
3609
3610 tmpv = alt_rightshift_ps(beta[y]->vec[jp+1][ip], beta[y]->vec[jp+1][ip-1]);
3611 tmpv = _mm_add_ps(tmpv, tscv);
3612 tmpv = _mm_add_ps(tmpv, escv);
3613 beta[v]->vec[jp][ip] = _mm_max_ps(beta[v]->vec[jp][ip], tmpv);
3614 break;
3615
3616 case ML_st:
3617 //escore = cm->oesc[y][dsq[i-1]];
3618 escv = _mm_setr_ps(i-1<i1?cm->oesc[y][dsq[i-1]]:-eslINFINITY,
3619 i <i1?cm->oesc[y][dsq[i ]]:-eslINFINITY,
3620 i+1<i1?cm->oesc[y][dsq[i+1]]:-eslINFINITY,
3621 i+2<i1?cm->oesc[y][dsq[i+2]]:-eslINFINITY);
3622
3623 tmpv = alt_rightshift_ps(beta[y]->vec[jp][ip], beta[y]->vec[jp][ip-1]);
3624 tmpv = _mm_add_ps(tmpv, tscv);
3625 tmpv = _mm_add_ps(tmpv, escv);
3626 beta[v]->vec[jp][ip] = _mm_max_ps(beta[v]->vec[jp][ip], tmpv);
3627 break;
3628
3629 case IL_st:
3630 //escore = cm->oesc[y][dsq[i-1]];
3631 escv = _mm_setr_ps(i-1<i1?cm->oesc[y][dsq[i-1]]:-eslINFINITY,
3632 i <i1?cm->oesc[y][dsq[i ]]:-eslINFINITY,
3633 i+1<i1?cm->oesc[y][dsq[i+1]]:-eslINFINITY,
3634 i+2<i1?cm->oesc[y][dsq[i+2]]:-eslINFINITY);
3635
3636 if (y == v) {
3637 for (k = 0; k < vecwidth; k++) {
3638 tmpv = alt_rightshift_ps(beta[y]->vec[jp][ip], beta[y]->vec[jp][ip-1]);
3639 tmpv = _mm_add_ps(tmpv, tscv);
3640 tmpv = _mm_add_ps(tmpv, escv);
3641 beta[v]->vec[jp][ip] = _mm_max_ps(beta[v]->vec[jp][ip], tmpv);
3642 }
3643 }
3644 else {
3645 tmpv = alt_rightshift_ps(beta[y]->vec[jp][ip], beta[y]->vec[jp][ip-1]);
3646 tmpv = _mm_add_ps(tmpv, tscv);
3647 tmpv = _mm_add_ps(tmpv, escv);
3648 beta[v]->vec[jp][ip] = _mm_max_ps(beta[v]->vec[jp][ip], tmpv);
3649 }
3650 break;
3651
3652 case MR_st:
3653 case IR_st:
3654 if (j == j0) continue;
3655 //escore = cm->oesc[y][dsq[j+1]];
3656 escv = _mm_set1_ps(cm->oesc[y][dsq[j+1]]);
3657
3658 tmpv = _mm_add_ps(beta[y]->vec[jp+1][ip], tscv);
3659 tmpv = _mm_add_ps(tmpv, escv);
3660 beta[v]->vec[jp][ip] = _mm_max_ps(beta[v]->vec[jp][ip], tmpv);
3661 break;
3662
3663 case S_st:
3664 case E_st:
3665 case D_st:
3666 tmpv = _mm_add_ps(beta[y]->vec[jp][ip], tscv);
3667 beta[v]->vec[jp][ip] = _mm_max_ps(beta[v]->vec[jp][ip], tmpv);
3668 break;
3669
3670 default: cm_Fail("bogus parent state %d\n", cm->sttype[y]);
3671 }/* end switch over states*/
3672 } /* ends for loop over parent states. we now know beta[v][j][d] for this d */
3673 //if (beta[v][jp][ip] < IMPOSSIBLE) beta[v][jp][ip] = IMPOSSIBLE;
3674
3675 } /* ends loop over ip. We know all beta[v][jp][ip] in this row jp */
3676
3677 }/* end loop over jp. We know the beta's for the whole deck.*/
3678
3679 //FIXME: potential code and time reduction if the following loop is folded in with the previous one
3680 /* Deal with local alignment
3681 * transitions v->EL, if we're doing local alignment and there's a
3682 * possible transition.
3683 */
3684 if (useEL && NOT_IMPOSSIBLE(cm->endsc[v])) {
3685 for (jp = j0-j1; jp >= 0; jp--) {
3686 j = jp+j1;
3687 /* handle ip = 0 separately */
3688 ip = 0;
3689 i = i0;
3690 loop_v = _mm_set1_ps((float) (j - i + 1));
3691 loop_v = _mm_add_ps(loop_v, ioffset);
3692 loop_v = _mm_mul_ps(el_self_v, loop_v);
3693 loop_v = _mm_add_ps(loop_v, _mm_set1_ps(cm->endsc[v]));
3694
3695 switch(cm->sttype[v]) {
3696 case MP_st: /* i == i0 boundary condition true */
3697 if (j == j0) continue;
3698 escv = _mm_setr_ps( -eslINFINITY,
3699 i <i1?cm->oesc[v][dsq[i ]*cm->abc->Kp+dsq[j+1]]:-eslINFINITY,
3700 i+1<i1?cm->oesc[v][dsq[i+1]*cm->abc->Kp+dsq[j+1]]:-eslINFINITY,
3701 i+2<i1?cm->oesc[v][dsq[i+2]*cm->abc->Kp+dsq[j+1]]:-eslINFINITY);
3702
3703 tmpv = alt_rightshift_ps(beta[v]->vec[jp+1][ip], neginfv);
3704 tmpv = _mm_add_ps(tmpv, loop_v);
3705 tmpv = _mm_add_ps(tmpv, escv);
3706 beta[v]->vec[jp][ip] = _mm_max_ps(beta[v]->vec[jp][ip], tmpv);
3707 break;
3708
3709 case ML_st:
3710 case IL_st:
3711 escv = _mm_setr_ps( -eslINFINITY,
3712 i <i1?cm->oesc[v][dsq[i ]]:-eslINFINITY,
3713 i+1<i1?cm->oesc[v][dsq[i+1]]:-eslINFINITY,
3714 i+2<i1?cm->oesc[v][dsq[i+2]]:-eslINFINITY);
3715
3716 tmpv = alt_rightshift_ps(beta[v]->vec[jp][ip], neginfv);
3717 tmpv = _mm_add_ps(tmpv, loop_v);
3718 tmpv = _mm_add_ps(tmpv, escv);
3719 beta[v]->vec[jp][ip] = _mm_max_ps(beta[v]->vec[jp][ip], tmpv);
3720 break;
3721
3722 case MR_st:
3723 case IR_st:
3724 if (j == j0) continue;
3725 escv = _mm_set1_ps(cm->oesc[v][dsq[j+1]]);
3726 tmpv = _mm_add_ps(beta[v]->vec[jp+1][ip], loop_v);
3727 tmpv = _mm_add_ps(tmpv, escv);
3728 beta[v]->vec[jp][ip] = _mm_max_ps(beta[v]->vec[jp][ip], tmpv);
3729 break;
3730
3731 case S_st:
3732 case E_st:
3733 case D_st:
3734 tmpv = _mm_add_ps(beta[v]->vec[jp][ip], loop_v);
3735 beta[v]->vec[jp][ip] = _mm_max_ps(beta[v]->vec[jp][ip], tmpv);
3736 break;
3737
3738 default: cm_Fail("bogus parent state %d\n", cm->sttype[v]);
3739 }/* end switch over states*/
3740
3741 for (ip = 1; ip <= sW; ip++)
3742 {
3743 i = ip*vecwidth+i0;
3744 loop_v = _mm_set1_ps((float) (j - i + 1));
3745 loop_v = _mm_add_ps(loop_v, ioffset);
3746 loop_v = _mm_mul_ps(el_self_v, loop_v);
3747 switch (cm->sttype[v]) {
3748 case MP_st:
3749 if (j == j0) continue;
3750 //escore = cm->oesc[v][dsq[i-1]*cm->abc->Kp+dsq[j+1]];
3751 escv = _mm_setr_ps(i-1<i1?cm->oesc[v][dsq[i-1]*cm->abc->Kp+dsq[j+1]]:-eslINFINITY,
3752 i <i1?cm->oesc[v][dsq[i ]*cm->abc->Kp+dsq[j+1]]:-eslINFINITY,
3753 i+1<i1?cm->oesc[v][dsq[i+1]*cm->abc->Kp+dsq[j+1]]:-eslINFINITY,
3754 i+2<i1?cm->oesc[v][dsq[i+2]*cm->abc->Kp+dsq[j+1]]:-eslINFINITY);
3755
3756 tmpv = alt_rightshift_ps(beta[v]->vec[jp+1][ip], beta[v]->vec[jp+1][ip-1]);
3757 tmpv = _mm_add_ps(tmpv, loop_v);
3758 tmpv = _mm_add_ps(tmpv, escv);
3759 beta[v]->vec[jp][ip] = _mm_max_ps(beta[v]->vec[jp][ip], tmpv);
3760 /* if ((sc = beta[v][jp+1][ip-1] + cm->endsc[v] +
3761 (cm->el_selfsc * (j-i+1)) + escore) > beta[cm->M][jp][ip])
3762 beta[cm->M][jp][ip] = sc; */
3763 break;
3764 case ML_st:
3765 case IL_st:
3766 //escore = cm->oesc[v][dsq[i-1]];
3767 escv = _mm_setr_ps(i-1<i1?cm->oesc[v][dsq[i-1]]:-eslINFINITY,
3768 i <i1?cm->oesc[v][dsq[i ]]:-eslINFINITY,
3769 i+1<i1?cm->oesc[v][dsq[i+1]]:-eslINFINITY,
3770 i+2<i1?cm->oesc[v][dsq[i+2]]:-eslINFINITY);
3771
3772 tmpv = alt_rightshift_ps(beta[v]->vec[jp][ip], beta[v]->vec[jp][ip-1]);
3773 tmpv = _mm_add_ps(tmpv, loop_v);
3774 tmpv = _mm_add_ps(tmpv, escv);
3775 beta[v]->vec[jp][ip] = _mm_max_ps(beta[v]->vec[jp][ip], tmpv);
3776 /* if ((sc = beta[v][jp][ip-1] + cm->endsc[v] +
3777 (cm->el_selfsc * (j-i+1)) + escore) > beta[cm->M][jp][ip])
3778 beta[cm->M][jp][ip] = sc; */
3779 break;
3780 case MR_st:
3781 case IR_st:
3782 if (j == j0) continue;
3783 escore = cm->oesc[v][dsq[j+1]];
3784 escv = _mm_set1_ps(cm->oesc[v][dsq[j+1]]);
3785
3786 tmpv = beta[v]->vec[jp+1][ip];
3787 tmpv = _mm_add_ps(tmpv, loop_v);
3788 tmpv = _mm_add_ps(tmpv, escv);
3789 beta[v]->vec[jp][ip] = _mm_max_ps(beta[v]->vec[jp][ip], tmpv);
3790 /*if ((sc = beta[v][jp+1][ip] + cm->endsc[v] +
3791 (cm->el_selfsc * (j-i+1)) + escore) > beta[cm->M][jp][ip])
3792 beta[cm->M][jp][ip] = sc; */
3793 break;
3794 case S_st:
3795 case D_st:
3796 case E_st:
3797 tmpv = beta[v]->vec[jp][ip];
3798 tmpv = _mm_add_ps(tmpv, loop_v);
3799 beta[v]->vec[jp][ip] = _mm_max_ps(beta[v]->vec[jp][ip], tmpv);
3800 /* if ((sc = beta[v][jp][ip] + cm->endsc[v] +
3801 (cm->el_selfsc * (j-i+1))) > beta[cm->M][jp][ip])
3802 beta[cm->M][jp][ip] = sc; */
3803 break;
3804 default: cm_Fail("bogus parent state %d\n", cm->sttype[v]);
3805 } /* end switch over parent v state type */
3806 } /* end loop over ip */
3807 } /* end loop over jp */
3808 }
3809
3810 /* Finished deck v.
3811 * now look at its parents; if we're reusing memory (! do_full)
3812 * push the parents that we don't need any more into the pool.
3813 */
3814 if (! do_full) {
3815 for (y = cm->plast[v]; y > cm->plast[v]-cm->pnum[v]; y--) {
3816 touch[y]--;
3817 if (touch[y] == 0) {
3818 sse_deckpool_push(dpool, beta[y]);
3819 beta[y] = NULL;
3820 }
3821 }
3822 }
3823
3824 } /* end loop over decks v. */
3825
3826 #if 0
3827 /* superfluous code, I think...*/
3828 /* Deal with the last step needed for local alignment
3829 * w.r.t. ends: left-emitting, zero-scoring EL->EL transitions.
3830 */
3831 if (useEL && cm->flags & CMH_LOCAL_END) {
3832 for (jp = j0-j1; jp >= 0; jp--)
3833 for (ip = 1; ip <= i1-i0; ip++) /* careful w/ boundary here */
3834 if ((sc = beta[cm->M][jp][ip-1]) > beta[cm->M][jp][ip])
3835 beta[cm->M][jp][ip] = sc;
3836 }
3837 #endif
3838
3839 /* If the caller doesn't want the matrix, free it.
3840 * (though it would be *stupid* for the caller not to want the
3841 * matrix in the current implementation!)
3842 */
3843 if (ret_beta == NULL) {
3844 for (v = r; v <= z; v++)
3845 if (beta[v] != NULL) { sse_deckpool_push(dpool, beta[v]); beta[v] = NULL; }
3846 if (cm->flags & CMH_LOCAL_END) {
3847 sse_deckpool_push(dpool, beta[cm->M]);
3848 beta[cm->M] = NULL;
3849 }
3850 free(beta);
3851 } else *ret_beta = beta;
3852
3853 /* If the caller doesn't want the deck pool, free it.
3854 * Else, pass it back to him.
3855 */
3856 if (ret_dpool == NULL) {
3857 sse_deck_t *a;
3858 while (sse_deckpool_pop(dpool, &a))
3859 sse_free_vji_deck(a);
3860 sse_deckpool_free(dpool);
3861 } else *ret_dpool = dpool;
3862
3863 free(touch);
3864 return;
3865
3866 ERROR:
3867 cm_Fail("Memory allocation error.\n");
3868 }
3869
3870 /*****************************************************************
3871 * The Filter engines
3872 * SSE_CYKFilter_epi16 - inside on scaled 16-bit ints, 8x vector
3873 *****************************************************************/
3874
3875 /* Function: SSE_CYKFilter_epi16()
3876 * Date: DLK, Fri May 1 2009
3877 *
3878 * Purpose: Run memory-efficient CYK on 8x 16-bit integers.
3879 * The normal expectation is that the alignment
3880 * will be calculated for the whole model, although
3881 * local begins and ends are allowed. We should
3882 * also allow alignment to a subsequence of the
3883 * input coordinates, in case the bounds aren't precise.
3884 *
3885 * No mechanism for traceback, reusing deck pools,
3886 * or returning the alpha matrix is provided.
3887 *
3888 * Args: cm - the model [0..M-1]
3889 * sq - the sequence [1..L]
3890 * vroot - first start state of subtree (0, for whole model)
3891 * vend - last end state of subtree (cm->M-1, for whole model)
3892 * i0 - first position in subseq to align (1, for whole seq)
3893 * j0 - last position in subseq to align (L, for whole seq)
3894 * allow_begin- TRUE to allow 0->b local alignment begin transitions.
3895 * ret_b - best local begin state, or NULL if unwanted
3896 * ret_bsc - score for using ret_b, or NULL if unwanted
3897 *
3898 *
3899 * Returns: Score of the optimal alignment.
3900 */
3901 // FIXME: General problem: -inf is not sticky in this function, meaning that
3902 // FIXME: a sequence can saturate in negative scores for a long time and then
3903 // FIXME: re-increase to a much higher score later.
3904 int
3905 SSE_CYKFilter_epi16(CM_OPTIMIZED *ocm, ESL_DSQ *dsq, int L, int vroot, int vend, int i0, int j0,
3906 int allow_begin, int *ret_b, int *ret_bsc, HitCoord_epi16 *ret_coord)
3907 {
3908 int status;
3909 int nends; /* counter that tracks when we can release end deck to the pool */
3910 int *touch; /* keeps track of how many higher decks still need this deck */
3911 int v,y,z; /* indices for states */
3912 int j,d,k; /* indices in sequence dimensions */
3913 int16_t sc; /* a temporary variable holding a score */
3914 int yoffset; /* y=base+offset -- counter in child states that v can transit to */
3915 int W; /* subsequence length */
3916 int jp; /* j': relative position in the subsequence */
3917 int b; /* best local begin state */
3918 int16_t bsc; /* score for using the best local begin state */
3919
3920 sse_deck_t **alpha = NULL;
3921 struct sse_deckpool_s *dpool = NULL;
3922
3923 const int vecwidth = 8;
3924 int sW;
3925 int dp, kp;
3926 int16_t *vec_access;
3927 int16_t tmp;
3928 __m128i zerov;
3929 __m128i neginfv;
3930 __m128i el_self_v;
3931 __m128i tscv;
3932 __m128i escv;
3933 __m128i *mem_Lesc;
3934 __m128i *vec_Lesc;
3935 __m128i *mem_Pesc;
3936 __m128i **vec_Pesc;
3937 int delta, x;
3938 int *esc_stale;
3939 __m128i doffset;
3940 __m128i tmpv;
3941 sse_deck_t *end;
3942 __m128i mask;
3943 #ifndef LOBAL_MODE
3944 __m128i *mem_unaligned_sc;
3945 __m128i *vec_unaligned_sc;
3946 __m128i vb_sc;
3947 __m128i tmp_j, vb_j;
3948 __m128i tmp_d, vb_d;
3949 __m128i tmp_v, vb_v;
3950 #endif
3951 float len;
3952 float p;
3953 float r;
3954 float constpart;
3955 int16_t logp;
3956
3957 /* Allocations and initializations
3958 */
3959 zerov = _mm_set1_epi16(0);
3960 neginfv = _mm_set1_epi16(-32768);
3961 el_self_v = _mm_set1_epi16(ocm->el_selfsc);
3962 doffset = _mm_setr_epi16(0, 1, 2, 3, 4, 5, 6, 7);
3963 b = -1;
3964 bsc = -32768;
3965 W = j0-i0+1; /* the length of the subsequence -- used in many loops */
3966 sW = W/vecwidth;
3967
3968 /* Set up memory for pre-vectorized emission scores */
3969 ESL_ALLOC(mem_Lesc, sizeof(__m128i ) * (sW+1) + 15);
3970 ESL_ALLOC(mem_Pesc, sizeof(__m128i ) * ocm->abc->Kp * (sW+1) + 15);
3971 ESL_ALLOC(vec_Pesc, sizeof(__m128i *) * ocm->abc->Kp);
3972 ESL_ALLOC(esc_stale,sizeof(int *) * ocm->abc->Kp);
3973
3974 vec_Lesc = (__m128i *) (((unsigned long int) mem_Lesc + 15) & (~0xf));
3975 vec_Pesc[0] = (__m128i *) (((unsigned long int) mem_Pesc + 15) & (~0xf));
3976 for (j = 1; j < ocm->abc->Kp; j++)
3977 {
3978 vec_Pesc[j] = vec_Pesc[0] + j*(sW+1);
3979 }
3980
3981 #ifndef LOBAL_MODE
3982 vb_sc = neginfv;
3983 vb_v = vb_j = vb_d = _mm_set1_epi16(-1);
3984
3985 ESL_ALLOC(mem_unaligned_sc, sizeof(__m128i) * (sW+1) + 15);
3986 vec_unaligned_sc = (__m128i *) (((unsigned long int) mem_unaligned_sc + 15) & (~0xf));
3987
3988 //FIXME: it's somewhat unclear here what to use as the 'sequence length' - whether
3989 //FIXME: that should be the entire sequence L, or just the (i0,j0) window that's
3990 //FIXME: actually being considered for alignment. Using 'len' here for ease of changing it.
3991 len = (float) j0-i0+1;
3992 p = len/(len + 2.);
3993 r = len/(len + 1.);
3994 constpart = 2*sreLOG2(1.-p) + len*sreLOG2(p) - len*sreLOG2(r) - sreLOG2(1.-r);
3995 tmp = wordify(ocm->scale_w, constpart);
3996 logp = wordify(ocm->scale_w, sreLOG2(p));
3997 for (dp = 0; dp <= sW; dp++) {
3998 tmpv = _mm_mullo_epi16(_mm_set1_epi16(logp), _mm_adds_epi16(doffset, _mm_set1_epi16(dp*vecwidth)));
3999 mask = _mm_cmpgt_epi16(zerov,tmpv);
4000 tmpv = _mm_and_si128(tmpv,mask);
4001 tmpv = _mm_or_si128(tmpv,_mm_andnot_si128(mask,_mm_set1_epi16(0x8000)));
4002 vec_unaligned_sc[dp] = _mm_subs_epi16(_mm_set1_epi16(tmp), tmpv);
4003 }
4004 #endif
4005
4006 /* Make a deck pool */
4007 if (dpool == NULL) dpool = sse_deckpool_create();
4008 if (! sse_deckpool_pop(dpool, &end))
4009 end = sse_alloc_vjd_deck(L, i0, j0, vecwidth);
4010 nends = 0;
4011 for (v = vroot; v <= vend; v++) { if (ocm->sttype[v] == E_st) nends++; }
4012 for (jp = 0; jp <= W; jp++) {
4013 j = i0+jp-1; /* e.g. j runs from 0..L on whole seq */
4014 end->ivec[j][0] = WORDRSHIFTX(neginfv, zerov, 1);
4015 for (d = 1; d <= jp/vecwidth; d++) end->ivec[j][d] = neginfv;
4016 }
4017
4018 /* Make an alpha matrix.
4019 * It's important to allocate for M+1 decks (deck M is for EL, local
4020 * alignment) - even though Inside doesn't need EL, Outside does,
4021 * and we might reuse this memory in a call to Outside.
4022 */
4023 if (alpha == NULL) {
4024 ESL_ALLOC(alpha, sizeof(sse_deck_t *) * (ocm->M+1));
4025 for (v = 0; v <= ocm->M; v++) alpha[v] = NULL;
4026 }
4027
4028 ESL_ALLOC(touch, sizeof(int) * (ocm->M+1));
4029 for (v = 0; v < vroot; v++) touch[v] = 0;
4030 for (v = vroot; v <= vend; v++) touch[v] = ocm->pnum[v];
4031 for (v = vend+1;v < ocm->M; v++) touch[v] = 0;
4032
4033 /* Main recursion
4034 */
4035 for (v = vend; v >= vroot; v--)
4036 {
4037 /* First we need a deck to fill in.
4038 * 1. if we're an E, reuse the end deck (and it's already calculated)
4039 * 2. else, see if we can take something from the pool
4040 * 3. else, allocate a new deck.
4041 */
4042 if (ocm->sttype[v] == E_st) {
4043 alpha[v] = end; continue;
4044 }
4045 if (! sse_deckpool_pop(dpool, &(alpha[v])))
4046 alpha[v] = sse_alloc_vjd_deck(L, i0, j0, vecwidth);
4047
4048 if (ocm->sttype[v] == D_st || ocm->sttype[v] == S_st)
4049 {
4050 for (jp = 0; jp <= W; jp++) {
4051 j = i0-1+jp;
4052 sW = jp/vecwidth;
4053 y = ocm->cfirst[v];
4054 for (dp = 0; dp <= sW; dp++)
4055 {
4056 // alpha[v][j][d] = cm->endsc[v] + (cm->el_selfsc * (d-StateDelta(cm->sttype[v])));
4057 tmpv = _mm_mullo_epi16(el_self_v, _mm_adds_epi16(_mm_set1_epi16(dp*vecwidth), doffset));
4058 mask = _mm_cmpgt_epi16(zerov,tmpv);
4059 tmpv = _mm_and_si128(tmpv,mask);
4060 tmpv = _mm_or_si128(tmpv,_mm_andnot_si128(mask,_mm_set1_epi16(0x8000)));
4061 alpha[v]->ivec[j][dp] = _mm_adds_epi16(_mm_set1_epi16(ocm->endsc[v]), tmpv);
4062 /* treat EL as emitting only on self transition */
4063 }
4064
4065 for (yoffset = 0; yoffset < ocm->cnum[v]; yoffset++) {
4066 tscv = _mm_set1_epi16(ocm->tsc[v][yoffset]);
4067 for (dp = 0; dp <= sW; dp++)
4068 {
4069 tmpv = _mm_adds_epi16(alpha[y+yoffset]->ivec[j][dp], tscv);
4070 alpha[v]->ivec[j][dp] = _mm_max_epi16(alpha[v]->ivec[j][dp], tmpv);
4071 }
4072 }
4073 //FIXME: SSE conversion is kind of ignoring the possibilty of underflow... this is bad.
4074 //if (alpha[v][j][d] < IMPOSSIBLE) alpha[v][j][d] = IMPOSSIBLE;
4075 }
4076 }
4077 else if (ocm->sttype[v] == B_st)
4078 {
4079 __m128i begr_v;
4080 for (jp = 0; jp <= W; jp++) {
4081 j = i0-1+jp;
4082 sW = jp/vecwidth;
4083 y = ocm->cfirst[v];
4084 z = ocm->cnum[v];
4085
4086 /* IMPORTANT NOTE!
4087 * while this function tries to be robust to variations in vecwidth
4088 * the section below is distinctly not, as I have enumerated the cases
4089 * of how d-k hits the vector boundaries
4090 */
4091 begr_v = _mm_set1_epi16(_mm_extract_epi16(alpha[z]->ivec[j][0],0));
4092 for (dp = 0; dp <= sW; dp++)
4093 {
4094 alpha[v]->ivec[j][dp] = _mm_adds_epi16(alpha[y]->ivec[j][dp], begr_v);
4095 }
4096 for (k = 8; k <= jp; k += vecwidth)
4097 {
4098 kp = k/vecwidth;
4099 begr_v = _mm_set1_epi16(_mm_extract_epi16(alpha[z]->ivec[j][kp],0));
4100
4101 tmpv = alpha[y]->ivec[j-k][0];
4102 tmpv = _mm_adds_epi16(tmpv, begr_v);
4103 alpha[v]->ivec[j][kp] = _mm_max_epi16(alpha[v]->ivec[j][kp], tmpv);
4104 for (dp = kp+1; dp <= sW; dp++)
4105 {
4106 tmpv = alpha[y]->ivec[j-k][dp-kp];
4107 tmpv = _mm_adds_epi16(tmpv, begr_v);
4108 alpha[v]->ivec[j][dp] = _mm_max_epi16(alpha[v]->ivec[j][dp], tmpv);
4109 }
4110 }
4111 /* Expanding all the cases just because the shift argument needs to be an immediate */
4112 for (k = 1; k <= jp; k += vecwidth)
4113 {
4114 kp = k/vecwidth;
4115 begr_v = _mm_set1_epi16(_mm_extract_epi16(alpha[z]->ivec[j][kp],1));
4116
4117 tmpv = WORDRSHIFTX(alpha[y]->ivec[j-k][0],neginfv,1);
4118 tmpv = _mm_adds_epi16(tmpv, begr_v);
4119 alpha[v]->ivec[j][kp] = _mm_max_epi16(alpha[v]->ivec[j][kp], tmpv);
4120 for (dp = kp+1; dp <= sW; dp++)
4121 {
4122 tmpv = WORDRSHIFTX(alpha[y]->ivec[j-k][dp-kp], alpha[y]->ivec[j-k][dp-kp-1],1);
4123 tmpv = _mm_adds_epi16(tmpv, begr_v);
4124 alpha[v]->ivec[j][dp] = _mm_max_epi16(alpha[v]->ivec[j][dp], tmpv);
4125 }
4126 }
4127 for (k = 2; k <= jp; k += vecwidth)
4128 {
4129 kp = k/vecwidth;
4130 begr_v = _mm_set1_epi16(_mm_extract_epi16(alpha[z]->ivec[j][kp],2));
4131
4132 tmpv = WORDRSHIFTX(alpha[y]->ivec[j-k][0],neginfv,2);
4133 tmpv = _mm_adds_epi16(tmpv, begr_v);
4134 alpha[v]->ivec[j][kp] = _mm_max_epi16(alpha[v]->ivec[j][kp], tmpv);
4135 for (dp = kp+1; dp <= sW; dp++)
4136 {
4137 tmpv = WORDRSHIFTX(alpha[y]->ivec[j-k][dp-kp], alpha[y]->ivec[j-k][dp-kp-1],2);
4138 tmpv = _mm_adds_epi16(tmpv, begr_v);
4139 alpha[v]->ivec[j][dp] = _mm_max_epi16(alpha[v]->ivec[j][dp], tmpv);
4140 }
4141 }
4142 for (k = 3; k <= jp; k += vecwidth)
4143 {
4144 kp = k/vecwidth;
4145 begr_v = _mm_set1_epi16(_mm_extract_epi16(alpha[z]->ivec[j][kp],3));
4146
4147 tmpv = WORDRSHIFTX(alpha[y]->ivec[j-k][0],neginfv,3);
4148 tmpv = _mm_adds_epi16(tmpv, begr_v);
4149 alpha[v]->ivec[j][kp] = _mm_max_epi16(alpha[v]->ivec[j][kp], tmpv);
4150 for (dp = kp+1; dp <= sW; dp++)
4151 {
4152 tmpv = WORDRSHIFTX(alpha[y]->ivec[j-k][dp-kp], alpha[y]->ivec[j-k][dp-kp-1],3);
4153 tmpv = _mm_adds_epi16(tmpv, begr_v);
4154 alpha[v]->ivec[j][dp] = _mm_max_epi16(alpha[v]->ivec[j][dp], tmpv);
4155 }
4156 }
4157 for (k = 4; k <= jp; k += vecwidth)
4158 {
4159 kp = k/vecwidth;
4160 begr_v = _mm_set1_epi16(_mm_extract_epi16(alpha[z]->ivec[j][kp],4));
4161
4162 tmpv = WORDRSHIFTX(alpha[y]->ivec[j-k][0],neginfv,4);
4163 tmpv = _mm_adds_epi16(tmpv, begr_v);
4164 alpha[v]->ivec[j][kp] = _mm_max_epi16(alpha[v]->ivec[j][kp], tmpv);
4165 for (dp = kp+1; dp <= sW; dp++)
4166 {
4167 tmpv = WORDRSHIFTX(alpha[y]->ivec[j-k][dp-kp], alpha[y]->ivec[j-k][dp-kp-1],4);
4168 tmpv = _mm_adds_epi16(tmpv, begr_v);
4169 alpha[v]->ivec[j][dp] = _mm_max_epi16(alpha[v]->ivec[j][dp], tmpv);
4170 }
4171 }
4172 for (k = 5; k <= jp; k += vecwidth)
4173 {
4174 kp = k/vecwidth;
4175 begr_v = _mm_set1_epi16(_mm_extract_epi16(alpha[z]->ivec[j][kp],5));
4176
4177 tmpv = WORDRSHIFTX(alpha[y]->ivec[j-k][0],neginfv,5);
4178 tmpv = _mm_adds_epi16(tmpv, begr_v);
4179 alpha[v]->ivec[j][kp] = _mm_max_epi16(alpha[v]->ivec[j][kp], tmpv);
4180 for (dp = kp+1; dp <= sW; dp++)
4181 {
4182 tmpv = WORDRSHIFTX(alpha[y]->ivec[j-k][dp-kp], alpha[y]->ivec[j-k][dp-kp-1],5);
4183 tmpv = _mm_adds_epi16(tmpv, begr_v);
4184 alpha[v]->ivec[j][dp] = _mm_max_epi16(alpha[v]->ivec[j][dp], tmpv);
4185 }
4186 }
4187 for (k = 6; k <= jp; k += vecwidth)
4188 {
4189 kp = k/vecwidth;
4190 begr_v = _mm_set1_epi16(_mm_extract_epi16(alpha[z]->ivec[j][kp],6));
4191
4192 tmpv = WORDRSHIFTX(alpha[y]->ivec[j-k][0],neginfv,6);
4193 tmpv = _mm_adds_epi16(tmpv, begr_v);
4194 alpha[v]->ivec[j][kp] = _mm_max_epi16(alpha[v]->ivec[j][kp], tmpv);
4195 for (dp = kp+1; dp <= sW; dp++)
4196 {
4197 tmpv = WORDRSHIFTX(alpha[y]->ivec[j-k][dp-kp], alpha[y]->ivec[j-k][dp-kp-1],6);
4198 tmpv = _mm_adds_epi16(tmpv, begr_v);
4199 alpha[v]->ivec[j][dp] = _mm_max_epi16(alpha[v]->ivec[j][dp], tmpv);
4200 }
4201 }
4202 for (k = 7; k <= jp; k += vecwidth)
4203 {
4204 kp = k/vecwidth;
4205 begr_v = _mm_set1_epi16(_mm_extract_epi16(alpha[z]->ivec[j][kp],7));
4206
4207 tmpv = WORDRSHIFTX(alpha[y]->ivec[j-k][0],neginfv,7);
4208 tmpv = _mm_adds_epi16(tmpv, begr_v);
4209 alpha[v]->ivec[j][kp] = _mm_max_epi16(alpha[v]->ivec[j][kp], tmpv);
4210 for (dp = kp+1; dp <= sW; dp++)
4211 {
4212 tmpv = WORDRSHIFTX(alpha[y]->ivec[j-k][dp-kp], alpha[y]->ivec[j-k][dp-kp-1],7);
4213 tmpv = _mm_adds_epi16(tmpv, begr_v);
4214 alpha[v]->ivec[j][dp] = _mm_max_epi16(alpha[v]->ivec[j][dp], tmpv);
4215 }
4216 }
4217 /*
4218 for (d = 0; d <= sW; d++)
4219 {
4220 for (k = 1; k < (d+1)*vecwidth; k++)
4221 if ((sc = alpha[y][j-k][d-k] + alpha[z][j][k]) > alpha[v][j][d]) {
4222 alpha[v][j][d] = sc;
4223 }
4224 if (alpha[v][j][d] < IMPOSSIBLE) alpha[v][j][d] = IMPOSSIBLE;
4225 }
4226 */
4227 }
4228 }
4229 else if (ocm->sttype[v] == MP_st)
4230 {
4231 for (x = 0; x < ocm->abc->Kp; x++)
4232 {
4233 esc_stale[x] = i0-1;
4234 for (dp = 0; dp <= W/vecwidth; dp ++) { vec_Pesc[x][dp] = neginfv; }
4235 vec_Pesc[x][0] = WORDRSHIFTX(vec_Pesc[x][0], _mm_set1_epi16(ocm->oesc[v][dsq[i0]*ocm->abc->Kp+x]), 1);
4236 }
4237 alpha[v]->ivec[i0-1][0] = neginfv; /* jp = 0 */
4238 for (jp = 1; jp <= W; jp++) {
4239 j = i0-1+jp;
4240 /* slide esc vec array over */
4241 sW = jp/vecwidth;
4242 delta = j>0 ? j - esc_stale[dsq[j]] : 0;
4243 tmpv = _mm_setr_epi16(jp<W ? ocm->oesc[v][dsq[j+1]*ocm->abc->Kp+dsq[j]] : -32768,
4244 jp>0 ? ocm->oesc[v][dsq[j ]*ocm->abc->Kp+dsq[j]] : -32768,
4245 jp>1 ? ocm->oesc[v][dsq[j-1]*ocm->abc->Kp+dsq[j]] : -32768,
4246 jp>2 ? ocm->oesc[v][dsq[j-2]*ocm->abc->Kp+dsq[j]] : -32768,
4247 jp>3 ? ocm->oesc[v][dsq[j-3]*ocm->abc->Kp+dsq[j]] : -32768,
4248 jp>4 ? ocm->oesc[v][dsq[j-4]*ocm->abc->Kp+dsq[j]] : -32768,
4249 jp>5 ? ocm->oesc[v][dsq[j-5]*ocm->abc->Kp+dsq[j]] : -32768,
4250 jp>6 ? ocm->oesc[v][dsq[j-6]*ocm->abc->Kp+dsq[j]] : -32768);
4251 /* Expanding all the cases just because the shift argument needs to be an immediate */
4252 if (delta == 1) {
4253 for (dp = sW; dp > 0; dp--)
4254 { vec_Pesc[dsq[j]][dp] = WORDRSHIFTX(vec_Pesc[dsq[j]][dp], vec_Pesc[dsq[j]][dp-1],1); }
4255 }
4256 if (delta == 2) {
4257 for (dp = sW; dp > 0; dp--)
4258 { vec_Pesc[dsq[j]][dp] = WORDRSHIFTX(vec_Pesc[dsq[j]][dp], vec_Pesc[dsq[j]][dp-1],2); }
4259 }
4260 if (delta == 3) {
4261 for (dp = sW; dp > 0; dp--)
4262 { vec_Pesc[dsq[j]][dp] = WORDRSHIFTX(vec_Pesc[dsq[j]][dp], vec_Pesc[dsq[j]][dp-1],3); }
4263 }
4264 if (delta == 4) {
4265 for (dp = sW; dp > 0; dp--)
4266 { vec_Pesc[dsq[j]][dp] = WORDRSHIFTX(vec_Pesc[dsq[j]][dp], vec_Pesc[dsq[j]][dp-1],4); }
4267 }
4268 if (delta == 5) {
4269 for (dp = sW; dp > 0; dp--)
4270 { vec_Pesc[dsq[j]][dp] = WORDRSHIFTX(vec_Pesc[dsq[j]][dp], vec_Pesc[dsq[j]][dp-1],5); }
4271 }
4272 if (delta == 6) {
4273 for (dp = sW; dp > 0; dp--)
4274 { vec_Pesc[dsq[j]][dp] = WORDRSHIFTX(vec_Pesc[dsq[j]][dp], vec_Pesc[dsq[j]][dp-1],6); }
4275 }
4276 if (delta == 7) {
4277 for (dp = sW; dp > 0; dp--)
4278 { vec_Pesc[dsq[j]][dp] = WORDRSHIFTX(vec_Pesc[dsq[j]][dp], vec_Pesc[dsq[j]][dp-1],7); }
4279 }
4280 else if (delta == vecwidth) {
4281 for (dp = sW; dp > 0; dp--) { vec_Pesc[dsq[j]][dp] = vec_Pesc[dsq[j]][dp-1]; }
4282 }
4283 vec_Pesc[dsq[j]][0] = tmpv;
4284 if (j>0) esc_stale[dsq[j]] = j;
4285
4286 tmpv = (__m128i) esl_sse_rightshift_ps((__m128) _mm_mullo_epi16(el_self_v, doffset), (__m128) neginfv);
4287 alpha[v]->ivec[j][0] = _mm_adds_epi16(_mm_set1_epi16(ocm->endsc[v]), tmpv);
4288 /* treat EL as emitting only on self transition */
4289 y = ocm->cfirst[v];
4290 for (yoffset = 0; yoffset < ocm->cnum[v]; yoffset++) {
4291 tscv = _mm_set1_epi16(ocm->tsc[v][yoffset]);
4292 if (j==0) tmpv = neginfv;
4293 else
4294 tmpv = (__m128i) esl_sse_rightshift_ps((__m128) alpha[y+yoffset]->ivec[j-1][0], (__m128) neginfv);
4295 tmpv = _mm_adds_epi16(tmpv, tscv);
4296 alpha[v]->ivec[j][0] = _mm_max_epi16(alpha[v]->ivec[j][0], tmpv);
4297 }
4298 //not sure this logic is correct... and might not be necessary anyway
4299 //escv = j>0 ? (__m128i) esl_sse_rightshift_ps((__m128) vec_Pesc[dsq[j]][0], (__m128) neginfv) : neginfv;
4300 escv = j>0 ? vec_Pesc[dsq[j]][0] : neginfv;
4301 alpha[v]->ivec[j][0] = _mm_adds_epi16(alpha[v]->ivec[j][0], escv);
4302
4303 for (dp = 1; dp <= sW; dp++)
4304 {
4305 tmpv = _mm_mullo_epi16(el_self_v, _mm_adds_epi16(_mm_set1_epi16(dp*vecwidth-2), doffset));
4306 mask = _mm_cmpgt_epi16(zerov,tmpv);
4307 tmpv = _mm_and_si128(tmpv,mask);
4308 tmpv = _mm_or_si128(tmpv,_mm_andnot_si128(mask,_mm_set1_epi16(0x8000)));
4309 alpha[v]->ivec[j][dp] = _mm_adds_epi16(_mm_set1_epi16(ocm->endsc[v]), tmpv);
4310 /* treat EL as emitting only on self transition */
4311 }
4312
4313 for (yoffset = 0; yoffset < ocm->cnum[v]; yoffset++) {
4314 tscv = _mm_set1_epi16(ocm->tsc[v][yoffset]);
4315 for (dp = 1; dp <= sW; dp++)
4316 {
4317 tmpv = WORDRSHIFTX(alpha[y+yoffset]->ivec[j-1][dp],alpha[y+yoffset]->ivec[j-1][dp-1],2);
4318 tmpv = _mm_adds_epi16(tmpv, tscv);
4319 alpha[v]->ivec[j][dp] = _mm_max_epi16(alpha[v]->ivec[j][dp], tmpv);
4320 }
4321 }
4322
4323 for (dp = 1; dp <= sW; dp++)
4324 {
4325 escv = vec_Pesc[dsq[j]][dp];
4326 alpha[v]->ivec[j][dp] = _mm_adds_epi16(alpha[v]->ivec[j][dp], escv);
4327 }
4328
4329 for (x = 0; x < ocm->abc->Kp; x++)
4330 {
4331 delta = j - esc_stale[x];
4332 if (delta == vecwidth) {
4333 for (dp = sW; dp > 0; dp--) { vec_Pesc[x][dp] = vec_Pesc[x][dp-1]; }
4334 vec_Pesc[x][0] = _mm_setr_epi16(jp<W ? ocm->oesc[v][dsq[j+1]*ocm->abc->Kp+x] : -32768,
4335 ocm->oesc[v][dsq[j ]*ocm->abc->Kp+x],
4336 ocm->oesc[v][dsq[j-1]*ocm->abc->Kp+x],
4337 ocm->oesc[v][dsq[j-2]*ocm->abc->Kp+x],
4338 ocm->oesc[v][dsq[j-3]*ocm->abc->Kp+x],
4339 ocm->oesc[v][dsq[j-4]*ocm->abc->Kp+x],
4340 ocm->oesc[v][dsq[j-5]*ocm->abc->Kp+x],
4341 ocm->oesc[v][dsq[j-6]*ocm->abc->Kp+x]);
4342 esc_stale[x] = j;
4343 }
4344 }
4345 }
4346 }
4347 /* Separate out ML_st from IL_st, since only IL_st has to worry abuot self-transitions */
4348 else if (ocm->sttype[v] == ML_st)
4349 {
4350 /* initialize esc vec array */
4351 vec_Lesc[0] = WORDRSHIFTX(neginfv,_mm_set1_epi16(ocm->oesc[v][dsq[i0]]),1);
4352 for (dp = 1; dp <= W/vecwidth; dp++) { vec_Lesc[dp] = neginfv; }
4353
4354 for (jp = 0; jp <= W; jp++) {
4355 j = i0-1+jp;
4356 tmpv = WORDRSHIFTX(_mm_mullo_epi16(el_self_v, doffset), neginfv, 1);
4357 alpha[v]->ivec[j][0] = _mm_adds_epi16(_mm_set1_epi16(ocm->endsc[v]), tmpv);
4358 /* treat EL as emitting only on self transition */
4359 y = ocm->cfirst[v];
4360 for (yoffset = 0; yoffset < ocm->cnum[v]; yoffset++) {
4361 tscv = _mm_set1_epi16(ocm->tsc[v][yoffset]);
4362 tmpv = WORDRSHIFTX(alpha[y+yoffset]->ivec[j][0], neginfv, 1);
4363 tmpv = _mm_adds_epi16(tmpv, tscv);
4364 alpha[v]->ivec[j][0] = _mm_max_epi16(alpha[v]->ivec[j][0], tmpv);
4365 }
4366 escv = sse_setlw_neginfv(vec_Lesc[0]);
4367 alpha[v]->ivec[j][0] = _mm_adds_epi16(alpha[v]->ivec[j][0], escv);
4368
4369 sW = jp/vecwidth;
4370 for (dp = 1; dp <= sW; dp++)
4371 {
4372 tmpv = _mm_mullo_epi16(el_self_v, _mm_adds_epi16(_mm_set1_epi16(dp*vecwidth - 1), doffset));
4373 mask = _mm_cmpgt_epi16(zerov,tmpv);
4374 tmpv = _mm_and_si128(tmpv,mask);
4375 tmpv = _mm_or_si128(tmpv,_mm_andnot_si128(mask,_mm_set1_epi16(0x8000)));
4376 alpha[v]->ivec[j][dp] = _mm_adds_epi16(_mm_set1_epi16(ocm->endsc[v]), tmpv);
4377 /* treat EL as emitting only on self transition */
4378 }
4379
4380 for (yoffset = 0; yoffset < ocm->cnum[v]; yoffset++) {
4381 tscv = _mm_set1_epi16(ocm->tsc[v][yoffset]);
4382 for (dp = 1; dp <= sW; dp++)
4383 {
4384 tmpv = WORDRSHIFTX(alpha[y+yoffset]->ivec[j][dp], alpha[y+yoffset]->ivec[j][dp-1], 1);
4385 tmpv = _mm_adds_epi16(tmpv, tscv);
4386 alpha[v]->ivec[j][dp] = _mm_max_epi16(alpha[v]->ivec[j][dp], tmpv);
4387 }
4388 }
4389
4390 for (dp = 1; dp <= sW; dp++)
4391 {
4392 escv = vec_Lesc[dp];
4393 alpha[v]->ivec[j][dp] = _mm_adds_epi16(alpha[v]->ivec[j][dp], escv);
4394 }
4395
4396 /* slide esc vec array over by one */
4397 if (sW < W/vecwidth) sW++;
4398 for (dp = sW; dp > 0; dp--) { vec_Lesc[dp] = WORDRSHIFTX(vec_Lesc[dp], vec_Lesc[dp-1], 1); }
4399 vec_Lesc[0] = WORDRSHIFTX(vec_Lesc[0], (jp<W-1) ? _mm_set1_epi16(ocm->oesc[v][dsq[j+2]]) : neginfv, 1);
4400 }
4401 }
4402 /* The self-transition loop on IL_st will need to be completely serialized, since
4403 v and j remain the same with only d varying in a non-striped implementation.
4404 The other possible transitions for IL_st can be treated normally, however. */
4405 else if (ocm->sttype[v] == IL_st)
4406 {
4407 /* initialize esc vec array */
4408 vec_Lesc[0] = WORDRSHIFTX(neginfv,_mm_set1_epi16(ocm->oesc[v][dsq[i0]]),1);
4409 for (dp = 1; dp <= W/vecwidth; dp++) { vec_Lesc[dp] = neginfv; }
4410
4411 for (jp = 0; jp <= W; jp++) {
4412 j = i0-1+jp;
4413 tmpv = WORDRSHIFTX(_mm_mullo_epi16(el_self_v, doffset), neginfv, 1);
4414 alpha[v]->ivec[j][0] = _mm_adds_epi16(_mm_set1_epi16(ocm->endsc[v]), tmpv);
4415 /* treat EL as emitting only on self transition */
4416 y = ocm->cfirst[v];
4417 for (yoffset = 1; yoffset < ocm->cnum[v]; yoffset++) {
4418 tscv = _mm_set1_epi16(ocm->tsc[v][yoffset]);
4419 tmpv = WORDRSHIFTX(alpha[y+yoffset]->ivec[j][0], neginfv, 1);
4420 tmpv = _mm_adds_epi16(tmpv, tscv);
4421 alpha[v]->ivec[j][0] = _mm_max_epi16(alpha[v]->ivec[j][0], tmpv);
4422 }
4423 escv = sse_setlw_neginfv(vec_Lesc[0]);
4424 alpha[v]->ivec[j][0] = _mm_adds_epi16(alpha[v]->ivec[j][0], escv);
4425 /* handle yoffset = 0, the self-transition case, seaparately */
4426 tscv = _mm_set1_epi16(ocm->tsc[v][0]);
4427 for (k = 2; k < vecwidth; k++) {
4428 tmpv = WORDRSHIFTX(alpha[y]->ivec[j][0], neginfv, 1);
4429 tmpv = _mm_adds_epi16(escv, _mm_adds_epi16(tscv, tmpv));
4430 alpha[v]->ivec[j][0] = _mm_max_epi16(alpha[v]->ivec[j][0], tmpv);
4431 /* could make this a do-while on whether any values changed */
4432 }
4433
4434 sW = jp/vecwidth;
4435 for (dp = 1; dp <= sW; dp++)
4436 {
4437 tmpv = _mm_mullo_epi16(el_self_v, _mm_adds_epi16(_mm_set1_epi16(dp*vecwidth - 1), doffset));
4438 mask = _mm_cmpgt_epi16(zerov,tmpv);
4439 tmpv = _mm_and_si128(tmpv,mask);
4440 tmpv = _mm_or_si128(tmpv,_mm_andnot_si128(mask,_mm_set1_epi16(0x8000)));
4441 alpha[v]->ivec[j][dp] = _mm_adds_epi16(_mm_set1_epi16(ocm->endsc[v]), tmpv);
4442 /* treat EL as emitting only on self transition */
4443 }
4444
4445 for (yoffset = 1; yoffset < ocm->cnum[v]; yoffset++) {
4446 tscv = _mm_set1_epi16(ocm->tsc[v][yoffset]);
4447 for (dp = 1; dp <= sW; dp++)
4448 {
4449 tmpv = WORDRSHIFTX(alpha[y+yoffset]->ivec[j][dp], alpha[y+yoffset]->ivec[j][dp-1], 1);
4450 tmpv = _mm_adds_epi16(tmpv, tscv);
4451 alpha[v]->ivec[j][dp] = _mm_max_epi16(alpha[v]->ivec[j][dp], tmpv);
4452 }
4453 }
4454
4455 tscv = _mm_set1_epi16(ocm->tsc[v][0]);
4456 for (dp = 1; dp <= sW; dp++)
4457 {
4458 escv = vec_Lesc[dp];
4459 alpha[v]->ivec[j][dp] = _mm_adds_epi16(alpha[v]->ivec[j][dp], escv);
4460
4461 /* handle yoffset = 0, the self-transition case, seaparately */
4462 for (k = 0; k < vecwidth; k++) {
4463 tmpv = WORDRSHIFTX(alpha[y]->ivec[j][dp], alpha[y]->ivec[j][dp-1], 1);
4464 tmpv = _mm_adds_epi16(escv, _mm_adds_epi16(tscv, tmpv));
4465 alpha[v]->ivec[j][dp] = _mm_max_epi16(alpha[v]->ivec[j][dp], tmpv);
4466 /* could make this a do-while on whether any values changed */
4467 }
4468 }
4469
4470 /* slide esc vec array over by one */
4471 if (sW < W/vecwidth) sW++;
4472 for (dp = sW; dp > 0; dp--) { vec_Lesc[dp] = WORDRSHIFTX(vec_Lesc[dp], vec_Lesc[dp-1], 1); }
4473 vec_Lesc[0] = WORDRSHIFTX(vec_Lesc[0], (jp<W-1) ? _mm_set1_epi16(ocm->oesc[v][dsq[j+2]]) : neginfv, 1);
4474 }
4475 }
4476 else if (ocm->sttype[v] == IR_st || ocm->sttype[v] == MR_st)
4477 {
4478 alpha[v]->ivec[i0-1][0] = neginfv; /* jp = 0 */
4479 for (jp = 1; jp <= W; jp++) {
4480 j = i0-1+jp;
4481 tmpv = WORDRSHIFTX(_mm_mullo_epi16(el_self_v, doffset), neginfv, 1);
4482 alpha[v]->ivec[j][0] = _mm_adds_epi16(_mm_set1_epi16(ocm->endsc[v]), tmpv);
4483 /* treat EL as emitting only on self transition */
4484 y = ocm->cfirst[v];
4485 for (yoffset = 0; yoffset < ocm->cnum[v]; yoffset++) {
4486 tscv = _mm_set1_epi16(ocm->tsc[v][yoffset]);
4487 if (j==0) tmpv = neginfv;
4488 else
4489 tmpv = WORDRSHIFTX(alpha[y+yoffset]->ivec[j-1][0], neginfv, 1);
4490 tmpv = _mm_adds_epi16(tmpv, tscv);
4491 alpha[v]->ivec[j][0] = _mm_max_epi16(alpha[v]->ivec[j][0], tmpv);
4492 }
4493 if (j==0) escv = neginfv;
4494 else
4495 escv = sse_setlw_neginfv(_mm_set1_epi16(ocm->oesc[v][dsq[j]]));
4496 alpha[v]->ivec[j][0] = _mm_adds_epi16(alpha[v]->ivec[j][0], escv);
4497
4498 sW = jp/vecwidth;
4499 if (j==0) escv = neginfv;
4500 else
4501 escv = _mm_set1_epi16(ocm->oesc[v][dsq[j]]);
4502
4503 for (dp = 1; dp <= sW; dp++)
4504 {
4505 tmpv = _mm_mullo_epi16(el_self_v, _mm_adds_epi16(_mm_set1_epi16(dp*vecwidth - 1), doffset));
4506 mask = _mm_cmpgt_epi16(zerov,tmpv);
4507 tmpv = _mm_and_si128(tmpv,mask);
4508 tmpv = _mm_or_si128(tmpv,_mm_andnot_si128(mask,_mm_set1_epi16(0x8000)));
4509 alpha[v]->ivec[j][dp] = _mm_adds_epi16(_mm_set1_epi16(ocm->endsc[v]), tmpv);
4510 /* treat EL as emitting only on self transition */
4511 }
4512
4513 for (yoffset = 0; yoffset < ocm->cnum[v]; yoffset++) {
4514 tscv = _mm_set1_epi16(ocm->tsc[v][yoffset]);
4515 for (dp = 1; dp <= sW; dp++)
4516 {
4517 tmpv = WORDRSHIFTX(alpha[y+yoffset]->ivec[j-1][dp], alpha[y+yoffset]->ivec[j-1][dp-1], 1);
4518 tmpv = _mm_adds_epi16(tmpv, tscv);
4519 alpha[v]->ivec[j][dp] = _mm_max_epi16(alpha[v]->ivec[j][dp], tmpv);
4520 }
4521
4522 alpha[v]->ivec[j][dp] = _mm_adds_epi16(alpha[v]->ivec[j][dp], escv);
4523 }
4524 }
4525 } /* finished calculating deck v. */
4526 /*
4527 int16_t tmpsc;
4528 for (jp = 1; jp <= W; jp++) {
4529 j = i0-1+jp;
4530 fprintf(stderr,"v%3d j%3d ",v,j);
4531 for (d=0; d<=jp; d++) {
4532 tmpsc = *((int16_t *) &alpha[v]->ivec[j][d/vecwidth]+d%vecwidth);
4533 tmpsc == -32768 ? fprintf(stderr,"%7.2f ",-eslINFINITY) : fprintf(stderr,"%7.2f ",tmpsc/500.0);
4534 }
4535 fprintf(stderr,"\n");
4536 }
4537 */
4538 #ifndef LOBAL_MODE
4539 //FIXME: whether you store the (j,d) that corresponds to the best score depends on
4540 //FIXME: whether you want tight alignment bounds for the next step in the pipeline
4541 //FIXME: (if so, could also report v, to reduce number of states if it is a small
4542 //FIXME: portion relative to the overall model
4543 //FIXME: On the other hand, saturation may counter-indicate this, as later alignments
4544 //FIXME: comprising larger parts of the model and larger sequence windows will not
4545 //FIXME: be able to exceed previously seen scores
4546 tmp_v = _mm_set1_epi16((int16_t)v);
4547 for (jp = 0; jp <= W; jp++) {
4548 j = i0-1+jp;
4549 //if(v<8 && j==26) fprintf(stderr,"v = %d j = %d beginsc %e %f\n",v,j,cm->beginsc[v],(float)ocm->beginsc[v]/ocm->scale_w);
4550 tmp_j = _mm_set1_epi16((int16_t)jp);
4551 sW = jp/vecwidth;
4552 for (dp = 0; dp <= sW; dp++) {
4553 tmp_d = _mm_adds_epi16(_mm_set1_epi16((int16_t)dp*vecwidth), doffset);
4554 //tmpv = _mm_adds_epi16(alpha[v]->ivec[j][dp], vec_unaligned_sc[dp]);
4555 //FIXME: Testing whether skipping unaligned_sc helps with coordinates returned here
4556 tmpv = alpha[v]->ivec[j][dp];
4557 /*
4558 if(v<8 && j==26) {
4559 fprintf(stderr,"dp = %d\n",dp);
4560 fprintf(stderr,"\talpha "); vecprint_epi16(ocm, alpha[v]->ivec[j][dp]); fprintf(stderr,"\n");
4561 fprintf(stderr,"\tunaln "); vecprint_epi16(ocm, vec_unaligned_sc[dp]); fprintf(stderr,"\n");
4562 fprintf(stderr,"\ttmpv1 "); vecprint_epi16(ocm, tmpv); fprintf(stderr,"\n");
4563 }
4564 if(v<8 && j==26) {
4565 fprintf(stderr,"\ttmpv2 "); vecprint_epi16(ocm, tmpv); fprintf(stderr,"\n");
4566 }
4567 */
4568 //FIXME: what is ocm->beginsc[0] set to - does this cover our
4569 //FIXME: root deck, or do we need to handle that separately?
4570 tmpv = _mm_adds_epi16(tmpv, _mm_set1_epi16(ocm->beginsc[v]));
4571 mask = _mm_cmpgt_epi16(tmpv, vb_sc);
4572 //mask = _mm_or_si128(mask,_mm_cmpeq_epi16(tmpv,vb_sc)); //FIXME: Test - break ties in favor of new values
4573 vb_sc = _mm_max_epi16(tmpv, vb_sc);
4574 vb_j = sse_select_si128(vb_j, tmp_j, mask);
4575 vb_d = sse_select_si128(vb_d, tmp_d, mask);
4576 vb_v = sse_select_si128(vb_v, tmp_v, mask);
4577 }
4578 }
4579 //if(v<8) { fprintf(stderr,"v = %d ",v); vecprint_epi16(ocm, vb_sc); fprintf(stderr,"\n"); }
4580 #endif
4581
4582 /* Check for local begin getting us to the root.
4583 */
4584 vec_access = (int16_t *) (&alpha[v]->ivec[j0][W/vecwidth]);
4585 tmp = *(vec_access + W%vecwidth);
4586 if (allow_begin && tmp + ocm->beginsc[v] > bsc)
4587 {
4588 b = v;
4589 bsc = tmp + ocm->beginsc[v];
4590 }
4591
4592 /* Reuse memory:
4593 * Look at our children; if they're fully released, take their deck
4594 * into the pool for reuse.
4595 */
4596 if (ocm->sttype[v] == B_st)
4597 { /* we can definitely release the S children of a bifurc. */
4598 y = ocm->cfirst[v]; sse_deckpool_push(dpool, alpha[y]); alpha[y] = NULL;
4599 z = ocm->cnum[v]; sse_deckpool_push(dpool, alpha[z]); alpha[z] = NULL;
4600 }
4601 else
4602 {
4603 for (y = ocm->cfirst[v]; y < ocm->cfirst[v]+ocm->cnum[v]; y++)
4604 {
4605 touch[y]--;
4606 if (touch[y] == 0)
4607 {
4608 if (ocm->sttype[y] == E_st) {
4609 nends--;
4610 if (nends == 0) { sse_deckpool_push(dpool, end); end = NULL;}
4611 } else
4612 sse_deckpool_push(dpool, alpha[y]);
4613 alpha[y] = NULL;
4614 }
4615 }
4616 }
4617 } /* end loop over all v */
4618
4619 /* debug_print_alpha(alpha, cm, L);*/
4620
4621 /* Now we free our memory.
4622 * Only vroot deck is valid now and all others vroot+1..vend are NULL,
4623 * and end is NULL.
4624 * We could check this status to be sure (and we used to) but now we trust.
4625 */
4626 vec_access = (int16_t *) (&alpha[vroot]->ivec[j0][W/vecwidth]);
4627 sc = *(vec_access + W%vecwidth);
4628 #ifndef LOBAL_MODE
4629 sc = esl_sse_hmax_epi16(vb_sc);
4630 if (ret_coord != NULL) {
4631 /* Narrow return coordinates to those that match highest score */
4632 ret_coord->score = sc;
4633 mask = _mm_cmpeq_epi16(_mm_set1_epi16(sc),vb_sc);
4634 vb_d = _mm_and_si128(vb_d,mask);
4635 vb_j = _mm_and_si128(vb_j,mask);
4636 vb_v = _mm_and_si128(vb_v,mask);
4637
4638 /* Break ties in favor of longer segments */
4639 ret_coord->d = esl_sse_hmax_epi16(vb_d);
4640 mask = _mm_cmpeq_epi16(_mm_set1_epi16(ret_coord->d),vb_d);
4641 vb_j = _mm_and_si128(vb_j,mask);
4642 vb_v = _mm_and_si128(vb_v,mask);
4643
4644 /* Break ties in favor of larger v (smaller model segments) */
4645 ret_coord->v = esl_sse_hmax_epi16(vb_v);
4646 mask = _mm_cmpeq_epi16(_mm_set1_epi16(ret_coord->v),vb_v);
4647 vb_j = _mm_and_si128(vb_j,mask);
4648
4649 /* Break (remaining?!) ties in favor of larger j */
4650 ret_coord->j = i0-1+esl_sse_hmax_epi16(vb_j);
4651 }
4652 #endif
4653 if (ret_b != NULL) *ret_b = b; /* b is -1 if allow_begin is FALSE. */
4654 if (ret_bsc != NULL) *ret_bsc = bsc; /* bsc is IMPOSSIBLE if allow_begin is FALSE */
4655
4656 /* Free alpha matrix (saving the decks in the pool)
4657 */
4658 for (v = vroot; v <= vend; v++) /* be careful of our reuse of the end deck -- free it only once */
4659 if (alpha[v] != NULL) {
4660 if (ocm->sttype[v] != E_st) { sse_deckpool_push(dpool, alpha[v]); alpha[v] = NULL; }
4661 else end = alpha[v];
4662 }
4663 if (end != NULL) { sse_deckpool_push(dpool, end); end = NULL; }
4664 free(alpha);
4665
4666 /* Free deck pool.
4667 */
4668 while (sse_deckpool_pop(dpool, &end)) sse_free_vjd_deck(end);
4669 sse_deckpool_free(dpool);
4670
4671 #ifndef LOBAL_MODE
4672 free(mem_unaligned_sc);
4673 #endif
4674
4675 free(esc_stale);
4676 free(vec_Pesc);
4677 free(mem_Pesc);
4678 free(mem_Lesc);
4679
4680 free(touch);
4681 return sc;
4682
4683 ERROR:
4684 cm_Fail("Memory allocation error.\n");
4685 return 0.; /* never reached */
4686 }
4687
4688 /*****************************************************************
4689 * The traceback routines
4690 * insideT - run inside(), append trace in postorder traversal
4691 * vinsideT - run vinside(), append trace in postorder traversal
4692 *****************************************************************/
4693
4694 /* Function: sse_insideT()
4695 * Date: DLK, 2009 Apr 10
4696 *
4697 * Purpose: Call inside, get vjd shadow matrix;
4698 * then trace back. Append the trace to a given
4699 * traceback, which already has state r at tr->n-1.
4700 *
4701 * NOTES: Accepts the vectorized traceback matrix from
4702 * sse_inside(), but is essentially a scalar function
4703 * (by design - don't need to track traceback from cells
4704 * that aren't on the main path)
4705 */
4706 static float
4707 sse_insideT(CM_t *cm, ESL_DSQ *dsq, int L, Parsetree_t *tr,
4708 int r, int z, int i0, int j0, int allow_begin)
4709 {
4710 int status;
4711 sse_deck_t **shadow; /* the traceback shadow matrix */
4712 float sc; /* the score of the CYK alignment */
4713 ESL_STACK *pda; /* stack that tracks bifurc parent of a right start */
4714 int v,j,d,i; /* indices for state, j, subseq len */
4715 int k;
4716 int y, yoffset;
4717 int bifparent;
4718 int b;
4719 float bsc;
4720 int vecwidth = 4;
4721
4722 sc = sse_inside(cm, dsq, L, r, z, i0, j0,
4723 BE_EFFICIENT, /* memory-saving mode */
4724 NULL, NULL, /* manage your own matrix, I don't want it */
4725 NULL, NULL, /* manage your own deckpool, I don't want it */
4726 &shadow, /* return a shadow matrix to me. */
4727 allow_begin, /* TRUE to allow local begins */
4728 &b, &bsc); /* if allow_begin is TRUE, gives info on optimal b */
4729
4730 pda = esl_stack_ICreate();
4731 if(pda == NULL) goto ERROR;
4732 v = r;
4733 j = j0;
4734 i = i0;
4735 d = j0-i0+1;
4736
4737 /*printf("Starting traceback in insideT()\n");*/
4738 while (1) {
4739 if (cm->sttype[v] == B_st) {
4740 //k = ((int **) shadow[v])[j][d]; /* k = len of right fragment */
4741 k = *((int *) &(shadow[v]->vec[j][d/vecwidth]) + d%vecwidth);
4742
4743 /* Store info about the right fragment that we'll retrieve later:
4744 */
4745 if((status = esl_stack_IPush(pda, j)) != eslOK) goto ERROR; /* remember the end j */
4746 if((status = esl_stack_IPush(pda, k)) != eslOK) goto ERROR; /* remember the subseq length k */
4747 if((status = esl_stack_IPush(pda, tr->n-1)) != eslOK) goto ERROR; /* remember the trace index of the parent B state */
4748
4749 /* Deal with attaching left start state.
4750 */
4751 j = j-k;
4752 d = d-k;
4753 i = j-d+1;
4754 y = cm->cfirst[v];
4755 InsertTraceNode(tr, tr->n-1, TRACE_LEFT_CHILD, i, j, y);
4756 v = y;
4757 } else if (cm->sttype[v] == E_st || cm->sttype[v] == EL_st) {
4758 /* We don't trace back from an E or EL. Instead, we're done with the
4759 * left branch of the tree, and we try to swing over to the right
4760 * branch by popping a right start off the stack and attaching
4761 * it. If the stack is empty, then we're done with the
4762 * traceback altogether. This is the only way to break the
4763 * while (1) loop.
4764 */
4765 if (esl_stack_IPop(pda, &bifparent) == eslEOD) break;
4766 esl_stack_IPop(pda, &d);
4767 esl_stack_IPop(pda, &j);
4768 v = tr->state[bifparent]; /* recover state index of B */
4769 y = cm->cnum[v]; /* find state index of right S */
4770 i = j-d+1;
4771 /* attach the S to the right */
4772 InsertTraceNode(tr, bifparent, TRACE_RIGHT_CHILD, i, j, y);
4773 v = y;
4774 } else {
4775 yoffset = *((int *) &(shadow[v]->vec[j][d/vecwidth]) + d%vecwidth);
4776
4777
4778 /*printf("v : %d | r : %d | z : %d | i0 : %d | \n", v, r, z, i0);*/
4779 /*printf("\tyoffset : %d\n", yoffset);*/
4780 switch (cm->sttype[v]) {
4781 case D_st: break;
4782 case MP_st: i++; j--; break;
4783 case ML_st: i++; break;
4784 case MR_st: j--; break;
4785 case IL_st: i++; break;
4786 case IR_st: j--; break;
4787 case S_st: break;
4788 default: cm_Fail("'Inconceivable!'\n'You keep using that word...'");
4789 }
4790 d = j-i+1;
4791
4792 if (yoffset == USED_EL)
4793 { /* a local alignment end */
4794 InsertTraceNode(tr, tr->n-1, TRACE_LEFT_CHILD, i, j, cm->M);
4795 v = cm->M; /* now we're in EL. */
4796 }
4797 else if (yoffset == USED_LOCAL_BEGIN)
4798 { /* local begin; can only happen once, from root */
4799 InsertTraceNode(tr, tr->n-1, TRACE_LEFT_CHILD, i, j, b);
4800 v = b;
4801 }
4802 else
4803 {
4804 y = cm->cfirst[v] + yoffset;
4805 InsertTraceNode(tr, tr->n-1, TRACE_LEFT_CHILD, i, j, y);
4806 v = y;
4807 }
4808 }
4809 }
4810 esl_stack_Destroy(pda); /* it should be empty; we could check; naaah. */
4811 //free_vjd_shadow_matrix(shadow, cm, i0, j0);
4812 sse_free_vjd_matrix(shadow, cm->M-1);
4813 return sc;
4814
4815 ERROR:
4816 cm_Fail("Memory allocation error.");
4817 return 0.; /* NEVERREACHED */
4818 }
4819
4820 /* Function: vinsideT()
4821 * Date: SRE, Sat Jun 2 14:40:13 2001 [St. Louis]
4822 *
4823 * Purpose: Call vinside(), get vji shadow matrix for a V problem;
4824 * then trace back. Append the trace to a
4825 * given traceback, which has state r already at
4826 * t->n-1.
4827 */
4828 static float
4829 sse_vinsideT(CM_t *cm, ESL_DSQ *dsq, int L, Parsetree_t *tr,
4830 int r, int z, int i0, int i1, int j1, int j0, int useEL, int allow_begin)
4831 {
4832 sse_deck_t **shadow;
4833 float sc;
4834 int v,y;
4835 int j,i;
4836 int jp,ip;
4837 int yoffset;
4838 int b;
4839 float bsc;
4840 int *vec_access;
4841 const int vecwidth = 4;
4842
4843 /* If we can deduce the traceback unambiguously without
4844 * doing any DP... do it.
4845 */
4846 if (r == z) {
4847 InsertTraceNode(tr, tr->n-1, TRACE_LEFT_CHILD, i0, j0, r);
4848 return 0.;
4849 }
4850
4851 sc = sse_vinside(cm, dsq, L, r, z, i0, i1, j1, j0, useEL,
4852 BE_EFFICIENT, /* memory-saving mode */
4853 NULL, NULL, /* manage your own matrix, I don't want it */
4854 NULL, NULL, /* manage your own deckpool, I don't want it */
4855 &shadow, /* return a shadow matrix to me. */
4856 allow_begin, /* TRUE to allow local begin transitions */
4857 &b, &bsc); /* info on optimal local begin */
4858 /* We've got a complete shadow matrix. Trace it back. We know
4859 * that the trace will begin with the start state r, at i0,j0
4860 * (e.g. jp=j0-j1, ip=0)
4861 */
4862 v = r;
4863 j = j0;
4864 i = i0;
4865
4866 /*printf("Starting traceback in vinsideT()\n");*/
4867 while (1) {
4868 jp = j-j1;
4869 ip = i-i0;
4870
4871 /* 1. figure out the next state (deck) in the shadow matrix.
4872 */
4873 /*printf("v : %d | jp : %d | ip : %d | i0 : %d | \n", v, jp, ip, i0);*/
4874 vec_access = (int *) &(shadow[v]->vec[jp][ip/vecwidth]) + ip%vecwidth;
4875 yoffset = *vec_access;
4876 /*printf("\tyoffset : %d\n", yoffset);*/
4877
4878 /* 2. figure out the i,j for state y, which is dependent
4879 * on what v emits (if anything)
4880 */
4881 switch (cm->sttype[v]) {
4882 case D_st: break;
4883 case MP_st: i++; j--; break;
4884 case ML_st: i++; break;
4885 case MR_st: j--; break;
4886 case IL_st: i++; break;
4887 case IR_st: j--; break;
4888 case S_st: break;
4889 default: cm_Fail("'Inconceivable!'\n'You keep using that word...'");
4890 }
4891
4892 /* If the traceback pointer (yoffset) is -1, that's a special
4893 * flag for a local alignment end, e.g. transition to EL (state "M").
4894 */
4895 if (yoffset == USED_EL)
4896 {
4897 InsertTraceNode(tr, tr->n-1, TRACE_LEFT_CHILD, i, j, cm->M);
4898 break; /* one way out of the while loop */
4899 }
4900 else if (yoffset == USED_LOCAL_BEGIN)
4901 {
4902 InsertTraceNode(tr, tr->n-1, TRACE_LEFT_CHILD, i, j, b);
4903 v = b;
4904 if (! useEL && v == z) break; /* the other way out of the while loop */
4905 }
4906 else
4907 {
4908 /* Attach y,i,j to the trace. This new node always attaches
4909 * to the end of the growing trace -- e.g. trace node
4910 * tr->n-1.
4911 */
4912 y = cm->cfirst[v] + yoffset;
4913 InsertTraceNode(tr, tr->n-1, TRACE_LEFT_CHILD, i, j, y);
4914 v = y;
4915 if (! useEL && v == z) break; /* the other way out of the while loop */
4916 }
4917 }
4918
4919 /* We're done. Our traceback has just ended. We have just attached
4920 * state z for i1,j1; it is in the traceback at node tr->n-1.
4921 */
4922 sse_free_vji_matrix(shadow, cm->M-1);
4923 return sc;
4924 }
4925
4926
4927 /*****************************************************************
4928 * The size calculators:
4929 * insideT_size() - Mb required by insideT
4930 * vinsideT_size() - Mb required by vinsideT
4931 *****************************************************************/
4932
4933 /* Function: insideT_size()
4934 * Date: SRE, Sun Jun 3 17:56:08 2001 [St. Louis]
4935 *
4936 * Purpose: Calculate the # of Mb required to run insideT()
4937 * and solve a generic or wedge problem without any
4938 * more divide/conquer.
4939 */
4940 float
4941 sse_insideT_size(CM_t *cm, int L, int r, int z, int i0, int j0, int x)
4942 {
4943 float Mb;
4944 int maxdecks;
4945 /*int nends;*/
4946 /*int nbif;*/
4947
4948 /*nends = CMSegmentCountStatetype(cm, r, z, E_st);*/
4949 /*nbif = CMSegmentCountStatetype(cm, r, z, B_st);*/
4950 maxdecks = cyk_deck_count(cm, r, z);
4951
4952 Mb = (float) (sizeof(sse_deck_t *) * cm->M) / 1000000.; /* the score matrix */
4953 Mb += (float) maxdecks * sse_size_vjd_deck(L, i0, j0, x);
4954 Mb += (float) (sizeof(int) * cm->M) / 1000000.; /* the touch array */
4955
4956 Mb += (float) (sizeof(sse_deck_t *) * cm->M) / 1000000.; /* shadow matrix */
4957 Mb += (float) (z-r+1) * sse_size_vjd_deck(L, i0, j0, x);
4958
4959 return Mb;
4960 }
4961
4962 float
4963 sse_vinsideT_size(CM_t *cm, int r, int z, int i0, int i1, int j1, int j0, int x)
4964 {
4965 float Mb;
4966 int maxdecks;
4967
4968 Mb = (float) (sizeof(float **) * cm->M) / 1000000.;
4969 maxdecks = cyk_deck_count(cm, r, z);
4970 Mb += maxdecks * sse_size_vji_deck(i0,i1,j1,j0,x);
4971 Mb += (float)(z-r) * sse_size_vji_deck(i0,i1,j1,j0,x);
4972 return Mb;
4973 }
4974
4975 /* Function: cyk_deck_count()
4976 * Date: SRE, Sun Jun 3 20:05:18 2001 [St. Louis]
4977 *
4978 * Purpose: calculate and return the maximum number of
4979 * decks that would be required in memory to
4980 * solve an alignment problem involving a CM
4981 * subgraph from r..z.
4982 *
4983 * For a whole model, except for trivially small models with no
4984 * stacked base pairs, this is almost invariably
4985 * 10+1+cyk_extra_decks(): MATP-MATP connections require
4986 * 10 decks (6 states in current node, 4 states in connected
4987 * split set of next node). We share 1 end state deck. All
4988 * other decks are retained S decks, needed for bifurcation
4989 * calculations.
4990 */
4991 static int
4992 cyk_deck_count(CM_t *cm, int r, int z)
4993 {
4994 int status;
4995 ESL_STACK *pda; /* pushdown stack simulating the deck pool */
4996 int v,w,y; /* state indices */
4997 int nends;
4998 int ndecks;
4999 int *touch; /* keeps track of how many higher decks still need this deck */
5000
5001 /* Initializations, mirroring key parts of CYKInside()
5002 */
5003 ndecks = 1; /* deck z, which we always need to start with. */
5004 nends = CMSegmentCountStatetype(cm, r, z, E_st);
5005 pda = esl_stack_ICreate();
5006 if(pda == NULL) goto ERROR;
5007
5008 ESL_ALLOC(touch, sizeof(int) * cm->M);
5009 for (v = 0; v < r; v++) touch[v] = 0;
5010 for (v = r; v < z; v++) touch[v] = cm->pnum[v];
5011 for (v = z; v < cm->M; v++) touch[v] = 0;
5012
5013 for (v = z; v >= r; v--)
5014 {
5015 if (cm->sttype[v] != E_st) {
5016 if (esl_stack_IPop(pda, &y) == eslEOD) ndecks++; /* simulated allocation of a new deck */
5017 }
5018
5019 if (cm->sttype[v] == B_st) { /* release both S children of a bifurc */
5020 w = cm->cfirst[v];
5021 y = cm->cnum[v];
5022 if((status =esl_stack_IPush(pda, w)) != eslOK) goto ERROR;
5023 if((status = esl_stack_IPush(pda, y)) != eslOK) goto ERROR;
5024 } else {
5025 for (w = cm->cfirst[v]; w < cm->cfirst[v]+cm->cnum[v]; w++)
5026 {
5027 touch[w]--;
5028 if (touch[w] == 0)
5029 {
5030 if (cm->sttype[w] == E_st) {
5031 nends--;
5032 if (nends == 0) { if((status = esl_stack_IPush(pda, cm->M-1)) != eslOK) goto ERROR; }
5033 } else
5034 if((status = esl_stack_IPush(pda, w)) != eslOK) goto ERROR;
5035 }
5036 }
5037 }
5038 }
5039 free(touch);
5040 esl_stack_Destroy(pda);
5041 return ndecks;
5042
5043 ERROR:
5044 cm_Fail("Memory allocation error.\n");
5045 return 0; /* never reached */
5046 }
5047
5048 /* Function: cyk_extra_decks()
5049 * Date: SRE, Sun Apr 7 14:42:48 2002 [St. Louis]
5050 *
5051 * Purpose: Calculate the number of extra
5052 * decks that will be needed to accommodate bifurc
5053 * calculations.
5054 *
5055 * Args: cm - the model.
5056 *
5057 * Returns: # of extra decks.
5058 */
5059 static int
5060 cyk_extra_decks(CM_t *cm)
5061 {
5062 int max;
5063 int x;
5064 int v;
5065
5066 max = x = 0;
5067 for (v = cm->M-1; v >= 0; v--)
5068 {
5069 if (cm->sttype[v] == S_st) x++;
5070 else if (cm->sttype[v] == B_st) x-=2;
5071 if (x > max) max = x;
5072 }
5073 return max-1; /* discount ROOT S */
5074 }
5075
5076 /*################################################################
5077 * The memory management routines.
5078 ################################################################*/
5079
5080 /*################################################################*/
5081 /* Functions: deckpool_*()
5082 * Date: SRE, Wed Aug 2 10:43:17 2000 [St. Louis]
5083 *
5084 * Purpose: Implementation of a pushdown stack for storing decks
5085 * of the inside or outside dynamic programming matrices, with the
5086 * usual _create, _push, _pop, and _free API.
5087 *
5088 * The deck pool allows us to efficiently reuse memory,
5089 * so long as our DP algorithms step through the decks
5090 * as their outermost loop.
5091 *
5092 * Works for either coordinate system (vjd or vji)
5093 * and subseq variants, because it's simply managing
5094 * a deck as a float **.
5095 */
5096 struct sse_deckpool_s *
5097 sse_deckpool_create(void)
5098 {
5099 int status;
5100 struct sse_deckpool_s *dpool;
5101
5102 ESL_ALLOC(dpool, sizeof(struct sse_deckpool_s));
5103 dpool->block = 10; /* configurable if you want */
5104 ESL_ALLOC(dpool->pool, sizeof(sse_deck_t *) * dpool->block);
5105 dpool->nalloc = dpool->block;;
5106 dpool->n = 0;
5107 return dpool;
5108 ERROR:
5109 cm_Fail("Memory allocation error.\n");
5110 return NULL; /* never reached */
5111 }
5112 void
5113 sse_deckpool_push(struct sse_deckpool_s *dpool, sse_deck_t *deck)
5114 {
5115 int status;
5116 void *tmp;
5117 if (dpool->n == dpool->nalloc) {
5118 dpool->nalloc += dpool->block;
5119 ESL_RALLOC(dpool->pool, tmp, sizeof(sse_deck_t *) * dpool->nalloc);
5120 }
5121 dpool->pool[dpool->n] = deck;
5122 dpool->n++;
5123 ESL_DPRINTF3(("sse_deckpool_push\n"));
5124 return;
5125 ERROR:
5126 cm_Fail("Memory reallocation error.\n");
5127 }
5128 int
5129 sse_deckpool_pop(struct sse_deckpool_s *d, sse_deck_t **ret_deck)
5130 {
5131 if (d->n == 0) { *ret_deck = NULL; return 0;}
5132 d->n--;
5133 *ret_deck = d->pool[d->n];
5134 ESL_DPRINTF3(("sse_deckpool_pop\n"));
5135 return 1;
5136 }
5137 void
5138 sse_deckpool_free(struct sse_deckpool_s *d)
5139 {
5140 free(d->pool);
5141 free(d);
5142 }
5143 /*================================================================*/
5144
5145
5146 /*################################################################*/
5147 /* Functions: *_vjd_*
5148 * Date: SRE, Sat Aug 12 16:27:37 2000 [Titusville]
5149 *
5150 * Purpose: Allocation and freeing of 3D matrices and 2D decks
5151 * in the vjd coord system. These can be called on
5152 * subsequences i..j, not just the full sequence 1..L,
5153 * so they need i,j... if you're doing the full sequence
5154 * just pass 1,L.
5155 *
5156 * Also deal with shadow matrices and shadow decks in the
5157 * vjd coordinate system. Note that bifurcation shadow decks
5158 * need more dynamic range than other shadow decks, hence
5159 * a separation into "kshadow" (BIFURC) and "yshadow" (other
5160 * states) decks, and some casting shenanigans in
5161 * a full ***shadow matrix.
5162 *
5163 * Values in yshad are offsets to the next connected state,
5164 * or a flag for local alignment. Possible offsets range from
5165 * 0..5 (maximum of 6 connected states). The flags are
5166 * USED_LOCAL_BEGIN (101) and USED_EL (102), defined at
5167 * the top of this file. Only yshad[0][L][L] (e.g. root state 0,
5168 * aligned to the whole sequence) may be set to USED_LOCAL_BEGIN.
5169 * (Remember that the dynamic range of yshad, as a char, is
5170 * 0..127, in ANSI C; we don't know if a machine will make it
5171 * signed or unsigned.)
5172 *
5173 * Args: L total sequence length
5174 * i,j active sequence range
5175 * x vector width (e.g., 4 for 4x32bit floats)
5176 *
5177 * Returns: mem direct memory allocation, unaligned
5178 * vec 'actual' deck, aligned on 128-bit boundaries
5179 */
5180 sse_deck_t*
5181 sse_alloc_vjd_deck(int L, int i, int j, int x)
5182 {
5183 int status;
5184 int jp;
5185 int sW;
5186 int vecs = 0;
5187 sse_deck_t *tmp;
5188 ESL_DPRINTF3(("alloc_vjd_deck : %.4f\n", size_vjd_deck(L,i,j)));
5189 ESL_ALLOC(tmp, sizeof(sse_deck_t));
5190 ESL_ALLOC(tmp->vec, sizeof(__m128 *) * (L+1)); /* always alloc 0..L rows, some of which are NULL */
5191 ESL_ALLOC(tmp->ivec, sizeof(__m128i *) * (L+1)); /* always alloc 0..L rows, some of which are NULL */
5192 for (jp = 0; jp <= j-i+1; jp++) {
5193 sW = jp/x + 1; /* integer division on purpose */
5194 vecs += sW;
5195 }
5196 ESL_ALLOC(tmp->mem , sizeof(__m128 ) * vecs + 15);
5197 for (jp = 0; jp < i-1; jp++) tmp->vec[jp] = NULL;
5198 for (jp = j+1; jp <= L; jp++) tmp->vec[jp] = NULL;
5199 tmp->vec[i-1] = (__m128 *) (((unsigned long int) tmp->mem + 15) & (~0xf));
5200 for (jp = 1; jp <= j-i+1; jp++) {
5201 sW = (jp-1)/x + 1;
5202 tmp->vec[jp+i-1] = tmp->vec[jp+i-2] + sW;
5203 }
5204 for (jp = 0; jp <=L; jp++) { tmp->ivec[jp] = (__m128i *) tmp->vec[jp]; }
5205 return tmp;
5206 ERROR:
5207 cm_Fail("Memory allocation error.");
5208 return NULL; /* never reached */
5209 }
5210 float
5211 sse_size_vjd_deck(int L, int i, int j, int x)
5212 {
5213 float Mb;
5214 int jp;
5215 Mb = (float) (sizeof(sse_deck_t));
5216 Mb += (float) (sizeof(__m128 *) * (L+1));
5217 Mb += (float) (sizeof(__m128i *) * (L+1));
5218 for (jp = 0; jp <= j-i+1; jp++)
5219 Mb += (float) (sizeof(__m128) * (jp/x + 1));
5220 return ((Mb+15) / 1000000.);
5221 }
5222 void
5223 sse_free_vjd_deck(sse_deck_t *a)
5224 {
5225 free(a->ivec);
5226 free(a->vec);
5227 free(a->mem);
5228 free(a);
5229 }
5230 void
5231 sse_free_vjd_matrix(sse_deck_t **a, int M)
5232 {
5233 int v;
5234 for (v = 0; v <= M; v++)
5235 if (a[v] != NULL) /* protect against double free's of reused decks (ends) */
5236 { sse_free_vjd_deck(a[v]); a[v] = NULL; }
5237 free(a);
5238 }
5239 char **
5240 sse_alloc_vjd_yshadow_deck(int L, int i, int j)
5241 {
5242 fprintf(stderr,"WARNING! sse_alloc_vjd_yshadow_deck has not been converted to SSE!\n");
5243 int status;
5244 char **a;
5245 int jp;
5246 ESL_ALLOC(a, sizeof(char *) * (L+1)); /* always alloc 0..L rows, same as alloc_deck */
5247 for (jp = 0; jp < i-1; jp++) a[jp] = NULL;
5248 for (jp = j+1; jp <= L; jp++) a[jp] = NULL;
5249 for (jp = 0; jp <= j-i+1; jp++) ESL_ALLOC(a[jp+i-1], sizeof(char) * (jp+1));
5250 return a;
5251 ERROR:
5252 cm_Fail("Memory allocation error.");
5253 return NULL; /* never reached */
5254 }
5255 float
5256 sse_size_vjd_yshadow_deck(int L, int i, int j)
5257 {
5258 fprintf(stderr,"WARNING! sse_size_vjd_yshadow_deck has not been converted to SSE!\n");
5259 float Mb;
5260 int jp;
5261 Mb = (float) (sizeof(char *) * (L+1));
5262 for (jp = 0; jp <= j-i+1; jp++)
5263 Mb += (float) (sizeof(char) * (jp+1));
5264 return Mb / 1000000.;
5265 }
5266 void
5267 sse_free_vjd_yshadow_deck(char **a, int i, int j)
5268 {
5269 fprintf(stderr,"WARNING! sse_free_vjd_yshadow_deck has not been converted to SSE!\n");
5270 int jp;
5271 for (jp = 0; jp <= j-i+1; jp++) if (a[jp+i-1] != NULL) free(a[jp+i-1]);
5272 free(a);
5273 }
5274 int **
5275 sse_alloc_vjd_kshadow_deck(int L, int i, int j)
5276 {
5277 fprintf(stderr,"WARNING! sse_alloc_vjd_kshadow_deck has not been converted to SSE!\n");
5278 int status;
5279 int **a;
5280 int jp;
5281 ESL_ALLOC(a, sizeof(int *) * (L+1)); /* always alloc 0..L rows, same as alloc_deck */
5282 for (jp = 0; jp < i-1; jp++) a[jp] = NULL;
5283 for (jp = 0; jp <= j-i+1; jp++) ESL_ALLOC(a[jp+i-1], sizeof(int) * (jp+1));
5284 for (jp = j+1; jp <= L; jp++) a[jp] = NULL;
5285 return a;
5286 ERROR:
5287 cm_Fail("Memory allocation error.");
5288 return NULL; /* never reached */
5289 }
5290 float
5291 sse_size_vjd_kshadow_deck(int L, int i, int j)
5292 {
5293 fprintf(stderr,"WARNING! sse_size_vjd_kshadow_deck has not been converted to SSE!\n");
5294 float Mb;
5295 int jp;
5296
5297 Mb = (float)(sizeof(int *) * (L+1));
5298 for (jp = 0; jp <= j-i+1; jp++)
5299 Mb += (float) (sizeof(int) * (jp+1));
5300 return Mb / 1000000.;
5301 }
5302 void
5303 sse_free_vjd_kshadow_deck(int **a, int i, int j)
5304 {
5305 fprintf(stderr,"WARNING! sse_free_vjd_kshadow_deck has not been converted to SSE!\n");
5306 int jp;
5307 /*11.14.05 old line: for (jp = 0; jp <= j-i+1; jp++) if (a[jp+i-1] != NULL) free(a[jp]);*/
5308 for (jp = 0; jp <= j-i+1; jp++) if (a[jp+i-1] != NULL) free(a[jp-i+1]);
5309 free(a);
5310 }
5311 void
5312 sse_free_vjd_shadow_matrix(void ***shadow, CM_t *cm, int i, int j)
5313 {
5314 fprintf(stderr,"WARNING! sse_free_vjd_shadow_matrix has not been converted to SSE!\n");
5315 int v;
5316 for (v = 0; v < cm->M; v++)
5317 if (shadow[v] != NULL) {
5318 if (cm->sttype[v] == B_st) free_vjd_kshadow_deck((int **) shadow[v], i, j);
5319 else free_vjd_yshadow_deck((char **) shadow[v], i, j);
5320 shadow[v] = NULL;
5321 }
5322 free(shadow);
5323 }
5324 /*================================================================*/
5325
5326
5327 /*################################################################*/
5328 /* Functions: *_vji_*
5329 * Date: SRE, Sat Aug 12 16:44:55 2000 [Titusville]
5330 *
5331 * Purpose: Allocation and freeing of 3D matrices and 2D decks
5332 * in the vji coordinate system. Since these are used
5333 * only for solving V problems, they work only
5334 * on a defined cube in the 3D matrix: they need
5335 * two triplets (r, i0, j0), (z, i1, j1)
5336 * defining the known optimal endpoints of a segment from
5337 * an S state to a B state.
5338 *
5339 * By definition of V problems, there's no B states
5340 * in between, so the shadow matrix doesn't need any
5341 * special casting tricks the way the more generally
5342 * used vjd system does.
5343 */
5344 sse_deck_t * /* allocation of a score deck. */
5345 sse_alloc_vji_deck(int i0, int i1, int j1, int j0, int x)
5346 {
5347 int status;
5348 int jp;
5349 int sW;
5350 sse_deck_t *tmp;
5351 sW = (i1-i0+1)/x + 1;
5352 ESL_DPRINTF3(("alloc_vji_deck : %.4f\n", size_vji_deck(i0,i1,j1,j0)));
5353 ESL_ALLOC(tmp, sizeof(sse_deck_t));
5354 ESL_ALLOC(tmp->vec, sizeof(__m128 *) * (j0-j1+1));
5355 ESL_ALLOC(tmp->mem, sizeof(__m128 ) * (j0-j1+1) * sW + 15);
5356 tmp->vec[0] = (__m128 *) (((unsigned long int) tmp->mem + 15) & (~0xf));
5357 for (jp = 1; jp <= j0-j1; jp++)
5358 tmp->vec[jp] = tmp->vec[jp-1] + sW;
5359 return tmp;
5360 ERROR:
5361 cm_Fail("Memory allocation error.");
5362 return NULL; /* never reached */
5363 }
5364 float
5365 sse_size_vji_deck(int i0, int i1, int j1, int j0, int x)
5366 {
5367 float Mb;
5368 int jp;
5369 Mb = (float)(sizeof(__m128 *) * (j0-j1+1));
5370 for (jp = 0; jp <= j0-j1; jp++)
5371 Mb += (float)(sizeof(__m128)*(i1-i0+1)/x);
5372 return Mb / 1000000.;
5373 }
5374 void /* free'ing a score deck */
5375 sse_free_vji_deck(sse_deck_t *a)
5376 {
5377 ESL_DPRINTF3(("free_vji_deck called\n"));
5378 free(a->vec);
5379 free(a->mem);
5380 free(a);
5381 }
5382 void
5383 sse_free_vji_matrix(sse_deck_t **a, int M)
5384 {
5385 int v;
5386 /* Free the whole matrix - even if we used only a subset of
5387 * the decks, all initialization routines init all decks 0..M
5388 * to NULL, so this is safe. (see bug #i2).
5389 */
5390 for (v = 0; v <= M; v++)
5391 if (a[v] != NULL) { sse_free_vji_deck(a[v]); a[v] = NULL; }
5392 free(a);
5393 }
5394 char ** /* allocation of a traceback ptr (shadow matrix) deck */
5395 sse_alloc_vji_shadow_deck(int i0, int i1, int j1, int j0)
5396 {
5397 fprintf(stderr,"WARNING! sse_alloc_vji_shadow_deck has not been converted to SSE!\n");
5398 int status;
5399 char **a;
5400 int jp;
5401 ESL_ALLOC(a, sizeof(char *) * (j0-j1+1));
5402 for (jp = 0; jp <= j0-j1; jp++)
5403 ESL_ALLOC(a[jp], sizeof(char)*(i1-i0+1));
5404 return a;
5405 ERROR:
5406 cm_Fail("Memory allocation error.");
5407 return NULL; /* never reached */
5408 }
5409 float /* allocation of a traceback ptr (shadow matrix) deck */
5410 sse_size_vji_shadow_deck(int i0, int i1, int j1, int j0)
5411 {
5412 fprintf(stderr,"WARNING! sse_size_vji_shadow_deck has not been converted to SSE!\n");
5413 float Mb;
5414 int jp;
5415 Mb = (float)(sizeof(char *) * (j0-j1+1));
5416 for (jp = 0; jp <= j0-j1; jp++)
5417 Mb += (float)(sizeof(char)*(i1-i0+1));
5418 return Mb / 1000000;
5419 }
5420 void /* free'ing a shadow deck */
5421 sse_free_vji_shadow_deck(char **a, int j1, int j0)
5422 {
5423 fprintf(stderr,"WARNING! sse_free_vji_shadow_deck has not been converted to SSE!\n");
5424 int jp;
5425 for (jp = 0; jp <= j0-j1; jp++)
5426 if (a[jp] != NULL) free(a[jp]);
5427 free(a);
5428 }
5429 void
5430 sse_free_vji_shadow_matrix(char ***a, int M, int j1, int j0)
5431 {
5432 fprintf(stderr,"WARNING! sse_free_vji_shadow_matrix has not been converted to SSE!\n");
5433 int v;
5434 for (v = 0; v < M; v++)
5435 if (a[v] != NULL) { free_vji_shadow_deck(a[v], j1, j0); a[v] = NULL; }
5436 free(a);
5437 }
5438
5439
5440 /*################################################################
5441 * Unused code -
5442 * a reference implementation of the real Outside() algorithm,
5443 * including bifurcations.
5444 *################################################################*/
5445 #if 0
5446 /* Function: CYKOutside()
5447 * Date: SRE, Mon Aug 7 07:45:37 2000 [St. Louis]
5448 */
5449 void
5450 CYKOutside(CM_t *cm, ESL_DSQ *dsq, int L, float ***alpha)
5451 {
5452 int status;
5453 float ***beta; /* the scoring cube [v=0..M-1][j=0..L][d=0..j]*/
5454 int v,y,z; /* indices for states */
5455 int j,d,i,k; /* indices in sequence dimensions */
5456 float sc; /* a temporary variable holding a score */
5457 struct deckpool_s *dpool; /* a pool of decks for beta that we can reuse */
5458 int *touch; /* keeps track of how many lower decks still need this deck */
5459 float escore; /* an emission score, tmp variable */
5460
5461 /* Allocations and initializations
5462 */
5463 ESL_ALLOC(beta, (sizeof(float **) * cm->M));
5464 for (v = 0; v < cm->M; v++) beta[v] = NULL;
5465
5466 dpool = deckpool_create();
5467
5468 ESL_ALLOC(touch, sizeof(int) * cm->M);
5469 for (v = 0; v < cm->M; v++)
5470 if (cm->sttype[v] == B_st) touch[v] = 2;
5471 else touch[v] = cm->cnum[v];
5472
5473 for (j = 0; j <= L; j++)
5474 for (d = 0; d <= j; j++)
5475 beta[0][j][d] = IMPOSSIBLE; /* can prob speed this initialization up */
5476 beta[0][L][L] = 0;
5477
5478 /* Main loop down through the decks
5479 */
5480 /* EPN bug fix 05.25.06. Durbin et. al. p.287 CM Outside alg uses state
5481 * indices 1..M, with state 1 = ROOT_S, so there's an off-by-one
5482 * w.r.t this implementation. Following loop followed Durbin convention,
5483 * but should follow implemented convention:
5484 * OLD LINE: for (v = 2; v < cm->M; v++)
5485 */
5486 for (v = 1; v < cm->M; v++)
5487 {
5488 /* First we need to fetch a deck of memory to fill in;
5489 * we try to reuse a deck but if one's not available we allocate
5490 * a fresh one.
5491 */
5492 if (! deckpool_pop(dpool, &(beta[v])))
5493 beta[v] = alloc_vjd_deck(L, 1, L);
5494
5495 /* main recursion:
5496 */
5497 for (j = L; j >= 0; j--)
5498 for (d = j; d >= 0; d--)
5499 {
5500 if (cm->stid[v] == BEGL_S)
5501 {
5502 y = cm->plast[v]; /* the parent bifurcation */
5503 z = cm->cnum[y]; /* the other (right) S state */
5504
5505 beta[v][j][d] = beta[y][j][d] + alpha[z][j][0]; /* init on k=0 */
5506 for (k = 1; k <= L-j; k++)
5507 if ((sc = beta[y][j+k][d+k] + alpha[z][j+k][k]) > beta[v][j][d])
5508 beta[v][j][d] = sc;
5509 }
5510 else if (cm->stid[v] == BEGR_S)
5511 {
5512 y = cm->plast[v]; /* the parent bifurcation */
5513 z = cm->cfirst[y]; /* the other (left) S state */
5514
5515 beta[v][j][d] = beta[y][j][d] + alpha[z][j-d][0]; /* init on k=0 */
5516 for (k = 1; k <= j-d; k++)
5517 if ((sc = beta[y][j][d+k] + alpha[z][j-d][k]) > beta[v][j][d])
5518 beta[v][j][d] = sc;
5519 }
5520 else
5521 {
5522 alpha[v][j][d] = IMPOSSIBLE;
5523 i = j-d+1;
5524 for (y = cm->plast[v]; y > cm->plast[v]-cm->pnum[v]; y--) {
5525 switch(cm->sttype[j]) {
5526 case MP_st:
5527 if (d == j || d == j-1) continue; /* boundary condition */
5528
5529 if (dsq[i-1] < cm->abc->K && dsq[j+1] < cm->abc->K)
5530 escore = cm->esc[y][(int) (dsq[i-1]*cm->abc->K+dsq[j+1])];
5531 else
5532 escore = DegeneratePairScore(cm->abc, cm->esc[y], dsq[i-1], dsq[j+1]);
5533
5534 if ((sc = beta[y][j+1][d+2] + cm->tsc[y][v] + escore) > beta[v][j][d])
5535 beta[v][j][d] = sc;
5536 break;
5537
5538 case ML_st:
5539 case IL_st:
5540 if (d == j) continue; /* boundary condition (note when j=0, d=0*/
5541
5542 if (dsq[i-1] < cm->abc->K)
5543 escore = cm->esc[y][(int) dsq[i-1]];
5544 else
5545 escore = esl_abc_FAvgScore(cm->abc, dsq[i-1], cm->esc[y]);
5546
5547 if ((sc = beta[y][j][d+1] + cm->tsc[y][v] + escore) > beta[v][j][d])
5548 beta[v][j][d] = sc;
5549 break;
5550
5551 case MR_st:
5552 case IR_st:
5553 if (d == j || j == L) continue;
5554
5555 if (dsq[j+1] < cm->abc->K)
5556 escore = cm->esc[y][(int) dsq[j+1]];
5557 else
5558 escore = esl_abc_FAvgScore(cm->abc, dsq[j+1], cm->esc[y]);
5559
5560 if ((sc = beta[y][j+1][d+1] + cm->tsc[y][v] + escore) > beta[v][j][d])
5561 beta[v][j][d] = sc;
5562 break;
5563
5564 case B_st:
5565 case E_st:
5566 case D_st:
5567 if ((sc = beta[y][j][d] + cm->tsc[y][v]) > beta[v][j][d])
5568 beta[v][j][d] = sc;
5569 break;
5570
5571 default: cm_Fail("bogus parent state %d\n", cm->sttype[y]);
5572 }/* end switch over states*/
5573 }
5574 }/*ends our handling of beta[v][j][d] */
5575 if (beta[v][j][d] < IMPOSSIBLE) beta[v][j][d] = IMPOSSIBLE;
5576 }
5577
5578 /* Finished deck v.
5579 * now worry about reuse of memory in beta:
5580 */
5581 for (y = cm->plast[v]; y > cm->plast[v]-cm->pnum[v]; y--)
5582 {
5583 touch[y]--;
5584 if (touch[y] == 0) {
5585 deckpool_push(dpool, beta[y]);
5586 beta[y] = NULL;
5587 }
5588 }
5589 } /* end loop over decks v. */
5590
5591 free(touch);
5592 /*dpool*/
5593 /*beta*/
5594 return;
5595 ERROR:
5596 cm_Fail("Memory allocation error.");
5597 }
5598 #endif
5599
5600 /******************************************************************/
5601 /* The below functions were written during debugging, and print
5602 out either the shadow or alpha matrix. They are kept
5603 here just in case they're needed again. Note : the functions
5604 that print out the entire matrix are really only useful
5605 when the BE_PARANOID flag is set, meaning that decks are
5606 never freed until the end.
5607 */
5608 /*================================================================*/
5609 /* EPN 05.09.05
5610 debug_print_shadow()
5611 * Function: debug_print_shadow
5612 *
5613 * Purpose: Print shadow matrix
5614 */
5615
5616 void
5617 sse_debug_print_shadow(void ***shadow, CM_t *cm, int L)
5618 {
5619 fprintf(stderr,"WARNING! sse_debug_print_shadow has not been converted to SSE!\n");
5620 int v, j, d;
5621 int yoffset;
5622
5623 printf("\nPrinting alpha matrix :\n");
5624 printf("************************************\n");
5625 for(v = 0; v < cm->M; v++)
5626 {
5627 printf("====================================\n");
5628 for(j = 0; j <= L; j++)
5629 {
5630 printf("------------------------------------\n");
5631 for(d = 0; d <= j; d++)
5632 {
5633 if(cm->sttype[v] == E_st)
5634 {
5635 printf("END state\n");
5636 }
5637 else
5638 {
5639 if(cm->sttype[v] == B_st)
5640 {
5641 yoffset = ((int **) shadow[v])[j][d];
5642 printf("INT shadow[%2d][%2d][%2d] : %d\n", v, j, d, yoffset);
5643 }
5644 else
5645 {
5646 yoffset = ((int **) shadow[v])[j][d];
5647 printf("CHAR shadow[%2d][%2d][%2d] : %d\n", v, j, d, yoffset);
5648 }
5649 }
5650 }
5651 }
5652 }
5653 printf("****************\n\n");
5654 }
5655
5656 /* EPN 05.09.05
5657 debug_print_alpha()
5658 * Function: debug_print_alpha
5659 *
5660 * Purpose: Print alpha matrix
5661 */
5662
5663 void
5664 sse_debug_print_alpha(float ***alpha, CM_t *cm, int L)
5665 {
5666 fprintf(stderr,"WARNING! sse_debug_print_alpha has not been converted to SSE!\n");
5667 int v, j, d, max_v;
5668
5669 printf("\nPrinting alpha matrix :\n");
5670 printf("************************************\n");
5671 max_v = cm->M-1;
5672 if(cm->flags & CMH_LOCAL_BEGIN)
5673 {
5674 max_v = cm->M;
5675 }
5676 for(v = 0; v <= max_v; v++)
5677 {
5678 printf("====================================\n");
5679 for(j = 0; j <= L; j++)
5680 {
5681 printf("------------------------------------\n");
5682 for(d = 0; d <= j; d++)
5683 {
5684 printf("alpha[%2d][%2d][%2d] : %6.2f\n", v, j, d, alpha[v][j][d]);
5685 }
5686 }
5687 }
5688 printf("****************\n\n");
5689 }
5690
5691
5692 /********************************************************************************
5693 * Test driver
5694 ********************************************************************************/
5695 #ifdef IMPLSSE_SMALL_TEST
5696 /* gcc -std=gnu99 -msse2 -I../ -L../ -I../../easel -L../../easel -I../../hmmer/src -L../../hmmer/src sse_cm_dpsmall.c -linfernal -lhmmer -leasel -lm -g -DIMPLSSE_SMALL_TEST -o sse-cyk
5697 */
5698
5699 #include <esl_config.h>
5700 #include "config.h"
5701
5702 #include <stdio.h>
5703 #include <stdlib.h>
5704 #include <string.h>
5705 #include <time.h>
5706
5707 #include "easel.h"
5708 #include "esl_getopts.h"
5709 #include "esl_random.h"
5710 #include "esl_randomseq.h"
5711 #include "esl_sqio.h"
5712 #include "esl_stopwatch.h"
5713
5714 #include "funcs.h"
5715 #include "structs.h"
5716
5717 static ESL_OPTIONS options[] = {
5718 /* name type default env range toggles reqs incomp help docgroup*/
5719 { "-h", eslARG_NONE, NULL, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
5720 { "-r", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "set random number seed randomly", 0 },
5721 { "-s", eslARG_INT, "33", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 },
5722 { "-e", eslARG_NONE, FALSE, NULL, NULL, "-L", NULL, NULL, "emit sequences from CM", 0 },
5723 { "--Lqdb", eslARG_NONE, FALSE, NULL, NULL, "-L", NULL, NULL, "use random sequences with lengths chosen from QDB distribution", 0},
5724 { "-L", eslARG_INT, "100", NULL, "n>0", NULL, NULL, "-e", "use random sequences of this length [default]",0 },
5725 { "-N", eslARG_INT, "1", NULL, "n>0", NULL, NULL, NULL, "number of target seqs", 0 },
5726 { "--scoreonly",eslARG_NONE,"default", NULL, NULL, NULL, NULL, NULL, "Score-only, low memory implementation",0 },
5727 { "--full", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "Use full Inside implementation", 0},
5728 { "--DnC", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "Use Divide and Conquer implementation", 0},
5729 { "--traceback",eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "Determine CYK trace",0 },
5730 { "--strict", eslARG_NONE, FALSE, NULL, NULL, NULL, "--traceback", NULL, "Compare traces stringently", 0},
5731 { "--verbose", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "Parsetree dump for traceback", 0},
5732 { "--CYKFilter",eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "Experimental epi16 CYK Filter", 0},
5733 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
5734 };
5735
5736 static char usage[] = "[-options] <cmfile>";
5737 static char banner[] = "test driver for an SSE implementation of CYK";
5738
5739 int
5740 main(int argc, char **argv)
5741 {
5742 int status;
5743 ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 1, argc, argv, banner, usage);
5744 CM_t *cm;
5745 ESL_STOPWATCH *w = esl_stopwatch_Create();
5746 ESL_RANDOMNESS *r = NULL;
5747 ESL_ALPHABET *abc = NULL;
5748 int L;
5749 int N = esl_opt_GetInteger(go, "-N");
5750 ESL_DSQ *dsq;
5751 int i;
5752 float sc1, sc2, ptsc1, ptsc2;
5753 char *cmfile = esl_opt_GetArg(go, 1);
5754 CMFILE *cmfp; /* open input CM file stream */
5755 seqs_to_aln_t *seqs_to_aln = NULL; /* sequences to align, either randomly created, or emitted from CM (if -e) */
5756 char errbuf[cmERRBUFSIZE];
5757 Parsetree_t *tr1, *tr2;
5758 double *dnull = NULL;
5759
5760 CM_OPTIMIZED *ocm = NULL;
5761
5762 if (esl_opt_GetBoolean(go, "-r")) r = esl_randomness_CreateTimeseeded();
5763 else r = esl_randomness_Create(esl_opt_GetInteger(go, "-s"));
5764
5765 if ((cmfp = CMFileOpen(cmfile, NULL)) == NULL) cm_Fail("Failed to open covariance model save file %s\n", cmfile);
5766 if ((status = CMFileRead(cmfp, errbuf, &abc, &cm) != eslOK)) cm_Fail("Failed to read CM");
5767 CMFileClose(cmfp);
5768
5769 cm->search_opts |= CM_SEARCH_NOQDB;
5770 ConfigCM(cm, errbuf, TRUE, NULL, NULL); /* TRUE says: calculate W */
5771
5772 if (esl_opt_GetBoolean(go, "--CYKFilter")) {
5773 ocm = cm_optimized_Convert(cm);
5774 }
5775
5776 /* get sequences */
5777 if(esl_opt_IsOn(go, "-L")) {
5778 L = esl_opt_GetInteger(go, "-L");
5779 ESL_DSQ *randdsq = NULL;
5780 ESL_ALLOC(randdsq, sizeof(ESL_DSQ)* (L+2));
5781 ESL_ALLOC(dnull, sizeof(double) * cm->abc->K);
5782 for(i = 0; i < cm->abc->K; i++) dnull[i] = (double) cm->null[i];
5783 esl_vec_DNorm(dnull, cm->abc->K);
5784 seqs_to_aln = CreateSeqsToAln(N, FALSE);
5785
5786 for (i = 0; i < N; i++) {
5787 if (esl_rsq_xIID(r, dnull, cm->abc->K, L, randdsq) != eslOK) cm_Fail("Failure creating random sequence.");
5788 if((seqs_to_aln->sq[i] = esl_sq_CreateDigitalFrom(abc, NULL, randdsq, L, NULL, NULL, NULL)) == NULL)
5789 cm_Fail("Failure digitizing/copying random sequence.");
5790 }
5791 seqs_to_aln->nseq = N;
5792
5793 free(randdsq);
5794 }
5795 else if(esl_opt_IsOn(go, "--Lqdb")) {
5796 ESL_ALLOC(dnull, sizeof(double) * cm->abc->K);
5797 for(i = 0; i < cm->abc->K; i++) dnull[i] = (double) cm->null[i];
5798 esl_vec_DNorm(dnull, cm->abc->K);
5799 /* get gamma[0] from the QDB calc alg, which will serve as the length distro for random seqs */
5800 int safe_windowlen = cm->clen * 2;
5801 double **gamma = NULL;
5802 while(!(BandCalculationEngine(cm, safe_windowlen, DEFAULT_HS_BETA, TRUE, NULL, NULL, &(gamma), NULL))) {
5803 FreeBandDensities(cm, gamma);
5804 safe_windowlen *= 2;
5805 if(safe_windowlen > (cm->clen * 1000)) cm_Fail("Error trying to get gamma[0], safe_windowlen big: %d\n", safe_windowlen);
5806 }
5807 seqs_to_aln = RandomEmitSeqsToAln(r, cm->abc, dnull, 1, N, gamma[0], safe_windowlen, FALSE);
5808 FreeBandDensities(cm, gamma);
5809 }
5810 else if(esl_opt_IsOn(go, "-e")) { /* don't randomly generate seqs, emit them from the CM */
5811 seqs_to_aln = CMEmitSeqsToAln(r, cm, 1, N, FALSE, NULL, FALSE);
5812 }
5813 else
5814 cm_Fail("No sequence generation method is active, aborting.");
5815
5816 for (i = 0; i < N; i++)
5817 {
5818 L = seqs_to_aln->sq[i]->n;
5819 dsq = seqs_to_aln->sq[i]->dsq;
5820 cm->search_opts &= ~CM_SEARCH_INSIDE;
5821 tr1 = tr2 = NULL;
5822
5823 if (esl_opt_GetBoolean(go, "--scoreonly")) {
5824 esl_stopwatch_Start(w);
5825 sc1 = CYKInsideScore(cm, dsq, L, 0, 1, L, NULL, NULL);
5826 printf("%4d %-30s %10.4f bits ", (i+1), "CYKInsideScore(): ", sc1);
5827 esl_stopwatch_Stop(w);
5828 esl_stopwatch_Display(stdout, w, " CPU time: ");
5829
5830 esl_stopwatch_Start(w);
5831 sc2 = SSE_CYKInsideScore(cm, dsq, L, 0, 1, L);
5832 printf("%4d %-30s %10.4f bits ", (i+1), "SSE_CYKInsideScore(): ", sc2);
5833 esl_stopwatch_Stop(w);
5834 esl_stopwatch_Display(stdout, w, " CPU time: ");
5835 }
5836
5837 if (esl_opt_GetBoolean(go, "--full")) {
5838 esl_stopwatch_Start(w);
5839 sc1 = CYKInside(cm, dsq, L, 0, 1, L, NULL, NULL, NULL);
5840 printf("%4d %-30s %10.4f bits ", (i+1), "CYKInside(): ", sc1);
5841 esl_stopwatch_Stop(w);
5842 esl_stopwatch_Display(stdout, w, " CPU time: ");
5843
5844 esl_stopwatch_Start(w);
5845 sc2 = SSE_CYKInside(cm, dsq, L, 0, 1, L, NULL);
5846 printf("%4d %-30s %10.4f bits ", (i+1), "SSE_CYKInside(): ", sc2);
5847 esl_stopwatch_Stop(w);
5848 esl_stopwatch_Display(stdout, w, " CPU time: ");
5849 }
5850
5851 if (esl_opt_GetBoolean(go, "--DnC")) {
5852 esl_stopwatch_Start(w);
5853 sc1 = CYKDivideAndConquer(cm, dsq, L, 0, 1, L, NULL, NULL, NULL);
5854 printf("%4d %-30s %10.4f bits ", (i+1), "CYKDivideAndConquer(): ", sc1);
5855 esl_stopwatch_Stop(w);
5856 esl_stopwatch_Display(stdout, w, " CPU time: ");
5857
5858 esl_stopwatch_Start(w);
5859 sc2 = SSE_CYKDivideAndConquer(cm, dsq, L, 0, 1, L, NULL);
5860 printf("%4d %-30s %10.4f bits ", (i+1), "SSE_CYKDivideAndConquer(): ", sc2);
5861 esl_stopwatch_Stop(w);
5862 esl_stopwatch_Display(stdout, w, " CPU time: ");
5863 }
5864
5865 if (esl_opt_GetBoolean(go, "--traceback")) {
5866 esl_stopwatch_Start(w);
5867 if (esl_opt_GetBoolean(go, "--DnC")) {
5868 sc1 = CYKDivideAndConquer(cm, dsq, L, 0, 1, L, &tr1, NULL, NULL);
5869 if (esl_opt_GetBoolean(go, "--verbose")) ParsetreeDump(stdout, tr1, cm, dsq, NULL, NULL);
5870 ParsetreeScore(cm, NULL, NULL, tr1, dsq, FALSE, &ptsc1, NULL, NULL, NULL, NULL);
5871 printf("%4d %-30s %10.4f bits %10.4f bits", (i+1), "CYKDivideAndConquer(): ", sc1, ptsc1);
5872 } else {
5873 sc1 = CYKInside(cm, dsq, L, 0, 1, L, &tr1, NULL, NULL);
5874 if (esl_opt_GetBoolean(go, "--verbose")) ParsetreeDump(stdout, tr1, cm, dsq, NULL, NULL);
5875 ParsetreeScore(cm, NULL, NULL, tr1, dsq, FALSE, &ptsc1, NULL, NULL, NULL, NULL);
5876 printf("%4d %-30s %10.4f bits %10.4f bits", (i+1), "CYKInside(): ", sc1, ptsc1);
5877 }
5878 esl_stopwatch_Stop(w);
5879 esl_stopwatch_Display(stdout, w, " CPU time: ");
5880
5881 esl_stopwatch_Start(w);
5882 if (esl_opt_GetBoolean(go, "--DnC")) {
5883 sc2 = SSE_CYKDivideAndConquer(cm, dsq, L, 0, 1, L, &tr2);
5884 if (esl_opt_GetBoolean(go, "--verbose")) ParsetreeDump(stdout, tr2, cm, dsq, NULL, NULL);
5885 ParsetreeScore(cm, NULL, NULL, tr2, dsq, FALSE, &ptsc2, NULL, NULL, NULL, NULL);
5886 printf("%4d %-30s %10.4f bits %10.4f bits", (i+1), "SSE_CYKDivideAndConquer(): ", sc2, ptsc2);
5887 } else {
5888 sc2 = SSE_CYKInside(cm, dsq, L, 0, 1, L, &tr2);
5889 if (esl_opt_GetBoolean(go, "--verbose")) ParsetreeDump(stdout, tr2, cm, dsq, NULL, NULL);
5890 ParsetreeScore(cm, NULL, NULL, tr2, dsq, FALSE, &ptsc2, NULL, NULL, NULL, NULL);
5891 printf("%4d %-30s %10.4f bits %10.4f bits", (i+1), "SSE_CYKInside(): ", sc2, ptsc2);
5892 }
5893 esl_stopwatch_Stop(w);
5894 esl_stopwatch_Display(stdout, w, " CPU time: ");
5895
5896 if (tr1 != NULL && fabs(sc1 - ptsc1) >= 0.01)
5897 cm_Die("CYKInside score differs from its parse tree's score\n");
5898 if (tr2 != NULL && fabs(sc2 - ptsc2) >= 0.01)
5899 cm_Die("SSE_CYKInside score differs from its parse tree's score\n");
5900 if (fabs(sc1 - sc2) >= 0.01)
5901 cm_Die("CYKInside score differs from SSE_CYKInside\n");
5902 if (tr1 != NULL && tr2 != NULL && esl_opt_GetBoolean(go, "--strict") && !ParsetreeCompare(tr1, tr2))
5903 cm_Die("Parse trees differ\n");
5904
5905 }
5906
5907 if (esl_opt_GetBoolean(go, "--CYKFilter")) {
5908 esl_stopwatch_Start(w);
5909 sc1 = SSE_CYKFilter_epi16(ocm,dsq,L,0,cm->M-1,1,L,TRUE,NULL,NULL,NULL)/500.0;
5910 printf("%4d %-30s %10.4f bits ", (i+1), "CYKFilter_epi16(): ", sc1);
5911 esl_stopwatch_Stop(w);
5912 esl_stopwatch_Display(stdout, w, " CPU time: ");
5913 }
5914
5915 if (tr1 != NULL) FreeParsetree(tr1);
5916 if (tr2 != NULL) FreeParsetree(tr2);
5917
5918 printf("\n");
5919 }
5920 if (ocm != NULL) cm_optimized_Free(ocm); free(ocm);
5921 FreeCM(cm);
5922 FreeSeqsToAln(seqs_to_aln);
5923 esl_alphabet_Destroy(abc);
5924 esl_stopwatch_Destroy(w);
5925 esl_randomness_Destroy(r);
5926 esl_getopts_Destroy(go);
5927
5928 if (dnull != NULL) free(dnull);
5929
5930 return 0;
5931
5932 ERROR:
5933 cm_Fail("memory allocation error");
5934 return 0; /* never reached */
5935 }
5936 #endif /*IMPLSSE_SMALL_TEST*/