Debian Codegraph @

hmmer/src/impl_sse/null2.c

raw symbols SCIP
1 /* "null2" model, biased composition correction; SSE implementations.
2 *
3 * Contents:
4 * 1. Null2 estimation algorithms.
5 * 2. Benchmark driver.
6 * 3. Unit tests.
7 * 4. Test driver.
8 *
9 * SRE, Mon Aug 18 08:31:11 2008 [Janelia]
10 */
11 #include <p7_config.h>
12
13 #include <stdlib.h>
14 #include <string.h>
15
16 #include <xmmintrin.h> /* SSE */
17 #include <emmintrin.h> /* SSE2 */
18
19 #include "easel.h"
20 #include "esl_sse.h"
21 #include "esl_vectorops.h"
22
23 #include "hmmer.h"
24 #include "impl_sse.h"
25
26 /*****************************************************************
27 * 1. Null2 estimation algorithms.
28 *****************************************************************/
29
30 /* Function: p7_Null2_ByExpectation()
31 * Synopsis: Calculate null2 model from posterior probabilities.
32 * Incept: SRE, Mon Aug 18 08:32:55 2008 [Janelia]
33 *
34 * Purpose: Identical to <p7_GNull2_ByExpectation()> except that
35 * <om>, <pp> are SSE optimized versions of the profile
36 * and the residue posterior probability matrix. See
37 * <p7_GNull2_ByExpectation()> documentation.
38 *
39 * Args: om - profile, in any mode, target length model set to <L>
40 * pp - posterior prob matrix, for <om> against domain envelope <dsq+i-1> (offset)
41 * null2 - RETURN: null2 log odds scores per residue; <0..Kp-1>; caller allocated space
42 */
43 int
44 p7_Null2_ByExpectation(const P7_OPROFILE *om, const P7_OMX *pp, float *null2)
45 {
46 int M = om->M;
47 int Ld = pp->L;
48 int Q = p7O_NQF(M);
49 float *xmx = pp->xmx; /* enables use of XMXo(i,s) macro */
50 float norm;
51 __m128 *rp;
52 __m128 sv;
53 float xfactor;
54 int i,q,x;
55
56 /* Calculate expected # of times that each emitting state was used
57 * in generating the Ld residues in this domain.
58 * The 0 row in <wrk> is used to hold these numbers.
59 */
60 memcpy(pp->dpf[0], pp->dpf[1], sizeof(__m128) * 3 * Q);
61 XMXo(0,p7X_N) = XMXo(1,p7X_N);
62 XMXo(0,p7X_C) = XMXo(1,p7X_C); /* 0.0 */
63 XMXo(0,p7X_J) = XMXo(1,p7X_J); /* 0.0 */
64
65 for (i = 2; i <= Ld; i++)
66 {
67 for (q = 0; q < Q; q++)
68 {
69 pp->dpf[0][q*3 + p7X_M] = _mm_add_ps(pp->dpf[i][q*3 + p7X_M], pp->dpf[0][q*3 + p7X_M]);
70 pp->dpf[0][q*3 + p7X_I] = _mm_add_ps(pp->dpf[i][q*3 + p7X_I], pp->dpf[0][q*3 + p7X_I]);
71 }
72 XMXo(0,p7X_N) += XMXo(i,p7X_N);
73 XMXo(0,p7X_C) += XMXo(i,p7X_C);
74 XMXo(0,p7X_J) += XMXo(i,p7X_J);
75 }
76
77 /* Convert those expected #'s to frequencies, to use as posterior weights. */
78 norm = 1.0 / (float) Ld;
79 sv = _mm_set1_ps(norm);
80 for (q = 0; q < Q; q++)
81 {
82 pp->dpf[0][q*3 + p7X_M] = _mm_mul_ps(pp->dpf[0][q*3 + p7X_M], sv);
83 pp->dpf[0][q*3 + p7X_I] = _mm_mul_ps(pp->dpf[0][q*3 + p7X_I], sv);
84 }
85 XMXo(0,p7X_N) *= norm;
86 XMXo(0,p7X_C) *= norm;
87 XMXo(0,p7X_J) *= norm;
88
89 /* Calculate null2's emission odds, by taking posterior weighted sum
90 * over all emission vectors used in paths explaining the domain.
91 */
92 xfactor = XMXo(0, p7X_N) + XMXo(0, p7X_C) + XMXo(0, p7X_J);
93 for (x = 0; x < om->abc->K; x++)
94 {
95 sv = _mm_setzero_ps();
96 rp = om->rfv[x];
97 for (q = 0; q < Q; q++)
98 {
99 sv = _mm_add_ps(sv, _mm_mul_ps(pp->dpf[0][q*3 + p7X_M], *rp)); rp++;
100 sv = _mm_add_ps(sv, pp->dpf[0][q*3 + p7X_I]); /* insert odds implicitly 1.0 */
101 // sv = _mm_add_ps(sv, _mm_mul_ps(pp->dpf[0][q*3 + p7X_I], *rp)); rp++;
102 }
103 esl_sse_hsum_ps(sv, &(null2[x]));
104 null2[x] += xfactor;
105 }
106 /* now null2[x] = \frac{f_d(x)}{f_0(x)} for all x in alphabet,
107 * 0..K-1, where f_d(x) are the ad hoc "null2" residue frequencies
108 * for this envelope.
109 */
110
111 /* make valid scores for all degeneracies, by averaging the odds ratios. */
112 esl_abc_FAvgScVec(om->abc, null2);
113 null2[om->abc->K] = 1.0; /* gap character */
114 null2[om->abc->Kp-2] = 1.0; /* nonresidue "*" */
115 null2[om->abc->Kp-1] = 1.0; /* missing data "~" */
116
117 return eslOK;
118 }
119
120
121 /* Function: p7_Null2_ByTrace()
122 * Synopsis: Assign null2 scores to an envelope by the sampling method.
123 * Incept: SRE, Mon Aug 18 10:22:49 2008 [Janelia]
124 *
125 * Purpose: Identical to <p7_GNull2_ByTrace()> except that
126 * <om>, <wrk> are SSE optimized versions of the profile
127 * and the residue posterior probability matrix. See
128 * <p7_GNull2_ByTrace()> documentation.
129 */
130 int
131 p7_Null2_ByTrace(const P7_OPROFILE *om, const P7_TRACE *tr, int zstart, int zend, P7_OMX *wrk, float *null2)
132 {
133 union { __m128 v; float p[4]; } u;
134 int Q = p7O_NQF(om->M);
135 int Ld = 0;
136 float *xmx = wrk->xmx; /* enables use of XMXo macro */
137 float norm;
138 float xfactor;
139 __m128 sv;
140 __m128 *rp;
141 int q, r;
142 int x;
143 int z;
144
145 /* We'll use the i=0 row in wrk for working space: dp[0][] and xmx[][0]. */
146 for (q = 0; q < Q; q++)
147 {
148 wrk->dpf[0][q*3 + p7X_M] = _mm_setzero_ps();
149 wrk->dpf[0][q*3 + p7X_I] = _mm_setzero_ps();
150 }
151 XMXo(0,p7X_N) = 0.0;
152 XMXo(0,p7X_C) = 0.0;
153 XMXo(0,p7X_J) = 0.0;
154
155 /* Calculate emitting state usage in this particular trace segment */
156 for (z = zstart; z <= zend; z++)
157 {
158 if (tr->i[z] == 0) continue; /* quick test for whether this trace elem emitted or not */
159 Ld++;
160 if (tr->k[z] > 0) /* must be an M or I */
161 { /* surely there's an easier way? but our workspace is striped, interleaved quads... */
162 // s = ( (tr->st[z] == p7T_M) ? p7X_M : p7X_I); // We don't need the state type <s>, but this is how you'd get it.
163 q = p7X_NSCELLS * ( (tr->k[z] - 1) % Q) + p7X_M;
164 r = (tr->k[z] - 1) / Q;
165 u.v = wrk->dpf[0][q];
166 u.p[r] += 1.0; /* all this to increment a count by one! */
167 wrk->dpf[0][q] = u.v;
168
169 }
170 else /* emitted an x_i with no k; must be an N,C,J */
171 {
172 switch (tr->st[z]) {
173 case p7T_N: XMXo(0,p7X_N) += 1.0; break;
174 case p7T_C: XMXo(0,p7X_C) += 1.0; break;
175 case p7T_J: XMXo(0,p7X_J) += 1.0; break;
176 }
177 }
178 }
179 norm = 1.0 / (float) Ld;
180 sv = _mm_set1_ps(norm);
181 for (q = 0; q < Q; q++)
182 {
183 wrk->dpf[0][q*3 + p7X_M] = _mm_mul_ps(wrk->dpf[0][q*3 + p7X_M], sv);
184 wrk->dpf[0][q*3 + p7X_I] = _mm_mul_ps(wrk->dpf[0][q*3 + p7X_I], sv);
185 }
186 XMXo(0,p7X_N) *= norm;
187 XMXo(0,p7X_C) *= norm;
188 XMXo(0,p7X_J) *= norm;
189
190 /* Calculate null2's emission odds, by taking posterior weighted sum
191 * over all emission vectors used in paths explaining the domain.
192 */
193 xfactor = XMXo(0,p7X_N) + XMXo(0,p7X_C) + XMXo(0,p7X_J);
194 for (x = 0; x < om->abc->K; x++)
195 {
196 sv = _mm_setzero_ps();
197 rp = om->rfv[x];
198 for (q = 0; q < Q; q++)
199 {
200 sv = _mm_add_ps(sv, _mm_mul_ps(wrk->dpf[0][q*3 + p7X_M], *rp)); rp++;
201 sv = _mm_add_ps(sv, wrk->dpf[0][q*3 + p7X_I]); /* insert emission odds implicitly 1.0 */
202 // sv = _mm_add_ps(sv, _mm_mul_ps(wrk->dpf[0][q*3 + p7X_I], *rp)); rp++;
203 }
204 esl_sse_hsum_ps(sv, &(null2[x]));
205 null2[x] += xfactor;
206 }
207 /* now null2[x] = \frac{f_d(x)}{f_0(x)} for all x in alphabet,
208 * 0..K-1, where f_d(x) are the ad hoc "null2" residue frequencies
209 * for this envelope.
210 */
211
212 /* make valid scores for all degeneracies, by averaging the odds ratios. */
213 esl_abc_FAvgScVec(om->abc, null2);
214 null2[om->abc->K] = 1.0; /* gap character */
215 null2[om->abc->Kp-2] = 1.0; /* nonresidue "*" */
216 null2[om->abc->Kp-1] = 1.0; /* missing data "~" */
217
218 return eslOK;
219 }
220
221
222 /*****************************************************************
223 * 2. Benchmark driver
224 *****************************************************************/
225 #ifdef p7NULL2_BENCHMARK
226 /*
227 icc -O3 -static -o null2_benchmark -I.. -L.. -I../../easel -L../../easel -Dp7NULL2_BENCHMARK null2.c -lhmmer -leasel -lm
228 ./null2_benchmark <hmmfile> Does the expectation version.
229 ./null2_benchmark -t <hmmfile> Does the stochastic-traceback-dependent version.
230 (This version isn't really dependent on M, so Mc/s may not be an appropriate measure.)
231
232 RRM_1 (M=72) Caudal_act (M=136) SMC_N (M=1151)
233 ----------------- ------------------ ---------------
234 21 Aug 2008 3.00u (480 Mc/s) 5.45u (499 Mc/s) 77.56u (297 Mc/s)
235 -t 21 Aug 2008 30.50u (47 Mc/s) 44.96u (61 Mc/s) 32.03u*10 ( 72 Mc/s)
236
237 */
238 #include <p7_config.h>
239
240 #include "easel.h"
241 #include "esl_alphabet.h"
242 #include "esl_getopts.h"
243 #include "esl_random.h"
244 #include "esl_randomseq.h"
245 #include "esl_stopwatch.h"
246
247 #include "hmmer.h"
248 #include "impl_sse.h"
249
250 static ESL_OPTIONS options[] = {
251 /* name type default env range toggles reqs incomp help docgroup*/
252 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
253 { "-s", eslARG_INT, "42", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 },
254 { "-t", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "benchmark the trace-dependent version of null2", 0 },
255 { "-L", eslARG_INT, "400", NULL, "n>0", NULL, NULL, NULL, "length of random target seqs", 0 },
256 { "-N", eslARG_INT, "50000", NULL, "n>0", NULL, NULL, NULL, "number of random target seqs", 0 },
257
258 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
259 };
260 static char usage[] = "[-options] <hmmfile>";
261 static char banner[] = "benchmark driver for null2 estimation, SSE version";
262
263 int
264 main(int argc, char **argv)
265 {
266 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 1, argc, argv, banner, usage);
267 char *hmmfile = esl_opt_GetArg(go, 1);
268 ESL_STOPWATCH *w = esl_stopwatch_Create();
269 ESL_RANDOMNESS *r = esl_randomness_CreateFast(esl_opt_GetInteger(go, "-s"));
270 ESL_ALPHABET *abc = NULL;
271 P7_HMMFILE *hfp = NULL;
272 P7_HMM *hmm = NULL;
273 P7_BG *bg = NULL;
274 P7_PROFILE *gm = NULL;
275 P7_OPROFILE *om = NULL;
276 P7_OMX *ox1 = NULL;
277 P7_OMX *ox2 = NULL;
278 int L = esl_opt_GetInteger(go, "-L");
279 int N = esl_opt_GetInteger(go, "-N");
280 ESL_DSQ *dsq = malloc(sizeof(ESL_DSQ) * (L+2));
281 float null2[p7_MAXCODE];
282 int i,j,d,pos;
283 int nsamples = 200;
284 float fsc, bsc;
285 double Mcs;
286
287 if (p7_hmmfile_Open(hmmfile, NULL, &hfp, NULL) != eslOK) p7_Fail("Failed to open HMM file %s", hmmfile);
288 if (p7_hmmfile_Read(hfp, &abc, &hmm) != eslOK) p7_Fail("Failed to read HMM");
289
290 bg = p7_bg_Create(abc); p7_bg_SetLength(bg, L);
291 gm = p7_profile_Create(hmm->M, abc); p7_ProfileConfig(hmm, bg, gm, L, p7_LOCAL);
292 om = p7_oprofile_Create(gm->M, abc); p7_oprofile_Convert(gm, om);
293 p7_oprofile_ReconfigLength(om, L);
294
295 ox1 = p7_omx_Create(gm->M, L, L);
296 ox2 = p7_omx_Create(gm->M, L, L);
297
298 esl_rsq_xfIID(r, bg->f, abc->K, L, dsq);
299 p7_Forward (dsq, L, om, ox1, &fsc);
300
301 if (esl_opt_GetBoolean(go, "-t"))
302 {
303 P7_TRACE *tr = p7_trace_Create();
304 float *n2sc = malloc(sizeof(float) * (L+1));
305
306 esl_stopwatch_Start(w);
307 for (i = 0; i < N; i++)
308 { /* This is approximately what p7_domaindef.c::region_trace_ensemble() is doing: */
309 for (j = 0; j < nsamples; j++)
310 {
311 p7_StochasticTrace(r, dsq, L, om, ox1, tr);
312 p7_trace_Index(tr);
313 pos = 1;
314 for (d = 0; d < tr->ndom; d++)
315 {
316 p7_Null2_ByTrace(om, tr, tr->tfrom[d], tr->tto[d], ox2, null2);
317 for (; pos <= tr->sqfrom[d]; pos++) n2sc[pos] += 1.0;
318 for (; pos < tr->sqto[d]; pos++) n2sc[pos] += null2[dsq[pos]];
319 }
320 for (; pos <= L; pos++) n2sc[pos] += 1.0;
321 p7_trace_Reuse(tr);
322 }
323
324 for (pos = 1; pos <= L; pos++)
325 n2sc[pos] = logf(n2sc[pos] / nsamples);
326 }
327 esl_stopwatch_Stop(w);
328
329 free(n2sc);
330 p7_trace_Destroy(tr);
331 }
332 else
333 {
334 p7_Backward(dsq, L, om, ox1, ox2, &bsc);
335 p7_Decoding(om, ox1, ox2, ox2);
336
337 esl_stopwatch_Start(w);
338 for (i = 0; i < N; i++)
339 p7_Null2_ByExpectation(om, ox2, null2);
340 esl_stopwatch_Stop(w);
341 }
342
343
344 Mcs = (double) N * (double) L * (double) gm->M * 1e-6 / (double) w->user;
345 esl_stopwatch_Display(stdout, w, "# CPU time: ");
346 printf("# M = %d\n", gm->M);
347 printf("# %.1f Mc/s\n", Mcs);
348
349 free(dsq);
350 p7_omx_Destroy(ox1);
351 p7_omx_Destroy(ox2);
352 p7_oprofile_Destroy(om);
353 p7_profile_Destroy(gm);
354 p7_bg_Destroy(bg);
355 p7_hmm_Destroy(hmm);
356 p7_hmmfile_Close(hfp);
357 esl_alphabet_Destroy(abc);
358 esl_stopwatch_Destroy(w);
359 esl_randomness_Destroy(r);
360 esl_getopts_Destroy(go);
361 return 0;
362 }
363 #endif /*p7NULL2_BENCHMARK*/
364 /*------------------ end, benchmark driver ----------------------*/
365
366
367
368
369 /*****************************************************************
370 * 3. Unit tests
371 *****************************************************************/
372 #ifdef p7NULL2_TESTDRIVE
373 #include "esl_random.h"
374 #include "esl_randomseq.h"
375 #include "esl_vectorops.h"
376
377 /* compare results to GDecoding(). */
378 static void
379 utest_null2_expectation(ESL_RANDOMNESS *r, ESL_ALPHABET *abc, P7_BG *bg, int M, int L, int N, float tolerance)
380 {
381 char *msg = "decoding unit test failed";
382 P7_HMM *hmm = NULL;
383 P7_PROFILE *gm = NULL;
384 P7_OPROFILE *om = NULL;
385 ESL_DSQ *dsq = malloc(sizeof(ESL_DSQ) * (L+2));
386 P7_OMX *fwd = p7_omx_Create(M, L, L);
387 P7_OMX *bck = p7_omx_Create(M, L, L);
388 P7_OMX *pp = p7_omx_Create(M, L, L);
389 P7_GMX *gxf = p7_gmx_Create(M, L);
390 P7_GMX *gxb = p7_gmx_Create(M, L);
391 P7_GMX *gpp = p7_gmx_Create(M, L);
392 float *on2 = malloc(sizeof(float) * abc->Kp);
393 float *gn2 = malloc(sizeof(float) * abc->Kp);
394 float fsc1, fsc2;
395 float bsc1, bsc2;
396
397 if (!gn2 || !on2) esl_fatal(msg);
398
399 if (p7_oprofile_Sample(r, abc, bg, M, L, &hmm, &gm, &om) != eslOK) esl_fatal(msg);
400 while (N--)
401 {
402 if (esl_rsq_xfIID(r, bg->f, abc->K, L, dsq) != eslOK) esl_fatal(msg);
403 if (p7_Forward (dsq, L, om, fwd, &fsc1) != eslOK) esl_fatal(msg);
404 if (p7_Backward (dsq, L, om, fwd, bck, &bsc1) != eslOK) esl_fatal(msg);
405 if (p7_Decoding(om, fwd, bck, pp) != eslOK) esl_fatal(msg);
406 if (p7_Null2_ByExpectation(om, pp, on2) != eslOK) esl_fatal(msg);
407
408 if (p7_GForward (dsq, L, gm, gxf, &fsc2) != eslOK) esl_fatal(msg);
409 if (p7_GBackward(dsq, L, gm, gxb, &bsc2) != eslOK) esl_fatal(msg);
410 if (p7_GDecoding(gm, gxf, gxb, gpp) != eslOK) esl_fatal(msg);
411 if (p7_GNull2_ByExpectation(gm, gpp, gn2) != eslOK) esl_fatal(msg);
412
413 if (esl_vec_FCompare(gn2, on2, abc->Kp, tolerance) != eslOK) esl_fatal(msg);
414 }
415
416 p7_gmx_Destroy(gpp);
417 p7_gmx_Destroy(gxf);
418 p7_gmx_Destroy(gxb);
419 p7_omx_Destroy(pp);
420 p7_omx_Destroy(fwd);
421 p7_omx_Destroy(bck);
422 free(on2);
423 free(gn2);
424 free(dsq);
425 p7_oprofile_Destroy(om);
426 p7_profile_Destroy(gm);
427 p7_hmm_Destroy(hmm);
428 }
429 #endif /*p7NULL2_TESTDRIVE*/
430 /*--------------------- end, unit tests -------------------------*/
431
432
433
434
435 /*****************************************************************
436 * 4. Test driver
437 *****************************************************************/
438 #ifdef p7NULL2_TESTDRIVE
439 /*
440 gcc -g -Wall -msse2 -std=gnu99 -o null2_utest -I.. -L.. -I../../easel -L../../easel -Dp7NULL2_TESTDRIVE null2.c -lhmmer -leasel -lm
441 ./null2_utest
442 */
443 #include <p7_config.h>
444
445 #include "easel.h"
446 #include "esl_alphabet.h"
447 #include "esl_getopts.h"
448 #include "esl_random.h"
449
450 #include "hmmer.h"
451 #include "impl_sse.h"
452
453 static ESL_OPTIONS options[] = {
454 /* name type default env range toggles reqs incomp help docgroup*/
455 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
456 { "-s", eslARG_INT, "42", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 },
457 { "-t", eslARG_REAL, "0.01", NULL, NULL, NULL, NULL, NULL, "floating point comparison tolerance", 0 },
458 { "-L", eslARG_INT, "100", NULL, NULL, NULL, NULL, NULL, "size of random sequences to sample", 0 },
459 { "-M", eslARG_INT, "72", NULL, NULL, NULL, NULL, NULL, "size of random models to sample", 0 },
460 { "-N", eslARG_INT, "10", NULL, NULL, NULL, NULL, NULL, "number of random sequences to sample", 0 },
461 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
462 };
463 static char usage[] = "[-options]";
464 static char banner[] = "test driver for SSE implementation of null2 model";
465
466 int
467 main(int argc, char **argv)
468 {
469 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 0, argc, argv, banner, usage);
470 ESL_RANDOMNESS *r = esl_randomness_CreateFast(esl_opt_GetInteger(go, "-s"));
471 ESL_ALPHABET *abc = esl_alphabet_Create(eslAMINO);
472 P7_BG *bg = p7_bg_Create(abc);
473 int M = esl_opt_GetInteger(go, "-M");
474 int L = esl_opt_GetInteger(go, "-L");
475 int N = esl_opt_GetInteger(go, "-N");
476 float tol = esl_opt_GetReal (go, "-t");
477
478 p7_FLogsumInit();
479
480 utest_null2_expectation(r, abc, bg, M, L, N, tol);
481
482 esl_getopts_Destroy(go);
483 esl_randomness_Destroy(r);
484 esl_alphabet_Destroy(abc);
485 p7_bg_Destroy(bg);
486 return eslOK;
487 }
488 #endif /*p7NULL2_TESTDRIVE*/
489 /*-------------------- end, test driver -------------------------*/
490
491
492
493
494
495