Debian Codegraph @

hmmer/src/generic_fwdback_banded.c

raw symbols SCIP
1 /* Forward/Backward, generic, with bands.
2 */
3
4 #include <p7_config.h>
5
6 #include "easel.h"
7 #include "esl_sq.h"
8
9 #include "hmmer.h"
10 #include "p7_gbands.h"
11 #include "p7_gmxb.h"
12
13 int
14 p7_GForwardBanded(const ESL_DSQ *dsq, int L, const P7_PROFILE *gm, P7_GMXB *gxb, float *opt_sc)
15 {
16 int *bnd_ip = gxb->bnd->imem; /* ptr to current ia, ib segment band in gxb->bnd */
17 int *bnd_kp = gxb->bnd->kmem; /* ptr to current ka, kb row band in gxb->bnd */
18 float *dpc = gxb->dp; /* ptr to current DP matrix cell */
19 float *xpc = gxb->xmx; /* ptr to current special cell */
20 float const *tsc = gm->tsc; /* sets up TSC() macro, access to profile's transitions */
21 float const *rsc; /* will be set up for MSC(), ISC() macros for residue scores */
22 float *dpp; /* ptr to previous DP matrix cell */
23 float *last_dpc; /* used to reinitialize dpp after each row */
24 int ia, ib; /* current segment band is rows ia..ib */
25 int last_ib; /* intersegment interval is last_ib+1..ia-1 */
26 int kac, kbc; /* current row band is kac..kbc */
27 int kap, kbp; /* previous row band is kap..kbp */
28 int kbc2; /* if kbc==M, kbc2=M-1, main loop goes kac..kbc2 and M is unrolled */
29 float xE, xN, xJ, xB, xC; /* tmp scores on special states. only stored when in row bands */
30 float mvp, ivp, dvp; /* M,I,D cell values from previous row i-1 */
31 float dc; /* precalculated D(i,k+1) value on current row */
32 float sc; /* temporary score calculation M(i,k) */
33 int g, i, k; /* indices running over segments, residues (rows) x_i, model positions (cols) k */
34 //float esc = p7_profile_IsLocal(gm) ? 0 : -eslINFINITY;
35
36 xN = 0.0f;
37 xJ = -eslINFINITY;
38 xC = -eslINFINITY;
39 last_ib = 0;
40
41 for (g = 0; g < gxb->bnd->nseg; g++)
42 {
43 ia = *bnd_ip++;
44 ib = *bnd_ip++;
45
46 /* kap,kbp initialization for i=ia:
47 * left overhang dpp advance must always eval to 0,
48 * {m,i,d}vp initialization must always eval to -eslINFINITY.
49 */
50 kap = kbp = gm->M+1;
51 dpp = dpc; /* re-initialize dpp */
52
53 /* re-initialization: specials for previous row ia-1 just outside banded segment: */
54 xE = -eslINFINITY;
55 xN = xN + (ia - last_ib - 1) * gm->xsc[p7P_N][p7P_LOOP];
56 xJ = xJ + (ia - last_ib - 1) * gm->xsc[p7P_J][p7P_LOOP];
57 xB = p7_FLogsum( xN + gm->xsc[p7P_N][p7P_MOVE], xJ + gm->xsc[p7P_J][p7P_MOVE]);
58 xC = xC + (ia - last_ib - 1) * gm->xsc[p7P_C][p7P_LOOP];
59
60 for (i = ia; i <= ib; i++)
61 {
62 rsc = gm->rsc[dsq[i]]; /* sets up MSC(k), ISC(k) residue scores for this row i */
63 dc = -eslINFINITY;
64 xE = -eslINFINITY;
65 last_dpc = dpc;
66
67 kac = *bnd_kp++; /* current row's band is cells k=kac..kbc */
68 kbc = *bnd_kp++;
69 kbc2 = (kbc == gm->M ? kbc-1 : kbc); /* a "do_M" flag works too, but this way we avoid an if statement */
70
71 /* dpp must advance by any left overhang of previous row; but no more than the entire row */
72 dpp += (kac-1 > kap ? ESL_MIN(kac-kap-1, kbp-kap+1) : 0);
73
74 if (kac > kap && kac-1 <= kbp) { mvp = *dpp++; ivp = *dpp++; dvp = *dpp++; }
75 else { mvp = -eslINFINITY; ivp = -eslINFINITY; dvp = -eslINFINITY; }
76
77 for (k = kac; k <= kbc2; k++)
78 {
79 *dpc++ = sc = MSC(k) + p7_FLogsum( p7_FLogsum(mvp + TSC(p7P_MM, k-1), ivp + TSC(p7P_IM, k-1)),
80 dvp + TSC(p7P_DM, k-1));
81 //p7_FLogsum(dvp + TSC(p7P_DM, k-1), xB + TSC(p7P_BM, k-1)));
82
83
84 if (k >= kap && k <= kbp) { mvp = *dpp++; ivp = *dpp++; dvp = *dpp++; } // an if seems unavoidable. alternatively, might unroll
85 else { mvp = -eslINFINITY; ivp = -eslINFINITY; dvp = -eslINFINITY; } // all possible (kap,kac)..(kbp,kbc) orderings, but this
86 // seems too complex
87
88 *dpc++ = ISC(k) + p7_FLogsum( mvp + TSC(p7P_MI, k), ivp + TSC(p7P_II, k));
89
90 //xE = p7_FLogsum( p7_FLogsum(sc + esc, dc + esc), xE);/* Mk->E accumulation */
91
92 /* next D_k+1 */
93 *dpc++ = dc;
94 dc = p7_FLogsum( sc + TSC(p7P_MD, k), dc + TSC(p7P_DD, k));
95 }
96
97 if (kbc2 < kbc) /* i.e., if kbc==M and we need to do the final M column: */
98 {
99 *dpc++ = sc = MSC(k) + p7_FLogsum( p7_FLogsum(mvp + TSC(p7P_MM, k-1), ivp + TSC(p7P_IM, k-1)),
100 p7_FLogsum(dvp + TSC(p7P_DM, k-1), xB + TSC(p7P_BM, k-1)));
101 *dpc++ = -eslINFINITY;
102 *dpc++ = dc;
103 xE = p7_FLogsum( p7_FLogsum(sc, dc), xE);
104 }
105
106 *xpc++ = xE;
107 *xpc++ = xN = xN + gm->xsc[p7P_N][p7P_LOOP];
108 *xpc++ = xJ = p7_FLogsum( xJ + gm->xsc[p7P_J][p7P_LOOP], xE + gm->xsc[p7P_E][p7P_MOVE]);
109 *xpc++ = xB = p7_FLogsum( xJ + gm->xsc[p7P_J][p7P_MOVE], xN + gm->xsc[p7P_N][p7P_MOVE]);
110 *xpc++ = xC = p7_FLogsum( xE + gm->xsc[p7P_E][p7P_MOVE], xC + gm->xsc[p7P_C][p7P_LOOP]);
111
112 dpp = last_dpc; /* this skips any right overhang on the previous row, so dpp advances (if necessary) to start of curr row */
113 kap = kac;
114 kbp = kbc;
115 }
116 last_ib = ib;
117 }
118
119 /* last_ib+1..L is outside any band segment, so it can only run through xC. */
120 if (opt_sc != NULL) *opt_sc = xC + (L-last_ib) * gm->xsc[p7P_C][p7P_LOOP] + gm->xsc[p7P_C][p7P_MOVE];
121 return eslOK;
122 }
123
124 /*****************************************************************
125 * x. Benchmark driver
126 *****************************************************************/
127 #ifdef p7GENERIC_FWDBACK_BANDED_BENCHMARK
128 #include <p7_config.h>
129
130 #include "easel.h"
131 #include "esl_alphabet.h"
132 #include "esl_getopts.h"
133 #include "esl_random.h"
134 #include "esl_randomseq.h"
135
136 #include "hmmer.h"
137 #include "p7_gbands.h"
138 #include "p7_gmxb.h"
139
140 static ESL_OPTIONS options[] = {
141 /* name type default env range toggles reqs incomp help docgroup*/
142 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
143 { "-s", eslARG_INT, "42", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 },
144 { "-L", eslARG_INT, "400", NULL, "n>0", NULL, NULL, NULL, "length of random target seqs", 0 },
145 { "-N", eslARG_INT, "2000", NULL, "n>0", NULL, NULL, NULL, "number of random target seqs", 0 },
146 { "-W", eslARG_INT, "3", NULL,"n>=0", NULL, NULL, NULL, "band halfwidth", 0 },
147 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
148 };
149 static char usage[] = "[-options] <hmmfile>";
150 static char banner[] = "benchmark driver for banded Forward/Backward";
151
152 int
153 main(int argc, char **argv)
154 {
155 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 1, argc, argv, banner, usage);
156 char *hmmfile = esl_opt_GetArg(go, 1);
157 ESL_STOPWATCH *w = esl_stopwatch_Create();
158 ESL_RANDOMNESS *r = esl_randomness_CreateFast(esl_opt_GetInteger(go, "-s"));
159 ESL_ALPHABET *abc = NULL;
160 P7_HMMFILE *hfp = NULL;
161 P7_HMM *hmm = NULL;
162 P7_BG *bg = NULL;
163 P7_PROFILE *gm = NULL;
164 P7_GBANDS *bnd = NULL;
165 P7_GMXB *fwd = NULL;
166 int L = esl_opt_GetInteger(go, "-L");
167 int N = esl_opt_GetInteger(go, "-N");
168 int W = esl_opt_GetInteger(go, "-W");
169 ESL_DSQ *dsq = malloc(sizeof(ESL_DSQ) * (L+2));
170 int idx, k, base;
171 float sc;
172 double base_time, bench_time, Mcs;
173
174 if (p7_hmmfile_Open(hmmfile, NULL, &hfp, NULL) != eslOK) p7_Fail("Failed to open HMM file %s", hmmfile);
175 if (p7_hmmfile_Read(hfp, &abc, &hmm) != eslOK) p7_Fail("Failed to read HMM");
176
177 bg = p7_bg_Create(abc);
178 p7_bg_SetLength(bg, L);
179 gm = p7_profile_Create(hmm->M, abc);
180 p7_ProfileConfig(hmm, bg, gm, L, p7_GLOCAL);
181
182 /* Contrive bands: one length M band of width W */
183 bnd = p7_gbands_Create(L, gm->M);
184 if (L < gm->M) p7_Fail("for now, L must be >=M, because we make a single band");
185 base = (L-gm->M) / 2;
186 for (k = 1; k <= gm->M; k++)
187 p7_gbands_Append(bnd, k+base, ESL_MAX(1,k-W), ESL_MIN(gm->M,k+W));
188
189 fwd = p7_gmxb_Create(bnd);
190
191 /* Baseline time. */
192 esl_stopwatch_Start(w);
193 for (idx = 0; idx < N; idx++) esl_rsq_xfIID(r, bg->f, abc->K, L, dsq);
194 esl_stopwatch_Stop(w);
195 base_time = w->user;
196
197 /* Benchmark time. */
198 esl_stopwatch_Start(w);
199 for (idx = 0; idx < N; idx++)
200 {
201 esl_rsq_xfIID(r, bg->f, abc->K, L, dsq);
202
203 p7_gmxb_Reinit(fwd, bnd);
204
205 p7_GForwardBanded(dsq, L, gm, fwd, &sc);
206
207 p7_gmxb_Reuse(fwd);
208 }
209 esl_stopwatch_Stop(w);
210 bench_time = w->user - base_time;
211 Mcs = (double) N * (double) L * (double) gm->M * 1e-6 / (double) bench_time;
212 esl_stopwatch_Display(stdout, w, "# CPU time: ");
213 printf("# M = %d\n", gm->M);
214 printf("# %.1f Mc/s\n", Mcs);
215
216 free(dsq);
217 p7_gmxb_Destroy(fwd);
218 p7_gbands_Destroy(bnd);
219 p7_profile_Destroy(gm);
220 p7_bg_Destroy(bg);
221 p7_hmm_Destroy(hmm);
222 p7_hmmfile_Close(hfp);
223 esl_alphabet_Destroy(abc);
224 esl_stopwatch_Destroy(w);
225 esl_randomness_Destroy(r);
226 esl_getopts_Destroy(go);
227 return 0;
228 }
229 #endif /*p7GENERIC_FWDBACK_BANDED_BENCHMARK*/
230 /************** end, benchmark driver*****************************/