Debian Codegraph @

hmmer/src/impl_neon/null2.c

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