Debian Codegraph @

hmmer/src/impl_sse/decoding.c

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