Debian Codegraph @

hmmer/src/p7_gmxchk.c

raw symbols SCIP
1 /* P7_GMXCHK implementation
2 *
3 * Checkpointed forward/backward dynamic programming matrix.
4 * Derived from P7_GMX structure; see p7_gmx.[ch]
5 *
6 * Contents:
7 * 1. The <P7_GMXCHK> object.
8 * 2. Debugging and testing tools.
9 * 3. Internal (static) routines.
10 * 4. Unit tests.
11 * 5. Test driver.
12 * 6. References.
13 */
14 #include <p7_config.h>
15
16 #include <stdlib.h>
17 #include <math.h>
18
19 #include "hmmer.h"
20 #include "p7_gmxchk.h"
21
22
23 static int set_row_layout (P7_GMXCHK *gxc, int allocL, int maxR);
24 static void set_full (P7_GMXCHK *gxc, int L);
25 static void set_checkpointed(P7_GMXCHK *gxc, int L, int R);
26 static void set_redlined (P7_GMXCHK *gxc, int L, double minR);
27
28 static double minimum_rows (int L);
29 static double checkpointed_rows(int L, int R);
30
31
32 /*****************************************************************
33 *= 1. The <P7_GMXCHK> object
34 *****************************************************************/
35
36 /* Function: p7_gmxchk_Create()
37 * Synopsis: Allocate a new <P7_GMXCHK> matrix.
38 *
39 * Purpose: Allocate a new <P7_GMXCHK> matrix sufficient to
40 * hold the checkpointed forward and two-row backwards
41 * DP matrices for a comparison of a query model of
42 * length <M> and a target sequence of length <L>.
43 *
44 * Try to keep allocation within <ramlimit> bytes in
45 * memory. <ramlimit=ESL_MBYTES(128)>, for example, means
46 * a recommended RAM limit of 128 MiB. The allocation can
47 * exceed this, if the <MxL> comparison requires it (if so,
48 * the matrix is fully checkpointed, minimizing the
49 * allocation); but any subsequent <p7_gmxchk_GrowTo()>
50 * call attempting to reuse the matrix will try to
51 * reallocate it back downwards to the <ramlimit>.
52 *
53 * Your choice for <ramlimit> should take into account how
54 * many parallel threads there are, each with its own
55 * <P7_GMXCHK> matrix allocation.
56 *
57 * By design spec, <M> and <L> are $\leq$ 100000.
58 *
59 * Args: M - query profile size in consensus positions (<=100000)
60 * L - target sequence length in residues (<=100000)
61 * ramlimit - recommended memory limit in bytes
62 *
63 * Returns: ptr to new <P7_GMXCHK> object on success.
64 *
65 * Throws: <NULL> on allocation failure.
66 */
67 P7_GMXCHK *
68 p7_gmxchk_Create(int M, int L, int64_t ramlimit)
69 {
70 P7_GMXCHK *gxc = NULL;
71 int maxR;
72 int r;
73 int status;
74
75 /* Validity of integer variable ranges may depend on design spec: */
76 ESL_DASSERT1( (M <= 100000) ); /* design spec says, model length M <= 100000 */
77 ESL_DASSERT1( (L <= 100000) ); /* ... and, seq length L <= 100000 */
78
79 /* Level 1 allocation: the structure itself */
80 ESL_ALLOC(gxc, sizeof(P7_GMXCHK));
81 gxc->dp = NULL;
82 gxc->dp_mem = NULL;
83
84 /* Set allocR, R{abc}, L{abc} fields: row layout, full vs. checkpointed */
85 gxc->R0 = 3; /* fwd[0]; bck[prv,cur] */
86 gxc->allocW = (M+1) * p7G_NSCELLS + p7GC_NXCELLS; /* M+1 because we're [0..M] in DP columns in generic mx. */
87 gxc->ncell_limit = ramlimit / sizeof(float);
88 maxR = (int) (gxc->ncell_limit / (int64_t) gxc->allocW);
89 set_row_layout(gxc, L, maxR);
90 gxc->allocR = gxc->R0 + gxc->Ra + gxc->Rb + gxc->Rc;
91 gxc->validR = gxc->allocR;
92
93 /* Level 2 allocations: row pointers and dp cell memory */
94 gxc->ncells = gxc->allocR * gxc->allocW;
95 ESL_ALLOC( gxc->dp_mem, sizeof(float) * gxc->ncells);
96 ESL_ALLOC( gxc->dp, sizeof(float *) * gxc->allocR);
97 for (r = 0; r < gxc->allocR; r++)
98 gxc->dp[r] = gxc->dp_mem + (r * gxc->allocW);
99
100 #if eslDEBUGLEVEL > 0
101 gxc->do_debugging = FALSE;
102 gxc->dfp = NULL;
103 gxc->dbg_width = 9;
104 gxc->dbg_precision = 4;
105 gxc->dbg_flags = p7_DEFAULT;
106 #endif
107
108 gxc->M = 0;
109 gxc->L = 0;
110 gxc->R = 0;
111 return gxc;
112
113 ERROR:
114 if (gxc) p7_gmxchk_Destroy(gxc);
115 return NULL;
116 }
117
118 /* Function: p7_gmxchk_GrowTo()
119 * Synopsis: Resize checkpointed matrix structure for new comparison.
120 *
121 * Purpose: Given an existing checkpointed matrix structure <gxc>,
122 * and the dimensions <M> and <L> of a new comparison,
123 * reallocate and reinitialize <gxc>.
124 *
125 * Essentially the same as free'ing the previous matrix
126 * and creating a new one -- but trying to minimize
127 * expensive memory allocation/reallocation calls.
128 *
129 * If <gxc> is redlined (over its recommended allocation)
130 * and the new problem size <M,L> can fit in the recommended
131 * allocation, <gxc> is reallocated to the smaller size.
132 *
133 * Args: gxc - existing checkpointed matrix
134 * M - new query profile length
135 * L - new target sequence length
136 *
137 * Returns: <eslOK> on success.
138 *
139 * Throws: <eslEMEM> if an allocation fails. The state of <gxc> is
140 * now undefined, and the caller should not use it.
141 */
142 int
143 p7_gmxchk_GrowTo(P7_GMXCHK *gxc, int M, int L)
144 {
145 int W = (M+1)*p7G_NSCELLS + p7GC_NXCELLS; /* actual row width in cells; <= allocW */
146 int minR_chk = (int) ceil(minimum_rows(L)) + gxc->R0; /* minimum number of DP rows needed */
147 int reset_dp_ptrs = FALSE;
148 int maxR;
149 int r;
150 int status;
151
152 /* Are the current allocations satisfactory ? */
153 if (W <= gxc->allocW && gxc->ncells <= gxc->ncell_limit)
154 {
155 if (L + gxc->R0 <= gxc->validR) { set_full (gxc, L); return eslOK; }
156 else if (minR_chk <= gxc->validR) { set_checkpointed(gxc, L, gxc->validR); return eslOK; }
157 }
158
159 /* Do individual matrix rows need to expand? */
160 if ( W > gxc->allocW)
161 {
162 gxc->allocW = W;
163 gxc->validR = (int) (gxc->ncells / (int64_t) gxc->allocW); /* validR must be <= allocR */
164 reset_dp_ptrs = TRUE;
165 }
166
167 /* Does matrix dp_mem need reallocation, either up or down? */
168 maxR = (int) (gxc->ncell_limit / (int64_t) gxc->allocW); /* max rows if we use up to the recommended allocation size. */
169 if ( (gxc->ncells > gxc->ncell_limit && minR_chk <= maxR) || /* we were redlined, and recommended alloc will work: so downsize */
170 minR_chk > gxc->validR) /* not enough memory for needed rows: so upsize */
171 {
172 set_row_layout(gxc, L, maxR);
173 gxc->validR = gxc->R0 + gxc->Ra + gxc->Rb + gxc->Rc; /* this may be > allocR now; we'll reallocate dp[] next, if so */
174 gxc->ncells = gxc->validR * gxc->allocW;
175 ESL_REALLOC(gxc->dp_mem, sizeof(float) * gxc->ncells);
176 reset_dp_ptrs = TRUE;
177 }
178 else /* current validR will suffice, either full or checkpointed; we still need to calculate a layout */
179 {
180 if (L+gxc->R0 <= gxc->validR) set_full(gxc, L);
181 else set_checkpointed(gxc, L, gxc->validR);
182 }
183
184 /* Does the array of row ptrs need reallocation? */
185 if (gxc->validR > gxc->allocR)
186 {
187 ESL_REALLOC(gxc->dp, sizeof(float *) * gxc->validR);
188 gxc->allocR = gxc->validR;
189 reset_dp_ptrs = TRUE;
190 }
191
192 /* Do the row ptrs need to be reset? */
193 if (reset_dp_ptrs)
194 for (r = 0; r < gxc->validR; r++)
195 gxc->dp[r] = gxc->dp_mem + (r * gxc->allocW);
196
197 gxc->M = 0;
198 gxc->L = 0;
199 gxc->R = 0;
200 return eslOK;
201
202 ERROR:
203 return status;
204 }
205
206
207 /* Function: p7_gmxchk_Sizeof()
208 * Synopsis: Returns the size of a checkpointed generic DP matrix, in bytes.
209 */
210 size_t
211 p7_gmxchk_Sizeof(const P7_GMXCHK *gxc)
212 {
213 size_t n = 0;
214
215 n += sizeof(P7_GMXCHK);
216 n += gxc->ncells * sizeof(float); /* main DP cells */
217 n += gxc->allocR * sizeof(float *); /* row ptrs */
218 return n;
219 }
220
221 /* Function: p7_gmxchk_Reuse()
222 * Synopsis: Recycle a checkpointed generic DP matrix.
223 *
224 * Purpose: Resets the checkpointed generic DP matrix <gxc> for reuse,
225 * minimizing free/malloc wastefulness. All information
226 * specific to the DP problem we just computed is
227 * reinitialized. All allocations (and information about
228 * those allocations) are preserved.
229 *
230 * Caller will still need to call <p7_gmxchk_GrowTo()>
231 * before each new DP, to be sure that the allocations are
232 * sufficient, and checkpointed rows are laid out.
233 *
234 * Returns: <eslOK> on success.
235 */
236 int
237 p7_gmxchk_Reuse(P7_GMXCHK *gxc)
238 {
239 gxc->M = 0;
240 gxc->L = 0;
241 gxc->R = 0;
242 /* the following aren't strictly necessary, in correct code, because
243 * GrowTo() will reset them
244 */
245 gxc->R0 = 3;
246 gxc->Ra = 0;
247 gxc->Rb = 0;
248 gxc->Rc = 0;
249 gxc->La = 0;
250 gxc->Lb = 0;
251 gxc->Lc = 0;
252
253 return eslOK;
254 }
255
256
257 /* Function: p7_gmxchk_Destroy()
258 * Synopsis: Frees a checkpointed generic DP matrix.
259 *
260 * Purpose: Free the checkpointed generic DP matrix <gxc>.
261 * <gxc> may be <NULL> or incompletely allocated.
262 */
263 void
264 p7_gmxchk_Destroy(P7_GMXCHK *gxc)
265 {
266 if (gxc)
267 {
268 if (gxc->dp_mem) free(gxc->dp_mem);
269 if (gxc->dp) free(gxc->dp);
270 free(gxc);
271 }
272 }
273 /*-------------- end, P7_GMXCHK object --------------------------*/
274
275
276
277
278 /*****************************************************************
279 *= 2. Debugging and testing tools
280 *****************************************************************/
281 #if eslDEBUGLEVEL > 0
282
283 /* Function: p7_gmxchk_Dump()
284 * Synopsis: Dump a checkpointed DP matrix to a stream.
285 *
286 * Purpose: Dump checkpointed DP Forward matrix <gxc> to stream <fp>
287 * for diagnostics.
288 *
289 * Caller first calls <p7_GForwardCheckpointed()> to create
290 * the checkpointed matrix <gxc>; then <p7_gmxchk_Dump()>
291 * to dump all or part of it.
292 *
293 * <flags> control some optional output behavior, as follows:
294 * | <p7_HIDE_SPECIALS> | don't show scores for <ENJBC> states |
295 * | <p7_SHOW_LOG> | <gxc> is in probs; show as log probs |
296 * Or, passing <p7_DEFAULT> means no flags.
297 *
298 * Returns: <eslOK> on success.
299 */
300 int
301 p7_gmxchk_Dump(FILE *ofp, P7_GMXCHK *gxc, int flags)
302 {
303 int kstart = 0; /* kstart,kend: make it easy to convert to a DumpWindow() sometime */
304 int kend = gxc->M;
305 int i, r, b, w;
306 int status;
307
308 p7_gmxchk_DumpHeader(ofp, gxc, kstart, kend, flags);
309
310 for (i = 0, r = gxc->R0-1; i <= gxc->La; i++, r++)
311 if ((status = p7_gmxchk_DumpRow(ofp, gxc, gxc->dp[r], i, kstart, kend, flags)) != eslOK) return status;
312
313 for (b = gxc->Rb + gxc->Rc, w = gxc->Lb; i <= gxc->L; i++)
314 {
315 if (!(--w)) {
316 w = b--;
317 if ((status = p7_gmxchk_DumpRow(ofp, gxc, gxc->dp[r], i, kstart, kend, flags)) != eslOK) return status;
318 r++;
319 }
320 }
321 return eslOK;
322 }
323
324 /* Function: p7_gmxchk_SetDumpMode()
325 * Synopsis: Set matrix to be dumped one row at a time in Backwards pass.
326 *
327 * Purpose: Set matrix <gxc> so that Backwards pass will dump it one
328 * row at a time, in reverse order. (The Backwards pass only
329 * keeps two rows of the Backward matrix in memory at any
330 * time, so you can't calculate first then dump the whole thing,
331 * as you can with the Forward matrix.)
332 *
333 * Caller first calls <p7_gmxchk_SetDumpMode()>, then calls the
334 * <p7_GBackwardCheckpointed()> calculation. <p7_BackwardCheckpointed()>
335 * will then dump the header and each row to <ofp>.
336 *
337 * If <ofp> is <NULL>, dumping is turned off.
338 *
339 * <flags> control some optional output behavior, as follows:
340 * | <p7_HIDE_SPECIALS> | don't show scores for <ENJBC> states |
341 * | <p7_SHOW_LOG> | <gxc> is in probs; show as log probs |
342 * Or, passing <p7_DEFAULT> means no flags.
343 * If <ofp> is <NULL>, <flags> has no effect.
344 *
345 * Args: gxc - checkpointed dp matrix we want to be dumping
346 * ofp - open stream for diagnostics, to set;
347 * or <NULL>, to unset dumping.
348 * flags - see above.
349 *
350 * Returns: <eslOK> on success.
351 */
352 int
353 p7_gmxchk_SetDumpMode(P7_GMXCHK *gxc, FILE *ofp, int flags)
354 {
355 if (ofp) {
356 gxc->do_debugging = TRUE;
357 gxc->dfp = ofp;
358 gxc->dbg_flags = flags;
359 } else {
360 gxc->do_debugging = FALSE;
361 gxc->dfp = NULL;
362 gxc->dbg_flags = 0;
363 }
364 return eslOK;
365 }
366
367
368 /* Function: p7_gmxchk_DumpHeader()
369 * Synopsis: Dump the header for a dumped checkpointed DP mx to stream.
370 *
371 * Purpose: Write the header (matrix column labels) for checkpointed
372 * DP matrix <gxc> to stream <fp> for diagnostics.
373 *
374 * This either gets called by <p7_gmxchk_Dump()> for a Forward
375 * matrix, or by <p7_GBackwardCheckpointed()> when debugging
376 * and about to start dumping a backwards matrix one row at a
377 * time.
378 *
379 * <flags> control some optional output behavior, as follows:
380 * | <p7_HIDE_SPECIALS> | don't show scores for <ENJBC> states |
381 * | <p7_SHOW_LOG> | <gxc> is in probs; show as log probs |
382 *
383 * Args: ofp - stream we're dumping diagnostics to (typically stdout)
384 * gxc - checkpointed matrix we're dumping
385 * kstart - start on profile columns 0..M (typically 0)
386 * kend - end on profile columns 0..M (typically M)
387 * flags - see above (typically <p7_DEFAULT>)
388 *
389 * Returns: <eslOK> on success.
390 */
391 int
392 p7_gmxchk_DumpHeader(FILE *ofp, P7_GMXCHK *gxc, int kstart, int kend, int flags)
393 {
394 int width = gxc->dbg_width;
395 int k,x;
396
397 fprintf(ofp, " ");
398 for (k = kstart; k <= kend; k++) fprintf(ofp, "%*d ", width, k);
399 if (! (flags & p7_HIDE_SPECIALS)) fprintf(ofp, "%*s %*s %*s %*s %*s %*s %*s\n", width, "E", width, "N", width, "JJ", width, "J", width, "B", width, "CC", width, "C");
400 fprintf(ofp, " ");
401 for (k = kstart; k <= kend; k++) fprintf(ofp, "%*.*s ", width, width, "----------");
402 if (! (flags & p7_HIDE_SPECIALS))
403 for (x = 0; x < 5; x++) fprintf(ofp, "%*.*s ", width, width, "----------");
404 fprintf(ofp, "\n");
405 return eslOK;
406 }
407
408
409 /* Function: p7_gmxchk_DumpRow()
410 * Synopsis: Dump one row of a checkpointed DP matrix for diagnostics.
411 *
412 * Purpose: Dump the row that <dpc> points to, from checkpointed
413 * matrix <gxc>, to stream <ofp> for debugging/diagnostics.
414 * Label the row by position <i> (typically 0..L+1) in
415 * the target.
416 *
417 * The meaning of <flags> is the same as in <p7_gmxchk_Dump()>.
418 *
419 * Args: ofp - stream to dump output to
420 * gxc - checkpointed DP matrix
421 * dpc - ptr to DP matrix row: gxc->dp[r] or gxc->dp[0|1]
422 * i - position on target sequence (0,1..L,L+1)
423 * kstart - start of dump window on query profile coords: 1..gxc->M
424 * kend - end of dump window on query profile coords: 1..gxc->M
425 * flags - option flags passed in from <p7_gmxchk_Dump()>
426 *
427 * Returns: <eslOK> on success.
428 */
429 int
430 p7_gmxchk_DumpRow(FILE *ofp, P7_GMXCHK *gxc, float *dpc, int i, int kstart, int kend, int flags)
431 {
432 int width = gxc->dbg_width;
433 int precision = gxc->dbg_precision;
434 float val;
435 int k, x;
436 int xoffset = (gxc->M+1)*p7G_NSCELLS; /* [ENJBC] cells start at this offset on each row. */
437
438 fprintf(ofp, "%3d M ", i);
439 for (k = kstart; k <= kend; k++)
440 {
441 val = dpc[k * p7G_NSCELLS + p7G_M];
442 if (flags & p7_SHOW_LOG) val = log(val);
443 fprintf(ofp, "%*.*f ", width, precision, val);
444 }
445
446 if (! (flags & p7_HIDE_SPECIALS))
447 {
448 for (x = 0; x < p7GC_NXCELLS; x++)
449 {
450 val = dpc[xoffset + x];
451 if (flags & p7_SHOW_LOG) val = log(val);
452 fprintf(ofp, "%*.*f ", width, precision, val);
453 }
454 }
455 fprintf(ofp, "\n");
456
457 fprintf(ofp, "%3d I ", i);
458 for (k = kstart; k <= kend; k++)
459 {
460 val = dpc[k * p7G_NSCELLS + p7G_I];
461 if (flags & p7_SHOW_LOG) val = log(val);
462 fprintf(ofp, "%*.*f ", width, precision, val);
463 }
464 fprintf(ofp, "\n");
465
466 fprintf(ofp, "%3d D ", i);
467 for (k = kstart; k <= kend; k++)
468 {
469 val = dpc[k * p7G_NSCELLS + p7G_D];
470 if (flags & p7_SHOW_LOG) val = log(val);
471 fprintf(ofp, "%*.*f ", width, precision, val);
472 }
473 fprintf(ofp, "\n\n");
474 return eslOK;
475 }
476 #endif // eslDEBUGLEVEL
477 /*------------- end, debugging and testing tools ----------------*/
478
479
480
481 /*****************************************************************
482 * 3. Internal routines
483 *****************************************************************/
484
485 /* set_row_layout()
486 *
487 * Determines the size of the a,b,c regions ("all", "between",
488 * "checkpointed") of rows in the DP matrix.
489 *
490 * Caller has already set <gxc->allocW> and <gxc->R0>. <gxc->allocW>,
491 * the row width, is something $\geq$ <allocM>+1. <gxc->R0> is the number
492 * of extra rows needed: here 3, (two bck[cur,prv], one boundary
493 * fwd[0]).
494 *
495 * Upon return, we've set the R{0abc} and L{abc} fields in the <gxc>
496 * structure.
497 *
498 * Design spec says <allocM> <= 100000, <allocL> <= 100000.
499 *
500 * <maxR> is the maximum number of rows the caller wants to use.
501 * We will exceed this for one comparison if absolutely necessary, but
502 * the next <_Reuse()> call will bring the allocation
503 * back down.
504 *
505 * So there's three possibilities:
506 * 1. A full matrix fits into our recommended max memory use.
507 * 2. A checkpointed matrix fits into our recommended memory.
508 * Make as much of the matrix uncheckpointed as we can,
509 * using every row in maxR.
510 * 3. We can't satisfy the recommended max memory, even fully
511 * checkpointed. Make a fully checkpointed matrix, in which
512 * R0+Ra+Rb+Rc will exceed maxR, and caller will have to
513 * allocate ("redlined").
514 */
515 static int
516 set_row_layout(P7_GMXCHK *gxc, int allocL, int maxR)
517 {
518 double Rbc = minimum_rows(allocL);
519 int minR_chk = gxc->R0 + (int) ceil(Rbc); /* min # rows we need for checkpointing */
520 int minR_all = gxc->R0 + allocL; /* min # rows we need for full matrix */
521
522 if (minR_all <= maxR) set_full (gxc, allocL);
523 else if (minR_chk <= maxR) set_checkpointed(gxc, allocL, maxR);
524 else set_redlined (gxc, allocL, Rbc);
525
526 return eslOK;
527 }
528
529 /* A "full" matrix is easy: Ra = La = L, using Ra+R0 <= maxR rows total.
530 */
531 static void
532 set_full(P7_GMXCHK *gxc, int L)
533 {
534 gxc->Ra = L;
535 gxc->Rb = 0;
536 gxc->Rc = 0;
537 gxc->La = L;
538 gxc->Lb = 0;
539 gxc->Lc = 0;
540 }
541
542 /* If we can fit a checkpointed matrix into maxR rows,
543 * then the trick is to use all maxR rows, and make the
544 * "a" region (all rows kept) as large as possible, to
545 * minimize computation. This means solving a little
546 * quadratic equation for Rb+Rc given L and maxR: see
547 * <checkpointed_rows()> for that solution.
548 */
549 static void
550 set_checkpointed(P7_GMXCHK *gxc, int L, int R)
551 {
552 double Rbc = checkpointed_rows(L, R-gxc->R0);
553 double Rc = floor(Rbc);
554
555 gxc->Rc = (int) Rc;
556 gxc->Rb = (Rbc > Rc ? 1 : 0);
557 gxc->Ra = R - gxc->Rb - gxc->Rc - gxc->R0;
558 gxc->Lc = ((gxc->Rc + 2) * (gxc->Rc + 1)) / 2 - 1;
559 gxc->La = gxc->Ra;
560 gxc->Lb = L - gxc->La - gxc->Lc;
561 }
562
563
564 /* If we can't fit in maxR rows, then we checkpoint
565 * the entire matrix; R0+Ra+Rb+Rc > maxR.
566 */
567 static void
568 set_redlined(P7_GMXCHK *gxc, int L, double minR)
569 {
570 double Rc = floor(minR);
571
572 gxc->Rc = (int) Rc;
573 gxc->Rb = (minR > Rc ? 1 : 0);
574 gxc->Ra = 0;
575 gxc->Lc = ((gxc->Rc + 2) * (gxc->Rc + 1)) / 2 - 1;
576 gxc->La = 0;
577 gxc->Lb = L - gxc->La - gxc->Lc;
578 }
579
580 /* minimum_rows()
581 *
582 * Calculate the minimum number of rows needed to checkpoint a
583 * forward matrix for a sequence of length L, exclusive of
584 * other constant row overhead (R0: two backwards rows, fwd[0]
585 * initial row).
586 *
587 * This value is a double; if it has a fraction, a partial checkpoint
588 * block ("b" region) is needed, as in this typical code:
589 * Rbc = minimum_rows(L);
590 * Rc = floor(Rbc);
591 * Rb = (Rbc > Rc ? 1 : 0);
592 * minR = (int) ceil(Rbc); // or, Rc+Rb
593 */
594 static double
595 minimum_rows(int L)
596 {
597 return (sqrt(9. + 8. * (double) L) - 3.) / 2.;
598 }
599
600 /* checkpointed_rows()
601 *
602 * Given L and maxR, solve for the number of checkpointed
603 * rows (Rb+Rc) we need. The value is a double; if it has
604 * a fractional part, then a partial checkpoint block is
605 * needed, Rb==1.
606 *
607 * This equation is obtained by solving
608 * L = Ra + (Rbc +2)(Rbc+1)/2 - 1
609 * for Rbc (i.e. Rb+Rc) using the quadratic equation,
610 * after substitution Ra = (maxR - Rbc - R0) to get
611 * Rbc in terms of L,maxR.
612 */
613 static double
614 checkpointed_rows(int L, int R)
615 {
616 return (sqrt(1. + 8. * (double) (L - R)) - 1.) / 2.;
617 }
618 /*----------------- end, internals ------------------------------*/
619
620
621
622 /*****************************************************************
623 * 4. Unit tests.
624 *****************************************************************/
625 #ifdef p7GMXCHK_TESTDRIVE
626
627 /* Use the "idiomatic" traversal patterns for Forward and Backward to
628 * write a test pattern into a DP matrix on the Forward pass, then
629 * read it back in the Backwards pass. The test pattern numbers each
630 * cell 0..ntot-1, for <ntot> total cells used in the DP matrix.
631 *
632 * This test pattern can catch a variety of bad layout issues in
633 * p7_gmxchk_GrowTo() and p7_gmxchk_Create(), and also serves as
634 * a partial example of the "idiomatic traversal patterns" for
635 * a checkpointed data structure.
636 */
637 static void
638 utest_testpattern(P7_GMXCHK *gxc, int M, int L)
639 {
640 char msg[] = "testpattern failed";
641 int n;
642 int ntot;
643 int b, w, i, k, s, r;
644 float *dpc;
645
646 if (L != gxc->La + gxc->Lb + gxc->Lc) esl_fatal(msg);
647
648 /* The test pattern will count cells in the checkpointed matrix,
649 * including bck rows 0,1 and row 0/col 0 boundary conditions
650 */
651 ntot = (gxc->R0+gxc->Ra+gxc->Rb+gxc->Rc) * ( (M+1)*p7G_NSCELLS + p7GC_NXCELLS);
652 n = 0;
653
654 /* Write a test pattern, via idiomatic forward traversal */
655 /* The Backwards and initialization rows 0..2 */
656 for (r = 0; r < gxc->R0; r++)
657 {
658 dpc = gxc->dp[r];
659 for (k = 0; k <= M; k++)
660 for (s = 0; s < p7G_NSCELLS; s++) { *dpc = n++; dpc++; }
661 for (s = 0; s < p7GC_NXCELLS; s++) { *dpc = n++; dpc++; }
662 }
663
664 /* Phase one: "a" region; uncheckpointed rows of the matrix */
665 for (i = 1, gxc->R = 0; i <= gxc->La; i++)
666 {
667 dpc = gxc->dp[gxc->R0+gxc->R]; gxc->R++;
668 for (k = 0; k <= M; k++)
669 for (s = 0; s < p7G_NSCELLS; s++) { *dpc = n++; dpc++; }
670 for (s = 0; s < p7GC_NXCELLS; s++) { *dpc = n++; dpc++; }
671 }
672 if (gxc->R != gxc->Ra) esl_fatal(msg);
673
674 /* Phase two: "b" and "c" regions: partially and fully checkpointed */
675 for (b = gxc->Rb + gxc->Rc, w = (gxc->Rb ? gxc->Lb : gxc->Rc+1); i <= L; i++)
676 {
677 if (! (--w))
678 {
679 dpc = gxc->dp[gxc->R0+gxc->R]; gxc->R++;
680 w = b--;
681
682 /* A checkpointed row: write test pattern */
683 for (k = 0; k <= M; k++)
684 for (s = 0; s < p7G_NSCELLS; s++) { *dpc = n++; dpc++; }
685 for (s = 0; s < p7GC_NXCELLS; s++) { *dpc = n++; dpc++; }
686 }
687 }
688 if (i != L+1) esl_fatal(msg);
689 if (gxc->R != gxc->Ra+gxc->Rb+gxc->Rc) esl_fatal(msg);
690 if (n != ntot) esl_fatal(msg);
691
692 /* Now read the test pattern back, using idiomatic Backward traversal */
693 n = ntot;
694 for (i = L, b = 1; b <= gxc->Rb + gxc->Rc; b++)
695 {
696 w = (b <= gxc->Rc ? b+1 : gxc->Lb);
697
698 /* The current row ends a block and is checkpointed:
699 * read backwards in both xmx and the checkpointed row
700 */
701 gxc->R--;
702 dpc = gxc->dp[gxc->R0+gxc->R] + (M+1)*p7G_NSCELLS + p7GC_NXCELLS - 1; /* dpc now on last cell in row */
703 for (s = p7GC_NXCELLS-1; s >= 0; s--) { if (*dpc != --n) esl_fatal(msg); dpc--; }
704 for (k = M; k >= 0; k--)
705 for (s = p7G_NSCELLS-1; s >= 0; s--) { if (*dpc != --n) esl_fatal(msg); dpc--; }
706
707 /* in most backwards traversals, now we'd compute
708 * Forwards rows from i2=i-w+1..i-1...
709 */
710
711 /* and then we'd compute a Backwards pass from rows
712 * i-1 up to i-w+1.
713 */
714
715 i -= w; /* a checkpointed block of width <w> is done. */
716 }
717 if (i != gxc->La) esl_fatal(msg);
718
719 /* The "a" region of the backwards traversal: every row is saved. */
720 for ( ; i >= 1; i--)
721 {
722 gxc->R--;
723 dpc = gxc->dp[gxc->R0+gxc->R] + (M+1)*p7G_NSCELLS + p7GC_NXCELLS - 1; /* dpc now on last cell in row */
724 for (s = p7GC_NXCELLS-1; s >= 0; s--) { if (*dpc != --n) esl_fatal(msg); dpc--; }
725 for (k = M; k >= 0; k--)
726 for (s = p7G_NSCELLS-1; s >= 0; s--) { if (*dpc != --n) esl_fatal(msg); dpc--; }
727 }
728
729 /* The R0 rows, boundary condition. */
730 for (r = gxc->R0-1; r >= 0; r--)
731 {
732 dpc = gxc->dp[r] + (M+1)*p7G_NSCELLS + p7GC_NXCELLS - 1; /* dpc now on last cell in row */
733 for (s = p7GC_NXCELLS-1; s >= 0; s--) { if (*dpc != --n) esl_fatal(msg); dpc--; }
734 for (k = M; k >= 0; k--)
735 for (s = p7G_NSCELLS-1; s >= 0; s--) { if (*dpc != --n) esl_fatal(msg); dpc--; }
736 }
737 }
738
739 /* utest_GrowTo()
740 *
741 * Exercises a variety of matrix expansion/contraction,
742 * writing the test pattern each time.
743 */
744 static void
745 utest_GrowTo(void)
746 {
747 int M, L;
748 P7_GMXCHK *gxc = NULL;
749
750 M = 20; L = 20; gxc = p7_gmxchk_Create(M, L, 0); utest_testpattern(gxc, M, L);
751 M = 40; L = 20; p7_gmxchk_GrowTo(gxc, M, L); utest_testpattern(gxc, M, L);
752 M = 40; L = 40; p7_gmxchk_GrowTo(gxc, M, L); utest_testpattern(gxc, M, L);
753 M = 80; L = 10; p7_gmxchk_GrowTo(gxc, M, L); utest_testpattern(gxc, M, L);
754 M = 10; L = 80; p7_gmxchk_GrowTo(gxc, M, L); utest_testpattern(gxc, M, L);
755 M = 100; L = 100; p7_gmxchk_GrowTo(gxc, M, L); utest_testpattern(gxc, M, L);
756 p7_gmxchk_Destroy(gxc);
757
758 M = 20; L = 20; gxc = p7_gmxchk_Create(M, L, ESL_MBYTES(32)); utest_testpattern(gxc, M, L);
759 M = 40; L = 20; p7_gmxchk_GrowTo(gxc, M, L); utest_testpattern(gxc, M, L);
760 M = 40; L = 40; p7_gmxchk_GrowTo(gxc, M, L); utest_testpattern(gxc, M, L);
761 M = 80; L = 10; p7_gmxchk_GrowTo(gxc, M, L); utest_testpattern(gxc, M, L);
762 M = 10; L = 80; p7_gmxchk_GrowTo(gxc, M, L); utest_testpattern(gxc, M, L);
763 M = 100; L = 100; p7_gmxchk_GrowTo(gxc, M, L); utest_testpattern(gxc, M, L);
764 p7_gmxchk_Destroy(gxc);
765 }
766
767 #endif /*p7GMXCHK_TESTDRIVE*/
768 /*---------------------- end, unit tests ------------------------*/
769
770 /*****************************************************************
771 * 5. Test driver.
772 *****************************************************************/
773 #ifdef p7GMXCHK_TESTDRIVE
774 /*
775 gcc -o p7_gmxchk_utest -msse2 -g -Wall -I. -L. -I../easel -L../easel -Dp7GMXCHK_TESTDRIVE p7_gmxchk.c -lhmmer -leasel -lm
776 ./p7_gmxchk_utest
777 */
778 #include <p7_config.h>
779
780 #include <stdio.h>
781 #include <math.h>
782
783 #include "easel.h"
784 #include "esl_getopts.h"
785 #include "esl_random.h"
786 #include "esl_randomseq.h"
787
788 #include "hmmer.h"
789 #include "p7_gmxchk.h"
790
791 static ESL_OPTIONS options[] = {
792 /* name type default env range togs reqs incomp help docgrp */
793 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show help and usage", 0},
794 { "-s", eslARG_INT, "42", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 },
795 { "-L", eslARG_INT, "40", NULL, NULL, NULL, NULL, NULL, "length of sampled sequences", 0 },
796 { "-M", eslARG_INT, "40", NULL, NULL, NULL, NULL, NULL, "length of sampled test profile", 0 },
797 { 0,0,0,0,0,0,0,0,0,0},
798 };
799 static char usage[] = "[-options]";
800 static char banner[] = "test driver for p7_gmxchk.c";
801
802 int
803 main(int argc, char **argv)
804 {
805 char *msg = "p7_gmxchk unit test driver failed";
806 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 0, argc, argv, banner, usage);
807 ESL_RANDOMNESS *r = esl_randomness_CreateFast(esl_opt_GetInteger(go, "-s"));
808 ESL_ALPHABET *abc = esl_alphabet_Create(eslAMINO);
809 P7_BG *bg = p7_bg_Create(abc);
810 P7_HMM *hmm = NULL;
811 P7_PROFILE *gm = NULL;
812 int M = esl_opt_GetInteger(go, "-M");
813 int L = esl_opt_GetInteger(go, "-L");
814
815 p7_FLogsumInit();
816
817 if (p7_hmm_Sample(r, M, abc, &hmm) != eslOK) esl_fatal(msg);
818 if ((gm = p7_profile_Create(hmm->M, abc)) == NULL) esl_fatal(msg);
819 if (p7_bg_SetLength(bg, L) != eslOK) esl_fatal(msg);
820 if (p7_ProfileConfig(hmm, bg, gm, L, p7_LOCAL) != eslOK) esl_fatal(msg);
821
822 utest_GrowTo();
823
824 esl_getopts_Destroy(go);
825 esl_randomness_Destroy(r);
826 esl_alphabet_Destroy(abc);
827 p7_bg_Destroy(bg);
828 p7_hmm_Destroy(hmm);
829 p7_profile_Destroy(gm);
830 return eslOK;
831 }
832 #endif /*p7GMXCHK_TESTDRIVE*/
833 /*---------------------- end, test driver ----------------------*/
834
835 /*
836 * References:
837 * SRE J8/109-112, Oct 2011: implementation plan.
838 */
839
840