Debian Codegraph @

hmmer/src/impl_vmx/mpi.c

raw symbols SCIP
1 /* Optional support for MPI parallelization.
2 *
3 * Contents:
4 * 1. Communicating P7_OPROFILE, a score profile.
5 * 2. Benchmark driver.
6 * 3. Unit tests.
7 * 4. Test driver.
8 *
9 * SRE, Thu Jun 14 09:59:20 2007 [Janelia] [Tom Waits, Orphans]
10 */
11 #include <p7_config.h>
12
13 #ifdef HMMER_MPI
14 #include <stdlib.h>
15 #include <stdio.h>
16 #include <string.h>
17
18 #include "mpi.h"
19
20 #ifndef __APPLE_ALTIVEC__
21 #include <altivec.h>
22 #endif
23
24 #include "easel.h"
25 #include "esl_mpi.h"
26 #include "esl_getopts.h"
27
28 #include "hmmer.h"
29
30 /*****************************************************************
31 * 1. Communicating P7_OPROFILE, an optimized model.
32 *****************************************************************/
33
34 /* Function: p7_oprofile_MPISend()
35 * Synopsis: Send an OPROFILE as an MPI work unit.
36 * Incept: MSF, Wed Oct 21, 2009 [Janelia]
37 *
38 * Purpose: Sends an OPROFILE <om> as a work unit to MPI process
39 * <dest> (where <dest> ranges from 0..<nproc-1>), tagged
40 * with MPI tag <tag>, for MPI communicator <comm>, as
41 * the sole workunit or result.
42 *
43 * Work units are prefixed by a status code. If <hmm> is
44 * <non-NULL>, the work unit is an <eslOK> code followed by
45 * the packed HMM. If <hmm> is NULL, the work unit is an
46 * <eslEOD> code, which <p7_hmm_MPIRecv()> knows how to
47 * interpret; this is typically used for an end-of-data
48 * signal to cleanly shut down worker processes.
49 *
50 * In order to minimize alloc/free cycles in this routine,
51 * caller passes a pointer to a working buffer <*buf> of
52 * size <*nalloc> characters. If necessary (i.e. if <hmm> is
53 * too big to fit), <*buf> will be reallocated and <*nalloc>
54 * increased to the new size. As a special case, if <*buf>
55 * is <NULL> and <*nalloc> is 0, the buffer will be
56 * allocated appropriately, but the caller is still
57 * responsible for free'ing it.
58 *
59 * Returns: <eslOK> on success; <*buf> may have been reallocated and
60 * <*nalloc> may have been increased.
61 *
62 * Throws: <eslESYS> if an MPI call fails; <eslEMEM> if a malloc/realloc
63 * fails. In either case, <*buf> and <*nalloc> remain valid and useful
64 * memory (though the contents of <*buf> are undefined).
65 *
66 * Note: Compare to p7_hmmfile_WriteBinary(). The two operations (sending
67 * an HMM via MPI, or saving it as a binary file to disk) are
68 * similar.
69 */
70 int
71 p7_oprofile_MPISend(P7_OPROFILE *om, int dest, int tag, MPI_Comm comm, char **buf, int *nalloc)
72 {
73 int status;
74 int code;
75 int sz, n, pos;
76
77 /* Figure out size */
78 if (MPI_Pack_size(1, MPI_INT, comm, &n) != 0) ESL_XEXCEPTION(eslESYS, "mpi pack size failed");
79 if (om != NULL) {
80 if ((status = p7_oprofile_MPIPackSize(om, comm, &sz)) != eslOK) return status;
81 n += sz;
82 }
83
84 /* Make sure the buffer is allocated appropriately */
85 if (*buf == NULL || n > *nalloc) {
86 void *tmp;
87 ESL_RALLOC(*buf, tmp, sizeof(char) * n);
88 *nalloc = n;
89 }
90
91 /* Pack the status code and OPROFILE into the buffer */
92 pos = 0;
93 code = (om == NULL) ? eslEOD : eslOK;
94 if (MPI_Pack(&code, 1, MPI_INT, *buf, n, &pos, comm) != 0) ESL_EXCEPTION(eslESYS, "mpi pack failed");
95 if (om != NULL) {
96 if ((status = p7_oprofile_MPIPack(om, *buf, n, &pos, comm)) != eslOK) return status;
97 }
98
99 /* Send the packed OPROFILE to the destination. */
100 if (MPI_Send(*buf, n, MPI_PACKED, dest, tag, comm) != 0) ESL_EXCEPTION(eslESYS, "mpi send failed");
101 return eslOK;
102
103 ERROR:
104 return status;
105 }
106
107 /* Function: p7_oprofile_MPIPackSize()
108 * Synopsis: Calculates size needed to pack an OPROFILE.
109 * Incept: MSF, Wed Oct 21, 2009 [Janelia]
110 *
111 * Purpose: Calculate an upper bound on the number of bytes
112 * that <p7_oprofile_MPIPack()> will need to pack an
113 * OPROFILE <om> in a packed MPI message for MPI
114 * communicator <comm>; return that number of bytes
115 * in <*ret_n>.
116 *
117 * Returns: <eslOK> on success, and <*ret_n> contains the answer.
118 *
119 * Throws: <eslESYS> if an MPI call fails, and <*ret_n> is 0.
120 */
121 int
122 p7_oprofile_MPIPackSize(P7_OPROFILE *om, MPI_Comm comm, int *ret_n)
123 {
124 int status;
125 int n = 0;
126 int K = om->abc->Kp;
127 int len = 0;
128 int cnt;
129 int sz;
130
131 int Q4 = p7O_NQF(om->M);
132 int Q8 = p7O_NQW(om->M);
133 int Q16 = p7O_NQB(om->M);
134 int vsz = sizeof(vector float);
135
136 /* MSV Filter information */
137 if (MPI_Pack_size(5, MPI_CHAR, comm, &sz) != 0) ESL_XEXCEPTION(eslESYS, "pack size failed"); n += sz;
138 if (MPI_Pack_size(1, MPI_FLOAT, comm, &sz) != 0) ESL_XEXCEPTION(eslESYS, "pack size failed"); n += sz;
139 if (MPI_Pack_size(vsz*Q16, MPI_CHAR, comm, &sz) != 0) ESL_XEXCEPTION(eslESYS, "pack size failed"); n += (K*sz);
140
141 /* Viterbi Filter information */
142 if (MPI_Pack_size(1, MPI_SHORT, comm, &sz) != 0) ESL_XEXCEPTION(eslESYS, "pack size failed"); n += ((p7O_NXSTATES*p7O_NXTRANS+2)*sz);
143 if (MPI_Pack_size(2, MPI_FLOAT, comm, &sz) != 0) ESL_XEXCEPTION(eslESYS, "pack size failed"); n += sz;
144 if (MPI_Pack_size(K*vsz*Q8, MPI_CHAR, comm, &sz) != 0) ESL_XEXCEPTION(eslESYS, "pack size failed"); n += sz;
145 if (MPI_Pack_size(8*vsz*Q8, MPI_CHAR, comm, &sz) != 0) ESL_XEXCEPTION(eslESYS, "pack size failed"); n += sz;
146
147 /* Forward/Backward information */
148 if (MPI_Pack_size(1, MPI_FLOAT, comm, &sz) != 0) ESL_XEXCEPTION(eslESYS, "pack size failed"); n += (p7O_NXSTATES*p7O_NXTRANS*sz);
149 if (MPI_Pack_size(K*vsz*Q4, MPI_CHAR, comm, &sz) != 0) ESL_XEXCEPTION(eslESYS, "pack size failed"); n += sz;
150 if (MPI_Pack_size(8*vsz*Q4, MPI_CHAR, comm, &sz) != 0) ESL_XEXCEPTION(eslESYS, "pack size failed"); n += sz;
151
152 /* disk offsets */
153 if (MPI_Pack_size(1, MPI_LONG_LONG_INT, comm, &sz) != 0) ESL_XEXCEPTION(eslESYS, "pack size failed"); n += ((p7_NOFFSETS+2)*sz);
154
155 /* annotation info */
156 if (om->name != NULL) len += strlen(om->name) + 1;
157 if (om->acc != NULL) len += strlen(om->acc) + 1;
158 if (om->desc != NULL) len += strlen(om->desc) + 1;
159 if (om->rf != NULL) len += strlen(om->rf) + 1;
160 if (om->mm != NULL) len += strlen(om->mm) + 1;
161 if (om->cs != NULL) len += strlen(om->cs) + 1;
162 if (om->consensus != NULL) len += strlen(om->consensus) + 1;
163 if (MPI_Pack_size(7, MPI_INT, comm, &sz) != 0) ESL_XEXCEPTION(eslESYS, "pack size failed"); n += sz;
164 if (MPI_Pack_size(len, MPI_CHAR, comm, &sz) != 0) ESL_XEXCEPTION(eslESYS, "pack size failed"); n += sz;
165 cnt = p7_NEVPARAM + p7_NCUTOFFS + p7_MAXABET;
166 if (MPI_Pack_size(cnt, MPI_FLOAT, comm, &sz) != 0) ESL_XEXCEPTION(eslESYS, "pack size failed"); n += sz;
167
168 /* current model size */
169 if (MPI_Pack_size(4, MPI_INT, comm, &sz) != 0) ESL_XEXCEPTION(eslESYS, "pack size failed"); n += sz;
170 if (MPI_Pack_size(1, MPI_FLOAT, comm, &sz) != 0) ESL_XEXCEPTION(eslESYS, "pack size failed"); n += sz;
171
172 *ret_n = n;
173 return eslOK;
174
175 ERROR:
176 *ret_n = 0;
177 return status;
178
179 }
180
181 /* Function: p7_oprofile_MPIPack()
182 * Synopsis: Packs an OPROFILE into MPI buffer.
183 * Incept: MSF, Wed Oct 21, 2009 [Janelia]
184 *
185 * Purpose: Packs OPROFILE <om> into an MPI packed message buffer <buf>
186 * of length <n> bytes, starting at byte position <*position>,
187 * for MPI communicator <comm>.
188 *
189 * The caller must know that <buf>'s allocation of <n>
190 * bytes is large enough to append the packed OPROFILE at
191 * position <*pos>. This typically requires a call to
192 * <p7_oprofile_MPIPackSize()> first, and reallocation if
193 * needed.
194 *
195 * Returns: <eslOK> on success; <buf> now contains the
196 * packed <om>, and <*position> is set to the byte
197 * immediately following the last byte of the OPROFILE
198 * in <buf>.
199 *
200 * Throws: <eslESYS> if an MPI call fails; or <eslEMEM> if the
201 * buffer's length <n> was overflowed in trying to pack
202 * <msa> into <buf>. In either case, the state of
203 * <buf> and <*position> is undefined, and both should
204 * be considered to be corrupted.
205 */
206 int
207 p7_oprofile_MPIPack(P7_OPROFILE *om, char *buf, int n, int *pos, MPI_Comm comm)
208 {
209 int K = om->abc->Kp;
210 int atype = om->abc->type;
211 int len;
212 int x;
213
214 int Q4 = p7O_NQF(om->M);
215 int Q8 = p7O_NQW(om->M);
216 int Q16 = p7O_NQB(om->M);
217 int vsz = sizeof(vector float);
218
219 /* model configuration */
220 if (MPI_Pack(&om->M, 1, MPI_INT, buf, n, pos, comm) != 0) ESL_EXCEPTION(eslESYS, "pack failed");
221 if (MPI_Pack(&atype, 1, MPI_INT, buf, n, pos, comm) != 0) ESL_EXCEPTION(eslESYS, "pack failed");
222 if (MPI_Pack(&om->L, 1, MPI_INT, buf, n, pos, comm) != 0) ESL_EXCEPTION(eslESYS, "pack failed");
223 if (MPI_Pack(&om->mode, 1, MPI_INT, buf, n, pos, comm) != 0) ESL_EXCEPTION(eslESYS, "pack failed");
224 if (MPI_Pack(&om->nj, 1, MPI_FLOAT, buf, n, pos, comm) != 0) ESL_EXCEPTION(eslESYS, "pack failed");
225
226 /* MSV Filter information */
227 if (MPI_Pack(&om->tbm_b, 1, MPI_CHAR, buf, n, pos, comm) != 0) ESL_EXCEPTION(eslESYS, "pack failed");
228 if (MPI_Pack(&om->tec_b, 1, MPI_CHAR, buf, n, pos, comm) != 0) ESL_EXCEPTION(eslESYS, "pack failed");
229 if (MPI_Pack(&om->tjb_b, 1, MPI_CHAR, buf, n, pos, comm) != 0) ESL_EXCEPTION(eslESYS, "pack failed");
230 if (MPI_Pack(&om->scale_b, 1, MPI_FLOAT, buf, n, pos, comm) != 0) ESL_EXCEPTION(eslESYS, "pack failed");
231 if (MPI_Pack(&om->base_b, 1, MPI_CHAR, buf, n, pos, comm) != 0) ESL_EXCEPTION(eslESYS, "pack failed");
232 if (MPI_Pack(&om->bias_b, 1, MPI_CHAR, buf, n, pos, comm) != 0) ESL_EXCEPTION(eslESYS, "pack failed");
233 for (x = 0; x < K; x++)
234 if (MPI_Pack( om->rbv[x], vsz*Q16, MPI_CHAR, buf, n, pos, comm) != 0) ESL_EXCEPTION(eslESYS, "pack failed");
235
236 /* Viterbi Filter information */
237 if (MPI_Pack(&om->scale_w, 1, MPI_FLOAT, buf, n, pos, comm) != 0) ESL_EXCEPTION(eslESYS, "pack failed");
238 if (MPI_Pack(&om->base_w, 1, MPI_SHORT, buf, n, pos, comm) != 0) ESL_EXCEPTION(eslESYS, "pack failed");
239 if (MPI_Pack(&om->ddbound_w, 1, MPI_SHORT, buf, n, pos, comm) != 0) ESL_EXCEPTION(eslESYS, "pack failed");
240 if (MPI_Pack(&om->ncj_roundoff, 1, MPI_FLOAT, buf, n, pos, comm) != 0) ESL_EXCEPTION(eslESYS, "pack failed");
241 if (MPI_Pack( om->twv, 8*vsz*Q8, MPI_CHAR, buf, n, pos, comm) != 0) ESL_EXCEPTION(eslESYS, "pack failed");
242 for (x = 0; x < p7O_NXSTATES; x++)
243 if (MPI_Pack( om->xw[x], p7O_NXTRANS, MPI_SHORT, buf, n, pos, comm) != 0) ESL_EXCEPTION(eslESYS, "pack failed");
244 for (x = 0; x < K; x++)
245 if (MPI_Pack( om->rwv[x], vsz*Q8, MPI_CHAR, buf, n, pos, comm) != 0) ESL_EXCEPTION(eslESYS, "pack failed");
246
247 /* Forward/Backward information */
248 if (MPI_Pack( om->tfv, 8*vsz*Q4, MPI_CHAR, buf, n, pos, comm) != 0) ESL_EXCEPTION(eslESYS, "pack failed");
249 for (x = 0; x < p7O_NXSTATES; x++)
250 if (MPI_Pack( om->xf[x], p7O_NXTRANS, MPI_FLOAT, buf, n, pos, comm) != 0) ESL_EXCEPTION(eslESYS, "pack failed");
251 for (x = 0; x < K; x++)
252 if (MPI_Pack( om->rfv[x], vsz*Q4, MPI_CHAR, buf, n, pos, comm) != 0) ESL_EXCEPTION(eslESYS, "pack failed");
253
254 /* Forward/Backward information */
255 if (MPI_Pack( om->offs, p7_NOFFSETS, MPI_LONG_LONG_INT, buf, n, pos, comm) != 0) ESL_EXCEPTION(eslESYS, "pack failed");
256 if (MPI_Pack(&om->roff, 1, MPI_LONG_LONG_INT, buf, n, pos, comm) != 0) ESL_EXCEPTION(eslESYS, "pack failed");
257 if (MPI_Pack(&om->eoff, 1, MPI_LONG_LONG_INT, buf, n, pos, comm) != 0) ESL_EXCEPTION(eslESYS, "pack failed");
258
259 /* Annotation information */
260 len = (om->name != NULL) ? strlen(om->name)+1 : 0;
261 if (MPI_Pack(&len, 1, MPI_INT, buf, n, pos, comm) != 0) ESL_EXCEPTION(eslESYS, "pack failed");
262 if (len > 0)
263 if (MPI_Pack( om->name, len, MPI_CHAR, buf, n, pos, comm) != 0) ESL_EXCEPTION(eslESYS, "pack failed");
264 len = (om->acc != NULL) ? strlen(om->acc)+1 : 0;
265 if (MPI_Pack(&len, 1, MPI_INT, buf, n, pos, comm) != 0) ESL_EXCEPTION(eslESYS, "pack failed");
266 if (len > 0)
267 if (MPI_Pack( om->acc, len, MPI_CHAR, buf, n, pos, comm) != 0) ESL_EXCEPTION(eslESYS, "pack failed");
268 len = (om->desc != NULL) ? strlen(om->desc)+1 : 0;
269 if (MPI_Pack(&len, 1, MPI_INT, buf, n, pos, comm) != 0) ESL_EXCEPTION(eslESYS, "pack failed");
270 if (len > 0)
271 if (MPI_Pack( om->desc, len, MPI_CHAR, buf, n, pos, comm) != 0) ESL_EXCEPTION(eslESYS, "pack failed");
272 len = (om->rf != NULL) ? strlen(om->rf)+1 : 0;
273 if (MPI_Pack(&len, 1, MPI_INT, buf, n, pos, comm) != 0) ESL_EXCEPTION(eslESYS, "pack failed");
274 if (len > 0)
275 if (MPI_Pack( om->rf, len, MPI_CHAR, buf, n, pos, comm) != 0) ESL_EXCEPTION(eslESYS, "pack failed");
276 len = (om->mm != NULL) ? strlen(om->mm)+1 : 0;
277 if (MPI_Pack(&len, 1, MPI_INT, buf, n, pos, comm) != 0) ESL_EXCEPTION(eslESYS, "pack failed");
278 if (len > 0)
279 if (MPI_Pack( om->mm, len, MPI_CHAR, buf, n, pos, comm) != 0) ESL_EXCEPTION(eslESYS, "pack failed");
280 len = (om->cs != NULL) ? strlen(om->cs)+1 : 0;
281 if (MPI_Pack(&len, 1, MPI_INT, buf, n, pos, comm) != 0) ESL_EXCEPTION(eslESYS, "pack failed");
282 if (len > 0)
283 if (MPI_Pack( om->cs, len, MPI_CHAR, buf, n, pos, comm) != 0) ESL_EXCEPTION(eslESYS, "pack failed");
284 len = (om->consensus != NULL) ? strlen(om->consensus)+1 : 0;
285 if (MPI_Pack(&len, 1, MPI_INT, buf, n, pos, comm) != 0) ESL_EXCEPTION(eslESYS, "pack failed");
286 if (len > 0)
287 if (MPI_Pack( om->consensus, len, MPI_CHAR, buf, n, pos, comm) != 0) ESL_EXCEPTION(eslESYS, "pack failed");
288
289 if (MPI_Pack( om->evparam, p7_NEVPARAM, MPI_FLOAT, buf, n, pos, comm) != 0) ESL_EXCEPTION(eslESYS, "pack failed");
290 if (MPI_Pack( om->cutoff, p7_NCUTOFFS, MPI_FLOAT, buf, n, pos, comm) != 0) ESL_EXCEPTION(eslESYS, "pack failed");
291 if (MPI_Pack( om->compo, p7_MAXABET, MPI_FLOAT, buf, n, pos, comm) != 0) ESL_EXCEPTION(eslESYS, "pack failed");
292
293 if (*pos > n) ESL_EXCEPTION(eslEMEM, "buffer overflow");
294 return eslOK;
295 }
296
297
298 /* Function: p7_oprofile_MPIUnpack()
299 * Synopsis: Unpacks an OPROFILE from an MPI buffer.
300 * Incept: MSF, Wed Oct 21, 2009 [Janelia]
301 *
302 * Purpose: Unpack a newly allocated OPROFILE from MPI packed buffer
303 * <buf>, starting from position <*pos>, where the total length
304 * of the buffer in bytes is <n>.
305 *
306 * Caller may or may not already know what alphabet the OPROFILE
307 * is expected to be in. A reference to the current
308 * alphabet is passed in <abc>. If the alphabet is unknown,
309 * pass <*abc = NULL>, and when the OPROFILE is received, an
310 * appropriate new alphabet object is allocated and passed
311 * back to the caller via <*abc>. If the alphabet is
312 * already known, <*abc> is that alphabet, and the new
313 * OPROFILE's alphabet type is verified to agree with it. This
314 * mechanism allows an application to let the first OPROFILE
315 * determine the alphabet type for the application, while
316 * still keeping the alphabet under the application's scope
317 * of control.
318 *
319 * Returns: <eslOK> on success. <*pos> is updated to the position of
320 * the next element in <buf> to unpack (if any). <*ret_om>
321 * contains a newly allocated OPROFILE, which the caller is
322 * responsible for free'ing. If <*abc> was passed as
323 * <NULL>, it now points to an <ESL_ALPHABET> object that
324 * was allocated here; caller is responsible for free'ing
325 * this.
326 *
327 * Returns <eslEINCOMPAT> if the OPROFILE is in a different
328 * alphabet than <*abc> said to expect. In this case,
329 * <*abc> is unchanged, <*buf> and <*nalloc> may have been
330 * changed, and <*ret_om> is <NULL>.
331 *
332 * Throws: <eslESYS> on an MPI call failure. <eslEMEM> on allocation failure.
333 * In either case, <*ret_om> is <NULL>, and the state of <buf>
334 * and <*pos> is undefined and should be considered to be corrupted.
335 */
336 int
337 p7_oprofile_MPIUnpack(char *buf, int n, int *pos, MPI_Comm comm, ESL_ALPHABET **abc, P7_OPROFILE **ret_om)
338 {
339 int status;
340 int M, K, atype;
341 int len;
342 int x;
343
344 int Q4, Q8, Q16;
345 int vsz = sizeof(vector float);
346
347 P7_OPROFILE *om = NULL;
348
349 if (MPI_Unpack(buf, n, pos, &M, 1, MPI_INT, comm) != 0) ESL_XEXCEPTION(eslESYS, "mpi unpack failed");
350 if (MPI_Unpack(buf, n, pos, &atype, 1, MPI_INT, comm) != 0) ESL_XEXCEPTION(eslESYS, "mpi unpack failed");
351
352 /* Set or verify the alphabet */
353 if (*abc == NULL) { /* still unknown: set it, pass control of it back to caller */
354 if ((*abc = esl_alphabet_Create(atype)) == NULL) { status = eslEMEM; goto ERROR; }
355 } else { /* already known: check it */
356 if ((*abc)->type != atype) { status = eslEINCOMPAT; goto ERROR; }
357 }
358
359 Q4 = p7O_NQF(M);
360 Q8 = p7O_NQW(M);
361 Q16 = p7O_NQB(M);
362
363 if ((om = p7_oprofile_Create(M, *abc)) == NULL) { status = eslEMEM; goto ERROR; }
364 om->M = M;
365
366 K = (*abc)->Kp;
367
368 /* model configuration */
369 if (MPI_Unpack(buf, n, pos, &om->L, 1, MPI_INT, comm) != 0) ESL_EXCEPTION(eslESYS, "mpi unpack failed");
370 if (MPI_Unpack(buf, n, pos, &om->mode, 1, MPI_INT, comm) != 0) ESL_EXCEPTION(eslESYS, "mpi unpack failed");
371 if (MPI_Unpack(buf, n, pos, &om->nj, 1, MPI_FLOAT, comm) != 0) ESL_EXCEPTION(eslESYS, "mpi unpack failed");
372
373 /* MSV Filter information */
374 if (MPI_Unpack(buf, n, pos, &om->tbm_b, 1, MPI_CHAR, comm) != 0) ESL_EXCEPTION(eslESYS, "mpi unpack failed");
375 if (MPI_Unpack(buf, n, pos, &om->tec_b, 1, MPI_CHAR, comm) != 0) ESL_EXCEPTION(eslESYS, "mpi unpack failed");
376 if (MPI_Unpack(buf, n, pos, &om->tjb_b, 1, MPI_CHAR, comm) != 0) ESL_EXCEPTION(eslESYS, "mpi unpack failed");
377 if (MPI_Unpack(buf, n, pos, &om->scale_b, 1, MPI_FLOAT, comm) != 0) ESL_EXCEPTION(eslESYS, "mpi unpack failed");
378 if (MPI_Unpack(buf, n, pos, &om->base_b, 1, MPI_CHAR, comm) != 0) ESL_EXCEPTION(eslESYS, "mpi unpack failed");
379 if (MPI_Unpack(buf, n, pos, &om->bias_b, 1, MPI_CHAR, comm) != 0) ESL_EXCEPTION(eslESYS, "mpi unpack failed");
380 for (x = 0; x < K; x++)
381 if (MPI_Unpack(buf, n, pos, om->rbv[x], vsz*Q16, MPI_CHAR, comm) != 0) ESL_EXCEPTION(eslESYS, "mpi unpack failed");
382
383 /* Viterbi Filter information */
384 if (MPI_Unpack(buf, n, pos, &om->scale_w, 1, MPI_FLOAT, comm) != 0) ESL_EXCEPTION(eslESYS, "mpi unpack failed");
385 if (MPI_Unpack(buf, n, pos, &om->base_w, 1, MPI_SHORT, comm) != 0) ESL_EXCEPTION(eslESYS, "mpi unpack failed");
386 if (MPI_Unpack(buf, n, pos, &om->ddbound_w, 1, MPI_SHORT, comm) != 0) ESL_EXCEPTION(eslESYS, "mpi unpack failed");
387 if (MPI_Unpack(buf, n, pos, &om->ncj_roundoff, 1, MPI_FLOAT, comm) != 0) ESL_EXCEPTION(eslESYS, "mpi unpack failed");
388 if (MPI_Unpack(buf, n, pos, om->twv, 8*vsz*Q8, MPI_CHAR, comm) != 0) ESL_EXCEPTION(eslESYS, "mpi unpack failed");
389 for (x = 0; x < p7O_NXSTATES; x++)
390 if (MPI_Unpack(buf, n, pos, om->xw[x], p7O_NXTRANS, MPI_SHORT, comm) != 0) ESL_EXCEPTION(eslESYS, "mpi unpack failed");
391 for (x = 0; x < K; x++)
392 if (MPI_Unpack(buf, n, pos, om->rwv[x], vsz*Q8, MPI_CHAR, comm) != 0) ESL_EXCEPTION(eslESYS, "mpi unpack failed");
393
394 /* Forward/Backward information */
395 if (MPI_Unpack(buf, n, pos, om->tfv, 8*vsz*Q4, MPI_CHAR, comm) != 0) ESL_EXCEPTION(eslESYS, "mpi unpack failed");
396 for (x = 0; x < p7O_NXSTATES; x++)
397 if (MPI_Unpack(buf, n, pos, om->xf[x], p7O_NXTRANS, MPI_FLOAT, comm) != 0) ESL_EXCEPTION(eslESYS, "mpi unpack failed");
398 for (x = 0; x < K; x++)
399 if (MPI_Unpack(buf, n, pos, om->rfv[x], vsz*Q4, MPI_CHAR, comm) != 0) ESL_EXCEPTION(eslESYS, "mpi unpack failed");
400
401 /* Forward/Backward information */
402 if (MPI_Unpack(buf, n, pos, om->offs, p7_NOFFSETS, MPI_LONG_LONG_INT, comm) != 0) ESL_EXCEPTION(eslESYS, "mpi unpack failed");
403 if (MPI_Unpack(buf, n, pos, &om->roff, 1, MPI_LONG_LONG_INT, comm) != 0) ESL_EXCEPTION(eslESYS, "mpi unpack failed");
404 if (MPI_Unpack(buf, n, pos, &om->eoff, 1, MPI_LONG_LONG_INT, comm) != 0) ESL_EXCEPTION(eslESYS, "mpi unpack failed");
405
406 /* Annotation information */
407 if (MPI_Unpack(buf, n, pos, &len, 1, MPI_INT, comm) != 0) ESL_EXCEPTION(eslESYS, "mpi unpack failed");
408 if (len > 0) {
409 ESL_ALLOC(om->name, len);
410 if (MPI_Unpack(buf, n, pos, om->name, len, MPI_CHAR, comm) != 0) ESL_EXCEPTION(eslESYS, "mpi unpack failed");
411 om->name[len-1] = '\0';
412 }
413 if (MPI_Unpack(buf, n, pos, &len, 1, MPI_INT, comm) != 0) ESL_EXCEPTION(eslESYS, "mpi unpack failed");
414 if (len > 0) {
415 ESL_ALLOC(om->acc, len);
416 if (MPI_Unpack(buf, n, pos, om->acc, len, MPI_CHAR, comm) != 0) ESL_EXCEPTION(eslESYS, "mpi unpack failed");
417 om->acc[len-1] = '\0';
418 }
419 if (MPI_Unpack(buf, n, pos, &len, 1, MPI_INT, comm) != 0) ESL_EXCEPTION(eslESYS, "mpi unpack failed");
420 if (len > 0) {
421 ESL_ALLOC(om->desc, len);
422 if (MPI_Unpack(buf, n, pos, om->desc, len, MPI_CHAR, comm) != 0) ESL_EXCEPTION(eslESYS, "mpi unpack failed");
423 om->desc[len-1] = '\0';
424 }
425 if (MPI_Unpack(buf, n, pos, &len, 1, MPI_INT, comm) != 0) ESL_EXCEPTION(eslESYS, "mpi unpack failed");
426 if (len > 0) {
427 ESL_ALLOC(om->rf, len);
428 if (MPI_Unpack(buf, n, pos, om->rf, len, MPI_CHAR, comm) != 0) ESL_EXCEPTION(eslESYS, "mpi unpack failed");
429 om->rf[len-1] = '\0';
430 }
431 if (MPI_Unpack(buf, n, pos, &len, 1, MPI_INT, comm) != 0) ESL_EXCEPTION(eslESYS, "mpi unpack failed");
432 if (len > 0) {
433 ESL_ALLOC(om->mm, len);
434 if (MPI_Unpack(buf, n, pos, om->mm, len, MPI_CHAR, comm) != 0) ESL_EXCEPTION(eslESYS, "mpi unpack failed");
435 om->mm[len-1] = '\0';
436 }
437 if (MPI_Unpack(buf, n, pos, &len, 1, MPI_INT, comm) != 0) ESL_EXCEPTION(eslESYS, "mpi unpack failed");
438 if (len > 0) {
439 ESL_ALLOC(om->cs, len);
440 if (MPI_Unpack(buf, n, pos, om->cs, len, MPI_CHAR, comm) != 0) ESL_EXCEPTION(eslESYS, "mpi unpack failed");
441 om->cs[len-1] = '\0';
442 }
443 if (MPI_Unpack(buf, n, pos, &len, 1, MPI_INT, comm) != 0) ESL_EXCEPTION(eslESYS, "mpi unpack failed");
444 if (len > 0) {
445 ESL_ALLOC(om->consensus, len);
446 if (MPI_Unpack(buf, n, pos, om->consensus, len, MPI_CHAR, comm) != 0) ESL_EXCEPTION(eslESYS, "mpi unpack failed");
447 om->consensus[len-1] = '\0';
448 }
449
450 if (MPI_Unpack(buf, n, pos, om->evparam, p7_NEVPARAM, MPI_FLOAT, comm) != 0) ESL_EXCEPTION(eslESYS, "mpi unpack failed");
451 if (MPI_Unpack(buf, n, pos, om->cutoff, p7_NCUTOFFS, MPI_FLOAT, comm) != 0) ESL_EXCEPTION(eslESYS, "mpi unpack failed");
452 if (MPI_Unpack(buf, n, pos, om->compo, p7_MAXABET, MPI_FLOAT, comm) != 0) ESL_EXCEPTION(eslESYS, "mpi unpack failed");
453
454 *ret_om = om;
455 return eslOK;
456
457 ERROR:
458 if (om != NULL) p7_oprofile_Destroy(om);
459 return status;
460 }
461
462
463 /* Function: p7_oprofile_MPIRecv()
464 * Synopsis: Receives an OPROFILE as a work unit from an MPI sender.
465 * Incept: MSF, Wed Oct 21, 2009 [Janelia]
466 *
467 * Purpose: Receive a work unit that consists of a single OPROFILE
468 * sent by MPI <source> (<0..nproc-1>, or
469 * <MPI_ANY_SOURCE>) tagged as <tag> for MPI communicator <comm>.
470 *
471 * Work units are prefixed by a status code. If the unit's
472 * code is <eslOK> and no errors are encountered, this
473 * routine will return <eslOK> and a non-<NULL> <*ret_om>.
474 * If the unit's code is <eslEOD> (a shutdown signal),
475 * this routine returns <eslEOD> and <*ret_om> is <NULL>.
476 *
477 * Caller provides a working buffer <*buf> of size
478 * <*nalloc> characters. These are passed by reference, so
479 * that <*buf> can be reallocated and <*nalloc> increased
480 * if necessary. As a special case, if <*buf> is <NULL> and
481 * <*nalloc> is 0, the buffer will be allocated
482 * appropriately, but the caller is still responsible for
483 * free'ing it.
484 *
485 * Caller may or may not already know what alphabet the OPROFILE
486 * is expected to be in. A reference to the current
487 * alphabet is passed in <abc>. If the alphabet is unknown,
488 * pass <*abc = NULL>, and when the OPROFILE is received, an
489 * appropriate new alphabet object is allocated and passed
490 * back to the caller via <*abc>. If the alphabet is
491 * already known, <*ret_abc> is that alphabet, and the new
492 * OPROFILE's alphabet type is verified to agree with it. This
493 * mechanism allows an application to let the first OPROFILE
494 * determine the alphabet type for the application, while
495 * still keeping the alphabet under the application's scope
496 * of control.
497 *
498 * Returns: <eslOK> on success. <*ret_om> contains the received OPROFILE;
499 * it is allocated here, and the caller is responsible for
500 * free'ing it. <*buf> may have been reallocated to a
501 * larger size, and <*nalloc> may have been increased. If
502 * <*abc> was passed as <NULL>, it now points to an
503 * <ESL_ALPHABET> object that was allocated here; caller is
504 * responsible for free'ing this.
505 *
506 * Returns <eslEOD> if an end-of-data signal was received.
507 * In this case, <*buf>, <*nalloc>, and <*abc> are left unchanged,
508 * and <*ret_om> is <NULL>.
509 *
510 * Returns <eslEINCOMPAT> if the OPROFILE is in a different alphabet
511 * than <*abc> said to expect. In this case, <*abc> is unchanged,
512 * <*buf> and <*nalloc> may have been changed, and <*ret_om> is
513 * <NULL>.
514 *
515 * Throws: <eslEMEM> on allocation error, in which case <*ret_om> is
516 * <NULL>.
517 */
518 int
519 p7_oprofile_MPIRecv(int source, int tag, MPI_Comm comm, char **buf, int *nalloc, ESL_ALPHABET **abc, P7_OPROFILE **ret_om)
520 {
521 int status;
522 int code;
523 P7_OPROFILE *om = NULL;
524 int n;
525 int pos;
526 MPI_Status mpistatus;
527
528 /* Probe first, because we need to know if our buffer is big enough. */
529 MPI_Probe(source, tag, comm, &mpistatus);
530 MPI_Get_count(&mpistatus, MPI_PACKED, &n);
531
532 /* Make sure the buffer is allocated appropriately */
533 if (*buf == NULL || n > *nalloc) {
534 void *tmp;
535 ESL_RALLOC(*buf, tmp, sizeof(char) * n);
536 *nalloc = n;
537 }
538
539 /* Receive the packed work unit */
540 MPI_Recv(*buf, n, MPI_PACKED, source, tag, comm, &mpistatus);
541
542 /* Unpack it, looking at the status code prefix for EOD/EOK */
543 pos = 0;
544 if (MPI_Unpack(*buf, n, &pos, &code, 1, MPI_INT, comm) != 0) ESL_XEXCEPTION(eslESYS, "mpi unpack failed");
545 if (code == eslEOD) { *ret_om = NULL; return eslEOD; }
546
547 return p7_oprofile_MPIUnpack(*buf, *nalloc, &pos, comm, abc, ret_om);
548
549 ERROR:
550 if (om != NULL) p7_oprofile_Destroy(om);
551 return status;
552 }
553
554 /*----------------- end, P7_OPROFILE communication -------------------*/
555
556
557 /*****************************************************************
558 * 2. Benchmark driver.
559 *****************************************************************/
560
561 #ifdef p7MPI_BENCHMARK
562 /*
563 mpicc -g -Wall -L. -I. -L ../easel -I ../easel -D p7MPI_BENCHMARK -o benchmark-mpi mpi.c -lhmmer -leasel -lm
564 qsub -N benchmark-mpi -j y -R y -b y -cwd -V -pe lam-mpi-tight 2 'mpirun C ./benchmark-mpi ~/notebook/1227-msp-statistics/Pfam.hmm > bench.out'
565 qsub -N benchmark-mpi -j y -R y -b y -cwd -V -pe lam-mpi-tight 2 'mpirun C ./benchmark-mpi -b ~/notebook/1227-msp-statistics/Pfam.hmm > bench.out'
566 */
567 #include <p7_config.h>
568
569 #include <string.h>
570 #include <math.h>
571
572 #include "easel.h"
573 #include "esl_getopts.h"
574 #include "esl_alphabet.h"
575 #include "esl_random.h"
576 #include "esl_stopwatch.h"
577
578 #include "hmmer.h"
579
580 static ESL_OPTIONS options[] = {
581 /* name type default env range toggles reqs incomp help docgroup*/
582 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
583 { "-b", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "baseline timing: don't send any HMMs", 0 },
584 { "--stall", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "arrest after start: for debugging MPI under gdb", 0 },
585 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
586 };
587 static char usage[] = "[-options] <hmmfile>";
588 static char banner[] = "benchmark driver for MPI communication";
589
590 int
591 main(int argc, char **argv)
592 {
593 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 1, argc, argv, banner, usage);
594 char *hmmfile = esl_opt_GetArg(go, 1);
595 ESL_ALPHABET *abc = esl_alphabet_Create(eslAMINO);
596 P7_BG *bg = p7_bg_Create(abc);
597 int my_rank;
598 int nproc;
599 char *buf = NULL;
600 int nbuf = 0;
601 int subtotalM = 0;
602 int allM = 0;
603 int stalling = esl_opt_GetBoolean(go, "--stall");
604
605 MPI_Init(&argc, &argv);
606 MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
607 MPI_Comm_size(MPI_COMM_WORLD, &nproc);
608
609 while (stalling);
610
611 /* Master MPI process: */
612 if (my_rank == 0)
613 {
614 ESL_STOPWATCH *w = esl_stopwatch_Create();
615 P7_HMMFILE *hfp = NULL;
616 P7_OPROFILE *om = NULL;
617 P7_HMM *hmm = NULL;
618
619 /* Read HMMs from a file. */
620 if (p7_hmmfile_Open(hmmfile, NULL, &hfp, NULL) != eslOK) p7_Fail("Failed to open HMM file %s", hmmfile);
621
622 esl_stopwatch_Start(w);
623 while (p7_oprofile_ReadMSV(hfp, &abc, &om) == eslOK &&
624 p7_oprofile_ReadRest(hfp, om) == eslOK)
625 {
626 if (!esl_opt_GetBoolean(go, "-b"))
627 p7_oprofile_MPISend(om, 1, 0, MPI_COMM_WORLD, &buf, &nbuf); /* 1 = dest; 0 = tag */
628
629 p7_hmm_Destroy(hmm);
630 p7_oprofile_Destroy(om);
631 }
632 p7_oprofile_MPISend(NULL, 1, 0, MPI_COMM_WORLD, &buf, &nbuf); /* send the "no more HMMs" sign */
633 MPI_Reduce(&subtotalM, &allM, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
634
635 printf("total: %d\n", allM);
636 esl_stopwatch_Stop(w);
637 esl_stopwatch_Display(stdout, w, "CPU Time: ");
638 esl_stopwatch_Destroy(w);
639 }
640 /* Worker MPI process: */
641 else
642 {
643 P7_OPROFILE *om_recd = NULL;
644
645 while (p7_oprofile_MPIRecv(0, 0, MPI_COMM_WORLD, &buf, &nbuf, &abc, &om_recd) == eslOK)
646 {
647 subtotalM += om_recd->M;
648 p7_oprofile_Destroy(om_recd);
649 }
650 MPI_Reduce(&subtotalM, &allM, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
651 }
652
653 free(buf);
654 p7_bg_Destroy(bg);
655 esl_alphabet_Destroy(abc);
656 esl_getopts_Destroy(go);
657 MPI_Finalize();
658 exit(0);
659 }
660
661 #endif /*p7MPI_BENCHMARK*/
662 /*---------------------- end, benchmark -------------------------*/
663
664
665 /*****************************************************************
666 * 3. Unit tests
667 *****************************************************************/
668 #ifdef p7MPI_TESTDRIVE
669
670 static void
671 utest_oprofileSendRecv(int my_rank, int nproc)
672 {
673 ESL_RANDOMNESS *r = esl_randomness_CreateFast(42);
674 ESL_ALPHABET *abc = esl_alphabet_Create(eslAMINO);
675 P7_HMM *hmm = NULL;
676 P7_BG *bg = NULL;
677 P7_PROFILE *gm = NULL;
678 P7_OPROFILE *om = NULL;
679 P7_OPROFILE *om2 = NULL;
680 int M = 200;
681 int L = 400;
682 char *wbuf = NULL;
683 int wn = 0;
684 int i;
685 char errbuf[eslERRBUFSIZE];
686
687 p7_hmm_Sample(r, M, abc, &hmm); /* master and worker's sampled profiles are identical */
688 bg = p7_bg_Create(abc);
689 gm = p7_profile_Create(hmm->M, abc);
690 om = p7_oprofile_Create(hmm->M, abc);
691 p7_ProfileConfig(hmm, bg, gm, L, p7_LOCAL);
692 p7_oprofile_Convert(gm, om);
693 p7_bg_SetLength (bg, L);
694
695 if (my_rank == 0)
696 {
697 for (i = 1; i < nproc; i++)
698 {
699 ESL_DPRINTF1(("Master: receiving test profile\n"));
700 p7_oprofile_MPIRecv(MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, &wbuf, &wn, &abc, &om2);
701 ESL_DPRINTF1(("Master: test profile received\n"));
702
703 if (p7_oprofile_Compare(om, om2, 0.001, errbuf) != eslOK)
704 p7_Die("Received profile not identical to what was sent\n%s", errbuf);
705
706 p7_oprofile_Destroy(om2);
707 }
708 }
709 else
710 {
711 ESL_DPRINTF1(("Worker %d: sending test profile\n", my_rank));
712 p7_oprofile_MPISend(om, 0, 0, MPI_COMM_WORLD, &wbuf, &wn);
713 ESL_DPRINTF1(("Worker %d: test profile sent\n", my_rank));
714 }
715
716 free(wbuf);
717 p7_profile_Destroy(gm);
718 p7_oprofile_Destroy(om);
719 p7_bg_Destroy(bg);
720 p7_hmm_Destroy(hmm);
721 esl_alphabet_Destroy(abc);
722 esl_randomness_Destroy(r);
723 return;
724 }
725 #endif /*p7MPI_TESTDRIVE*/
726 /*---------------------- end, unit tests ------------------------*/
727
728
729 /*****************************************************************
730 * 4. Test driver.
731 *****************************************************************/
732 #ifdef p7MPI_TESTDRIVE
733
734 /* mpicc -o mpi_utest -g -Wall -I../easel -L../easel -I. -L. -Dp7MPI_TESTDRIVE mpi.c -lhmmer -leasel -lm
735 * In an MPI environment: (qlogin -pe lam-mpi-tight 2; setenv JOB_ID <jobid>; setenv TMPDIR /tmp/<jobid>....
736 * mpirun C ./mpi_utest
737 */
738 #include "esl_getopts.h"
739
740 static ESL_OPTIONS options[] = {
741 /* name type default env range toggles reqs incomp help docgroup*/
742 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
743 { "--stall", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "arrest after start: for debugging MPI under gdb", 0 },
744 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
745 };
746 static char usage[] = "[-options]";
747 static char banner[] = "test driver for mpi.c";
748
749 int
750 main(int argc, char **argv)
751 {
752 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 0, argc, argv, banner, usage);
753 int my_rank;
754 int nproc;
755
756 /* For debugging: stall until GDB can be attached */
757 if (esl_opt_GetBoolean(go, "--stall")) pause();
758
759 MPI_Init(&argc, &argv);
760 MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
761 MPI_Comm_size(MPI_COMM_WORLD, &nproc);
762
763 utest_oprofileSendRecv(my_rank, nproc);
764
765 MPI_Finalize();
766 return 0;
767 }
768
769 #endif /*p7MPI_TESTDRIVE*/
770 /*---------------------- end, test driver -----------------------*/
771
772
773 #else /*!HMMER_MPI*/
774 /* If we don't have MPI compiled in, provide some nothingness to:
775 * a. prevent Mac OS/X ranlib from bitching about .o file that "has no symbols"
776 * b. prevent compiler from bitching about "empty compilation unit"
777 * c. automatically pass the automated tests.
778 */
779 void p7_mpi_DoAbsolutelyNothing(void) { return; }
780 #if defined p7MPI_TESTDRIVE || p7MPI_BENCHMARK || p7MPI_EXAMPLE
781 int main(void) { return 0; }
782 #endif
783 #endif /*HMMER_MPI*/
784
785