2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2004, The GROMACS development team.
6 * Copyright (c) 2013,2014, by the GROMACS development team, led by
7 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8 * and including many others, as listed in the AUTHORS file in the
9 * top-level source directory and at http://www.gromacs.org.
11 * GROMACS is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 2.1
14 * of the License, or (at your option) any later version.
16 * GROMACS is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with GROMACS; if not, see
23 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * If you want to redistribute modifications to GROMACS, please
27 * consider that scientific software is very special. Version
28 * control is crucial - bugs must be traceable. We will be happy to
29 * consider code for inclusion in the official distribution, but
30 * derived work must not be called official GROMACS. Details are found
31 * in the README & COPYING files - if they are missing, get the
32 * official version at http://www.gromacs.org.
34 * To help us fund GROMACS development, we humbly ask that you cite
35 * the research papers on the package. Check out http://www.gromacs.org.
48 #include "types/commrec.h"
52 #include "gromacs/utility/basenetwork.h"
53 #include "gromacs/utility/cstringutil.h"
54 #include "gromacs/utility/fatalerror.h"
55 #include "gromacs/utility/futil.h"
56 #include "gromacs/utility/gmxmpi.h"
57 #include "gromacs/utility/smalloc.h"
59 /* The source code in this file should be thread-safe.
60 Please keep it that way. */
62 void gmx_fill_commrec_from_mpi(t_commrec gmx_unused *cr)
65 gmx_call("gmx_fill_commrec_from_mpi");
67 if (!gmx_mpi_initialized())
69 gmx_comm("MPI has not been initialized properly");
72 cr->nnodes = gmx_node_num();
73 cr->nodeid = gmx_node_rank();
74 cr->sim_nodeid = cr->nodeid;
75 cr->mpi_comm_mysim = MPI_COMM_WORLD;
76 cr->mpi_comm_mygroup = MPI_COMM_WORLD;
81 t_commrec *init_commrec()
88 gmx_fill_commrec_from_mpi(cr);
90 cr->mpi_comm_mysim = NULL;
91 cr->mpi_comm_mygroup = NULL;
94 cr->nodeid = cr->sim_nodeid;
97 // TODO cr->duty should not be initialized here
98 cr->duty = (DUTY_PP | DUTY_PME);
100 #if defined GMX_MPI && !defined MPI_IN_PLACE_EXISTS
101 /* initialize the MPI_IN_PLACE replacement buffers */
103 cr->mpb->ibuf = NULL;
104 cr->mpb->libuf = NULL;
105 cr->mpb->fbuf = NULL;
106 cr->mpb->dbuf = NULL;
107 cr->mpb->ibuf_alloc = 0;
108 cr->mpb->libuf_alloc = 0;
109 cr->mpb->fbuf_alloc = 0;
110 cr->mpb->dbuf_alloc = 0;
116 t_commrec *reinitialize_commrec_for_this_thread(const t_commrec gmx_unused *cro)
118 #ifdef GMX_THREAD_MPI
121 /* make a thread-specific commrec */
123 /* now copy the whole thing, so settings like the number of PME nodes
127 /* and we start setting our own thread-specific values for things */
128 gmx_fill_commrec_from_mpi(cr);
130 // TODO cr->duty should not be initialized here
131 cr->duty = (DUTY_PP | DUTY_PME);
139 void gmx_setup_nodecomm(FILE gmx_unused *fplog, t_commrec *cr)
142 int n, rank, nodehash, ng, ni;
144 /* Many MPI implementations do not optimize MPI_Allreduce
145 * (and probably also other global communication calls)
146 * for multi-core nodes connected by a network.
147 * We can optimize such communication by using one MPI call
148 * within each node and one between the nodes.
149 * For MVAPICH2 and Intel MPI this reduces the time for
150 * the global_stat communication by 25%
151 * for 2x2-core 3 GHz Woodcrest connected by mixed DDR/SDR Infiniband.
152 * B. Hess, November 2007
158 #ifndef GMX_THREAD_MPI
160 MPI_Comm_size(cr->mpi_comm_mygroup, &n);
161 MPI_Comm_rank(cr->mpi_comm_mygroup, &rank);
163 nodehash = gmx_physicalnode_id_hash();
167 fprintf(debug, "In gmx_setup_nodecomm: splitting communicator of size %d\n", n);
171 /* The intra-node communicator, split on node number */
172 MPI_Comm_split(cr->mpi_comm_mygroup, nodehash, rank, &nc->comm_intra);
173 MPI_Comm_rank(nc->comm_intra, &nc->rank_intra);
176 fprintf(debug, "In gmx_setup_nodecomm: node ID %d rank within node %d\n",
177 rank, nc->rank_intra);
179 /* The inter-node communicator, split on rank_intra.
180 * We actually only need the one for rank=0,
181 * but it is easier to create them all.
183 MPI_Comm_split(cr->mpi_comm_mygroup, nc->rank_intra, rank, &nc->comm_inter);
184 /* Check if this really created two step communication */
185 MPI_Comm_size(nc->comm_inter, &ng);
186 MPI_Comm_size(nc->comm_intra, &ni);
189 fprintf(debug, "In gmx_setup_nodecomm: groups %d, my group size %d\n",
193 if (getenv("GMX_NO_NODECOMM") == NULL &&
194 ((ng > 1 && ng < n) || (ni > 1 && ni < n)))
199 fprintf(fplog, "Using two step summing over %d groups of on average %.1f ranks\n\n",
200 ng, (real)n/(real)ng);
202 if (nc->rank_intra > 0)
204 MPI_Comm_free(&nc->comm_inter);
209 /* One group or all processes in a separate group, use normal summing */
210 MPI_Comm_free(&nc->comm_inter);
211 MPI_Comm_free(&nc->comm_intra);
214 fprintf(debug, "In gmx_setup_nodecomm: not unsing separate inter- and intra-node communicators.\n");
219 /* tMPI runs only on a single node so just use the nodeid */
220 nc->rank_intra = cr->nodeid;
224 void gmx_init_intranode_counters(t_commrec *cr)
226 /* counters for PP+PME and PP-only processes on my physical node */
227 int nrank_intranode, rank_intranode;
228 int nrank_pp_intranode, rank_pp_intranode;
229 /* thread-MPI is not initialized when not running in parallel */
230 #if defined GMX_MPI && !defined GMX_THREAD_MPI
231 int nrank_world, rank_world;
232 int i, myhash, *hash, *hash_s, *hash_pp, *hash_pp_s;
234 MPI_Comm_size(MPI_COMM_WORLD, &nrank_world);
235 MPI_Comm_rank(MPI_COMM_WORLD, &rank_world);
237 /* Get a (hopefully unique) hash that identifies our physical node */
238 myhash = gmx_physicalnode_id_hash();
240 /* We can't rely on MPI_IN_PLACE, so we need send and receive buffers */
241 snew(hash, nrank_world);
242 snew(hash_s, nrank_world);
243 snew(hash_pp, nrank_world);
244 snew(hash_pp_s, nrank_world);
246 hash_s[rank_world] = myhash;
247 hash_pp_s[rank_world] = (cr->duty & DUTY_PP) ? myhash : -1;
249 MPI_Allreduce(hash_s, hash, nrank_world, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
250 MPI_Allreduce(hash_pp_s, hash_pp, nrank_world, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
254 nrank_pp_intranode = 0;
255 rank_pp_intranode = 0;
256 for (i = 0; i < nrank_world; i++)
258 if (hash[i] == myhash)
266 if (hash_pp[i] == myhash)
268 nrank_pp_intranode++;
269 if ((cr->duty & DUTY_PP) && i < rank_world)
280 /* Serial or thread-MPI code: we run within a single physical node */
281 nrank_intranode = cr->nnodes;
282 rank_intranode = cr->sim_nodeid;
283 nrank_pp_intranode = cr->nnodes - cr->npmenodes;
284 rank_pp_intranode = cr->nodeid;
290 if (cr->duty & DUTY_PP && cr->duty & DUTY_PME)
292 sprintf(sbuf, "PP+PME");
296 sprintf(sbuf, "%s", cr->duty & DUTY_PP ? "PP" : "PME");
298 fprintf(debug, "On %3s rank %d: nrank_intranode=%d, rank_intranode=%d, "
299 "nrank_pp_intranode=%d, rank_pp_intranode=%d\n",
300 sbuf, cr->sim_nodeid,
301 nrank_intranode, rank_intranode,
302 nrank_pp_intranode, rank_pp_intranode);
305 cr->nrank_intranode = nrank_intranode;
306 cr->rank_intranode = rank_intranode;
307 cr->nrank_pp_intranode = nrank_pp_intranode;
308 cr->rank_pp_intranode = rank_pp_intranode;
312 void gmx_barrier(const t_commrec gmx_unused *cr)
315 gmx_call("gmx_barrier");
317 MPI_Barrier(cr->mpi_comm_mygroup);
321 void gmx_bcast(int gmx_unused nbytes, void gmx_unused *b, const t_commrec gmx_unused *cr)
324 gmx_call("gmx_bast");
326 MPI_Bcast(b, nbytes, MPI_BYTE, MASTERRANK(cr), cr->mpi_comm_mygroup);
330 void gmx_bcast_sim(int gmx_unused nbytes, void gmx_unused *b, const t_commrec gmx_unused *cr)
333 gmx_call("gmx_bast");
335 MPI_Bcast(b, nbytes, MPI_BYTE, MASTERRANK(cr), cr->mpi_comm_mysim);
339 void gmx_sumd(int gmx_unused nr, double gmx_unused r[], const t_commrec gmx_unused *cr)
342 gmx_call("gmx_sumd");
344 #if defined(MPI_IN_PLACE_EXISTS)
347 if (cr->nc.rank_intra == 0)
349 /* Use two step summing. */
350 MPI_Reduce(MPI_IN_PLACE, r, nr, MPI_DOUBLE, MPI_SUM, 0,
352 /* Sum the roots of the internal (intra) buffers. */
353 MPI_Allreduce(MPI_IN_PLACE, r, nr, MPI_DOUBLE, MPI_SUM,
358 /* This is here because of the silly MPI specification
359 that MPI_IN_PLACE should be put in sendbuf instead of recvbuf */
360 MPI_Reduce(r, NULL, nr, MPI_DOUBLE, MPI_SUM, 0, cr->nc.comm_intra);
362 MPI_Bcast(r, nr, MPI_DOUBLE, 0, cr->nc.comm_intra);
366 MPI_Allreduce(MPI_IN_PLACE, r, nr, MPI_DOUBLE, MPI_SUM,
367 cr->mpi_comm_mygroup);
372 if (nr > cr->mpb->dbuf_alloc)
374 cr->mpb->dbuf_alloc = nr;
375 srenew(cr->mpb->dbuf, cr->mpb->dbuf_alloc);
379 /* Use two step summing */
380 MPI_Allreduce(r, cr->mpb->dbuf, nr, MPI_DOUBLE, MPI_SUM, cr->nc.comm_intra);
381 if (cr->nc.rank_intra == 0)
383 /* Sum with the buffers reversed */
384 MPI_Allreduce(cr->mpb->dbuf, r, nr, MPI_DOUBLE, MPI_SUM,
387 MPI_Bcast(r, nr, MPI_DOUBLE, 0, cr->nc.comm_intra);
391 MPI_Allreduce(r, cr->mpb->dbuf, nr, MPI_DOUBLE, MPI_SUM,
392 cr->mpi_comm_mygroup);
393 for (i = 0; i < nr; i++)
395 r[i] = cr->mpb->dbuf[i];
402 void gmx_sumf(int gmx_unused nr, float gmx_unused r[], const t_commrec gmx_unused *cr)
405 gmx_call("gmx_sumf");
407 #if defined(MPI_IN_PLACE_EXISTS)
410 /* Use two step summing. */
411 if (cr->nc.rank_intra == 0)
413 MPI_Reduce(MPI_IN_PLACE, r, nr, MPI_FLOAT, MPI_SUM, 0,
415 /* Sum the roots of the internal (intra) buffers */
416 MPI_Allreduce(MPI_IN_PLACE, r, nr, MPI_FLOAT, MPI_SUM,
421 /* This is here because of the silly MPI specification
422 that MPI_IN_PLACE should be put in sendbuf instead of recvbuf */
423 MPI_Reduce(r, NULL, nr, MPI_FLOAT, MPI_SUM, 0, cr->nc.comm_intra);
425 MPI_Bcast(r, nr, MPI_FLOAT, 0, cr->nc.comm_intra);
429 MPI_Allreduce(MPI_IN_PLACE, r, nr, MPI_FLOAT, MPI_SUM, cr->mpi_comm_mygroup);
434 if (nr > cr->mpb->fbuf_alloc)
436 cr->mpb->fbuf_alloc = nr;
437 srenew(cr->mpb->fbuf, cr->mpb->fbuf_alloc);
441 /* Use two step summing */
442 MPI_Allreduce(r, cr->mpb->fbuf, nr, MPI_FLOAT, MPI_SUM, cr->nc.comm_intra);
443 if (cr->nc.rank_intra == 0)
445 /* Sum with the buffers reversed */
446 MPI_Allreduce(cr->mpb->fbuf, r, nr, MPI_FLOAT, MPI_SUM,
449 MPI_Bcast(r, nr, MPI_FLOAT, 0, cr->nc.comm_intra);
453 MPI_Allreduce(r, cr->mpb->fbuf, nr, MPI_FLOAT, MPI_SUM,
454 cr->mpi_comm_mygroup);
455 for (i = 0; i < nr; i++)
457 r[i] = cr->mpb->fbuf[i];
464 void gmx_sumi(int gmx_unused nr, int gmx_unused r[], const t_commrec gmx_unused *cr)
467 gmx_call("gmx_sumi");
469 #if defined(MPI_IN_PLACE_EXISTS)
472 /* Use two step summing */
473 if (cr->nc.rank_intra == 0)
475 MPI_Reduce(MPI_IN_PLACE, r, nr, MPI_INT, MPI_SUM, 0, cr->nc.comm_intra);
476 /* Sum with the buffers reversed */
477 MPI_Allreduce(MPI_IN_PLACE, r, nr, MPI_INT, MPI_SUM, cr->nc.comm_inter);
481 /* This is here because of the silly MPI specification
482 that MPI_IN_PLACE should be put in sendbuf instead of recvbuf */
483 MPI_Reduce(r, NULL, nr, MPI_INT, MPI_SUM, 0, cr->nc.comm_intra);
485 MPI_Bcast(r, nr, MPI_INT, 0, cr->nc.comm_intra);
489 MPI_Allreduce(MPI_IN_PLACE, r, nr, MPI_INT, MPI_SUM, cr->mpi_comm_mygroup);
494 if (nr > cr->mpb->ibuf_alloc)
496 cr->mpb->ibuf_alloc = nr;
497 srenew(cr->mpb->ibuf, cr->mpb->ibuf_alloc);
501 /* Use two step summing */
502 MPI_Allreduce(r, cr->mpb->ibuf, nr, MPI_INT, MPI_SUM, cr->nc.comm_intra);
503 if (cr->nc.rank_intra == 0)
505 /* Sum with the buffers reversed */
506 MPI_Allreduce(cr->mpb->ibuf, r, nr, MPI_INT, MPI_SUM, cr->nc.comm_inter);
508 MPI_Bcast(r, nr, MPI_INT, 0, cr->nc.comm_intra);
512 MPI_Allreduce(r, cr->mpb->ibuf, nr, MPI_INT, MPI_SUM, cr->mpi_comm_mygroup);
513 for (i = 0; i < nr; i++)
515 r[i] = cr->mpb->ibuf[i];
522 void gmx_sumli(int gmx_unused nr, gmx_int64_t gmx_unused r[], const t_commrec gmx_unused *cr)
525 gmx_call("gmx_sumli");
527 #if defined(MPI_IN_PLACE_EXISTS)
530 /* Use two step summing */
531 if (cr->nc.rank_intra == 0)
533 MPI_Reduce(MPI_IN_PLACE, r, nr, MPI_INT64_T, MPI_SUM, 0,
535 /* Sum with the buffers reversed */
536 MPI_Allreduce(MPI_IN_PLACE, r, nr, MPI_INT64_T, MPI_SUM,
541 /* This is here because of the silly MPI specification
542 that MPI_IN_PLACE should be put in sendbuf instead of recvbuf */
543 MPI_Reduce(r, NULL, nr, MPI_INT64_T, MPI_SUM, 0, cr->nc.comm_intra);
545 MPI_Bcast(r, nr, MPI_INT64_T, 0, cr->nc.comm_intra);
549 MPI_Allreduce(MPI_IN_PLACE, r, nr, MPI_INT64_T, MPI_SUM, cr->mpi_comm_mygroup);
554 if (nr > cr->mpb->libuf_alloc)
556 cr->mpb->libuf_alloc = nr;
557 srenew(cr->mpb->libuf, cr->mpb->libuf_alloc);
561 /* Use two step summing */
562 MPI_Allreduce(r, cr->mpb->libuf, nr, MPI_INT64_T, MPI_SUM,
564 if (cr->nc.rank_intra == 0)
566 /* Sum with the buffers reversed */
567 MPI_Allreduce(cr->mpb->libuf, r, nr, MPI_INT64_T, MPI_SUM,
570 MPI_Bcast(r, nr, MPI_INT64_T, 0, cr->nc.comm_intra);
574 MPI_Allreduce(r, cr->mpb->libuf, nr, MPI_INT64_T, MPI_SUM,
575 cr->mpi_comm_mygroup);
576 for (i = 0; i < nr; i++)
578 r[i] = cr->mpb->libuf[i];
588 static void gmx_sumd_comm(int nr, double r[], MPI_Comm mpi_comm)
590 #if defined(MPI_IN_PLACE_EXISTS)
591 MPI_Allreduce(MPI_IN_PLACE, r, nr, MPI_DOUBLE, MPI_SUM, mpi_comm);
593 /* this function is only used in code that is not performance critical,
594 (during setup, when comm_rec is not the appropriate communication
595 structure), so this isn't as bad as it looks. */
600 MPI_Allreduce(r, buf, nr, MPI_DOUBLE, MPI_SUM, mpi_comm);
601 for (i = 0; i < nr; i++)
611 static void gmx_sumf_comm(int nr, float r[], MPI_Comm mpi_comm)
613 #if defined(MPI_IN_PLACE_EXISTS)
614 MPI_Allreduce(MPI_IN_PLACE, r, nr, MPI_FLOAT, MPI_SUM, mpi_comm);
616 /* this function is only used in code that is not performance critical,
617 (during setup, when comm_rec is not the appropriate communication
618 structure), so this isn't as bad as it looks. */
623 MPI_Allreduce(r, buf, nr, MPI_FLOAT, MPI_SUM, mpi_comm);
624 for (i = 0; i < nr; i++)
633 void gmx_sumd_sim(int gmx_unused nr, double gmx_unused r[], const gmx_multisim_t gmx_unused *ms)
636 gmx_call("gmx_sumd_sim");
638 gmx_sumd_comm(nr, r, ms->mpi_comm_masters);
642 void gmx_sumf_sim(int gmx_unused nr, float gmx_unused r[], const gmx_multisim_t gmx_unused *ms)
645 gmx_call("gmx_sumf_sim");
647 gmx_sumf_comm(nr, r, ms->mpi_comm_masters);
651 void gmx_sumi_sim(int gmx_unused nr, int gmx_unused r[], const gmx_multisim_t gmx_unused *ms)
654 gmx_call("gmx_sumi_sim");
656 #if defined(MPI_IN_PLACE_EXISTS)
657 MPI_Allreduce(MPI_IN_PLACE, r, nr, MPI_INT, MPI_SUM, ms->mpi_comm_masters);
659 /* this is thread-unsafe, but it will do for now: */
662 if (nr > ms->mpb->ibuf_alloc)
664 ms->mpb->ibuf_alloc = nr;
665 srenew(ms->mpb->ibuf, ms->mpb->ibuf_alloc);
667 MPI_Allreduce(r, ms->mpb->ibuf, nr, MPI_INT, MPI_SUM, ms->mpi_comm_masters);
668 for (i = 0; i < nr; i++)
670 r[i] = ms->mpb->ibuf[i];
676 void gmx_sumli_sim(int gmx_unused nr, gmx_int64_t gmx_unused r[], const gmx_multisim_t gmx_unused *ms)
679 gmx_call("gmx_sumli_sim");
681 #if defined(MPI_IN_PLACE_EXISTS)
682 MPI_Allreduce(MPI_IN_PLACE, r, nr, MPI_INT64_T, MPI_SUM,
683 ms->mpi_comm_masters);
685 /* this is thread-unsafe, but it will do for now: */
688 if (nr > ms->mpb->libuf_alloc)
690 ms->mpb->libuf_alloc = nr;
691 srenew(ms->mpb->libuf, ms->mpb->libuf_alloc);
693 MPI_Allreduce(r, ms->mpb->libuf, nr, MPI_INT64_T, MPI_SUM,
694 ms->mpi_comm_masters);
695 for (i = 0; i < nr; i++)
697 r[i] = ms->mpb->libuf[i];
703 gmx_bool gmx_fexist_master(const char *fname, t_commrec *cr)
709 bExist = gmx_fexist(fname);
713 gmx_bcast(sizeof(bExist), &bExist, cr);
718 void gmx_fatal_collective(int f_errno, const char *file, int line,
719 const t_commrec *cr, gmx_domdec_t *dd,
720 const char *fmt, ...)
723 gmx_bool bMaster, bFinalize;
726 /* Check if we are calling on all processes in MPI_COMM_WORLD */
729 MPI_Comm_compare(cr->mpi_comm_mysim, MPI_COMM_WORLD, &result);
733 MPI_Comm_compare(dd->mpi_comm_all, MPI_COMM_WORLD, &result);
735 /* Any result except MPI_UNEQUAL allows us to call MPI_Finalize */
736 bFinalize = (result != MPI_UNEQUAL);
740 bMaster = (cr != NULL && MASTER(cr)) || (dd != NULL && DDMASTER(dd));
743 gmx_fatal_mpi_va(f_errno, file, line, bMaster, bFinalize, fmt, ap);