| 1 | /* P7_GMXCHK implementation |
| 2 | * |
| 3 | * Checkpointed forward/backward dynamic programming matrix. |
| 4 | * |
| 5 | * Contents: |
| 6 | * 1. Exegesis: layout of rows in a checkpointed matrix. |
| 7 | * 2. Exegesis: layout of cells in a single DP row. |
| 8 | * 3. The P7_GMXCHK data structure. |
| 9 | * 4. Declarations for the p7_gmxchk API |
| 10 | * 5. References. |
| 11 | */ |
| 12 | #ifndef P7_GMXCHK_INCLUDED |
| 13 | #define P7_GMXCHK_INCLUDED |
| 14 | |
| 15 | #include <p7_config.h> |
| 16 | |
| 17 | /***************************************************************** |
| 18 | * 1. Exegesis: layout of rows in a checkpointed matrix. |
| 19 | *****************************************************************/ |
| 20 | |
| 21 | /* |
| 22 | * One P7_GMXCHK data structure is used for both Forward and Backward |
| 23 | * computations on a target sequence. The Forward calculation is |
| 24 | * checkpointed. The Backward calculation is linear memory in two |
| 25 | * rows. The end result is a Forward score and a posterior-decoded set |
| 26 | * of DP bands. |
| 27 | * |
| 28 | * In the diagram below, showing the row layout for the main matrix (MDI states): |
| 29 | * O = a checkpointed row; |
| 30 | * x = row that isn't checkpointed; |
| 31 | * * = boundary row 0, plus row(s) used for Backwards |
| 32 | * |
| 33 | * i = index of residues in a target sequence of length L |
| 34 | * r = index of rows in the DP matrix, R0+R in total |
| 35 | * |
| 36 | * |------------------------- L -------------------------------| |
| 37 | * |-----La----| |-Lb-| |-------------- Lc --------------------| |
| 38 | * i = . . 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
| 39 | * * * * O O O O O x O x x x x O x x x O x x O x O |
| 40 | * r = 0 1 2 3 4 5 6 7 . 8 . . . . 9 . . . 10 . . 11 . 12 |
| 41 | * |--R0-| |-----Ra----| |-Rb-| |-------------- Rc --------------------| |
| 42 | * |------------------------- R -------------------------------| |
| 43 | * |
| 44 | * There are four regions in the rows: |
| 45 | * region 0 (R0) : boundary row 0, and Backwards' two rows |
| 46 | * region a ("all"; Ra) : all rows are kept (no checkpointing) |
| 47 | * region b ("between"; Rb) : partially checkpointed |
| 48 | * region c ("checkpointed; Rc) : fully checkpointed |
| 49 | * |
| 50 | * In region a, La = Rb |
| 51 | * In region b, Rb = 0|1, Lb = 0..Rc+1 |
| 52 | * more specificially: (Rb=0 && Lb=0) || (Rb=1 && 1 <= Lb <= Rc+1) |
| 53 | * In region c, Lc = {{Rc+2} \choose {2}}-1 = (Rc+2)(Rc+1)/2 - 1 |
| 54 | * |
| 55 | * In this example: |
| 56 | * R0 = 3 |
| 57 | * Ra = 5 La = 5 |
| 58 | * Rb = 1 La = 2 |
| 59 | * Rc = 4 Lc = 14 |
| 60 | * |
| 61 | * In checkpointed regions, we refer to "blocks", often indexed |
| 62 | * <b>. There are Rb+Rc blocks, and each block ends in a checkpointed |
| 63 | * row. The "width" of each block, often called <w>, decrements from |
| 64 | * Rc+1 down to 2 in the fully checkpointed region. |
| 65 | * |
| 66 | * The reason to mix checkpointing and non-checkpointing is that we |
| 67 | * use as many rows as we can, given a set memory ceiling, to minimize |
| 68 | * computation time. |
| 69 | * |
| 70 | * The special states (ENJBC) are kept in xmx for all rows 1..L, just |
| 71 | * as in a normal (uncheckpointed) P7_GMX. |
| 72 | */ |
| 73 | |
| 74 | |
| 75 | |
| 76 | /***************************************************************** |
| 77 | * 2. Exegesis: layout of rows in a checkpointed matrix. |
| 78 | *****************************************************************/ |
| 79 | |
| 80 | /* Layout of memory in a single DP row: |
| 81 | * |
| 82 | * dpc: [M I D] [M I D] [M I D] ... [M I D] [E N JJ J B CC C] |
| 83 | * k: |-- 0 --| |-- 1 --| |-- 2 --| ... |-- M --| |
| 84 | * |------------- (M+1)*p7G_NSCELLS -----------| |---- p7GC_NXCELLS ---| |
| 85 | * |
| 86 | * Row dp[r] = gxc->dp_mem+(r*allocW) = dpc |
| 87 | * Main state s={MID} at node k={0..M}: dpc[k*p7G_NSCELLS+s] |
| 88 | * Special state s={ENJBC,CC,JJ}: dpc[(M+1)*p7G_NSCELLS+s] |
| 89 | * |
| 90 | * We need to store "JJ" and "CC" states -- the partial path |
| 91 | * probabilities from C(i-1)->C and J(i-1)->J -- because the |
| 92 | * checkpointed implementation does not necessarily have access to |
| 93 | * values on row i-1 when it does posterior decoding. <p7G_NXCELLS> |
| 94 | * from <P7_GMX> is replaced by <p7GC_NXCELLS> in <P7_GMXCHK>, and |
| 95 | * <enum p7g_xcells_e> from 0..4 {ENJBC} with <p7gc_xcells_e> from |
| 96 | * 0..6 {E,N,JJ,J,B,CC,C}. |
| 97 | */ |
| 98 | |
| 99 | |
| 100 | /***************************************************************** |
| 101 | * 3. The P7_GMXCHK data structure. |
| 102 | *****************************************************************/ |
| 103 | |
| 104 | /* p7GC_NXCELLS and p7gc_xcells_e |
| 105 | * |
| 106 | * For main states we share p7G_NSCELLS and p7G_{MID} with P7_GMX. |
| 107 | * For special states, we replace <enum p7g_xcells_e> with an array |
| 108 | * that inserts CC and JJ cells, which the checkpointed implementation |
| 109 | * needs. (See note 2 above.) Note that the order of the p7GC_{X} |
| 110 | * special states is not the same as p7G_{X}, so they should not be |
| 111 | * mixed. |
| 112 | */ |
| 113 | enum p7gc_xcells_e { |
| 114 | p7GC_E = 0, |
| 115 | p7GC_N = 1, |
| 116 | p7GC_JJ = 2, |
| 117 | p7GC_J = 3, |
| 118 | p7GC_B = 4, |
| 119 | p7GC_CC = 5, |
| 120 | p7GC_C = 6 |
| 121 | }; |
| 122 | #define p7GC_NXCELLS 7 |
| 123 | |
| 124 | |
| 125 | typedef struct p7_gmxchk_s { |
| 126 | int M; /* actual query model dimension of current comparison */ |
| 127 | int L; /* actual target sequence dimension of current comparison */ |
| 128 | int R; /* actual # rows in current fwd matrix (<= Ra+Rb+Rc), excluding R0 */ |
| 129 | |
| 130 | /* Checkpointed layout, mapping rows 1..R to residues 1..L: */ |
| 131 | int R0; /* # of extra rows: one for fwd[0] boundary, two for bck[prv,cur] */ |
| 132 | int Ra; /* # of rows used in "all" region (uncheckpointed) */ |
| 133 | int Rb; /* # of rows in "between" region (one incomplete checkpoint segment) */ |
| 134 | int Rc; /* # of rows in "checkpointed" region */ |
| 135 | int La; /* residues 1..La are in "all" region */ |
| 136 | int Lb; /* residues La+1..La+Lb are in "between" region */ |
| 137 | int Lc; /* residues La+Lb+1..La+Lb+Lc=L are in "checkpointed" region */ |
| 138 | |
| 139 | float *dp_mem; /* raw memory allocation, that dp[] rows point into */ |
| 140 | int allocW; /* allocated width/row, in cells ((M+1)*p7G_NSCELLS+p7G_NXCELLS) <= allocW) */ |
| 141 | int64_t ncells; /* total # of alloc'ed cells: ncells >= (validR)(allocW) */ |
| 142 | int64_t ncell_limit; /* recommended RAM limit on dp_mem; can temporarily exceed it */ |
| 143 | |
| 144 | float **dp; /* DP row pointers, dp[0..R0-1,R0..R0+R-1]. See note above for layout. */ |
| 145 | int allocR; /* allocated size of dp[]. R+R0 <= R0+Ra+Rb+Rc <= validR <= allocR */ |
| 146 | int validR; /* # of rows pointing at DP memory; may be < allocR after a GrowTo() call */ |
| 147 | |
| 148 | /* Info for debugging mode (conditionally compiled) */ |
| 149 | #if eslDEBUGLEVEL > 0 |
| 150 | int do_debugging; /* TRUE if we're in debugging mode */ |
| 151 | FILE *dfp; /* output stream for debugging diagnostics */ |
| 152 | int dbg_width; /* cell values in diagnostic output are fprintf'ed */ |
| 153 | int dbg_precision; /* dfp, "%*.*f", dbg_width, dbg_precision, val */ |
| 154 | int dbg_flags; /* p7_DEFAULT | p7_HIDE_SPECIALS | p7_SHOW_LOG */ |
| 155 | #endif |
| 156 | } P7_GMXCHK; |
| 157 | |
| 158 | #define MMR(p, k) ((p)[(k)* p7G_NSCELLS + p7G_M]) |
| 159 | #define IMR(p, k) ((p)[(k)* p7G_NSCELLS + p7G_I]) |
| 160 | #define DMR(p, k) ((p)[(k)* p7G_NSCELLS + p7G_D]) |
| 161 | #define XMR(p, s) ((p)[(M+1)* p7G_NSCELLS + s]) |
| 162 | |
| 163 | /***************************************************************** |
| 164 | * 4. Declarations of the p7_gmxchk API |
| 165 | *****************************************************************/ |
| 166 | |
| 167 | extern P7_GMXCHK *p7_gmxchk_Create (int M, int L, int64_t ramlimit); |
| 168 | extern int p7_gmxchk_GrowTo (P7_GMXCHK *gxc, int M, int L); |
| 169 | extern size_t p7_gmxchk_Sizeof (const P7_GMXCHK *gxc); |
| 170 | extern int p7_gmxchk_Reuse (P7_GMXCHK *gxc); |
| 171 | extern void p7_gmxchk_Destroy(P7_GMXCHK *gxc); |
| 172 | |
| 173 | extern int p7_gmxchk_Dump(FILE *ofp, P7_GMXCHK *gxc, int flags); |
| 174 | extern int p7_gmxchk_SetDumpMode(P7_GMXCHK *gxc, FILE *ofp, int flags); |
| 175 | extern int p7_gmxchk_DumpHeader(FILE *ofp, P7_GMXCHK *gxc, int kstart, int kend, int flags); |
| 176 | extern int p7_gmxchk_DumpRow(FILE *ofp, P7_GMXCHK *gxc, float *dpc, int i, int kstart, int kend, int flags); |
| 177 | |
| 178 | /* |
| 179 | * References: |
| 180 | * SRE:J8/109-112, Oct 2011: Implementation plan |
| 181 | */ |
| 182 | #endif /*P7_GMXCHK_INCLUDED*/ |