Debian Codegraph @

src/cp9_mx.c

raw symbols SCIP
1 /* CP9_MX functions: dynamic programming matrix for CP9 HMMs
2 *
3 * EPN, Wed Nov 28 05:11:51 2007
4 */
5
6 #include <esl_config.h>
7 #include <p7_config.h>
8 #include "config.h"
9
10 #include <stdio.h>
11 #include <stdlib.h>
12
13 #include "easel.h"
14 #include "esl_vectorops.h"
15
16 #include "hmmer.h"
17
18 #include "infernal.h"
19
20 /*****************************************************************
21 * 1. CP9_MX data structure functions,
22 * matrix of integer log odd scores for CP9 HMM alignment/search
23 *****************************************************************/
24
25 /* Function: CreateCP9Matrix()
26 * based on CreatePlan7Matrix() <-- this function's comments below
27 * Purpose: Create a dynamic programming matrix for standard Forward,
28 * Backward, or Viterbi, with scores kept as scaled log-odds
29 * integers. Keeps 2D arrays compact in RAM in an attempt
30 * to maximize cache hits.
31 *
32 * The mx structure can be dynamically grown, if a new
33 * HMM or seq exceeds the currently allocated size. Dynamic
34 * growing is more efficient than an alloc/free of a whole
35 * matrix for every new target. The ResizePlan7Matrix()
36 * call does this reallocation, if needed. Here, in the
37 * creation step, we set up some pads - to inform the resizing
38 * call how much to overallocate when it realloc's.
39 *
40 * Args: N - N+1 rows are allocated, usually N == 1 for
41 * scanning in memory efficient mode, or N == L, length of sequence.
42 * M - size of model in nodes
43 *
44 * Return: mx
45 * mx is allocated here. Caller frees with FreeCP9Matrix(mx).
46 */
47 CP9_MX *
48 CreateCP9Matrix(int N, int M)
49 {
50 int status;
51 CP9_MX *mx;
52 int i;
53
54 ESL_ALLOC(mx, sizeof(CP9_MX));
55 ESL_ALLOC(mx->mmx, sizeof(int *) * (N+1));
56 ESL_ALLOC(mx->imx, sizeof(int *) * (N+1));
57 ESL_ALLOC(mx->dmx, sizeof(int *) * (N+1));
58 ESL_ALLOC(mx->elmx,sizeof(int *) * (N+1));
59 /* slightly wasteful, some nodes can't go to EL (for ex: right half of MATPs) */
60 ESL_ALLOC(mx->erow, sizeof(int) * (N+1));
61 ESL_ALLOC(mx->mmx_mem, sizeof(int) * ((N+1)*(M+1)));
62 ESL_ALLOC(mx->imx_mem, sizeof(int) * ((N+1)*(M+1)));
63 ESL_ALLOC(mx->dmx_mem, sizeof(int) * ((N+1)*(M+1)));
64 ESL_ALLOC(mx->elmx_mem,sizeof(int) * ((N+1)*(M+1)));
65
66 /* The indirect assignment below looks wasteful; it's actually
67 * used for aligning data on 16-byte boundaries as a cache
68 * optimization in the fast altivec implementation
69 */
70 mx->mmx[0] = (int *) mx->mmx_mem;
71 mx->imx[0] = (int *) mx->imx_mem;
72 mx->dmx[0] = (int *) mx->dmx_mem;
73 mx->elmx[0]= (int *) mx->elmx_mem;
74 for (i = 1; i <= N; i++)
75 {
76 mx->mmx[i] = mx->mmx[0] + (i*(M+1));
77 mx->imx[i] = mx->imx[0] + (i*(M+1));
78 mx->dmx[i] = mx->dmx[0] + (i*(M+1));
79 mx->elmx[i]= mx->elmx[0]+ (i*(M+1));
80 }
81
82 mx->M = M;
83 mx->rows = N;
84 mx->kmin = NULL;
85 mx->kmax = NULL;
86 mx->ncells_allocated = (M+1) * (N+1);
87 mx->ncells_valid = (M+1) * (N+1);
88 mx->size_Mb = (float) sizeof(CP9_MX);
89 mx->size_Mb += (float) (sizeof(int *) * (mx->rows+1) * 4); /* mx->*mx ptrs */
90 mx->size_Mb += (float) (sizeof(int) * (mx->rows+1) * (M+1) * 4); /* mx->*mx_mem */
91 mx->size_Mb += (float) (sizeof(int) * (mx->rows+1)); /* mx->erow */
92 mx->size_Mb /= 1000000.;
93
94 return mx;
95
96 ERROR:
97 cm_Fail("Memory allocation error.");
98 return NULL; /* never reached */
99 }
100
101
102 /* Function: FreeCP9Matrix()
103 * based on FreePlan7Matrix() <-- this function's comments below
104 * Purpose: Free a dynamic programming matrix allocated by CreatePlan7Matrix().
105 *
106 * Return: (void)
107 */
108 void
109 FreeCP9Matrix(CP9_MX *mx)
110 {
111 free (mx->mmx_mem);
112 free (mx->imx_mem);
113 free (mx->dmx_mem);
114 free (mx->elmx_mem);
115 free (mx->mmx);
116 free (mx->imx);
117 free (mx->dmx);
118 free (mx->elmx);
119 free (mx->erow);
120 /* don't free mx->kmin, mx->kmax, they could be used by multiple matrices,
121 * kmin and kmax in this structure are just pointers to those arrays */
122 free (mx);
123 }
124
125
126 /* Function: GrowCP9Matrix()
127 *
128 * Purpose: Reallocate a CP9 dp matrix, if necessary, for seq for
129 * length N, or 2 rows (if we're scanning in memory
130 * efficient mode, in this case N == 1, nrows = N+1).
131 *
132 * Note: unlike HMMER, M never changes, so we only have
133 * to worry about increasing the number of rows if nec.
134 *
135 * Returns individual ptrs to the four matrix components
136 * as a convenience.
137 *
138 * This function allocates the requested matrix regardless
139 * of it's size.
140 *
141 * If kmin and kmax are non-NULL, the matrix will be a p7
142 * HMM banded matrix as defined by bands in kmin, kmax.
143 * In this case N must be length of the sequence. If caller
144 * wants a non-banded CP9 matrix, pass kmin = kmax = NULL.
145 *
146 * Args: mx - an already allocated matrix to grow.
147 * N - seq length to allocate for; N+1 rows
148 * M - size of model, contract enforces this must == mx->M
149 * kmin - OPTIONAL: [0.1..i..N] minimum k for residue i
150 * kmax - OPTIONAL: [0.1..i..N] maximum k for residue i
151 * mmx, imx, dmx, elmx, erow
152 * - RETURN: ptrs to four mx components as a convenience
153 *
154 * Return: eslOK on success, eslEINCOMPAT if contract is violated,
155 * mx is (re)allocated here.
156 */
157 int
158 GrowCP9Matrix(CP9_MX *mx, char *errbuf, int N, int M, int *kmin, int *kmax, int ***mmx, int ***imx, int ***dmx, int ***elmx, int **erow)
159 {
160 int status;
161 void *p;
162 int i;
163 int ncells_needed = 0;
164 int do_banded;
165 int cur_ncells = 0;
166 int do_reallocate;
167
168 if(mx->M != M) ESL_FAIL(eslEINCOMPAT, errbuf, "GrowCP9Matrix(), mx->M: %d != M passed in: %d\n", mx->M, M);
169 if(N < 0) ESL_FAIL(eslEINCOMPAT, errbuf, "GrowCP9Matrix(), N: %d < 0\n", N);
170
171 do_banded = (kmin != NULL && kmax == NULL) ? TRUE : FALSE;
172 if(do_banded) {
173 for (i = 0; i <= N; i++) ncells_needed += (kmax[i] - kmin[i] + 1);
174 }
175 else ncells_needed = (N+1) * (M+1);
176 do_reallocate = (ncells_needed <= mx->ncells_allocated) ? FALSE : TRUE;
177
178 if(do_reallocate) {
179 /* we need more space */
180 ESL_RALLOC(mx->mmx, p, sizeof(int *) * (N+1));
181 ESL_RALLOC(mx->imx, p, sizeof(int *) * (N+1));
182 ESL_RALLOC(mx->dmx, p, sizeof(int *) * (N+1));
183 ESL_RALLOC(mx->elmx, p, sizeof(int *) * (N+1));
184 ESL_RALLOC(mx->erow, p, sizeof(int) * (N+1));
185 ESL_RALLOC(mx->mmx_mem, p, sizeof(int) * ncells_needed);
186 ESL_RALLOC(mx->imx_mem, p, sizeof(int) * ncells_needed);
187 ESL_RALLOC(mx->dmx_mem, p, sizeof(int) * ncells_needed);
188 ESL_RALLOC(mx->elmx_mem, p, sizeof(int) * ncells_needed);
189 mx->ncells_allocated = ncells_needed;
190
191 /* update size */
192 mx->size_Mb = (float) sizeof(CP9_MX);
193 mx->size_Mb += (float) (sizeof(int *) * (N+1) * 4); /* mx->*mx ptrs */
194 mx->size_Mb += (float) (sizeof(int) * (ncells_needed * 4)); /* mx->*mx_mem */
195 mx->size_Mb += (float) (sizeof(int) * (N+1)); /* mx->erow */
196 mx->size_Mb /= 1000000.;
197 }
198
199 if(do_banded || do_reallocate) { /* rearrange pointers */
200 mx->mmx[0] = mx->mmx_mem;
201 mx->imx[0] = mx->imx_mem;
202 mx->dmx[0] = mx->dmx_mem;
203 mx->elmx[0] = mx->elmx_mem;
204
205 if(do_banded) {
206 cur_ncells = kmax[0] - kmin[0] + 1;
207 for (i = 1; i <= N; i++) {
208 mx->mmx[i] = mx->mmx[0] + cur_ncells;
209 mx->imx[i] = mx->imx[0] + cur_ncells;
210 mx->dmx[i] = mx->dmx[0] + cur_ncells;
211 mx->elmx[i]= mx->elmx[0]+ cur_ncells;
212 cur_ncells += kmax[i] - kmin[i] + 1;
213 }
214 }
215 else { /* non-banded, we only get here if we didn't go to done, i.e. we reallocated */
216 for (i = 1; i <= N; i++) {
217 mx->mmx[i] = mx->mmx[0] + (i*(M+1));
218 mx->imx[i] = mx->imx[0] + (i*(M+1));
219 mx->dmx[i] = mx->dmx[0] + (i*(M+1));
220 mx->elmx[i]= mx->elmx[0]+ (i*(M+1));
221 }
222 }
223 }
224
225 mx->rows = N;
226 mx->kmin = kmin; /* could be NULL */
227 mx->kmax = kmax; /* could be NULL */
228 mx->ncells_valid = ncells_needed;
229 if (mmx != NULL) *mmx = mx->mmx;
230 if (imx != NULL) *imx = mx->imx;
231 if (dmx != NULL) *dmx = mx->dmx;
232 if (elmx!= NULL) *elmx= mx->elmx;
233 if (erow != NULL) *erow = mx->erow;
234 return eslOK;
235
236 ERROR:
237 ESL_FAIL(status, errbuf, ("GrowCP9Matrix(), memory reallocation error."));
238 }
239
240 /* Function: InitializeCP9Matrix()
241 * Purpose: Set all valid cells in a CP9 matrix to -INFTY
242 *
243 * Return: (void)
244 */
245 void
246 InitializeCP9Matrix(CP9_MX *mx)
247 {
248 esl_vec_ISet(mx->mmx_mem, mx->ncells_valid, -INFTY);
249 esl_vec_ISet(mx->imx_mem, mx->ncells_valid, -INFTY);
250 esl_vec_ISet(mx->dmx_mem, mx->ncells_valid, -INFTY);
251 esl_vec_ISet(mx->elmx_mem, mx->ncells_valid, -INFTY);
252 esl_vec_ISet(mx->erow, mx->rows, -INFTY);
253 return;
254 }
255
256 /* Function: SizeNeededCP9Matrix()
257 * Date: EPN, Fri Oct 14 13:40:04 2022
258 *
259 * Purpose: Determine size in Mb required for a CP9 matrix
260 * for a seq of length N, or 2 rows (if we're scanning in memory
261 * efficient mode, in this case N == 1, nrows = N+1).
262 *
263 * If kmin and kmax are non-NULL, the matrix will be a p7
264 * HMM banded matrix as defined by bands in kmin, kmax.
265 * In this case N must be length of the sequence. If caller
266 * wants a non-banded CP9 matrix, pass kmin = kmax = NULL.
267 *
268 * Args: N - seq length to allocate for; N+1 rows
269 * M - size of model
270 * kmin - OPTIONAL: [0.1..i..N] minimum k for residue i
271 * kmax - OPTIONAL: [0.1..i..N] maximum k for residue i
272 *
273 * Return: Mb needed for the CP9_MX
274 */
275 int
276 SizeNeededCP9Matrix(int N, int M, int *kmin, int *kmax)
277 {
278 int i;
279 int64_t ncells_needed = 0;
280 int do_banded;
281 float ret_mb = 0.;
282
283 do_banded = (kmin != NULL && kmax == NULL) ? TRUE : FALSE;
284 if(do_banded) {
285 for (i = 0; i <= N; i++) ncells_needed += (kmax[i] - kmin[i] + 1);
286 }
287 else ncells_needed = (N+1) * (M+1);
288
289 ret_mb = (float) sizeof(CP9_MX);
290 ret_mb += (float) (sizeof(int *) * (N+1) * 4); /* mx->*mx ptrs */
291 ret_mb += (float) (sizeof(int) * (ncells_needed * 4)); /* mx->*mx_mem */
292 ret_mb += (float) (sizeof(int) * (N+1)); /* mx->erow */
293 ret_mb /= 1000000.;
294
295 return ret_mb;
296 }