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,2015,2016,2017,2018,2019, 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 "gromacs/commandline/filenm.h"
49 #include "gromacs/mdrunutility/multisim.h"
50 #include "gromacs/mdtypes/commrec.h"
51 #include "gromacs/utility/basenetwork.h"
52 #include "gromacs/utility/cstringutil.h"
53 #include "gromacs/utility/fatalerror.h"
54 #include "gromacs/utility/futil.h"
55 #include "gromacs/utility/gmxmpi.h"
56 #include "gromacs/utility/mpiinplacebuffers.h"
57 #include "gromacs/utility/real.h"
58 #include "gromacs/utility/smalloc.h"
60 /* The source code in this file should be thread-safe.
61 Please keep it that way. */
63 CommrecHandle init_commrec(MPI_Comm communicator, const gmx_multisim_t* ms)
71 int rankInCommunicator, sizeOfCommunicator;
74 GMX_RELEASE_ASSERT(gmx_mpi_initialized(), "Must have initialized MPI before building commrec");
76 MPI_Comm_rank(communicator, &rankInCommunicator);
77 MPI_Comm_size(communicator, &sizeOfCommunicator);
79 GMX_UNUSED_VALUE(communicator);
80 rankInCommunicator = 0;
81 sizeOfCommunicator = 1;
87 cr->nnodes = sizeOfCommunicator / ms->nsim;
88 MPI_Comm_split(communicator, ms->sim, rankInCommunicator, &cr->mpi_comm_mysim);
89 cr->mpi_comm_mygroup = cr->mpi_comm_mysim;
90 MPI_Comm_rank(cr->mpi_comm_mysim, &cr->sim_nodeid);
91 MPI_Comm_rank(cr->mpi_comm_mygroup, &cr->nodeid);
96 cr->nnodes = sizeOfCommunicator;
97 cr->nodeid = rankInCommunicator;
98 cr->sim_nodeid = cr->nodeid;
99 cr->mpi_comm_mysim = communicator;
100 cr->mpi_comm_mygroup = communicator;
103 // TODO cr->duty should not be initialized here
104 cr->duty = (DUTY_PP | DUTY_PME);
106 #if GMX_MPI && !MPI_IN_PLACE_EXISTS
107 /* initialize the MPI_IN_PLACE replacement buffers */
109 cr->mpb->ibuf = nullptr;
110 cr->mpb->libuf = nullptr;
111 cr->mpb->fbuf = nullptr;
112 cr->mpb->dbuf = nullptr;
113 cr->mpb->ibuf_alloc = 0;
114 cr->mpb->libuf_alloc = 0;
115 cr->mpb->fbuf_alloc = 0;
116 cr->mpb->dbuf_alloc = 0;
122 void done_commrec(t_commrec* cr)
126 if (nullptr != cr->dd)
129 // done_domdec(cr->dd);
131 done_mpi_in_place_buf(cr->mpb);
134 // TODO We need to be able to free communicators, but the
135 // structure of the commrec and domdec initialization code makes
136 // it hard to avoid both leaks and double frees.
137 bool mySimIsMyGroup = (cr->mpi_comm_mysim == cr->mpi_comm_mygroup);
138 if (cr->mpi_comm_mysim != MPI_COMM_NULL && cr->mpi_comm_mysim != MPI_COMM_WORLD)
141 // MPI_Comm_free(&cr->mpi_comm_mysim);
143 if (!mySimIsMyGroup && cr->mpi_comm_mygroup != MPI_COMM_NULL && cr->mpi_comm_mygroup != MPI_COMM_WORLD)
146 // MPI_Comm_free(&cr->mpi_comm_mygroup);
152 void gmx_setup_nodecomm(FILE gmx_unused* fplog, t_commrec* cr)
156 /* Many MPI implementations do not optimize MPI_Allreduce
157 * (and probably also other global communication calls)
158 * for multi-core nodes connected by a network.
159 * We can optimize such communication by using one MPI call
160 * within each node and one between the nodes.
161 * For MVAPICH2 and Intel MPI this reduces the time for
162 * the global_stat communication by 25%
163 * for 2x2-core 3 GHz Woodcrest connected by mixed DDR/SDR Infiniband.
164 * B. Hess, November 2007
174 // TODO PhysicalNodeCommunicator could be extended/used to handle
175 // the need for per-node per-group communicators.
176 MPI_Comm_size(cr->mpi_comm_mygroup, &n);
177 MPI_Comm_rank(cr->mpi_comm_mygroup, &rank);
179 int nodehash = gmx_physicalnode_id_hash();
183 fprintf(debug, "In gmx_setup_nodecomm: splitting communicator of size %d\n", n);
187 /* The intra-node communicator, split on node number */
188 MPI_Comm_split(cr->mpi_comm_mygroup, nodehash, rank, &nc->comm_intra);
189 MPI_Comm_rank(nc->comm_intra, &nc->rank_intra);
192 fprintf(debug, "In gmx_setup_nodecomm: node ID %d rank within node %d\n", rank, nc->rank_intra);
194 /* The inter-node communicator, split on rank_intra.
195 * We actually only need the one for rank=0,
196 * but it is easier to create them all.
198 MPI_Comm_split(cr->mpi_comm_mygroup, nc->rank_intra, rank, &nc->comm_inter);
199 /* Check if this really created two step communication */
202 MPI_Comm_size(nc->comm_inter, &ng);
203 MPI_Comm_size(nc->comm_intra, &ni);
206 fprintf(debug, "In gmx_setup_nodecomm: groups %d, my group size %d\n", ng, ni);
209 if (getenv("GMX_NO_NODECOMM") == nullptr && ((ng > 1 && ng < n) || (ni > 1 && ni < n)))
214 fprintf(fplog, "Using two step summing over %d groups of on average %.1f ranks\n\n", ng,
217 if (nc->rank_intra > 0)
219 MPI_Comm_free(&nc->comm_inter);
224 /* One group or all processes in a separate group, use normal summing */
225 MPI_Comm_free(&nc->comm_inter);
226 MPI_Comm_free(&nc->comm_intra);
230 "In gmx_setup_nodecomm: not unsing separate inter- and intra-node "
236 /* tMPI runs only on a single node so just use the nodeid */
237 nc->rank_intra = cr->nodeid;
241 void gmx_barrier(const t_commrec gmx_unused* cr)
244 gmx_call("gmx_barrier");
246 MPI_Barrier(cr->mpi_comm_mygroup);
250 void gmx_bcast(int gmx_unused nbytes, void gmx_unused* b, const t_commrec gmx_unused* cr)
253 gmx_call("gmx_bast");
255 MPI_Bcast(b, nbytes, MPI_BYTE, MASTERRANK(cr), cr->mpi_comm_mygroup);
259 void gmx_bcast_sim(int gmx_unused nbytes, void gmx_unused* b, const t_commrec gmx_unused* cr)
262 gmx_call("gmx_bast");
264 MPI_Bcast(b, nbytes, MPI_BYTE, MASTERRANK(cr), cr->mpi_comm_mysim);
268 void gmx_sumd(int gmx_unused nr, double gmx_unused r[], const t_commrec gmx_unused* cr)
271 gmx_call("gmx_sumd");
273 # if MPI_IN_PLACE_EXISTS
276 if (cr->nc.rank_intra == 0)
278 /* Use two step summing. */
279 MPI_Reduce(MPI_IN_PLACE, r, nr, MPI_DOUBLE, MPI_SUM, 0, cr->nc.comm_intra);
280 /* Sum the roots of the internal (intra) buffers. */
281 MPI_Allreduce(MPI_IN_PLACE, r, nr, MPI_DOUBLE, MPI_SUM, cr->nc.comm_inter);
285 /* This is here because of the silly MPI specification
286 that MPI_IN_PLACE should be put in sendbuf instead of recvbuf */
287 MPI_Reduce(r, nullptr, nr, MPI_DOUBLE, MPI_SUM, 0, cr->nc.comm_intra);
289 MPI_Bcast(r, nr, MPI_DOUBLE, 0, cr->nc.comm_intra);
293 MPI_Allreduce(MPI_IN_PLACE, r, nr, MPI_DOUBLE, MPI_SUM, cr->mpi_comm_mygroup);
298 if (nr > cr->mpb->dbuf_alloc)
300 cr->mpb->dbuf_alloc = nr;
301 srenew(cr->mpb->dbuf, cr->mpb->dbuf_alloc);
305 /* Use two step summing */
306 MPI_Allreduce(r, cr->mpb->dbuf, nr, MPI_DOUBLE, MPI_SUM, cr->nc.comm_intra);
307 if (cr->nc.rank_intra == 0)
309 /* Sum with the buffers reversed */
310 MPI_Allreduce(cr->mpb->dbuf, r, nr, MPI_DOUBLE, MPI_SUM, cr->nc.comm_inter);
312 MPI_Bcast(r, nr, MPI_DOUBLE, 0, cr->nc.comm_intra);
316 MPI_Allreduce(r, cr->mpb->dbuf, nr, MPI_DOUBLE, MPI_SUM, cr->mpi_comm_mygroup);
317 for (i = 0; i < nr; i++)
319 r[i] = cr->mpb->dbuf[i];
326 void gmx_sumf(int gmx_unused nr, float gmx_unused r[], const t_commrec gmx_unused* cr)
329 gmx_call("gmx_sumf");
331 # if MPI_IN_PLACE_EXISTS
334 /* Use two step summing. */
335 if (cr->nc.rank_intra == 0)
337 MPI_Reduce(MPI_IN_PLACE, r, nr, MPI_FLOAT, MPI_SUM, 0, cr->nc.comm_intra);
338 /* Sum the roots of the internal (intra) buffers */
339 MPI_Allreduce(MPI_IN_PLACE, r, nr, MPI_FLOAT, MPI_SUM, cr->nc.comm_inter);
343 /* This is here because of the silly MPI specification
344 that MPI_IN_PLACE should be put in sendbuf instead of recvbuf */
345 MPI_Reduce(r, nullptr, nr, MPI_FLOAT, MPI_SUM, 0, cr->nc.comm_intra);
347 MPI_Bcast(r, nr, MPI_FLOAT, 0, cr->nc.comm_intra);
351 MPI_Allreduce(MPI_IN_PLACE, r, nr, MPI_FLOAT, MPI_SUM, cr->mpi_comm_mygroup);
356 if (nr > cr->mpb->fbuf_alloc)
358 cr->mpb->fbuf_alloc = nr;
359 srenew(cr->mpb->fbuf, cr->mpb->fbuf_alloc);
363 /* Use two step summing */
364 MPI_Allreduce(r, cr->mpb->fbuf, nr, MPI_FLOAT, MPI_SUM, cr->nc.comm_intra);
365 if (cr->nc.rank_intra == 0)
367 /* Sum with the buffers reversed */
368 MPI_Allreduce(cr->mpb->fbuf, r, nr, MPI_FLOAT, MPI_SUM, cr->nc.comm_inter);
370 MPI_Bcast(r, nr, MPI_FLOAT, 0, cr->nc.comm_intra);
374 MPI_Allreduce(r, cr->mpb->fbuf, nr, MPI_FLOAT, MPI_SUM, cr->mpi_comm_mygroup);
375 for (i = 0; i < nr; i++)
377 r[i] = cr->mpb->fbuf[i];
384 void gmx_sumi(int gmx_unused nr, int gmx_unused r[], const t_commrec gmx_unused* cr)
387 gmx_call("gmx_sumi");
389 # if MPI_IN_PLACE_EXISTS
392 /* Use two step summing */
393 if (cr->nc.rank_intra == 0)
395 MPI_Reduce(MPI_IN_PLACE, r, nr, MPI_INT, MPI_SUM, 0, cr->nc.comm_intra);
396 /* Sum with the buffers reversed */
397 MPI_Allreduce(MPI_IN_PLACE, r, nr, MPI_INT, MPI_SUM, cr->nc.comm_inter);
401 /* This is here because of the silly MPI specification
402 that MPI_IN_PLACE should be put in sendbuf instead of recvbuf */
403 MPI_Reduce(r, nullptr, nr, MPI_INT, MPI_SUM, 0, cr->nc.comm_intra);
405 MPI_Bcast(r, nr, MPI_INT, 0, cr->nc.comm_intra);
409 MPI_Allreduce(MPI_IN_PLACE, r, nr, MPI_INT, MPI_SUM, cr->mpi_comm_mygroup);
414 if (nr > cr->mpb->ibuf_alloc)
416 cr->mpb->ibuf_alloc = nr;
417 srenew(cr->mpb->ibuf, cr->mpb->ibuf_alloc);
421 /* Use two step summing */
422 MPI_Allreduce(r, cr->mpb->ibuf, nr, MPI_INT, MPI_SUM, cr->nc.comm_intra);
423 if (cr->nc.rank_intra == 0)
425 /* Sum with the buffers reversed */
426 MPI_Allreduce(cr->mpb->ibuf, r, nr, MPI_INT, MPI_SUM, cr->nc.comm_inter);
428 MPI_Bcast(r, nr, MPI_INT, 0, cr->nc.comm_intra);
432 MPI_Allreduce(r, cr->mpb->ibuf, nr, MPI_INT, MPI_SUM, cr->mpi_comm_mygroup);
433 for (i = 0; i < nr; i++)
435 r[i] = cr->mpb->ibuf[i];
442 void gmx_sumli(int gmx_unused nr, int64_t gmx_unused r[], const t_commrec gmx_unused* cr)
445 gmx_call("gmx_sumli");
447 # if MPI_IN_PLACE_EXISTS
450 /* Use two step summing */
451 if (cr->nc.rank_intra == 0)
453 MPI_Reduce(MPI_IN_PLACE, r, nr, MPI_INT64_T, MPI_SUM, 0, cr->nc.comm_intra);
454 /* Sum with the buffers reversed */
455 MPI_Allreduce(MPI_IN_PLACE, r, nr, MPI_INT64_T, MPI_SUM, cr->nc.comm_inter);
459 /* This is here because of the silly MPI specification
460 that MPI_IN_PLACE should be put in sendbuf instead of recvbuf */
461 MPI_Reduce(r, nullptr, nr, MPI_INT64_T, MPI_SUM, 0, cr->nc.comm_intra);
463 MPI_Bcast(r, nr, MPI_INT64_T, 0, cr->nc.comm_intra);
467 MPI_Allreduce(MPI_IN_PLACE, r, nr, MPI_INT64_T, MPI_SUM, cr->mpi_comm_mygroup);
472 if (nr > cr->mpb->libuf_alloc)
474 cr->mpb->libuf_alloc = nr;
475 srenew(cr->mpb->libuf, cr->mpb->libuf_alloc);
479 /* Use two step summing */
480 MPI_Allreduce(r, cr->mpb->libuf, nr, MPI_INT64_T, MPI_SUM, cr->nc.comm_intra);
481 if (cr->nc.rank_intra == 0)
483 /* Sum with the buffers reversed */
484 MPI_Allreduce(cr->mpb->libuf, r, nr, MPI_INT64_T, MPI_SUM, cr->nc.comm_inter);
486 MPI_Bcast(r, nr, MPI_INT64_T, 0, cr->nc.comm_intra);
490 MPI_Allreduce(r, cr->mpb->libuf, nr, MPI_INT64_T, MPI_SUM, cr->mpi_comm_mygroup);
491 for (i = 0; i < nr; i++)
493 r[i] = cr->mpb->libuf[i];
500 const char* opt2fn_master(const char* opt, int nfile, const t_filenm fnm[], t_commrec* cr)
502 return SIMMASTER(cr) ? opt2fn(opt, nfile, fnm) : nullptr;
505 void gmx_fatal_collective(int f_errno,
510 gmx_fmtstr const char* fmt,
517 /* Check if we are calling on all processes in MPI_COMM_WORLD */
518 MPI_Comm_compare(comm, MPI_COMM_WORLD, &result);
519 /* Any result except MPI_UNEQUAL allows us to call MPI_Finalize */
520 bFinalize = (result != MPI_UNEQUAL);
522 GMX_UNUSED_VALUE(comm);
527 gmx_fatal_mpi_va(f_errno, file, line, bMaster, bFinalize, fmt, ap);
531 void simulationBarrier(const t_commrec* cr)
536 MPI_Barrier(cr->mpi_comm_mysim);