Debian Codegraph @

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