Debian Codegraph @

hmmer/src/impl_vmx/decoding.c

raw symbols SCIP
1 /* Posterior decoding algorithms; VMX versions.
2 *
3 * Contents:
4 * 1. Posterior decoding algorithms.
5 * 2. Benchmark driver.
6 * 3. Unit tests.
7 * 4. Test driver.
8 *
9 * SRE, Mon Aug 18 08:15:50 2008 [Janelia]
10 */
11 #include <p7_config.h>
12
13 #include <stdio.h>
14 #include <math.h>
15
16 #ifndef __APPLE_ALTIVEC__
17 #include <altivec.h>
18 #endif
19
20 #include "easel.h"
21 #include "esl_vmx.h"
22
23 #include "hmmer.h"
24 #include "impl_vmx.h"
25
26 /*****************************************************************
27 * 1. Posterior decoding algorithms.
28 *****************************************************************/
29
30 /* Function: p7_Decoding()
31 * Synopsis: Posterior decoding of residue assignment.
32 * Incept: SRE, Fri Aug 8 14:29:42 2008 [UA217 to SFO]
33 *
34 * Purpose: Identical to <p7_GDecoding()> except that <om>, <oxf>,
35 * <oxb> are VMX optimized versions. See <p7_GDecoding()>
36 * documentation for more info.
37 *
38 * Args: om - profile (must be the same that was used to fill <oxf>, <oxb>).
39 * oxf - filled Forward matrix
40 * oxb - filled Backward matrix
41 * pp - RESULT: posterior decoding matrix.
42 *
43 * Returns: <eslOK> on success.
44 *
45 * Returns <eslERANGE> if numeric range of floating-point
46 * values is exceeded during posterior probability
47 * calculations. In this case, the <pp> matrix must not be
48 * used by the caller; it will contain <NaN> values. To be
49 * safe, the caller should recalculate a generic posterior
50 * decoding matrix instead -- generic calculations are done
51 * in log probability space and are robust.
52 *
53 * However, I currently believe that this overflow only
54 * occurs on an unusual and ignorable situation: when a
55 * <p7_UNILOCAL> model is used on a region that contains
56 * two or more high scoring distinct alignments to the
57 * model. And that only happens if domain definition fails,
58 * after stochastic clustering, and an envelope that we
59 * pass to p7_domaindef.c::rescore_isolated_domain()
60 * erroneously contains 2+ distinct domains. (Note that
61 * this is different from having 2+ expected B states: that
62 * can happen normally, if a single consistent domain is
63 * better described by 2+ passes through the model). And I
64 * strongly believe all this only can happen on repetitive
65 * or biased-composition junk that we want to ignore anyway.
66 * Therefore the caller should be safe in ignoring any domain
67 * for which <p7_Decoding()> returns <eslERANGE>.
68 *
69 * Throws: (no abnormal error conditions)
70 *
71 * Xref: [J3/119-121]: for analysis of numeric range issues when
72 * <scaleproduct> overflows.
73 */
74 int
75 p7_Decoding(const P7_OPROFILE *om, const P7_OMX *oxf, P7_OMX *oxb, P7_OMX *pp)
76 {
77 vector float *ppv;
78 vector float *fv;
79 vector float *bv;
80 vector float totrv;
81 vector float zerov;
82 int L = oxf->L;
83 int M = om->M;
84 int Q = p7O_NQF(M);
85 int i,q;
86 float scaleproduct = 1.0 / oxb->xmx[p7X_N];
87
88 pp->M = M;
89 pp->L = L;
90
91 zerov = (vector float) vec_splat_u32(0);
92
93 ppv = pp->dpf[0];
94 for (q = 0; q < Q; q++) {
95 *ppv = (vector float) vec_splat_u32(0); ppv++;
96 *ppv = (vector float) vec_splat_u32(0); ppv++;
97 *ppv = (vector float) vec_splat_u32(0); ppv++;
98 }
99 pp->xmx[p7X_E] = 0.0;
100 pp->xmx[p7X_N] = 0.0;
101 pp->xmx[p7X_J] = 0.0;
102 pp->xmx[p7X_C] = 0.0;
103 pp->xmx[p7X_B] = 0.0;
104
105 for (i = 1; i <= L; i++)
106 {
107 ppv = pp->dpf[i];
108 fv = oxf->dpf[i];
109 bv = oxb->dpf[i];
110
111 totrv = esl_vmx_set_float(scaleproduct * oxf->xmx[i*p7X_NXCELLS+p7X_SCALE]);
112
113 for (q = 0; q < Q; q++)
114 {
115 /* M */
116 *ppv = vec_madd(*fv, *bv, zerov);
117 *ppv = vec_madd(*ppv, totrv, zerov);
118 ppv++; fv++; bv++;
119
120 /* D */
121 *ppv = (vector float) vec_splat_u32(0);
122 ppv++; fv++; bv++;
123
124 /* I */
125 *ppv = vec_madd(*fv, *bv, zerov);
126 *ppv = vec_madd(*ppv, totrv, zerov);
127 ppv++; fv++; bv++;
128 }
129 pp->xmx[i*p7X_NXCELLS+p7X_E] = 0.0;
130 pp->xmx[i*p7X_NXCELLS+p7X_N] = oxf->xmx[(i-1)*p7X_NXCELLS+p7X_N] * oxb->xmx[i*p7X_NXCELLS+p7X_N] * om->xf[p7O_N][p7O_LOOP] * scaleproduct;
131 pp->xmx[i*p7X_NXCELLS+p7X_J] = oxf->xmx[(i-1)*p7X_NXCELLS+p7X_J] * oxb->xmx[i*p7X_NXCELLS+p7X_J] * om->xf[p7O_J][p7O_LOOP] * scaleproduct;
132 pp->xmx[i*p7X_NXCELLS+p7X_C] = oxf->xmx[(i-1)*p7X_NXCELLS+p7X_C] * oxb->xmx[i*p7X_NXCELLS+p7X_C] * om->xf[p7O_C][p7O_LOOP] * scaleproduct;
133 pp->xmx[i*p7X_NXCELLS+p7X_B] = 0.0;
134
135 if (oxb->has_own_scales) scaleproduct *= oxf->xmx[i*p7X_NXCELLS+p7X_SCALE] / oxb->xmx[i*p7X_NXCELLS+p7X_SCALE];
136 }
137
138 if (isinf(scaleproduct)) return eslERANGE;
139 else return eslOK;
140 }
141
142 /* Function: p7_DomainDecoding()
143 * Synopsis: Posterior decoding of domain location.
144 * Incept: SRE, Tue Aug 5 08:39:07 2008 [Janelia]
145 *
146 * Purpose: Identical to <p7_GDomainDecoding()> except that <om>, <oxf>,
147 * <oxb> are VMX optimized versions. See <p7_GDomainDecoding()>
148 * documentation for more info.
149 *
150 * Args: gm - profile
151 * oxf - filled Forward matrix
152 * oxb - filled Backward matrix
153 * ddef - container for the results.
154 *
155 * Returns: <eslOK> on success.
156 *
157 * <eslERANGE> on numeric overflow. See commentary in
158 * <p7_Decoding()>.
159 *
160 * Throws: (no abnormal error conditions)
161 */
162 int
163 p7_DomainDecoding(const P7_OPROFILE *om, const P7_OMX *oxf, const P7_OMX *oxb, P7_DOMAINDEF *ddef)
164 {
165 int L = oxf->L;
166 float scaleproduct = 1.0 / oxb->xmx[p7X_N];
167 float njcp;
168 int i;
169
170 ddef->btot[0] = 0.0;
171 ddef->etot[0] = 0.0;
172 ddef->mocc[0] = 0.0;
173 for (i = 1; i <= L; i++)
174 {
175 /* scaleproduct is prod_j=0^i-2 now */
176 ddef->btot[i] = ddef->btot[i-1] +
177 (oxf->xmx[(i-1)*p7X_NXCELLS+p7X_B] * oxb->xmx[(i-1)*p7X_NXCELLS+p7X_B] * oxf->xmx[(i-1)*p7X_NXCELLS+p7X_SCALE] * scaleproduct);
178
179 if (oxb->has_own_scales) scaleproduct *= oxf->xmx[(i-1)*p7X_NXCELLS+p7X_SCALE] / oxb->xmx[(i-1)*p7X_NXCELLS+p7X_SCALE];
180 /* scaleproduct is prod_j=0^i-1 now */
181
182 ddef->etot[i] = ddef->etot[i-1] +
183 (oxf->xmx[i*p7X_NXCELLS+p7X_E] * oxb->xmx[i*p7X_NXCELLS+p7X_E] * oxf->xmx[i*p7X_NXCELLS+p7X_SCALE] * scaleproduct);
184
185 njcp = oxf->xmx[(i-1)*p7X_NXCELLS+p7X_N] * oxb->xmx[i*p7X_NXCELLS+p7X_N] * om->xf[p7O_N][p7O_LOOP] * scaleproduct;
186 njcp += oxf->xmx[(i-1)*p7X_NXCELLS+p7X_J] * oxb->xmx[i*p7X_NXCELLS+p7X_J] * om->xf[p7O_J][p7O_LOOP] * scaleproduct;
187 njcp += oxf->xmx[(i-1)*p7X_NXCELLS+p7X_C] * oxb->xmx[i*p7X_NXCELLS+p7X_C] * om->xf[p7O_C][p7O_LOOP] * scaleproduct;
188 ddef->mocc[i] = 1. - njcp;
189 }
190 ddef->L = oxf->L;
191
192 if (isinf(scaleproduct)) return eslERANGE;
193 else return eslOK;
194 }
195 /*------------------ end, posterior decoding --------------------*/
196
197 /*****************************************************************
198 * 2. Benchmark driver
199 *****************************************************************/
200 #ifdef p7DECODING_BENCHMARK
201 /*
202 icc -O3 -static -o decoding_benchmark -I.. -L.. -I../../easel -L../../easel -Dp7DECODING_BENCHMARK decoding.c -lhmmer -leasel -lm
203 ./decoding_benchmark <hmmfile>
204 RRM_1 (M=72) Caudal_act (M=136) SMC_N (M=1151)
205 ----------------- ------------------ ---------------
206 21 Aug 08 3.52u (409 Mc/s) 15.36u (177 Mc/s) 318.78u (72.2 Mc/s)
207
208 The length dependency probably indicates L1 cache missing; because we're
209 manipulating 3 matrices at the same time, we can't fit the calculation
210 in cache.
211 */
212 #include <p7_config.h>
213
214 #include "easel.h"
215 #include "esl_alphabet.h"
216 #include "esl_getopts.h"
217 #include "esl_random.h"
218 #include "esl_randomseq.h"
219 #include "esl_stopwatch.h"
220
221 #include "hmmer.h"
222 #include "impl_vmx.h"
223
224 static ESL_OPTIONS options[] = {
225 /* name type default env range toggles reqs incomp help docgroup*/
226 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
227 { "-s", eslARG_INT, "42", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 },
228 { "-L", eslARG_INT, "400", NULL, "n>0", NULL, NULL, NULL, "length of random target seqs", 0 },
229 { "-N", eslARG_INT, "50000", NULL, "n>0", NULL, NULL, NULL, "number of random target seqs", 0 },
230 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
231 };
232 static char usage[] = "[-options] <hmmfile>";
233 static char banner[] = "benchmark driver for posterior residue decoding, VMX version";
234
235 int
236 main(int argc, char **argv)
237 {
238 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 1, argc, argv, banner, usage);
239 char *hmmfile = esl_opt_GetArg(go, 1);
240 ESL_STOPWATCH *w = esl_stopwatch_Create();
241 ESL_RANDOMNESS *r = esl_randomness_CreateFast(esl_opt_GetInteger(go, "-s"));
242 ESL_ALPHABET *abc = NULL;
243 P7_HMMFILE *hfp = NULL;
244 P7_HMM *hmm = NULL;
245 P7_BG *bg = NULL;
246 P7_PROFILE *gm = NULL;
247 P7_OPROFILE *om = NULL;
248 P7_OMX *fwd = NULL;
249 P7_OMX *bck = NULL;
250 P7_OMX *pp = NULL;
251 int L = esl_opt_GetInteger(go, "-L");
252 int N = esl_opt_GetInteger(go, "-N");
253 ESL_DSQ *dsq = malloc(sizeof(ESL_DSQ) * (L+2));
254 int i;
255 float fsc, bsc;
256 double Mcs;
257
258 if (p7_hmmfile_Open(hmmfile, NULL, &hfp, NULL) != eslOK) p7_Fail("Failed to open HMM file %s", hmmfile);
259 if (p7_hmmfile_Read(hfp, &abc, &hmm) != eslOK) p7_Fail("Failed to read HMM");
260
261 bg = p7_bg_Create(abc); p7_bg_SetLength(bg, L);
262 gm = p7_profile_Create(hmm->M, abc); p7_ProfileConfig(hmm, bg, gm, L, p7_LOCAL);
263 om = p7_oprofile_Create(gm->M, abc); p7_oprofile_Convert(gm, om);
264 p7_oprofile_ReconfigLength(om, L);
265
266 fwd = p7_omx_Create(gm->M, L, L);
267 bck = p7_omx_Create(gm->M, L, L);
268 pp = p7_omx_Create(gm->M, L, L);
269
270 esl_rsq_xfIID(r, bg->f, abc->K, L, dsq);
271 p7_Forward (dsq, L, om, fwd, &fsc);
272 p7_Backward(dsq, L, om, fwd, bck, &bsc);
273
274 esl_stopwatch_Start(w);
275 for (i = 0; i < N; i++)
276 p7_Decoding(om, fwd, bck, pp);
277 esl_stopwatch_Stop(w);
278
279 Mcs = (double) N * (double) L * (double) gm->M * 1e-6 / (double) w->user;
280 esl_stopwatch_Display(stdout, w, "# CPU time: ");
281 printf("# M = %d\n", gm->M);
282 printf("# %.1f Mc/s\n", Mcs);
283
284 free(dsq);
285 p7_omx_Destroy(fwd);
286 p7_omx_Destroy(bck);
287 p7_omx_Destroy(pp);
288 p7_oprofile_Destroy(om);
289 p7_profile_Destroy(gm);
290 p7_bg_Destroy(bg);
291 p7_hmm_Destroy(hmm);
292 p7_hmmfile_Close(hfp);
293 esl_alphabet_Destroy(abc);
294 esl_stopwatch_Destroy(w);
295 esl_randomness_Destroy(r);
296 esl_getopts_Destroy(go);
297 return eslOK;
298 }
299 #endif /*p7DECODING_BENCHMARK*/
300 /*------------------ end, benchmark driver ----------------------*/
301
302
303
304
305 /*****************************************************************
306 * 3. Unit tests
307 *****************************************************************/
308 #ifdef p7DECODING_TESTDRIVE
309 #include "esl_random.h"
310 #include "esl_randomseq.h"
311
312 /* compare results to GDecoding(). */
313 static void
314 utest_decoding(ESL_RANDOMNESS *r, ESL_ALPHABET *abc, P7_BG *bg, int M, int L, int N, float tolerance)
315 {
316 char *msg = "decoding unit test failed";
317 P7_HMM *hmm = NULL;
318 P7_PROFILE *gm = NULL;
319 P7_OPROFILE *om = NULL;
320 ESL_DSQ *dsq = malloc(sizeof(ESL_DSQ) * (L+2));
321 P7_OMX *fwd = p7_omx_Create(M, L, L);
322 P7_OMX *bck = p7_omx_Create(M, L, L);
323 P7_OMX *pp = p7_omx_Create(M, L, L);
324 P7_GMX *gxf = p7_gmx_Create(M, L);
325 P7_GMX *gxb = p7_gmx_Create(M, L);
326 P7_GMX *gxp1 = p7_gmx_Create(M, L);
327 P7_GMX *gxp2 = p7_gmx_Create(M, L);
328 float fsc1, fsc2;
329 float bsc1, bsc2;
330
331 if (p7_oprofile_Sample(r, abc, bg, M, L, &hmm, &gm, &om) != eslOK) esl_fatal(msg);
332 while (N--)
333 {
334 if (esl_rsq_xfIID(r, bg->f, abc->K, L, dsq) != eslOK) esl_fatal(msg);
335 if (p7_Forward (dsq, L, om, fwd, &fsc1) != eslOK) esl_fatal(msg);
336 if (p7_Backward (dsq, L, om, fwd, bck, &bsc1) != eslOK) esl_fatal(msg);
337 if (p7_Decoding(om, fwd, bck, pp) != eslOK) esl_fatal(msg);
338 if (p7_omx_FDeconvert(pp, gxp1) != eslOK) esl_fatal(msg);
339
340 if (p7_GForward (dsq, L, gm, gxf, &fsc2) != eslOK) esl_fatal(msg);
341 if (p7_GBackward(dsq, L, gm, gxb, &bsc2) != eslOK) esl_fatal(msg);
342 if (p7_GDecoding(gm, gxf, gxb, gxp2) != eslOK) esl_fatal(msg);
343
344 //p7_gmx_Dump(stdout, gxp1, p7_DEFAULT);
345 //p7_gmx_Dump(stdout, gxp2, p7_DEFAULT);
346
347 if (p7_gmx_Compare(gxp1, gxp2, tolerance) != eslOK) esl_fatal(msg);
348 }
349
350 p7_gmx_Destroy(gxp1);
351 p7_gmx_Destroy(gxp2);
352 p7_gmx_Destroy(gxf);
353 p7_gmx_Destroy(gxb);
354 p7_omx_Destroy(fwd);
355 p7_omx_Destroy(bck);
356 p7_omx_Destroy(pp);
357 free(dsq);
358 p7_oprofile_Destroy(om);
359 p7_profile_Destroy(gm);
360 p7_hmm_Destroy(hmm);
361 }
362 #endif /*p7DECODING_TESTDRIVE*/
363 /*--------------------- end, unit tests -------------------------*/
364
365
366
367
368 /*****************************************************************
369 * 4. Test driver
370 *****************************************************************/
371 #ifdef p7DECODING_TESTDRIVE
372
373 /*
374 gcc -o decoding_utest -maltivec -g -Wall -I.. -L.. -I../../easel -L../../easel -Dp7DECODING_TESTDRIVE decoding.c -lhmmer -leasel -lm
375 ./decoding_utest
376 */
377 #include <p7_config.h>
378
379 #include <stdio.h>
380 #include <math.h>
381
382 #include "easel.h"
383 #include "esl_getopts.h"
384 #include "esl_random.h"
385 #include "esl_randomseq.h"
386
387 #include "hmmer.h"
388 #include "impl_vmx.h"
389
390 static ESL_OPTIONS options[] = {
391 /* name type default env range togs reqs incomp help docgrp */
392 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show help and usage", 0},
393 { "-s", eslARG_INT, "42", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 },
394 { "-t", eslARG_REAL, "0.01", NULL, NULL, NULL, NULL, NULL, "floating point comparison tolerance", 0 },
395 { "-L", eslARG_INT, "40", NULL, NULL, NULL, NULL, NULL, "length of sampled sequences", 0 },
396 { "-M", eslARG_INT, "40", NULL, NULL, NULL, NULL, NULL, "length of sampled test profile", 0 },
397 { "-N", eslARG_INT, "10", NULL, NULL, NULL, NULL, NULL, "number of sampled test sequences", 0 },
398 { 0,0,0,0,0,0,0,0,0,0},
399 };
400 static char usage[] = "[-options]";
401 static char banner[] = "test driver for VMX posterior decoding";
402
403 int
404 main(int argc, char **argv)
405 {
406 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 0, argc, argv, banner, usage);
407 ESL_RANDOMNESS *r = esl_randomness_CreateFast(esl_opt_GetInteger(go, "-s"));
408 ESL_ALPHABET *abc = esl_alphabet_Create(eslAMINO);
409 P7_BG *bg = p7_bg_Create(abc);
410 int M = esl_opt_GetInteger(go, "-M");
411 int L = esl_opt_GetInteger(go, "-L");
412 int N = esl_opt_GetInteger(go, "-N");
413 float tol = esl_opt_GetReal (go, "-t");
414
415 p7_FLogsumInit();
416
417 utest_decoding(r, abc, bg, M, L, N, tol);
418
419 esl_getopts_Destroy(go);
420 esl_randomness_Destroy(r);
421 esl_alphabet_Destroy(abc);
422 p7_bg_Destroy(bg);
423 return eslOK;
424 }
425 #endif /*p7DECODING_TESTDRIVE*/
426 /*-------------------- end, test driver -------------------------*/
427
428
429
430