Debian Codegraph @

hmmer/src/p7_gmx.c

raw symbols SCIP
1 /* P7_GMX implementation: a generic dynamic programming matrix
2 *
3 * Contents:
4 * 1. The <P7_GMX> object
5 * 2. Debugging aids
6 * 3. Unit tests
7 * 4. Test driver
8 */
9 #include <p7_config.h>
10
11 #include "hmmer.h"
12
13 /*****************************************************************
14 *= 1. The <P7_GMX> object.
15 *****************************************************************/
16
17 /* Function: p7_gmx_Create()
18 * Synopsis: Allocate a new <P7_GMX>.
19 *
20 * Purpose: Allocate a reusable, resizeable <P7_GMX> for models up to
21 * size <allocM> and sequences up to length <allocL>.
22 *
23 * We've set this up so it should be easy to allocate
24 * aligned memory, though we're not doing this yet.
25 *
26 * Returns: a pointer to the new <P7_GMX>.
27 *
28 * Throws: <NULL> on allocation error.
29 */
30 P7_GMX *
31 p7_gmx_Create(int allocM, int allocL)
32 {
33 int status;
34 P7_GMX *gx = NULL;
35 int i;
36
37 /* don't try to make large allocs on 32-bit systems */
38 if ( (uint64_t) (allocM+1) * (uint64_t) (allocL+1) * sizeof(float) * p7G_NSCELLS > SIZE_MAX / 2)
39 return NULL;
40
41 /* level 1: the structure itself */
42 ESL_ALLOC(gx, sizeof(P7_GMX));
43 gx->dp = NULL;
44 gx->xmx = NULL;
45 gx->dp_mem = NULL;
46
47 /* level 2: row pointers, 0.1..L; and dp cell memory */
48 ESL_ALLOC(gx->dp, sizeof(float *) * (allocL+1));
49 ESL_ALLOC(gx->xmx, sizeof(float) * (allocL+1) * p7G_NXCELLS);
50 ESL_ALLOC(gx->dp_mem, sizeof(float) * (allocL+1) * (allocM+1) * p7G_NSCELLS);
51
52 /* Set the row pointers. */
53 for (i = 0; i <= allocL; i++)
54 gx->dp[i] = gx->dp_mem + (ptrdiff_t) i * (ptrdiff_t) (allocM+1) * (ptrdiff_t) p7G_NSCELLS;
55
56 /* Initialize memory that's allocated but unused, only to keep
57 * valgrind and friends happy.
58 */
59 for (i = 0; i <= allocL; i++)
60 {
61 gx->dp[i][0 * p7G_NSCELLS + p7G_M] = -eslINFINITY; /* M_0 */
62 gx->dp[i][0 * p7G_NSCELLS + p7G_I] = -eslINFINITY; /* I_0 */
63 gx->dp[i][0 * p7G_NSCELLS + p7G_D] = -eslINFINITY; /* D_0 */
64 gx->dp[i][1 * p7G_NSCELLS + p7G_D] = -eslINFINITY; /* D_1 */
65 gx->dp[i][allocM * p7G_NSCELLS + p7G_I] = -eslINFINITY; /* I_M */
66 }
67
68 gx->M = 0;
69 gx->L = 0;
70 gx->allocW = allocM+1;
71 gx->allocR = allocL+1;
72 gx->validR = allocL+1;
73 gx->ncells = (uint64_t) (allocM+1)* (uint64_t) (allocL+1);
74 return gx;
75
76 ERROR:
77 if (gx != NULL) p7_gmx_Destroy(gx);
78 return NULL;
79 }
80
81 /* Function: p7_gmx_GrowTo()
82 * Synopsis: Assure that DP matrix is big enough.
83 *
84 * Purpose: Assures that a DP matrix <gx> is allocated
85 * for a model of size up to <M> and a sequence of
86 * length up to <L>; reallocates if necessary.
87 *
88 * This function does not respect the configured
89 * <RAMLIMIT>; it will allocate what it's told to
90 * allocate.
91 *
92 * Returns: <eslOK> on success, and <gx> may be reallocated upon
93 * return; any data that may have been in <gx> must be
94 * assumed to be invalidated.
95 *
96 * Throws: <eslEMEM> on allocation failure, and any data that may
97 * have been in <gx> must be assumed to be invalidated.
98 */
99 int
100 p7_gmx_GrowTo(P7_GMX *gx, int M, int L)
101 {
102 int status;
103 void *p;
104 int i;
105 uint64_t ncells;
106 int do_reset = FALSE;
107
108 if (M < gx->allocW && L < gx->validR) return eslOK;
109
110 /* don't try to make large allocs on 32-bit systems */
111 if ( (uint64_t) (M+1) * (uint64_t) (L+1) * sizeof(float) * p7G_NSCELLS > SIZE_MAX / 2) return eslEMEM;
112
113 /* must we realloc the 2D matrices? (or can we get away with just
114 * jiggering the row pointers, if we are growing in one dimension
115 * while shrinking in another?)
116 */
117 ncells = (uint64_t) (M+1) * (uint64_t) (L+1);
118 if (ncells > gx->ncells)
119 {
120 ESL_RALLOC(gx->dp_mem, p, sizeof(float) * ncells * p7G_NSCELLS);
121 gx->ncells = ncells;
122 do_reset = TRUE;
123 }
124
125 /* must we reallocate the row pointers? */
126 if (L >= gx->allocR)
127 {
128 ESL_RALLOC(gx->xmx, p, sizeof(float) * (L+1) * p7G_NXCELLS);
129 ESL_RALLOC(gx->dp, p, sizeof(float *) * (L+1));
130 gx->allocR = L+1; /* allocW will also get set, in the do_reset block */
131 do_reset = TRUE;
132 }
133
134 /* must we widen the rows? */
135 if (M >= gx->allocW) do_reset = TRUE;
136
137 /* must we set some more valid row pointers? */
138 if (L >= gx->validR) do_reset = TRUE;
139
140 /* resize the rows and reset all the valid row pointers.*/
141 if (do_reset)
142 {
143 gx->allocW = M+1;
144 gx->validR = ESL_MIN(gx->ncells / gx->allocW, gx->allocR);
145 for (i = 0; i < gx->validR; i++)
146 gx->dp[i] = gx->dp_mem + (ptrdiff_t) i * (ptrdiff_t) (gx->allocW) * (ptrdiff_t) p7G_NSCELLS;
147 }
148
149 gx->M = 0;
150 gx->L = 0;
151 return eslOK;
152
153 ERROR:
154 return status;
155 }
156
157 /* Function: p7_gmx_Sizeof()
158 * Synopsis: Returns the allocation size of DP matrix, in bytes.
159 */
160 size_t
161 p7_gmx_Sizeof(P7_GMX *gx)
162 {
163 size_t n = 0;
164
165 n += sizeof(P7_GMX);
166 n += gx->ncells * p7G_NSCELLS * sizeof(float); /* main dp cells: gx->dp_mem */
167 n += gx->allocR * sizeof(float *); /* row ptrs: gx->dp[] */
168 n += gx->allocR * p7G_NXCELLS * sizeof(float); /* specials: gx->xmx */
169 return n;
170 }
171
172
173
174 /* Function: p7_gmx_Reuse()
175 * Synopsis: Recycle a generic DP matrix.
176 *
177 * Purpose: Recycles <gx> for reuse.
178 *
179 * Returns: <eslOK> on success.
180 */
181 int
182 p7_gmx_Reuse(P7_GMX *gx)
183 {
184 /* not much to do here. The memory rearrangement for a new seq is all in GrowTo(). */
185 gx->M = 0;
186 gx->L = 0;
187 return eslOK;
188 }
189
190
191 /* Function: p7_gmx_Destroy()
192 * Synopsis: Frees a DP matrix.
193 *
194 * Purpose: Frees a <P7_GMX>.
195 *
196 * Returns: (void)
197 */
198 void
199 p7_gmx_Destroy(P7_GMX *gx)
200 {
201 if (gx == NULL) return;
202
203 if (gx->dp != NULL) free(gx->dp);
204 if (gx->xmx != NULL) free(gx->xmx);
205 if (gx->dp_mem != NULL) free(gx->dp_mem);
206 free(gx);
207 return;
208 }
209
210 /*****************************************************************
211 * 2. Debugging aids
212 *****************************************************************/
213
214 /* Function: p7_gmx_Compare()
215 * Synopsis: Compare two DP matrices for equality within given tolerance.
216 *
217 * Purpose: Compare all the values in DP matrices <gx1> and <gx2> using
218 * <esl_FCompare_old()> and relative epsilon <tolerance>. If any
219 * value pairs differ by more than the acceptable <tolerance>
220 * return <eslFAIL>. If all value pairs are identical within
221 * tolerance, return <eslOK>.
222 */
223 int
224 p7_gmx_Compare(P7_GMX *gx1, P7_GMX *gx2, float tolerance)
225 {
226 int i,k,x;
227 if (gx1->M != gx2->M) return eslFAIL;
228 if (gx1->L != gx2->L) return eslFAIL;
229
230 for (i = 0; i <= gx1->L; i++)
231 {
232 for (k = 1; k <= gx1->M; k++) /* k=0 is a boundary; doesn't need to be checked */
233 {
234 if (esl_FCompare_old(gx1->dp[i][k * p7G_NSCELLS + p7G_M], gx2->dp[i][k * p7G_NSCELLS + p7G_M], tolerance) != eslOK) return eslFAIL;
235 if (esl_FCompare_old(gx1->dp[i][k * p7G_NSCELLS + p7G_I], gx2->dp[i][k * p7G_NSCELLS + p7G_I], tolerance) != eslOK) return eslFAIL;
236 if (esl_FCompare_old(gx1->dp[i][k * p7G_NSCELLS + p7G_D], gx2->dp[i][k * p7G_NSCELLS + p7G_D], tolerance) != eslOK) return eslFAIL;
237 }
238 for (x = 0; x < p7G_NXCELLS; x++)
239 if (esl_FCompare_old(gx1->xmx[i * p7G_NXCELLS + x], gx2->xmx[i * p7G_NXCELLS + x], tolerance) != eslOK) return eslFAIL;
240 }
241 return eslOK;
242 }
243
244
245
246 /* Function: p7_gmx_Dump()
247 * Synopsis: Dump a DP matrix to a stream, for diagnostics.
248 *
249 * Purpose: Dump matrix <gx> to stream <fp> for diagnostics.
250 *
251 * <flags> control some optional output behaviors, as follows:
252 * | <p7_HIDE_SPECIALS> | don't show scores for <ENJBC> states |
253 * | <p7_SHOW_LOG> | <gx> is in probs; show as log probs |
254 */
255 int
256 p7_gmx_Dump(FILE *ofp, P7_GMX *gx, int flags)
257 {
258 return p7_gmx_DumpWindow(ofp, gx, 0, gx->L, 0, gx->M, flags);
259 }
260
261
262 /* Function: p7_gmx_DumpWindow()
263 * Synopsis: Dump a window of a DP matrix to a stream for diagnostics.
264 *
265 * Purpose: Dump a window of matrix <gx> to stream <fp> for diagnostics,
266 * from row <istart> to <iend>, from column <kstart> to <kend>.
267 *
268 * Asking for <0..L,0..M> with <flags=p7_SHOW_SPECIALS> is the
269 * same as <p7_gmx_Dump()>.
270 *
271 * <flags> control some optional output behaviors, as follows:
272 * | <p7_HIDE_SPECIALS> | don't show scores for <ENJBC> states |
273 * | <p7_SHOW_LOG> | <gx> is in probs; show as log probs |
274 *
275 * Returns: <eslOK> on success.
276 */
277 int
278 p7_gmx_DumpWindow(FILE *ofp, P7_GMX *gx, int istart, int iend, int kstart, int kend, int flags)
279 {
280 int width = 9;
281 int precision = 4;
282 int i, k, x;
283 float val;
284
285 /* Header */
286 fprintf(ofp, " ");
287 for (k = kstart; k <= kend; k++) fprintf(ofp, "%*d ", width, k);
288 if (! (flags & p7_HIDE_SPECIALS)) fprintf(ofp, "%*s %*s %*s %*s %*s\n", width, "E", width, "N", width, "J", width, "B", width, "C");
289 fprintf(ofp, " ");
290 for (k = kstart; k <= kend; k++) fprintf(ofp, "%*.*s ", width, width, "----------");
291 if (! (flags & p7_HIDE_SPECIALS))
292 for (x = 0; x < 5; x++) fprintf(ofp, "%*.*s ", width, width, "----------");
293 fprintf(ofp, "\n");
294
295 /* DP matrix data */
296 for (i = istart; i <= iend; i++)
297 {
298 fprintf(ofp, "%3d M ", i);
299 for (k = kstart; k <= kend; k++)
300 {
301 val = gx->dp[i][k * p7G_NSCELLS + p7G_M];
302 if (flags & p7_SHOW_LOG) val = log(val);
303 fprintf(ofp, "%*.*f ", width, precision, val);
304 }
305 if (! (flags & p7_HIDE_SPECIALS))
306 {
307 for (x = 0; x < p7G_NXCELLS; x++)
308 {
309 val = gx->xmx[ i * p7G_NXCELLS + x];
310 if (flags & p7_SHOW_LOG) val = log(val);
311 fprintf(ofp, "%*.*f ", width, precision, val);
312 }
313 }
314 fprintf(ofp, "\n");
315
316 fprintf(ofp, "%3d I ", i);
317 for (k = kstart; k <= kend; k++)
318 {
319 val = gx->dp[i][k * p7G_NSCELLS + p7G_I];
320 if (flags & p7_SHOW_LOG) val = log(val);
321 fprintf(ofp, "%*.*f ", width, precision, val);
322 }
323 fprintf(ofp, "\n");
324
325 fprintf(ofp, "%3d D ", i);
326 for (k = kstart; k <= kend; k++)
327 {
328 val = gx->dp[i][k * p7G_NSCELLS + p7G_D];
329 if (flags & p7_SHOW_LOG) val = log(val);
330 fprintf(ofp, "%*.*f ", width, precision, val);
331 }
332 fprintf(ofp, "\n\n");
333 }
334 return eslOK;
335 }
336
337
338 /*****************************************************************
339 * 3. Unit tests
340 *****************************************************************/
341 #ifdef p7GMX_TESTDRIVE
342 #include "esl_random.h"
343 #include "esl_randomseq.h"
344
345 /* gmx_testpattern()
346 *
347 * Write a test pattern into the cells of <gx>, and read it back
348 * in different ways, to test that the memory is laid out as expected.
349 *
350 * <gmx> stores floats, and the test pattern depends on numbering each
351 * dp cell with an integer, so we must make sure our count doesn't
352 * exceed the largest exactly representable integer in a float:
353 * 2^24 = 16777216. Limiting the test pattern to 2^24 cells also helps
354 * keep runtime down.
355 */
356 static void
357 gmx_testpattern(P7_GMX *gx, int M, int L)
358 {
359 int64_t i; // among other things, i counts thru gx->ncells, which is int64_t
360 int64_t n,n2; // test pattern counts cells, so n and n2 also have to be int64_t
361 int k,s;
362 int maxL = 16777216 / (3 * (M+1));
363 int64_t start_row = (L > maxL ? L-maxL+1 : 0);
364
365 /* Write a test pattern, via the dp[i] pointers */
366 n = 0;
367 for (i = start_row; i <= L; i++)
368 for (k = 0; k <= M; k++)
369 for (s = 0; s < p7G_NSCELLS; s++)
370 gx->dp[i][k*p7G_NSCELLS+s] = (float) n++;
371
372 /* Read it back, via the dp[i] pointers */
373 n = 0;
374 for (i = start_row; i <= L; i++)
375 for (k = 0; k <= M; k++)
376 for (s = 0; s < p7G_NSCELLS; s++)
377 {
378 if ((int) gx->dp[i][k*p7G_NSCELLS+s] != n) esl_fatal("gmx unit test failed: test pattern corrupted");
379 n++;
380 }
381
382 /* Reading it back via the dp_mem vector itself ought to be the same */
383 if (gx->allocR == gx->validR && gx->ncells == (int64_t) gx->validR * (int64_t) gx->allocW)
384 {
385 n2 = 0;
386 for (i = start_row * gx->allocW; i < gx->ncells; i++)
387 for (s = 0; s < p7G_NSCELLS; s++)
388 {
389 if (gx->dp_mem[i*p7G_NSCELLS+s] != n2) esl_fatal("gmx unit test failed: test pattern corrupted (2nd test)");
390 n2++;
391 }
392 /* and the number of cells ought to match too */
393 if (n != n2) esl_fatal("gmx unit test failed: unexpected # of cells");
394 }
395 }
396
397
398 static void
399 utest_GrowTo(void)
400 {
401 P7_GMX *gx = NULL;
402 int M, L;
403 #if !defined(eslENABLE_ASAN) && !defined(eslENABLE_TSAN)
404 int64_t nbytes;
405 #endif
406
407 M = 20; L = 20; gx= p7_gmx_Create(M, L); gmx_testpattern(gx, M, L);
408 M = 40; L = 20; p7_gmx_GrowTo(gx, M, L); gmx_testpattern(gx, M, L); /* grow in M, not L */
409 M = 40; L = 40; p7_gmx_GrowTo(gx, M, L); gmx_testpattern(gx, M, L); /* grow in L, not M */
410 M = 80; L = 10; p7_gmx_GrowTo(gx, M, L); gmx_testpattern(gx, M, L); /* grow in M, but with enough ncells */
411 M = 10; L = 80; p7_gmx_GrowTo(gx, M, L); gmx_testpattern(gx, M, L); /* grow in L, but with enough ncells */
412 M = 100; L = 100; p7_gmx_GrowTo(gx, M, L); gmx_testpattern(gx, M, L); /* grow in both L and M */
413
414 /* The next two calls are carefully constructed to exercise bug #h79.
415 * GrowTo() must shrink allocW, if M shrinks and L grows enough to force increase in allocR, with sufficient ncells.
416 */
417 M = 179; L = 55; p7_gmx_GrowTo(gx, M, L); gmx_testpattern(gx, M, L);
418 M = 87; L = 57; p7_gmx_GrowTo(gx, M, L); gmx_testpattern(gx, M, L);
419
420 /* and this exercises iss#176. Only do this if a large alloc is possible (we need 8.6G to exercise the bug!) */
421 #if !defined(eslENABLE_ASAN) && !defined(eslENABLE_TSAN) // I've seen asan/tsan fail here in a Linux VM just because of the large alloc
422 M = 71582; L = 10000;
423 nbytes = (int64_t) (M+1) * (int64_t) (L+1) * (int64_t) p7G_NSCELLS * (int64_t) sizeof(float);
424 if ( nbytes < SIZE_MAX / 2)
425 {
426 void *p = malloc(nbytes); // check that the allocation succeeds at all
427 if (p != NULL)
428 {
429 free(p);
430 p7_gmx_GrowTo(gx, M, L); gmx_testpattern(gx, M, L);
431 }
432 }
433 #endif
434
435 p7_gmx_Destroy(gx);
436 }
437
438 static void
439 utest_Compare(ESL_RANDOMNESS *r, P7_PROFILE *gm, P7_BG *bg, int L, float tolerance)
440 {
441 char *msg = "gmx_Compare unit test failure";
442 ESL_DSQ *dsq = malloc(sizeof(ESL_DSQ) *(L+2));
443 P7_GMX *gx1 = p7_gmx_Create(gm->M, L);
444 P7_GMX *gx2 = p7_gmx_Create(5, 4);
445 float fsc;
446
447 if (!r || !gm || !dsq || !gx1 || !gx2 ) esl_fatal(msg);
448 if (esl_rsq_xfIID(r, bg->f, gm->abc->K, L, dsq) != eslOK) esl_fatal(msg);
449 if (p7_gmx_GrowTo(gx2, gm->M, L) != eslOK) esl_fatal(msg);
450 if (p7_GForward(dsq, L, gm, gx1, &fsc) != eslOK) esl_fatal(msg);
451 if (p7_GForward(dsq, L, gm, gx2, &fsc) != eslOK) esl_fatal(msg);
452 if (p7_gmx_Compare(gx1, gx2, tolerance) != eslOK) esl_fatal(msg);
453
454 p7_gmx_Destroy(gx1);
455 p7_gmx_Destroy(gx2);
456 free(dsq);
457 }
458
459 #endif /*p7GMX_TESTDRIVE*/
460 /*------------------- end, unit tests ---------------------------*/
461
462
463 /*****************************************************************
464 * 4. Test driver
465 *****************************************************************/
466 #ifdef p7GMX_TESTDRIVE
467 /*
468 gcc -o p7_gmx_utest -msse2 -g -Wall -I. -L. -I../easel -L../easel -Dp7GMX_TESTDRIVE p7_gmx.c -lhmmer -leasel -lm
469 ./p7_gmx_utest
470 */
471 #include <p7_config.h>
472
473 #include <stdio.h>
474 #include <math.h>
475
476 #include "easel.h"
477 #include "esl_getopts.h"
478 #include "esl_random.h"
479 #include "esl_randomseq.h"
480
481 #include "hmmer.h"
482
483 static ESL_OPTIONS options[] = {
484 /* name type default env range togs reqs incomp help docgrp */
485 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show help and usage", 0},
486 { "-s", eslARG_INT, "42", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 },
487 { "-t", eslARG_REAL, "1e-5", NULL, NULL, NULL, NULL, NULL, "floating point comparison tolerance", 0 },
488 { "-L", eslARG_INT, "40", NULL, NULL, NULL, NULL, NULL, "length of sampled sequences", 0 },
489 { "-M", eslARG_INT, "40", NULL, NULL, NULL, NULL, NULL, "length of sampled test profile", 0 },
490 { 0,0,0,0,0,0,0,0,0,0},
491 };
492 static char usage[] = "[-options]";
493 static char banner[] = "test driver for p7_gmx.c";
494
495 int
496 main(int argc, char **argv)
497 {
498 char *msg = "p7_gmx unit test driver failed";
499 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 0, argc, argv, banner, usage);
500 ESL_RANDOMNESS *r = esl_randomness_CreateFast(esl_opt_GetInteger(go, "-s"));
501 ESL_ALPHABET *abc = esl_alphabet_Create(eslAMINO);
502 P7_BG *bg = p7_bg_Create(abc);
503 P7_HMM *hmm = NULL;
504 P7_PROFILE *gm = NULL;
505 int M = esl_opt_GetInteger(go, "-M");
506 int L = esl_opt_GetInteger(go, "-L");
507 float tol = esl_opt_GetReal (go, "-t");
508
509 p7_FLogsumInit();
510
511 if (p7_hmm_Sample(r, M, abc, &hmm) != eslOK) esl_fatal(msg);
512 if ((gm = p7_profile_Create(hmm->M, abc)) == NULL) esl_fatal(msg);
513 if (p7_bg_SetLength(bg, L) != eslOK) esl_fatal(msg);
514 if (p7_ProfileConfig(hmm, bg, gm, L, p7_UNILOCAL) != eslOK) esl_fatal(msg);
515
516 utest_GrowTo();
517 utest_Compare(r, gm, bg, L, tol);
518
519 esl_getopts_Destroy(go);
520 esl_randomness_Destroy(r);
521 esl_alphabet_Destroy(abc);
522 p7_bg_Destroy(bg);
523 p7_hmm_Destroy(hmm);
524 p7_profile_Destroy(gm);
525 return eslOK;
526 }
527 #endif /*p7GMX_TESTDRIVE*/
528 /*------------------ end, test driver ---------------------------*/
529
530
531