Debian Codegraph @

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