2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2005,2006,2007,2008,2009 by the GROMACS development team.
5 * Copyright (c) 2010,2011,2012,2013,2014 by the GROMACS development team.
6 * Copyright (c) 2015,2016,2017,2018,2019 by the GROMACS development team.
7 * Copyright (c) 2020,2021, by the GROMACS development team, led by
8 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
9 * and including many others, as listed in the AUTHORS file in the
10 * top-level source directory and at http://www.gromacs.org.
12 * GROMACS is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 2.1
15 * of the License, or (at your option) any later version.
17 * GROMACS is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with GROMACS; if not, see
24 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 * If you want to redistribute modifications to GROMACS, please
28 * consider that scientific software is very special. Version
29 * control is crucial - bugs must be traceable. We will be happy to
30 * consider code for inclusion in the official distribution, but
31 * derived work must not be called official GROMACS. Details are found
32 * in the README & COPYING files - if they are missing, get the
33 * official version at http://www.gromacs.org.
35 * To help us fund GROMACS development, we humbly ask that you cite
36 * the research papers on the package. Check out http://www.gromacs.org.
56 #include "gromacs/domdec/builder.h"
57 #include "gromacs/domdec/collect.h"
58 #include "gromacs/domdec/dlb.h"
59 #include "gromacs/domdec/dlbtiming.h"
60 #include "gromacs/domdec/domdec_network.h"
61 #include "gromacs/domdec/ga2la.h"
62 #include "gromacs/domdec/gpuhaloexchange.h"
63 #include "gromacs/domdec/localtopologychecker.h"
64 #include "gromacs/domdec/options.h"
65 #include "gromacs/domdec/partition.h"
66 #include "gromacs/domdec/reversetopology.h"
67 #include "gromacs/gmxlib/network.h"
68 #include "gromacs/gmxlib/nrnb.h"
69 #include "gromacs/gpu_utils/device_stream_manager.h"
70 #include "gromacs/gpu_utils/gpu_utils.h"
71 #include "gromacs/hardware/hw_info.h"
72 #include "gromacs/math/vec.h"
73 #include "gromacs/math/vectypes.h"
74 #include "gromacs/mdlib/calc_verletbuf.h"
75 #include "gromacs/mdlib/constr.h"
76 #include "gromacs/mdlib/constraintrange.h"
77 #include "gromacs/mdlib/updategroups.h"
78 #include "gromacs/mdlib/vcm.h"
79 #include "gromacs/mdlib/vsite.h"
80 #include "gromacs/mdtypes/commrec.h"
81 #include "gromacs/mdtypes/forceoutput.h"
82 #include "gromacs/mdtypes/inputrec.h"
83 #include "gromacs/mdtypes/mdrunoptions.h"
84 #include "gromacs/mdtypes/state.h"
85 #include "gromacs/pbcutil/ishift.h"
86 #include "gromacs/pbcutil/pbc.h"
87 #include "gromacs/pulling/pull.h"
88 #include "gromacs/timing/wallcycle.h"
89 #include "gromacs/topology/block.h"
90 #include "gromacs/topology/idef.h"
91 #include "gromacs/topology/ifunc.h"
92 #include "gromacs/topology/mtop_lookup.h"
93 #include "gromacs/topology/mtop_util.h"
94 #include "gromacs/topology/topology.h"
95 #include "gromacs/utility/basedefinitions.h"
96 #include "gromacs/utility/basenetwork.h"
97 #include "gromacs/utility/cstringutil.h"
98 #include "gromacs/utility/enumerationhelpers.h"
99 #include "gromacs/utility/exceptions.h"
100 #include "gromacs/utility/fatalerror.h"
101 #include "gromacs/utility/gmxmpi.h"
102 #include "gromacs/utility/logger.h"
103 #include "gromacs/utility/real.h"
104 #include "gromacs/utility/smalloc.h"
105 #include "gromacs/utility/strconvert.h"
106 #include "gromacs/utility/stringstream.h"
107 #include "gromacs/utility/stringutil.h"
108 #include "gromacs/utility/textwriter.h"
110 #include "atomdistribution.h"
112 #include "cellsizes.h"
113 #include "distribute.h"
114 #include "domdec_constraints.h"
115 #include "domdec_internal.h"
116 #include "domdec_setup.h"
117 #include "domdec_vsite.h"
118 #include "redistribute.h"
121 // TODO remove this when moving domdec into gmx namespace
123 using gmx::DdRankOrder;
124 using gmx::DlbOption;
125 using gmx::DomdecOptions;
126 using gmx::RangePartitioning;
128 static const char* enumValueToString(DlbState enumValue)
130 static constexpr gmx::EnumerationArray<DlbState, const char*> dlbStateNames = {
131 "off", "off", "auto", "locked", "on", "on"
133 return dlbStateNames[enumValue];
136 /* The size per atom group of the cggl_flag buffer in gmx_domdec_comm_t */
139 /* The flags for the cggl_flag buffer in gmx_domdec_comm_t */
140 #define DD_FLAG_NRCG 65535
141 #define DD_FLAG_FW(d) (1 << (16 + (d)*2))
142 #define DD_FLAG_BW(d) (1 << (16 + (d)*2 + 1))
144 /* The DD zone order */
145 static const ivec dd_zo[DD_MAXZONE] = { { 0, 0, 0 }, { 1, 0, 0 }, { 1, 1, 0 }, { 0, 1, 0 },
146 { 0, 1, 1 }, { 0, 0, 1 }, { 1, 0, 1 }, { 1, 1, 1 } };
148 /* The non-bonded zone-pair setup for domain decomposition
149 * The first number is the i-zone, the second number the first j-zone seen by
150 * this i-zone, the third number the last+1 j-zone seen by this i-zone.
151 * As is, this is for 3D decomposition, where there are 4 i-zones.
152 * With 2D decomposition use only the first 2 i-zones and a last+1 j-zone of 4.
153 * With 1D decomposition use only the first i-zone and a last+1 j-zone of 2.
155 static const int ddNonbondedZonePairRanges[DD_MAXIZONE][3] = { { 0, 0, 8 },
160 static void ddindex2xyz(const ivec nc, int ind, ivec xyz)
162 xyz[XX] = ind / (nc[YY] * nc[ZZ]);
163 xyz[YY] = (ind / nc[ZZ]) % nc[YY];
164 xyz[ZZ] = ind % nc[ZZ];
167 static int ddcoord2ddnodeid(gmx_domdec_t* dd, ivec c)
171 const CartesianRankSetup& cartSetup = dd->comm->cartesianRankSetup;
172 const int ddindex = dd_index(dd->numCells, c);
173 if (cartSetup.bCartesianPP_PME)
175 ddnodeid = cartSetup.ddindex2ddnodeid[ddindex];
177 else if (cartSetup.bCartesianPP)
180 MPI_Cart_rank(dd->mpi_comm_all, c, &ddnodeid);
191 int ddglatnr(const gmx_domdec_t* dd, int i)
201 if (i >= dd->comm->atomRanges.numAtomsTotal())
204 "glatnr called with %d, which is larger than the local number of atoms (%d)",
206 dd->comm->atomRanges.numAtomsTotal());
208 atnr = dd->globalAtomIndices[i] + 1;
214 void dd_store_state(const gmx_domdec_t& dd, t_state* state)
216 if (state->ddp_count != dd.ddp_count)
218 gmx_incons("The MD state does not match the domain decomposition state");
221 state->cg_gl.resize(dd.ncg_home);
222 for (int i = 0; i < dd.ncg_home; i++)
224 state->cg_gl[i] = dd.globalAtomGroupIndices[i];
227 state->ddp_count_cg_gl = dd.ddp_count;
230 gmx_domdec_zones_t* domdec_zones(gmx_domdec_t* dd)
232 return &dd->comm->zones;
235 int dd_numAtomsZones(const gmx_domdec_t& dd)
237 return dd.comm->atomRanges.end(DDAtomRanges::Type::Zones);
240 int dd_numHomeAtoms(const gmx_domdec_t& dd)
242 return dd.comm->atomRanges.numHomeAtoms();
245 int dd_natoms_mdatoms(const gmx_domdec_t& dd)
247 /* We currently set mdatoms entries for all atoms:
248 * local + non-local + communicated for vsite + constraints
251 return dd.comm->atomRanges.numAtomsTotal();
254 int dd_natoms_vsite(const gmx_domdec_t& dd)
256 return dd.comm->atomRanges.end(DDAtomRanges::Type::Vsites);
259 void dd_get_constraint_range(const gmx_domdec_t& dd, int* at_start, int* at_end)
261 *at_start = dd.comm->atomRanges.start(DDAtomRanges::Type::Constraints);
262 *at_end = dd.comm->atomRanges.end(DDAtomRanges::Type::Constraints);
265 void dd_move_x(gmx_domdec_t* dd, const matrix box, gmx::ArrayRef<gmx::RVec> x, gmx_wallcycle* wcycle)
267 wallcycle_start(wcycle, WallCycleCounter::MoveX);
269 rvec shift = { 0, 0, 0 };
271 gmx_domdec_comm_t* comm = dd->comm;
274 int nat_tot = comm->atomRanges.numHomeAtoms();
275 for (int d = 0; d < dd->ndim; d++)
277 const bool bPBC = (dd->ci[dd->dim[d]] == 0);
278 const bool bScrew = (bPBC && dd->unitCellInfo.haveScrewPBC && dd->dim[d] == XX);
281 copy_rvec(box[dd->dim[d]], shift);
283 gmx_domdec_comm_dim_t* cd = &comm->cd[d];
284 for (const gmx_domdec_ind_t& ind : cd->ind)
286 DDBufferAccess<gmx::RVec> sendBufferAccess(comm->rvecBuffer, ind.nsend[nzone + 1]);
287 gmx::ArrayRef<gmx::RVec>& sendBuffer = sendBufferAccess.buffer;
291 for (int j : ind.index)
293 sendBuffer[n] = x[j];
299 for (int j : ind.index)
301 /* We need to shift the coordinates */
302 for (int d = 0; d < DIM; d++)
304 sendBuffer[n][d] = x[j][d] + shift[d];
311 for (int j : ind.index)
314 sendBuffer[n][XX] = x[j][XX] + shift[XX];
316 * This operation requires a special shift force
317 * treatment, which is performed in calc_vir.
319 sendBuffer[n][YY] = box[YY][YY] - x[j][YY];
320 sendBuffer[n][ZZ] = box[ZZ][ZZ] - x[j][ZZ];
325 DDBufferAccess<gmx::RVec> receiveBufferAccess(
326 comm->rvecBuffer2, cd->receiveInPlace ? 0 : ind.nrecv[nzone + 1]);
328 gmx::ArrayRef<gmx::RVec> receiveBuffer;
329 if (cd->receiveInPlace)
331 receiveBuffer = gmx::arrayRefFromArray(x.data() + nat_tot, ind.nrecv[nzone + 1]);
335 receiveBuffer = receiveBufferAccess.buffer;
337 /* Send and receive the coordinates */
338 ddSendrecv(dd, d, dddirBackward, sendBuffer, receiveBuffer);
340 if (!cd->receiveInPlace)
343 for (int zone = 0; zone < nzone; zone++)
345 for (int i = ind.cell2at0[zone]; i < ind.cell2at1[zone]; i++)
347 x[i] = receiveBuffer[j++];
351 nat_tot += ind.nrecv[nzone + 1];
356 wallcycle_stop(wcycle, WallCycleCounter::MoveX);
359 void dd_move_f(gmx_domdec_t* dd, gmx::ForceWithShiftForces* forceWithShiftForces, gmx_wallcycle* wcycle)
361 wallcycle_start(wcycle, WallCycleCounter::MoveF);
363 gmx::ArrayRef<gmx::RVec> f = forceWithShiftForces->force();
364 gmx::ArrayRef<gmx::RVec> fshift = forceWithShiftForces->shiftForces();
366 gmx_domdec_comm_t& comm = *dd->comm;
367 int nzone = comm.zones.n / 2;
368 int nat_tot = comm.atomRanges.end(DDAtomRanges::Type::Zones);
369 for (int d = dd->ndim - 1; d >= 0; d--)
371 /* Only forces in domains near the PBC boundaries need to
372 consider PBC in the treatment of fshift */
373 const bool shiftForcesNeedPbc =
374 (forceWithShiftForces->computeVirial() && dd->ci[dd->dim[d]] == 0);
375 const bool applyScrewPbc =
376 (shiftForcesNeedPbc && dd->unitCellInfo.haveScrewPBC && dd->dim[d] == XX);
377 /* Determine which shift vector we need */
378 ivec vis = { 0, 0, 0 };
380 const int is = gmx::ivecToShiftIndex(vis);
382 /* Loop over the pulses */
383 const gmx_domdec_comm_dim_t& cd = comm.cd[d];
384 for (int p = cd.numPulses() - 1; p >= 0; p--)
386 const gmx_domdec_ind_t& ind = cd.ind[p];
387 DDBufferAccess<gmx::RVec> receiveBufferAccess(comm.rvecBuffer, ind.nsend[nzone + 1]);
388 gmx::ArrayRef<gmx::RVec>& receiveBuffer = receiveBufferAccess.buffer;
390 nat_tot -= ind.nrecv[nzone + 1];
392 DDBufferAccess<gmx::RVec> sendBufferAccess(
393 comm.rvecBuffer2, cd.receiveInPlace ? 0 : ind.nrecv[nzone + 1]);
395 gmx::ArrayRef<gmx::RVec> sendBuffer;
396 if (cd.receiveInPlace)
398 sendBuffer = gmx::arrayRefFromArray(f.data() + nat_tot, ind.nrecv[nzone + 1]);
402 sendBuffer = sendBufferAccess.buffer;
404 for (int zone = 0; zone < nzone; zone++)
406 for (int i = ind.cell2at0[zone]; i < ind.cell2at1[zone]; i++)
408 sendBuffer[j++] = f[i];
412 /* Communicate the forces */
413 ddSendrecv(dd, d, dddirForward, sendBuffer, receiveBuffer);
414 /* Add the received forces */
416 if (!shiftForcesNeedPbc)
418 for (int j : ind.index)
420 for (int d = 0; d < DIM; d++)
422 f[j][d] += receiveBuffer[n][d];
427 else if (!applyScrewPbc)
429 for (int j : ind.index)
431 for (int d = 0; d < DIM; d++)
433 f[j][d] += receiveBuffer[n][d];
435 /* Add this force to the shift force */
436 for (int d = 0; d < DIM; d++)
438 fshift[is][d] += receiveBuffer[n][d];
445 for (int j : ind.index)
447 /* Rotate the force */
448 f[j][XX] += receiveBuffer[n][XX];
449 f[j][YY] -= receiveBuffer[n][YY];
450 f[j][ZZ] -= receiveBuffer[n][ZZ];
451 if (shiftForcesNeedPbc)
453 /* Add this force to the shift force */
454 for (int d = 0; d < DIM; d++)
456 fshift[is][d] += receiveBuffer[n][d];
465 wallcycle_stop(wcycle, WallCycleCounter::MoveF);
468 /* Convenience function for extracting a real buffer from an rvec buffer
470 * To reduce the number of temporary communication buffers and avoid
471 * cache polution, we reuse gmx::RVec buffers for storing reals.
472 * This functions return a real buffer reference with the same number
473 * of elements as the gmx::RVec buffer (so 1/3 of the size in bytes).
475 static gmx::ArrayRef<real> realArrayRefFromRvecArrayRef(gmx::ArrayRef<gmx::RVec> arrayRef)
477 return gmx::arrayRefFromArray(as_rvec_array(arrayRef.data())[0], arrayRef.size());
480 void dd_atom_spread_real(gmx_domdec_t* dd, real v[])
482 gmx_domdec_comm_t* comm = dd->comm;
485 int nat_tot = comm->atomRanges.numHomeAtoms();
486 for (int d = 0; d < dd->ndim; d++)
488 gmx_domdec_comm_dim_t* cd = &comm->cd[d];
489 for (const gmx_domdec_ind_t& ind : cd->ind)
491 /* Note: We provision for RVec instead of real, so a factor of 3
492 * more than needed. The buffer actually already has this size
493 * and we pass a plain pointer below, so this does not matter.
495 DDBufferAccess<gmx::RVec> sendBufferAccess(comm->rvecBuffer, ind.nsend[nzone + 1]);
496 gmx::ArrayRef<real> sendBuffer = realArrayRefFromRvecArrayRef(sendBufferAccess.buffer);
498 for (int j : ind.index)
500 sendBuffer[n++] = v[j];
503 DDBufferAccess<gmx::RVec> receiveBufferAccess(
504 comm->rvecBuffer2, cd->receiveInPlace ? 0 : ind.nrecv[nzone + 1]);
506 gmx::ArrayRef<real> receiveBuffer;
507 if (cd->receiveInPlace)
509 receiveBuffer = gmx::arrayRefFromArray(v + nat_tot, ind.nrecv[nzone + 1]);
513 receiveBuffer = realArrayRefFromRvecArrayRef(receiveBufferAccess.buffer);
515 /* Send and receive the data */
516 ddSendrecv(dd, d, dddirBackward, sendBuffer, receiveBuffer);
517 if (!cd->receiveInPlace)
520 for (int zone = 0; zone < nzone; zone++)
522 for (int i = ind.cell2at0[zone]; i < ind.cell2at1[zone]; i++)
524 v[i] = receiveBuffer[j++];
528 nat_tot += ind.nrecv[nzone + 1];
534 void dd_atom_sum_real(gmx_domdec_t* dd, real v[])
536 gmx_domdec_comm_t* comm = dd->comm;
538 int nzone = comm->zones.n / 2;
539 int nat_tot = comm->atomRanges.end(DDAtomRanges::Type::Zones);
540 for (int d = dd->ndim - 1; d >= 0; d--)
542 gmx_domdec_comm_dim_t* cd = &comm->cd[d];
543 for (int p = cd->numPulses() - 1; p >= 0; p--)
545 const gmx_domdec_ind_t& ind = cd->ind[p];
547 /* Note: We provision for RVec instead of real, so a factor of 3
548 * more than needed. The buffer actually already has this size
549 * and we typecast, so this works as intended.
551 DDBufferAccess<gmx::RVec> receiveBufferAccess(comm->rvecBuffer, ind.nsend[nzone + 1]);
552 gmx::ArrayRef<real> receiveBuffer = realArrayRefFromRvecArrayRef(receiveBufferAccess.buffer);
553 nat_tot -= ind.nrecv[nzone + 1];
555 DDBufferAccess<gmx::RVec> sendBufferAccess(
556 comm->rvecBuffer2, cd->receiveInPlace ? 0 : ind.nrecv[nzone + 1]);
558 gmx::ArrayRef<real> sendBuffer;
559 if (cd->receiveInPlace)
561 sendBuffer = gmx::arrayRefFromArray(v + nat_tot, ind.nrecv[nzone + 1]);
565 sendBuffer = realArrayRefFromRvecArrayRef(sendBufferAccess.buffer);
567 for (int zone = 0; zone < nzone; zone++)
569 for (int i = ind.cell2at0[zone]; i < ind.cell2at1[zone]; i++)
571 sendBuffer[j++] = v[i];
575 /* Communicate the forces */
576 ddSendrecv(dd, d, dddirForward, sendBuffer, receiveBuffer);
577 /* Add the received forces */
579 for (int j : ind.index)
581 v[j] += receiveBuffer[n];
589 real dd_cutoff_multibody(const gmx_domdec_t* dd)
591 const gmx_domdec_comm_t& comm = *dd->comm;
592 const DDSystemInfo& systemInfo = comm.systemInfo;
595 if (systemInfo.haveInterDomainMultiBodyBondeds)
597 if (comm.cutoff_mbody > 0)
599 r = comm.cutoff_mbody;
603 /* cutoff_mbody=0 means we do not have DLB */
604 r = comm.cellsize_min[dd->dim[0]];
605 for (int di = 1; di < dd->ndim; di++)
607 r = std::min(r, comm.cellsize_min[dd->dim[di]]);
609 if (comm.systemInfo.filterBondedCommunication)
611 r = std::max(r, comm.cutoff_mbody);
615 r = std::min(r, systemInfo.cutoff);
623 real dd_cutoff_twobody(const gmx_domdec_t* dd)
625 const real r_mb = dd_cutoff_multibody(dd);
627 return std::max(dd->comm->systemInfo.cutoff, r_mb);
631 static void dd_cart_coord2pmecoord(const DDRankSetup& ddRankSetup,
632 const CartesianRankSetup& cartSetup,
636 const int nc = ddRankSetup.numPPCells[cartSetup.cartpmedim];
637 const int ntot = cartSetup.ntot[cartSetup.cartpmedim];
638 copy_ivec(coord, coord_pme);
639 coord_pme[cartSetup.cartpmedim] =
640 nc + (coord[cartSetup.cartpmedim] * (ntot - nc) + (ntot - nc) / 2) / nc;
643 /* Returns the PME rank index in 0...npmenodes-1 for the PP cell with index ddCellIndex */
644 static int ddindex2pmeindex(const DDRankSetup& ddRankSetup, const int ddCellIndex)
646 const int npp = ddRankSetup.numPPRanks;
647 const int npme = ddRankSetup.numRanksDoingPme;
649 /* Here we assign a PME node to communicate with this DD node
650 * by assuming that the major index of both is x.
651 * We add npme/2 to obtain an even distribution.
653 return (ddCellIndex * npme + npme / 2) / npp;
656 static std::vector<int> dd_interleaved_pme_ranks(const DDRankSetup& ddRankSetup)
658 std::vector<int> pmeRanks(ddRankSetup.numRanksDoingPme);
661 for (int i = 0; i < ddRankSetup.numPPRanks; i++)
663 const int p0 = ddindex2pmeindex(ddRankSetup, i);
664 const int p1 = ddindex2pmeindex(ddRankSetup, i + 1);
665 if (i + 1 == ddRankSetup.numPPRanks || p1 > p0)
669 fprintf(debug, "pme_rank[%d] = %d\n", n, i + 1 + n);
671 pmeRanks[n] = i + 1 + n;
679 static int gmx_ddcoord2pmeindex(const gmx_domdec_t& dd, int x, int y, int z)
686 const int slab = ddindex2pmeindex(dd.comm->ddRankSetup, dd_index(dd.numCells, coords));
691 static int ddcoord2simnodeid(const t_commrec* cr, int x, int y, int z)
693 const CartesianRankSetup& cartSetup = cr->dd->comm->cartesianRankSetup;
694 ivec coords = { x, y, z };
697 if (cartSetup.bCartesianPP_PME)
700 MPI_Cart_rank(cr->mpi_comm_mysim, coords, &nodeid);
705 int ddindex = dd_index(cr->dd->numCells, coords);
706 if (cartSetup.bCartesianPP)
708 nodeid = cartSetup.ddindex2simnodeid[ddindex];
712 if (cr->dd->comm->ddRankSetup.usePmeOnlyRanks)
714 nodeid = ddindex + gmx_ddcoord2pmeindex(*cr->dd, x, y, z);
726 static int dd_simnode2pmenode(const DDRankSetup& ddRankSetup,
727 const CartesianRankSetup& cartSetup,
728 gmx::ArrayRef<const int> pmeRanks,
729 const t_commrec gmx_unused* cr,
730 const int sim_nodeid)
734 /* This assumes a uniform x domain decomposition grid cell size */
735 if (cartSetup.bCartesianPP_PME)
738 ivec coord, coord_pme;
739 MPI_Cart_coords(cr->mpi_comm_mysim, sim_nodeid, DIM, coord);
740 if (coord[cartSetup.cartpmedim] < ddRankSetup.numPPCells[cartSetup.cartpmedim])
742 /* This is a PP rank */
743 dd_cart_coord2pmecoord(ddRankSetup, cartSetup, coord, coord_pme);
744 MPI_Cart_rank(cr->mpi_comm_mysim, coord_pme, &pmenode);
748 else if (cartSetup.bCartesianPP)
750 if (sim_nodeid < ddRankSetup.numPPRanks)
752 pmenode = ddRankSetup.numPPRanks + ddindex2pmeindex(ddRankSetup, sim_nodeid);
757 /* This assumes DD cells with identical x coordinates
758 * are numbered sequentially.
760 if (pmeRanks.empty())
762 if (sim_nodeid < ddRankSetup.numPPRanks)
764 /* The DD index equals the nodeid */
765 pmenode = ddRankSetup.numPPRanks + ddindex2pmeindex(ddRankSetup, sim_nodeid);
771 while (sim_nodeid > pmeRanks[i])
775 if (sim_nodeid < pmeRanks[i])
777 pmenode = pmeRanks[i];
785 NumPmeDomains getNumPmeDomains(const gmx_domdec_t* dd)
789 return { dd->comm->ddRankSetup.npmenodes_x, dd->comm->ddRankSetup.npmenodes_y };
797 std::vector<int> get_pme_ddranks(const t_commrec* cr, const int pmenodeid)
799 const DDRankSetup& ddRankSetup = cr->dd->comm->ddRankSetup;
800 const CartesianRankSetup& cartSetup = cr->dd->comm->cartesianRankSetup;
801 GMX_RELEASE_ASSERT(ddRankSetup.usePmeOnlyRanks,
802 "This function should only be called when PME-only ranks are in use");
803 std::vector<int> ddranks;
804 ddranks.reserve((ddRankSetup.numPPRanks + ddRankSetup.numRanksDoingPme - 1) / ddRankSetup.numRanksDoingPme);
806 for (int x = 0; x < ddRankSetup.numPPCells[XX]; x++)
808 for (int y = 0; y < ddRankSetup.numPPCells[YY]; y++)
810 for (int z = 0; z < ddRankSetup.numPPCells[ZZ]; z++)
812 if (cartSetup.bCartesianPP_PME)
814 ivec coord = { x, y, z };
816 dd_cart_coord2pmecoord(ddRankSetup, cartSetup, coord, coord_pme);
817 if (cr->dd->ci[XX] == coord_pme[XX] && cr->dd->ci[YY] == coord_pme[YY]
818 && cr->dd->ci[ZZ] == coord_pme[ZZ])
820 ddranks.push_back(ddcoord2simnodeid(cr, x, y, z));
825 /* The slab corresponds to the nodeid in the PME group */
826 if (gmx_ddcoord2pmeindex(*cr->dd, x, y, z) == pmenodeid)
828 ddranks.push_back(ddcoord2simnodeid(cr, x, y, z));
837 static gmx_bool receive_vir_ener(const gmx_domdec_t* dd, gmx::ArrayRef<const int> pmeRanks, const t_commrec* cr)
839 bool bReceive = true;
841 const DDRankSetup& ddRankSetup = dd->comm->ddRankSetup;
842 if (ddRankSetup.usePmeOnlyRanks)
844 const CartesianRankSetup& cartSetup = dd->comm->cartesianRankSetup;
845 if (cartSetup.bCartesianPP_PME)
848 int pmenode = dd_simnode2pmenode(ddRankSetup, cartSetup, pmeRanks, cr, cr->sim_nodeid);
850 MPI_Cart_coords(cr->mpi_comm_mysim, cr->sim_nodeid, DIM, coords);
851 coords[cartSetup.cartpmedim]++;
852 if (coords[cartSetup.cartpmedim] < dd->numCells[cartSetup.cartpmedim])
855 MPI_Cart_rank(cr->mpi_comm_mysim, coords, &rank);
856 if (dd_simnode2pmenode(ddRankSetup, cartSetup, pmeRanks, cr, rank) == pmenode)
858 /* This is not the last PP node for pmenode */
865 "Without MPI we should not have Cartesian PP-PME with #PMEnodes < #DDnodes");
870 int pmenode = dd_simnode2pmenode(ddRankSetup, cartSetup, pmeRanks, cr, cr->sim_nodeid);
871 if (cr->sim_nodeid + 1 < cr->nnodes
872 && dd_simnode2pmenode(ddRankSetup, cartSetup, pmeRanks, cr, cr->sim_nodeid + 1) == pmenode)
874 /* This is not the last PP node for pmenode */
883 static void set_slb_pme_dim_f(gmx_domdec_t* dd, int dim, real** dim_f)
885 gmx_domdec_comm_t* comm = dd->comm;
887 snew(*dim_f, dd->numCells[dim] + 1);
889 for (int i = 1; i < dd->numCells[dim]; i++)
891 if (comm->slb_frac[dim])
893 (*dim_f)[i] = (*dim_f)[i - 1] + comm->slb_frac[dim][i - 1];
897 (*dim_f)[i] = static_cast<real>(i) / static_cast<real>(dd->numCells[dim]);
900 (*dim_f)[dd->numCells[dim]] = 1;
903 static void init_ddpme(gmx_domdec_t* dd, gmx_ddpme_t* ddpme, int dimind)
905 const DDRankSetup& ddRankSetup = dd->comm->ddRankSetup;
907 if (dimind == 0 && dd->dim[0] == YY && ddRankSetup.npmenodes_x == 1)
915 ddpme->dim_match = (ddpme->dim == dd->dim[dimind]);
917 ddpme->nslab = (ddpme->dim == 0 ? ddRankSetup.npmenodes_x : ddRankSetup.npmenodes_y);
919 if (ddpme->nslab <= 1)
924 const int nso = ddRankSetup.numRanksDoingPme / ddpme->nslab;
925 /* Determine for each PME slab the PP location range for dimension dim */
926 snew(ddpme->pp_min, ddpme->nslab);
927 snew(ddpme->pp_max, ddpme->nslab);
928 for (int slab = 0; slab < ddpme->nslab; slab++)
930 ddpme->pp_min[slab] = dd->numCells[dd->dim[dimind]] - 1;
931 ddpme->pp_max[slab] = 0;
933 for (int i = 0; i < dd->nnodes; i++)
936 ddindex2xyz(dd->numCells, i, xyz);
937 /* For y only use our y/z slab.
938 * This assumes that the PME x grid size matches the DD grid size.
940 if (dimind == 0 || xyz[XX] == dd->ci[XX])
942 const int pmeindex = ddindex2pmeindex(ddRankSetup, i);
943 const int slab = (dimind == 0) ? (pmeindex / nso) : (pmeindex % ddpme->nslab);
944 ddpme->pp_min[slab] = std::min(ddpme->pp_min[slab], xyz[dimind]);
945 ddpme->pp_max[slab] = std::max(ddpme->pp_max[slab], xyz[dimind]);
949 set_slb_pme_dim_f(dd, ddpme->dim, &ddpme->slb_dim_f);
952 int dd_pme_maxshift_x(const gmx_domdec_t& dd)
954 const DDRankSetup& ddRankSetup = dd.comm->ddRankSetup;
956 if (ddRankSetup.ddpme[0].dim == XX)
958 return ddRankSetup.ddpme[0].maxshift;
966 int dd_pme_maxshift_y(const gmx_domdec_t& dd)
968 const DDRankSetup& ddRankSetup = dd.comm->ddRankSetup;
970 if (ddRankSetup.ddpme[0].dim == YY)
972 return ddRankSetup.ddpme[0].maxshift;
974 else if (ddRankSetup.npmedecompdim >= 2 && ddRankSetup.ddpme[1].dim == YY)
976 return ddRankSetup.ddpme[1].maxshift;
984 bool ddUsesUpdateGroups(const gmx_domdec_t& dd)
986 return dd.comm->systemInfo.useUpdateGroups;
989 void dd_cycles_add(const gmx_domdec_t* dd, float cycles, int ddCycl)
991 /* Note that the cycles value can be incorrect, either 0 or some
992 * extremely large value, when our thread migrated to another core
993 * with an unsynchronized cycle counter. If this happens less often
994 * that once per nstlist steps, this will not cause issues, since
995 * we later subtract the maximum value from the sum over nstlist steps.
996 * A zero count will slightly lower the total, but that's a small effect.
997 * Note that the main purpose of the subtraction of the maximum value
998 * is to avoid throwing off the load balancing when stalls occur due
999 * e.g. system activity or network congestion.
1001 dd->comm->cycl[ddCycl] += cycles;
1002 dd->comm->cycl_n[ddCycl]++;
1003 if (cycles > dd->comm->cycl_max[ddCycl])
1005 dd->comm->cycl_max[ddCycl] = cycles;
1010 static void make_load_communicator(gmx_domdec_t* dd, int dim_ind, ivec loc)
1012 MPI_Comm c_row = MPI_COMM_NULL;
1014 bool bPartOfGroup = false;
1016 const int dim = dd->dim[dim_ind];
1017 copy_ivec(loc, loc_c);
1018 for (int i = 0; i < dd->numCells[dim]; i++)
1021 const int rank = dd_index(dd->numCells, loc_c);
1022 if (rank == dd->rank)
1024 /* This process is part of the group */
1025 bPartOfGroup = TRUE;
1028 MPI_Comm_split(dd->mpi_comm_all, bPartOfGroup ? 0 : MPI_UNDEFINED, dd->rank, &c_row);
1031 dd->comm->mpi_comm_load[dim_ind] = c_row;
1032 if (!isDlbDisabled(dd->comm))
1034 DDCellsizesWithDlb& cellsizes = dd->comm->cellsizesWithDlb[dim_ind];
1036 if (dd->ci[dim] == dd->master_ci[dim])
1038 /* This is the root process of this row */
1039 cellsizes.rowMaster = std::make_unique<RowMaster>();
1041 RowMaster& rowMaster = *cellsizes.rowMaster;
1042 rowMaster.cellFrac.resize(ddCellFractionBufferSize(dd, dim_ind));
1043 rowMaster.oldCellFrac.resize(dd->numCells[dim] + 1);
1044 rowMaster.isCellMin.resize(dd->numCells[dim]);
1047 rowMaster.bounds.resize(dd->numCells[dim]);
1049 rowMaster.buf_ncd.resize(dd->numCells[dim]);
1053 /* This is not a root process, we only need to receive cell_f */
1054 cellsizes.fracRow.resize(ddCellFractionBufferSize(dd, dim_ind));
1057 if (dd->ci[dim] == dd->master_ci[dim])
1059 snew(dd->comm->load[dim_ind].load, dd->numCells[dim] * DD_NLOAD_MAX);
1065 void dd_setup_dlb_resource_sharing(const t_commrec* cr, int gpu_id)
1068 MPI_Comm mpi_comm_pp_physicalnode = MPI_COMM_NULL;
1070 if (!thisRankHasDuty(cr, DUTY_PP) || gpu_id < 0)
1072 /* Only ranks with short-ranged tasks (currently) use GPUs.
1073 * If we don't have GPUs assigned, there are no resources to share.
1078 const int physicalnode_id_hash = gmx_physicalnode_id_hash();
1080 gmx_domdec_t* dd = cr->dd;
1084 fprintf(debug, "dd_setup_dd_dlb_gpu_sharing:\n");
1085 fprintf(debug, "DD PP rank %d physical node hash %d gpu_id %d\n", dd->rank, physicalnode_id_hash, gpu_id);
1087 /* Split the PP communicator over the physical nodes */
1088 /* TODO: See if we should store this (before), as it's also used for
1089 * for the nodecomm summation.
1091 // TODO PhysicalNodeCommunicator could be extended/used to handle
1092 // the need for per-node per-group communicators.
1093 MPI_Comm_split(dd->mpi_comm_all, physicalnode_id_hash, dd->rank, &mpi_comm_pp_physicalnode);
1094 MPI_Comm_split(mpi_comm_pp_physicalnode, gpu_id, dd->rank, &dd->comm->mpi_comm_gpu_shared);
1095 MPI_Comm_free(&mpi_comm_pp_physicalnode);
1096 MPI_Comm_size(dd->comm->mpi_comm_gpu_shared, &dd->comm->nrank_gpu_shared);
1100 fprintf(debug, "nrank_gpu_shared %d\n", dd->comm->nrank_gpu_shared);
1103 /* Note that some ranks could share a GPU, while others don't */
1105 if (dd->comm->nrank_gpu_shared == 1)
1107 MPI_Comm_free(&dd->comm->mpi_comm_gpu_shared);
1110 GMX_UNUSED_VALUE(cr);
1111 GMX_UNUSED_VALUE(gpu_id);
1115 static void make_load_communicators(gmx_domdec_t gmx_unused* dd)
1122 fprintf(debug, "Making load communicators\n");
1125 dd->comm->load = new domdec_load_t[std::max(dd->ndim, 1)];
1126 snew(dd->comm->mpi_comm_load, std::max(dd->ndim, 1));
1134 make_load_communicator(dd, 0, loc);
1137 const int dim0 = dd->dim[0];
1138 for (int i = 0; i < dd->numCells[dim0]; i++)
1141 make_load_communicator(dd, 1, loc);
1146 const int dim0 = dd->dim[0];
1147 for (int i = 0; i < dd->numCells[dim0]; i++)
1150 const int dim1 = dd->dim[1];
1151 for (int j = 0; j < dd->numCells[dim1]; j++)
1154 make_load_communicator(dd, 2, loc);
1161 fprintf(debug, "Finished making load communicators\n");
1166 /*! \brief Sets up the relation between neighboring domains and zones */
1167 static void setup_neighbor_relations(gmx_domdec_t* dd)
1170 GMX_ASSERT((dd->ndim >= 0) && (dd->ndim <= DIM), "Must have valid number of dimensions for DD");
1172 for (int d = 0; d < dd->ndim; d++)
1174 const int dim = dd->dim[d];
1175 copy_ivec(dd->ci, tmp);
1176 tmp[dim] = (tmp[dim] + 1) % dd->numCells[dim];
1177 dd->neighbor[d][0] = ddcoord2ddnodeid(dd, tmp);
1178 copy_ivec(dd->ci, tmp);
1179 tmp[dim] = (tmp[dim] - 1 + dd->numCells[dim]) % dd->numCells[dim];
1180 dd->neighbor[d][1] = ddcoord2ddnodeid(dd, tmp);
1184 "DD rank %d neighbor ranks in dir %d are + %d - %d\n",
1188 dd->neighbor[d][1]);
1192 int nzone = (1 << dd->ndim);
1193 int nizone = (1 << std::max(dd->ndim - 1, 0));
1194 assert(nizone >= 1 && nizone <= DD_MAXIZONE);
1196 gmx_domdec_zones_t* zones = &dd->comm->zones;
1198 for (int i = 0; i < nzone; i++)
1201 clear_ivec(zones->shift[i]);
1202 for (int d = 0; d < dd->ndim; d++)
1204 zones->shift[i][dd->dim[d]] = dd_zo[i][m++];
1209 for (int i = 0; i < nzone; i++)
1211 for (int d = 0; d < DIM; d++)
1213 s[d] = dd->ci[d] - zones->shift[i][d];
1216 s[d] += dd->numCells[d];
1218 else if (s[d] >= dd->numCells[d])
1220 s[d] -= dd->numCells[d];
1224 for (int iZoneIndex = 0; iZoneIndex < nizone; iZoneIndex++)
1227 ddNonbondedZonePairRanges[iZoneIndex][0] == iZoneIndex,
1228 "The first element for each ddNonbondedZonePairRanges should match its index");
1230 DDPairInteractionRanges iZone;
1231 iZone.iZoneIndex = iZoneIndex;
1232 /* dd_zp3 is for 3D decomposition, for fewer dimensions use only
1233 * j-zones up to nzone.
1235 iZone.jZoneRange = gmx::Range<int>(std::min(ddNonbondedZonePairRanges[iZoneIndex][1], nzone),
1236 std::min(ddNonbondedZonePairRanges[iZoneIndex][2], nzone));
1237 for (int dim = 0; dim < DIM; dim++)
1239 if (dd->numCells[dim] == 1)
1241 /* All shifts should be allowed */
1242 iZone.shift0[dim] = -1;
1243 iZone.shift1[dim] = 1;
1247 /* Determine the min/max j-zone shift wrt the i-zone */
1248 iZone.shift0[dim] = 1;
1249 iZone.shift1[dim] = -1;
1250 for (int jZone : iZone.jZoneRange)
1252 int shift_diff = zones->shift[jZone][dim] - zones->shift[iZoneIndex][dim];
1253 if (shift_diff < iZone.shift0[dim])
1255 iZone.shift0[dim] = shift_diff;
1257 if (shift_diff > iZone.shift1[dim])
1259 iZone.shift1[dim] = shift_diff;
1265 zones->iZones.push_back(iZone);
1268 if (!isDlbDisabled(dd->comm))
1270 dd->comm->cellsizesWithDlb.resize(dd->ndim);
1273 if (dd->comm->ddSettings.recordLoad)
1275 make_load_communicators(dd);
1279 static void make_pp_communicator(const gmx::MDLogger& mdlog,
1281 t_commrec gmx_unused* cr,
1282 bool gmx_unused reorder)
1285 gmx_domdec_comm_t* comm = dd->comm;
1286 CartesianRankSetup& cartSetup = comm->cartesianRankSetup;
1288 if (cartSetup.bCartesianPP)
1290 /* Set up cartesian communication for the particle-particle part */
1292 .appendTextFormatted("Will use a Cartesian communicator: %d x %d x %d",
1298 for (int i = 0; i < DIM; i++)
1302 MPI_Comm comm_cart = MPI_COMM_NULL;
1303 MPI_Cart_create(cr->mpi_comm_mygroup, DIM, dd->numCells, periods, static_cast<int>(reorder), &comm_cart);
1304 /* We overwrite the old communicator with the new cartesian one */
1305 cr->mpi_comm_mygroup = comm_cart;
1308 dd->mpi_comm_all = cr->mpi_comm_mygroup;
1309 MPI_Comm_rank(dd->mpi_comm_all, &dd->rank);
1311 if (cartSetup.bCartesianPP_PME)
1313 /* Since we want to use the original cartesian setup for sim,
1314 * and not the one after split, we need to make an index.
1316 cartSetup.ddindex2ddnodeid.resize(dd->nnodes);
1317 cartSetup.ddindex2ddnodeid[dd_index(dd->numCells, dd->ci)] = dd->rank;
1318 gmx_sumi(dd->nnodes, cartSetup.ddindex2ddnodeid.data(), cr);
1319 /* Get the rank of the DD master,
1320 * above we made sure that the master node is a PP node.
1322 int rank = MASTER(cr) ? dd->rank : 0;
1323 MPI_Allreduce(&rank, &dd->masterrank, 1, MPI_INT, MPI_SUM, dd->mpi_comm_all);
1325 else if (cartSetup.bCartesianPP)
1327 if (!comm->ddRankSetup.usePmeOnlyRanks)
1329 /* The PP communicator is also
1330 * the communicator for this simulation
1332 cr->mpi_comm_mysim = cr->mpi_comm_mygroup;
1334 cr->nodeid = dd->rank;
1336 MPI_Cart_coords(dd->mpi_comm_all, dd->rank, DIM, dd->ci);
1338 /* We need to make an index to go from the coordinates
1339 * to the nodeid of this simulation.
1341 cartSetup.ddindex2simnodeid.resize(dd->nnodes);
1342 std::vector<int> buf(dd->nnodes);
1343 if (thisRankHasDuty(cr, DUTY_PP))
1345 buf[dd_index(dd->numCells, dd->ci)] = cr->sim_nodeid;
1347 /* Communicate the ddindex to simulation nodeid index */
1348 MPI_Allreduce(buf.data(), cartSetup.ddindex2simnodeid.data(), dd->nnodes, MPI_INT, MPI_SUM, cr->mpi_comm_mysim);
1350 /* Determine the master coordinates and rank.
1351 * The DD master should be the same node as the master of this sim.
1353 for (int i = 0; i < dd->nnodes; i++)
1355 if (cartSetup.ddindex2simnodeid[i] == 0)
1357 ddindex2xyz(dd->numCells, i, dd->master_ci);
1358 MPI_Cart_rank(dd->mpi_comm_all, dd->master_ci, &dd->masterrank);
1363 fprintf(debug, "The master rank is %d\n", dd->masterrank);
1368 /* No Cartesian communicators */
1369 /* We use the rank in dd->comm->all as DD index */
1370 ddindex2xyz(dd->numCells, dd->rank, dd->ci);
1371 /* The simulation master nodeid is 0, so the DD master rank is also 0 */
1373 clear_ivec(dd->master_ci);
1378 .appendTextFormatted("Domain decomposition rank %d, coordinates %d %d %d\n",
1386 "Domain decomposition rank %d, coordinates %d %d %d\n\n",
1394 static void receive_ddindex2simnodeid(gmx_domdec_t* dd, t_commrec* cr)
1397 CartesianRankSetup& cartSetup = dd->comm->cartesianRankSetup;
1399 if (!cartSetup.bCartesianPP_PME && cartSetup.bCartesianPP)
1401 cartSetup.ddindex2simnodeid.resize(dd->nnodes);
1402 std::vector<int> buf(dd->nnodes);
1403 if (thisRankHasDuty(cr, DUTY_PP))
1405 buf[dd_index(dd->numCells, dd->ci)] = cr->sim_nodeid;
1407 /* Communicate the ddindex to simulation nodeid index */
1408 MPI_Allreduce(buf.data(), cartSetup.ddindex2simnodeid.data(), dd->nnodes, MPI_INT, MPI_SUM, cr->mpi_comm_mysim);
1411 GMX_UNUSED_VALUE(dd);
1412 GMX_UNUSED_VALUE(cr);
1416 static CartesianRankSetup split_communicator(const gmx::MDLogger& mdlog,
1418 const DdRankOrder ddRankOrder,
1419 bool gmx_unused reorder,
1420 const DDRankSetup& ddRankSetup,
1422 std::vector<int>* pmeRanks)
1424 CartesianRankSetup cartSetup;
1426 cartSetup.bCartesianPP = (ddRankOrder == DdRankOrder::cartesian);
1427 cartSetup.bCartesianPP_PME = false;
1429 const ivec& numDDCells = ddRankSetup.numPPCells;
1430 /* Initially we set ntot to the number of PP cells */
1431 copy_ivec(numDDCells, cartSetup.ntot);
1433 if (cartSetup.bCartesianPP)
1435 const int numDDCellsTot = ddRankSetup.numPPRanks;
1437 for (int i = 1; i < DIM; i++)
1439 bDiv[i] = ((ddRankSetup.numRanksDoingPme * numDDCells[i]) % numDDCellsTot == 0);
1441 if (bDiv[YY] || bDiv[ZZ])
1443 cartSetup.bCartesianPP_PME = TRUE;
1444 /* If we have 2D PME decomposition, which is always in x+y,
1445 * we stack the PME only nodes in z.
1446 * Otherwise we choose the direction that provides the thinnest slab
1447 * of PME only nodes as this will have the least effect
1448 * on the PP communication.
1449 * But for the PME communication the opposite might be better.
1451 if (bDiv[ZZ] && (ddRankSetup.npmenodes_y > 1 || !bDiv[YY] || numDDCells[YY] > numDDCells[ZZ]))
1453 cartSetup.cartpmedim = ZZ;
1457 cartSetup.cartpmedim = YY;
1459 cartSetup.ntot[cartSetup.cartpmedim] +=
1460 (ddRankSetup.numRanksDoingPme * numDDCells[cartSetup.cartpmedim]) / numDDCellsTot;
1465 .appendTextFormatted(
1466 "Number of PME-only ranks (%d) is not a multiple of nx*ny (%d*%d) or "
1468 ddRankSetup.numRanksDoingPme,
1474 .appendText("Will not use a Cartesian communicator for PP <-> PME\n");
1478 if (cartSetup.bCartesianPP_PME)
1485 .appendTextFormatted(
1486 "Will use a Cartesian communicator for PP <-> PME: %d x %d x %d",
1489 cartSetup.ntot[ZZ]);
1491 for (int i = 0; i < DIM; i++)
1495 MPI_Comm comm_cart = MPI_COMM_NULL;
1496 MPI_Cart_create(cr->mpi_comm_mysim, DIM, cartSetup.ntot, periods, static_cast<int>(reorder), &comm_cart);
1497 MPI_Comm_rank(comm_cart, &rank);
1498 if (MASTER(cr) && rank != 0)
1500 gmx_fatal(FARGS, "MPI rank 0 was renumbered by MPI_Cart_create, we do not allow this");
1503 /* With this assigment we loose the link to the original communicator
1504 * which will usually be MPI_COMM_WORLD, unless have multisim.
1506 cr->mpi_comm_mysim = comm_cart;
1507 cr->sim_nodeid = rank;
1509 MPI_Cart_coords(cr->mpi_comm_mysim, cr->sim_nodeid, DIM, ddCellIndex);
1512 .appendTextFormatted("Cartesian rank %d, coordinates %d %d %d\n",
1518 if (ddCellIndex[cartSetup.cartpmedim] < numDDCells[cartSetup.cartpmedim])
1522 if (!ddRankSetup.usePmeOnlyRanks
1523 || ddCellIndex[cartSetup.cartpmedim] >= numDDCells[cartSetup.cartpmedim])
1525 cr->duty = DUTY_PME;
1528 /* Split the sim communicator into PP and PME only nodes */
1529 MPI_Comm_split(cr->mpi_comm_mysim,
1530 getThisRankDuties(cr),
1531 dd_index(cartSetup.ntot, ddCellIndex),
1532 &cr->mpi_comm_mygroup);
1534 GMX_UNUSED_VALUE(ddCellIndex);
1539 switch (ddRankOrder)
1541 case DdRankOrder::pp_pme:
1542 GMX_LOG(mdlog.info).appendText("Order of the ranks: PP first, PME last");
1544 case DdRankOrder::interleave:
1545 /* Interleave the PP-only and PME-only ranks */
1546 GMX_LOG(mdlog.info).appendText("Interleaving PP and PME ranks");
1547 *pmeRanks = dd_interleaved_pme_ranks(ddRankSetup);
1549 case DdRankOrder::cartesian: break;
1550 default: gmx_fatal(FARGS, "Invalid ddRankOrder=%d", static_cast<int>(ddRankOrder));
1553 if (dd_simnode2pmenode(ddRankSetup, cartSetup, *pmeRanks, cr, cr->sim_nodeid) == -1)
1555 cr->duty = DUTY_PME;
1562 /* Split the sim communicator into PP and PME only nodes */
1563 MPI_Comm_split(cr->mpi_comm_mysim, getThisRankDuties(cr), cr->nodeid, &cr->mpi_comm_mygroup);
1564 MPI_Comm_rank(cr->mpi_comm_mygroup, &cr->nodeid);
1569 .appendTextFormatted("This rank does only %s work.\n",
1570 thisRankHasDuty(cr, DUTY_PP) ? "particle-particle" : "PME-mesh");
1575 /*! \brief Makes the PP communicator and the PME communicator, when needed
1577 * Returns the Cartesian rank setup.
1578 * Sets \p cr->mpi_comm_mygroup
1579 * For PP ranks, sets the DD PP cell index in \p ddCellIndex.
1580 * With separate PME ranks in interleaved order, set the PME ranks in \p pmeRanks.
1582 static CartesianRankSetup makeGroupCommunicators(const gmx::MDLogger& mdlog,
1583 const DDSettings& ddSettings,
1584 const DdRankOrder ddRankOrder,
1585 const DDRankSetup& ddRankSetup,
1588 std::vector<int>* pmeRanks)
1590 CartesianRankSetup cartSetup;
1592 // As a default, both group and sim communicators are equal to the default communicator
1593 cr->mpi_comm_mygroup = cr->mpiDefaultCommunicator;
1594 cr->mpi_comm_mysim = cr->mpiDefaultCommunicator;
1595 cr->nnodes = cr->sizeOfDefaultCommunicator;
1596 cr->nodeid = cr->rankInDefaultCommunicator;
1597 cr->sim_nodeid = cr->rankInDefaultCommunicator;
1599 if (ddRankSetup.usePmeOnlyRanks)
1601 /* Split the communicator into a PP and PME part */
1602 cartSetup = split_communicator(
1603 mdlog, cr, ddRankOrder, ddSettings.useCartesianReorder, ddRankSetup, ddCellIndex, pmeRanks);
1607 /* All nodes do PP and PME */
1608 /* We do not require separate communicators */
1609 cartSetup.bCartesianPP = false;
1610 cartSetup.bCartesianPP_PME = false;
1616 /*! \brief For PP ranks, sets or makes the communicator
1618 * For PME ranks get the rank id.
1619 * For PP only ranks, sets the PME-only rank.
1621 static void setupGroupCommunication(const gmx::MDLogger& mdlog,
1622 const DDSettings& ddSettings,
1623 gmx::ArrayRef<const int> pmeRanks,
1625 const int numAtomsInSystem,
1628 const DDRankSetup& ddRankSetup = dd->comm->ddRankSetup;
1629 const CartesianRankSetup& cartSetup = dd->comm->cartesianRankSetup;
1631 if (thisRankHasDuty(cr, DUTY_PP))
1633 /* Copy or make a new PP communicator */
1635 /* We (possibly) reordered the nodes in split_communicator,
1636 * so it is no longer required in make_pp_communicator.
1638 const bool useCartesianReorder = (ddSettings.useCartesianReorder && !cartSetup.bCartesianPP_PME);
1640 make_pp_communicator(mdlog, dd, cr, useCartesianReorder);
1644 receive_ddindex2simnodeid(dd, cr);
1647 if (!thisRankHasDuty(cr, DUTY_PME))
1649 /* Set up the commnuication to our PME node */
1650 dd->pme_nodeid = dd_simnode2pmenode(ddRankSetup, cartSetup, pmeRanks, cr, cr->sim_nodeid);
1651 dd->pme_receive_vir_ener = receive_vir_ener(dd, pmeRanks, cr);
1655 "My pme_nodeid %d receive ener %s\n",
1657 gmx::boolToString(dd->pme_receive_vir_ener));
1662 dd->pme_nodeid = -1;
1665 /* We can not use DDMASTER(dd), because dd->masterrank is set later */
1668 dd->ma = std::make_unique<AtomDistribution>(dd->numCells, numAtomsInSystem, numAtomsInSystem);
1672 static real* get_slb_frac(const gmx::MDLogger& mdlog, const char* dir, int nc, const char* size_string)
1674 real* slb_frac = nullptr;
1675 if (nc > 1 && size_string != nullptr)
1677 GMX_LOG(mdlog.info).appendTextFormatted("Using static load balancing for the %s direction", dir);
1680 for (int i = 0; i < nc; i++)
1684 sscanf(size_string, "%20lf%n", &dbl, &n);
1688 "Incorrect or not enough DD cell size entries for direction %s: '%s'",
1697 std::string relativeCellSizes = "Relative cell sizes:";
1698 for (int i = 0; i < nc; i++)
1701 relativeCellSizes += gmx::formatString(" %5.3f", slb_frac[i]);
1703 GMX_LOG(mdlog.info).appendText(relativeCellSizes);
1709 static int multi_body_bondeds_count(const gmx_mtop_t& mtop)
1712 for (const auto ilists : IListRange(mtop))
1714 for (auto& ilist : extractILists(ilists.list(), IF_BOND))
1716 if (NRAL(ilist.functionType) > 2)
1718 n += ilists.nmol() * (ilist.iatoms.size() / ilistStride(ilist));
1726 static int dd_getenv(const gmx::MDLogger& mdlog, const char* env_var, int def)
1729 char* val = getenv(env_var);
1732 if (sscanf(val, "%20d", &nst) <= 0)
1736 GMX_LOG(mdlog.info).appendTextFormatted("Found env.var. %s = %s, using value %d", env_var, val, nst);
1742 static void check_dd_restrictions(const gmx_domdec_t* dd, const t_inputrec& inputrec, const gmx::MDLogger& mdlog)
1744 if (inputrec.pbcType == PbcType::Screw
1745 && (dd->numCells[XX] == 1 || dd->numCells[YY] > 1 || dd->numCells[ZZ] > 1))
1748 "With pbc=%s can only do domain decomposition in the x-direction",
1749 c_pbcTypeNames[inputrec.pbcType].c_str());
1752 if (inputrec.nstlist == 0)
1754 gmx_fatal(FARGS, "Domain decomposition does not work with nstlist=0");
1757 if (inputrec.comm_mode == ComRemovalAlgorithm::Angular && inputrec.pbcType != PbcType::No)
1759 GMX_LOG(mdlog.warning)
1761 "comm-mode angular will give incorrect results when the comm group "
1762 "partially crosses a periodic boundary");
1766 static real average_cellsize_min(const gmx_ddbox_t& ddbox, const ivec numDomains)
1768 real r = ddbox.box_size[XX];
1769 for (int d = 0; d < DIM; d++)
1771 if (numDomains[d] > 1)
1773 /* Check using the initial average cell size */
1774 r = std::min(r, ddbox.box_size[d] * ddbox.skew_fac[d] / numDomains[d]);
1781 /*! \brief Depending on the DLB initial value return the DLB switched off state or issue an error.
1783 static DlbState forceDlbOffOrBail(DlbState cmdlineDlbState,
1784 const std::string& reasonStr,
1785 const gmx::MDLogger& mdlog)
1787 std::string dlbNotSupportedErr = "Dynamic load balancing requested, but ";
1788 std::string dlbDisableNote = "NOTE: disabling dynamic load balancing as ";
1790 if (cmdlineDlbState == DlbState::onUser)
1792 gmx_fatal(FARGS, "%s", (dlbNotSupportedErr + reasonStr).c_str());
1794 else if (cmdlineDlbState == DlbState::offCanTurnOn)
1796 GMX_LOG(mdlog.info).appendText(dlbDisableNote + reasonStr);
1798 return DlbState::offForever;
1801 /*! \brief Return the dynamic load balancer's initial state based on initial conditions and user inputs.
1803 * This function parses the parameters of "-dlb" command line option setting
1804 * corresponding state values. Then it checks the consistency of the determined
1805 * state with other run parameters and settings. As a result, the initial state
1806 * may be altered or an error may be thrown if incompatibility of options is detected.
1808 * \param [in] mdlog Logger.
1809 * \param [in] dlbOption Enum value for the DLB option.
1810 * \param [in] bRecordLoad True if the load balancer is recording load information.
1811 * \param [in] mdrunOptions Options for mdrun.
1812 * \param [in] inputrec Pointer mdrun to input parameters.
1813 * \returns DLB initial/startup state.
1815 static DlbState determineInitialDlbState(const gmx::MDLogger& mdlog,
1816 DlbOption dlbOption,
1817 gmx_bool bRecordLoad,
1818 const gmx::MdrunOptions& mdrunOptions,
1819 const t_inputrec& inputrec)
1821 DlbState dlbState = DlbState::offCanTurnOn;
1825 case DlbOption::turnOnWhenUseful: dlbState = DlbState::offCanTurnOn; break;
1826 case DlbOption::no: dlbState = DlbState::offUser; break;
1827 case DlbOption::yes: dlbState = DlbState::onUser; break;
1828 default: gmx_incons("Invalid dlbOption enum value");
1831 /* Reruns don't support DLB: bail or override auto mode */
1832 if (mdrunOptions.rerun)
1834 std::string reasonStr = "it is not supported in reruns.";
1835 return forceDlbOffOrBail(dlbState, reasonStr, mdlog);
1838 /* Unsupported integrators */
1839 if (!EI_DYNAMICS(inputrec.eI))
1842 gmx::formatString("it is only supported with dynamics, not with integrator '%s'.",
1843 enumValueToString(inputrec.eI));
1844 return forceDlbOffOrBail(dlbState, reasonStr, mdlog);
1847 /* Without cycle counters we can't time work to balance on */
1850 std::string reasonStr =
1851 "cycle counters unsupported or not enabled in the operating system kernel.";
1852 return forceDlbOffOrBail(dlbState, reasonStr, mdlog);
1855 if (mdrunOptions.reproducible)
1857 std::string reasonStr = "you started a reproducible run.";
1860 case DlbState::offUser: break;
1861 case DlbState::offForever:
1862 GMX_RELEASE_ASSERT(false, "DlbState::offForever is not a valid initial state");
1864 case DlbState::offCanTurnOn: return forceDlbOffOrBail(dlbState, reasonStr, mdlog);
1865 case DlbState::onCanTurnOff:
1866 GMX_RELEASE_ASSERT(false, "DlbState::offCanTurnOff is not a valid initial state");
1868 case DlbState::onUser:
1869 return forceDlbOffOrBail(
1872 + " In load balanced runs binary reproducibility cannot be "
1877 "Death horror: undefined case (%d) for load balancing choice",
1878 static_cast<int>(dlbState));
1885 static gmx_domdec_comm_t* init_dd_comm()
1887 gmx_domdec_comm_t* comm = new gmx_domdec_comm_t;
1889 comm->n_load_have = 0;
1890 comm->n_load_collect = 0;
1892 comm->haveTurnedOffDlb = false;
1894 for (int i = 0; i < static_cast<int>(DDAtomRanges::Type::Number); i++)
1896 comm->sum_nat[i] = 0;
1900 comm->load_step = 0;
1903 clear_ivec(comm->load_lim);
1907 /* This should be replaced by a unique pointer */
1908 comm->balanceRegion = ddBalanceRegionAllocate();
1913 static void setupUpdateGroups(const gmx::MDLogger& mdlog,
1914 const gmx_mtop_t& mtop,
1915 ArrayRef<const RangePartitioning> updateGroupingsPerMoleculeType,
1916 const bool useUpdateGroups,
1917 const real maxUpdateGroupRadius,
1918 DDSystemInfo* systemInfo)
1920 systemInfo->updateGroupingsPerMoleculeType = updateGroupingsPerMoleculeType;
1921 systemInfo->useUpdateGroups = useUpdateGroups;
1922 systemInfo->maxUpdateGroupRadius = maxUpdateGroupRadius;
1924 if (systemInfo->useUpdateGroups)
1926 int numUpdateGroups = 0;
1927 for (const auto& molblock : mtop.molblock)
1929 numUpdateGroups += molblock.nmol
1930 * systemInfo->updateGroupingsPerMoleculeType[molblock.type].numBlocks();
1934 .appendTextFormatted(
1935 "Using update groups, nr %d, average size %.1f atoms, max. radius %.3f "
1938 mtop.natoms / static_cast<double>(numUpdateGroups),
1939 systemInfo->maxUpdateGroupRadius);
1943 UnitCellInfo::UnitCellInfo(const t_inputrec& ir) :
1944 npbcdim(numPbcDimensions(ir.pbcType)),
1945 numBoundedDimensions(inputrec2nboundeddim(&ir)),
1946 ddBoxIsDynamic(numBoundedDimensions < DIM || inputrecDynamicBox(&ir)),
1947 haveScrewPBC(ir.pbcType == PbcType::Screw)
1951 /* Returns whether molecules are always whole, i.e. not broken by PBC */
1952 static bool moleculesAreAlwaysWhole(const gmx_mtop_t& mtop,
1953 const bool useUpdateGroups,
1954 gmx::ArrayRef<const gmx::RangePartitioning> updateGroupingsPerMoleculeType)
1956 if (useUpdateGroups)
1958 GMX_RELEASE_ASSERT(updateGroupingsPerMoleculeType.size() == mtop.moltype.size(),
1959 "Need one grouping per moltype");
1960 for (size_t mol = 0; mol < mtop.moltype.size(); mol++)
1962 if (updateGroupingsPerMoleculeType[mol].numBlocks() > 1)
1970 for (const auto& moltype : mtop.moltype)
1972 if (moltype.atoms.nr > 1)
1982 /*! \brief Generate the simulation system information */
1983 static DDSystemInfo getSystemInfo(const gmx::MDLogger& mdlog,
1985 MPI_Comm communicator,
1986 const DomdecOptions& options,
1987 const gmx_mtop_t& mtop,
1988 const t_inputrec& ir,
1990 ArrayRef<const RangePartitioning> updateGroupingPerMoleculeType,
1991 const bool useUpdateGroups,
1992 const real maxUpdateGroupRadius,
1993 gmx::ArrayRef<const gmx::RVec> xGlobal)
1995 const real tenPercentMargin = 1.1;
1997 DDSystemInfo systemInfo;
2000 mdlog, mtop, updateGroupingPerMoleculeType, useUpdateGroups, maxUpdateGroupRadius, &systemInfo);
2002 systemInfo.moleculesAreAlwaysWhole = moleculesAreAlwaysWhole(
2003 mtop, systemInfo.useUpdateGroups, systemInfo.updateGroupingsPerMoleculeType);
2004 systemInfo.haveInterDomainBondeds =
2005 (!systemInfo.moleculesAreAlwaysWhole || mtop.bIntermolecularInteractions);
2006 systemInfo.haveInterDomainMultiBodyBondeds =
2007 (systemInfo.haveInterDomainBondeds && multi_body_bondeds_count(mtop) > 0);
2009 if (systemInfo.useUpdateGroups)
2011 systemInfo.mayHaveSplitConstraints = false;
2012 systemInfo.mayHaveSplitSettles = false;
2016 systemInfo.mayHaveSplitConstraints = (gmx_mtop_ftype_count(mtop, F_CONSTR) > 0
2017 || gmx_mtop_ftype_count(mtop, F_CONSTRNC) > 0);
2018 systemInfo.mayHaveSplitSettles = (gmx_mtop_ftype_count(mtop, F_SETTLE) > 0);
2023 /* Set the cut-off to some very large value,
2024 * so we don't need if statements everywhere in the code.
2025 * We use sqrt, since the cut-off is squared in some places.
2027 systemInfo.cutoff = GMX_CUTOFF_INF;
2031 systemInfo.cutoff = atomToAtomIntoDomainToDomainCutoff(systemInfo, ir.rlist);
2033 systemInfo.minCutoffForMultiBody = 0;
2035 /* Determine the minimum cell size limit, affected by many factors */
2036 systemInfo.cellsizeLimit = 0;
2037 systemInfo.filterBondedCommunication = false;
2039 /* We do not allow home atoms to move beyond the neighboring domain
2040 * between domain decomposition steps, which limits the cell size.
2041 * Get an estimate of cell size limit due to atom displacement.
2042 * In most cases this is a large overestimate, because it assumes
2043 * non-interaction atoms.
2044 * We set the chance to 1 in a trillion steps.
2046 constexpr real c_chanceThatAtomMovesBeyondDomain = 1e-12;
2047 const real limitForAtomDisplacement = minCellSizeForAtomDisplacement(
2048 mtop, ir, systemInfo.updateGroupingsPerMoleculeType, c_chanceThatAtomMovesBeyondDomain);
2049 GMX_LOG(mdlog.info).appendTextFormatted("Minimum cell size due to atom displacement: %.3f nm", limitForAtomDisplacement);
2051 systemInfo.cellsizeLimit = std::max(systemInfo.cellsizeLimit, limitForAtomDisplacement);
2053 /* TODO: PME decomposition currently requires atoms not to be more than
2054 * 2/3 of comm->cutoff, which is >=rlist, outside of their domain.
2055 * In nearly all cases, limitForAtomDisplacement will be smaller
2056 * than 2/3*rlist, so the PME requirement is satisfied.
2057 * But it would be better for both correctness and performance
2058 * to use limitForAtomDisplacement instead of 2/3*comm->cutoff.
2059 * Note that we would need to improve the pairlist buffer case.
2062 if (systemInfo.haveInterDomainBondeds)
2064 if (options.minimumCommunicationRange > 0)
2066 systemInfo.minCutoffForMultiBody =
2067 atomToAtomIntoDomainToDomainCutoff(systemInfo, options.minimumCommunicationRange);
2068 if (options.useBondedCommunication)
2070 systemInfo.filterBondedCommunication =
2071 (systemInfo.minCutoffForMultiBody > systemInfo.cutoff);
2075 systemInfo.cutoff = std::max(systemInfo.cutoff, systemInfo.minCutoffForMultiBody);
2078 else if (ir.bPeriodicMols)
2080 /* Can not easily determine the required cut-off */
2081 GMX_LOG(mdlog.warning)
2083 "NOTE: Periodic molecules are present in this system. Because of this, "
2084 "the domain decomposition algorithm cannot easily determine the "
2085 "minimum cell size that it requires for treating bonded interactions. "
2086 "Instead, domain decomposition will assume that half the non-bonded "
2087 "cut-off will be a suitable lower bound.");
2088 systemInfo.minCutoffForMultiBody = systemInfo.cutoff / 2;
2095 if (ddRole == DDRole::Master)
2097 dd_bonded_cg_distance(mdlog, mtop, ir, xGlobal, box, options.ddBondedChecking, &r_2b, &r_mb);
2099 gmx_bcast(sizeof(r_2b), &r_2b, communicator);
2100 gmx_bcast(sizeof(r_mb), &r_mb, communicator);
2102 /* We use an initial margin of 10% for the minimum cell size,
2103 * except when we are just below the non-bonded cut-off.
2105 if (options.useBondedCommunication)
2107 if (std::max(r_2b, r_mb) > systemInfo.cutoff)
2109 const real r_bonded = std::max(r_2b, r_mb);
2110 systemInfo.minCutoffForMultiBody = tenPercentMargin * r_bonded;
2111 /* This is the (only) place where we turn on the filtering */
2112 systemInfo.filterBondedCommunication = true;
2116 const real r_bonded = r_mb;
2117 systemInfo.minCutoffForMultiBody =
2118 std::min(tenPercentMargin * r_bonded, systemInfo.cutoff);
2120 /* We determine cutoff_mbody later */
2121 systemInfo.increaseMultiBodyCutoff = true;
2125 /* No special bonded communication,
2126 * simply increase the DD cut-off.
2128 systemInfo.minCutoffForMultiBody = tenPercentMargin * std::max(r_2b, r_mb);
2129 systemInfo.cutoff = std::max(systemInfo.cutoff, systemInfo.minCutoffForMultiBody);
2133 .appendTextFormatted("Minimum cell size due to bonded interactions: %.3f nm",
2134 systemInfo.minCutoffForMultiBody);
2136 systemInfo.cellsizeLimit = std::max(systemInfo.cellsizeLimit, systemInfo.minCutoffForMultiBody);
2139 systemInfo.constraintCommunicationRange = 0;
2140 if (systemInfo.mayHaveSplitConstraints && options.constraintCommunicationRange <= 0)
2142 /* There is a cell size limit due to the constraints (P-LINCS) */
2143 systemInfo.constraintCommunicationRange = gmx::constr_r_max(mdlog, &mtop, &ir);
2145 .appendTextFormatted("Estimated maximum distance required for P-LINCS: %.3f nm",
2146 systemInfo.constraintCommunicationRange);
2147 if (systemInfo.constraintCommunicationRange > systemInfo.cellsizeLimit)
2151 "This distance will limit the DD cell size, you can override this with "
2155 else if (options.constraintCommunicationRange > 0)
2157 /* Here we do not check for dd->splitConstraints.
2158 * because one can also set a cell size limit for virtual sites only
2159 * and at this point we don't know yet if there are intercg v-sites.
2162 .appendTextFormatted("User supplied maximum distance required for P-LINCS: %.3f nm",
2163 options.constraintCommunicationRange);
2164 systemInfo.constraintCommunicationRange = options.constraintCommunicationRange;
2166 systemInfo.cellsizeLimit = std::max(systemInfo.cellsizeLimit, systemInfo.constraintCommunicationRange);
2171 /*! \brief Exit with a fatal error if the DDGridSetup cannot be
2173 static void checkDDGridSetup(const DDGridSetup& ddGridSetup,
2175 MPI_Comm communicator,
2177 const DomdecOptions& options,
2178 const DDSettings& ddSettings,
2179 const DDSystemInfo& systemInfo,
2180 const real cellsizeLimit,
2181 const gmx_ddbox_t& ddbox)
2183 if (options.numCells[XX] <= 0 && (ddGridSetup.numDomains[XX] == 0))
2185 const bool bC = (systemInfo.mayHaveSplitConstraints
2186 && systemInfo.constraintCommunicationRange > systemInfo.minCutoffForMultiBody);
2187 std::string message =
2188 gmx::formatString("Change the number of ranks or mdrun option %s%s%s",
2189 !bC ? "-rdd" : "-rcon",
2190 ddSettings.initialDlbState != DlbState::offUser ? " or -dds" : "",
2191 bC ? " or your LINCS settings" : "");
2193 gmx_fatal_collective(FARGS,
2195 ddRole == DDRole::Master,
2196 "There is no domain decomposition for %d ranks that is compatible "
2197 "with the given box and a minimum cell size of %g nm\n"
2199 "Look in the log file for details on the domain decomposition",
2200 numNodes - ddGridSetup.numPmeOnlyRanks,
2205 const real acs = average_cellsize_min(ddbox, ddGridSetup.numDomains);
2206 if (acs < cellsizeLimit)
2208 if (options.numCells[XX] <= 0)
2212 "dd_choose_grid() should return a grid that satisfies the cell size limits");
2216 gmx_fatal_collective(
2219 ddRole == DDRole::Master,
2220 "The initial cell size (%f) is smaller than the cell size limit (%f), change "
2221 "options -dd, -rdd or -rcon, see the log file for details",
2227 const int numPPRanks =
2228 ddGridSetup.numDomains[XX] * ddGridSetup.numDomains[YY] * ddGridSetup.numDomains[ZZ];
2229 if (numNodes - numPPRanks != ddGridSetup.numPmeOnlyRanks)
2231 gmx_fatal_collective(FARGS,
2233 ddRole == DDRole::Master,
2234 "The size of the domain decomposition grid (%d) does not match the "
2235 "number of PP ranks (%d). The total number of ranks is %d",
2237 numNodes - ddGridSetup.numPmeOnlyRanks,
2240 if (ddGridSetup.numPmeOnlyRanks > numPPRanks)
2242 gmx_fatal_collective(FARGS,
2244 ddRole == DDRole::Master,
2245 "The number of separate PME ranks (%d) is larger than the number of "
2246 "PP ranks (%d), this is not supported.",
2247 ddGridSetup.numPmeOnlyRanks,
2252 /*! \brief Set the cell size and interaction limits, as well as the DD grid */
2253 static DDRankSetup getDDRankSetup(const gmx::MDLogger& mdlog,
2255 const DDGridSetup& ddGridSetup,
2256 const t_inputrec& ir)
2259 .appendTextFormatted("Domain decomposition grid %d x %d x %d, separate PME ranks %d",
2260 ddGridSetup.numDomains[XX],
2261 ddGridSetup.numDomains[YY],
2262 ddGridSetup.numDomains[ZZ],
2263 ddGridSetup.numPmeOnlyRanks);
2265 DDRankSetup ddRankSetup;
2267 ddRankSetup.numPPRanks = numNodes - ddGridSetup.numPmeOnlyRanks;
2268 copy_ivec(ddGridSetup.numDomains, ddRankSetup.numPPCells);
2270 ddRankSetup.usePmeOnlyRanks = (ddGridSetup.numPmeOnlyRanks > 0);
2271 if (ddRankSetup.usePmeOnlyRanks)
2273 ddRankSetup.numRanksDoingPme = ddGridSetup.numPmeOnlyRanks;
2277 ddRankSetup.numRanksDoingPme =
2278 ddGridSetup.numDomains[XX] * ddGridSetup.numDomains[YY] * ddGridSetup.numDomains[ZZ];
2281 if (EEL_PME(ir.coulombtype) || EVDW_PME(ir.vdwtype))
2283 /* The following choices should match those
2284 * in comm_cost_est in domdec_setup.c.
2285 * Note that here the checks have to take into account
2286 * that the decomposition might occur in a different order than xyz
2287 * (for instance through the env.var. GMX_DD_ORDER_ZYX),
2288 * in which case they will not match those in comm_cost_est,
2289 * but since that is mainly for testing purposes that's fine.
2291 if (ddGridSetup.numDDDimensions >= 2 && ddGridSetup.ddDimensions[0] == XX
2292 && ddGridSetup.ddDimensions[1] == YY
2293 && ddRankSetup.numRanksDoingPme > ddGridSetup.numDomains[XX]
2294 && ddRankSetup.numRanksDoingPme % ddGridSetup.numDomains[XX] == 0
2295 && getenv("GMX_PMEONEDD") == nullptr)
2297 ddRankSetup.npmedecompdim = 2;
2298 ddRankSetup.npmenodes_x = ddGridSetup.numDomains[XX];
2299 ddRankSetup.npmenodes_y = ddRankSetup.numRanksDoingPme / ddRankSetup.npmenodes_x;
2303 /* In case nc is 1 in both x and y we could still choose to
2304 * decompose pme in y instead of x, but we use x for simplicity.
2306 ddRankSetup.npmedecompdim = 1;
2307 if (ddGridSetup.ddDimensions[0] == YY)
2309 ddRankSetup.npmenodes_x = 1;
2310 ddRankSetup.npmenodes_y = ddRankSetup.numRanksDoingPme;
2314 ddRankSetup.npmenodes_x = ddRankSetup.numRanksDoingPme;
2315 ddRankSetup.npmenodes_y = 1;
2319 .appendTextFormatted("PME domain decomposition: %d x %d x %d",
2320 ddRankSetup.npmenodes_x,
2321 ddRankSetup.npmenodes_y,
2326 ddRankSetup.npmedecompdim = 0;
2327 ddRankSetup.npmenodes_x = 0;
2328 ddRankSetup.npmenodes_y = 0;
2334 /*! \brief Set the cell size and interaction limits */
2335 static void set_dd_limits(const gmx::MDLogger& mdlog,
2338 const DomdecOptions& options,
2339 const DDSettings& ddSettings,
2340 const DDSystemInfo& systemInfo,
2341 const DDGridSetup& ddGridSetup,
2342 const int numPPRanks,
2343 const gmx_mtop_t& mtop,
2344 const t_inputrec& ir,
2345 const gmx_ddbox_t& ddbox)
2347 gmx_domdec_comm_t* comm = dd->comm;
2348 comm->ddSettings = ddSettings;
2350 /* Initialize to GPU share count to 0, might change later */
2351 comm->nrank_gpu_shared = 0;
2353 comm->dlbState = comm->ddSettings.initialDlbState;
2354 dd_dlb_set_should_check_whether_to_turn_dlb_on(dd, TRUE);
2355 /* To consider turning DLB on after 2*nstlist steps we need to check
2356 * at partitioning count 3. Thus we need to increase the first count by 2.
2358 comm->ddPartioningCountFirstDlbOff += 2;
2360 comm->bPMELoadBalDLBLimits = FALSE;
2362 /* Allocate the charge group/atom sorting struct */
2363 comm->sort = std::make_unique<gmx_domdec_sort_t>();
2365 comm->systemInfo = systemInfo;
2367 if (systemInfo.useUpdateGroups)
2369 /* Note: We would like to use dd->nnodes for the atom count estimate,
2370 * but that is not yet available here. But this anyhow only
2371 * affect performance up to the second dd_partition_system call.
2373 const int homeAtomCountEstimate = mtop.natoms / numPPRanks;
2374 comm->updateGroupsCog = std::make_unique<gmx::UpdateGroupsCog>(
2375 mtop, systemInfo.updateGroupingsPerMoleculeType, maxReferenceTemperature(ir), homeAtomCountEstimate);
2378 /* Set the DD setup given by ddGridSetup */
2379 copy_ivec(ddGridSetup.numDomains, dd->numCells);
2380 dd->ndim = ddGridSetup.numDDDimensions;
2381 copy_ivec(ddGridSetup.ddDimensions, dd->dim);
2383 dd->nnodes = dd->numCells[XX] * dd->numCells[YY] * dd->numCells[ZZ];
2385 snew(comm->slb_frac, DIM);
2386 if (isDlbDisabled(comm))
2388 comm->slb_frac[XX] = get_slb_frac(mdlog, "x", dd->numCells[XX], options.cellSizeX);
2389 comm->slb_frac[YY] = get_slb_frac(mdlog, "y", dd->numCells[YY], options.cellSizeY);
2390 comm->slb_frac[ZZ] = get_slb_frac(mdlog, "z", dd->numCells[ZZ], options.cellSizeZ);
2393 /* Set the multi-body cut-off and cellsize limit for DLB */
2394 comm->cutoff_mbody = systemInfo.minCutoffForMultiBody;
2395 comm->cellsize_limit = systemInfo.cellsizeLimit;
2396 if (systemInfo.haveInterDomainBondeds && systemInfo.increaseMultiBodyCutoff)
2398 if (systemInfo.filterBondedCommunication || !isDlbDisabled(comm))
2400 /* Set the bonded communication distance to halfway
2401 * the minimum and the maximum,
2402 * since the extra communication cost is nearly zero.
2404 real acs = average_cellsize_min(ddbox, dd->numCells);
2405 comm->cutoff_mbody = 0.5 * (systemInfo.minCutoffForMultiBody + acs);
2406 if (!isDlbDisabled(comm))
2408 /* Check if this does not limit the scaling */
2409 comm->cutoff_mbody = std::min(comm->cutoff_mbody, options.dlbScaling * acs);
2411 if (!systemInfo.filterBondedCommunication)
2413 /* Without bBondComm do not go beyond the n.b. cut-off */
2414 comm->cutoff_mbody = std::min(comm->cutoff_mbody, systemInfo.cutoff);
2415 if (comm->cellsize_limit >= systemInfo.cutoff)
2417 /* We don't loose a lot of efficieny
2418 * when increasing it to the n.b. cut-off.
2419 * It can even be slightly faster, because we need
2420 * less checks for the communication setup.
2422 comm->cutoff_mbody = systemInfo.cutoff;
2425 /* Check if we did not end up below our original limit */
2426 comm->cutoff_mbody = std::max(comm->cutoff_mbody, systemInfo.minCutoffForMultiBody);
2428 if (comm->cutoff_mbody > comm->cellsize_limit)
2430 comm->cellsize_limit = comm->cutoff_mbody;
2433 /* Without DLB and cutoff_mbody<cutoff, cutoff_mbody is dynamic */
2439 "Bonded atom communication beyond the cut-off: %s\n"
2440 "cellsize limit %f\n",
2441 gmx::boolToString(systemInfo.filterBondedCommunication),
2442 comm->cellsize_limit);
2445 if (ddRole == DDRole::Master)
2447 check_dd_restrictions(dd, ir, mdlog);
2451 static void writeSettings(gmx::TextWriter* log,
2453 const gmx_mtop_t& mtop,
2454 const t_inputrec& ir,
2455 gmx_bool bDynLoadBal,
2457 const gmx_ddbox_t* ddbox)
2459 gmx_domdec_comm_t* comm = dd->comm;
2463 log->writeString("The maximum number of communication pulses is:");
2464 for (int d = 0; d < dd->ndim; d++)
2466 log->writeStringFormatted(" %c %d", dim2char(dd->dim[d]), comm->cd[d].np_dlb);
2468 log->ensureLineBreak();
2469 log->writeLineFormatted("The minimum size for domain decomposition cells is %.3f nm",
2470 comm->cellsize_limit);
2471 log->writeLineFormatted("The requested allowed shrink of DD cells (option -dds) is: %.2f", dlb_scale);
2472 log->writeString("The allowed shrink of domain decomposition cells is:");
2473 for (int d = 0; d < DIM; d++)
2475 if (dd->numCells[d] > 1)
2478 (d >= ddbox->npbcdim && dd->numCells[d] == 2)
2480 : comm->cellsize_min_dlb[d]
2481 / (ddbox->box_size[d] * ddbox->skew_fac[d] / dd->numCells[d]);
2482 log->writeStringFormatted(" %c %.2f", dim2char(d), shrink);
2485 log->ensureLineBreak();
2490 set_dd_cell_sizes_slb(dd, ddbox, setcellsizeslbPULSE_ONLY, np);
2491 log->writeString("The initial number of communication pulses is:");
2492 for (int d = 0; d < dd->ndim; d++)
2494 log->writeStringFormatted(" %c %d", dim2char(dd->dim[d]), np[dd->dim[d]]);
2496 log->ensureLineBreak();
2497 log->writeString("The initial domain decomposition cell size is:");
2498 for (int d = 0; d < DIM; d++)
2500 if (dd->numCells[d] > 1)
2502 log->writeStringFormatted(" %c %.2f nm", dim2char(d), dd->comm->cellsize_min[d]);
2505 log->ensureLineBreak();
2509 const bool haveInterDomainVsites =
2510 (countInterUpdategroupVsites(mtop, comm->systemInfo.updateGroupingsPerMoleculeType) != 0);
2512 if (comm->systemInfo.haveInterDomainBondeds || haveInterDomainVsites
2513 || comm->systemInfo.mayHaveSplitConstraints || comm->systemInfo.mayHaveSplitSettles)
2515 std::string decompUnits;
2516 if (comm->systemInfo.useUpdateGroups)
2518 decompUnits = "atom groups";
2522 decompUnits = "atoms";
2525 log->writeLineFormatted("The maximum allowed distance for %s involved in interactions is:",
2526 decompUnits.c_str());
2527 log->writeLineFormatted(
2528 "%40s %-7s %6.3f nm", "non-bonded interactions", "", comm->systemInfo.cutoff);
2533 limit = dd->comm->cellsize_limit;
2537 if (dd->unitCellInfo.ddBoxIsDynamic)
2540 "(the following are initial values, they could change due to box "
2543 limit = dd->comm->cellsize_min[XX];
2544 for (int d = 1; d < DIM; d++)
2546 limit = std::min(limit, dd->comm->cellsize_min[d]);
2550 if (comm->systemInfo.haveInterDomainBondeds)
2552 log->writeLineFormatted("%40s %-7s %6.3f nm",
2553 "two-body bonded interactions",
2555 std::max(comm->systemInfo.cutoff, comm->cutoff_mbody));
2556 log->writeLineFormatted("%40s %-7s %6.3f nm",
2557 "multi-body bonded interactions",
2559 (comm->systemInfo.filterBondedCommunication || isDlbOn(dd->comm))
2560 ? comm->cutoff_mbody
2561 : std::min(comm->systemInfo.cutoff, limit));
2563 if (haveInterDomainVsites)
2565 log->writeLineFormatted("%40s %-7s %6.3f nm", "virtual site constructions", "(-rcon)", limit);
2567 if (comm->systemInfo.mayHaveSplitConstraints || comm->systemInfo.mayHaveSplitSettles)
2569 std::string separation =
2570 gmx::formatString("atoms separated by up to %d constraints", 1 + ir.nProjOrder);
2571 log->writeLineFormatted("%40s %-7s %6.3f nm\n", separation.c_str(), "(-rcon)", limit);
2573 log->ensureLineBreak();
2577 static void logSettings(const gmx::MDLogger& mdlog,
2579 const gmx_mtop_t& mtop,
2580 const t_inputrec& ir,
2582 const gmx_ddbox_t* ddbox)
2584 gmx::StringOutputStream stream;
2585 gmx::TextWriter log(&stream);
2586 writeSettings(&log, dd, mtop, ir, isDlbOn(dd->comm), dlb_scale, ddbox);
2587 if (dd->comm->dlbState == DlbState::offCanTurnOn)
2590 log.ensureEmptyLine();
2592 "When dynamic load balancing gets turned on, these settings will change to:");
2594 writeSettings(&log, dd, mtop, ir, true, dlb_scale, ddbox);
2596 GMX_LOG(mdlog.info).asParagraph().appendText(stream.toString());
2599 static void set_cell_limits_dlb(const gmx::MDLogger& mdlog,
2602 const t_inputrec& inputrec,
2603 const gmx_ddbox_t* ddbox)
2606 int npulse_d_max = 0;
2609 gmx_domdec_comm_t* comm = dd->comm;
2611 bool bNoCutOff = (inputrec.rvdw == 0 || inputrec.rcoulomb == 0);
2613 /* Determine the maximum number of comm. pulses in one dimension */
2615 comm->cellsize_limit = std::max(comm->cellsize_limit, comm->cutoff_mbody);
2617 /* Determine the maximum required number of grid pulses */
2618 if (comm->cellsize_limit >= comm->systemInfo.cutoff)
2620 /* Only a single pulse is required */
2623 else if (!bNoCutOff && comm->cellsize_limit > 0)
2625 /* We round down slightly here to avoid overhead due to the latency
2626 * of extra communication calls when the cut-off
2627 * would be only slightly longer than the cell size.
2628 * Later cellsize_limit is redetermined,
2629 * so we can not miss interactions due to this rounding.
2631 npulse = static_cast<int>(0.96 + comm->systemInfo.cutoff / comm->cellsize_limit);
2635 /* There is no cell size limit */
2636 npulse = std::max(dd->numCells[XX] - 1, std::max(dd->numCells[YY] - 1, dd->numCells[ZZ] - 1));
2639 if (!bNoCutOff && npulse > 1)
2641 /* See if we can do with less pulses, based on dlb_scale */
2643 for (int d = 0; d < dd->ndim; d++)
2645 int dim = dd->dim[d];
2646 npulse_d = static_cast<int>(
2648 + dd->numCells[dim] * comm->systemInfo.cutoff
2649 / (ddbox->box_size[dim] * ddbox->skew_fac[dim] * dlb_scale));
2650 npulse_d_max = std::max(npulse_d_max, npulse_d);
2652 npulse = std::min(npulse, npulse_d_max);
2655 /* This env var can override npulse */
2656 const int ddPulseEnv = dd_getenv(mdlog, "GMX_DD_NPULSE", 0);
2659 npulse = ddPulseEnv;
2663 comm->bVacDLBNoLimit = (inputrec.pbcType == PbcType::No);
2664 for (int d = 0; d < dd->ndim; d++)
2666 comm->cd[d].np_dlb = std::min(npulse, dd->numCells[dd->dim[d]] - 1);
2667 comm->maxpulse = std::max(comm->maxpulse, comm->cd[d].np_dlb);
2668 if (comm->cd[d].np_dlb < dd->numCells[dd->dim[d]] - 1)
2670 comm->bVacDLBNoLimit = FALSE;
2674 /* cellsize_limit is set for LINCS in init_domain_decomposition */
2675 if (!comm->bVacDLBNoLimit)
2677 comm->cellsize_limit = std::max(comm->cellsize_limit, comm->systemInfo.cutoff / comm->maxpulse);
2679 comm->cellsize_limit = std::max(comm->cellsize_limit, comm->cutoff_mbody);
2680 /* Set the minimum cell size for each DD dimension */
2681 for (int d = 0; d < dd->ndim; d++)
2683 if (comm->bVacDLBNoLimit || comm->cd[d].np_dlb * comm->cellsize_limit >= comm->systemInfo.cutoff)
2685 comm->cellsize_min_dlb[dd->dim[d]] = comm->cellsize_limit;
2689 comm->cellsize_min_dlb[dd->dim[d]] = comm->systemInfo.cutoff / comm->cd[d].np_dlb;
2692 if (comm->cutoff_mbody <= 0)
2694 comm->cutoff_mbody = std::min(comm->systemInfo.cutoff, comm->cellsize_limit);
2702 bool dd_moleculesAreAlwaysWhole(const gmx_domdec_t& dd)
2704 return dd.comm->systemInfo.moleculesAreAlwaysWhole;
2707 bool dd_bonded_molpbc(const gmx_domdec_t& dd, PbcType pbcType)
2709 /* If each molecule is a single charge group
2710 * or we use domain decomposition for each periodic dimension,
2711 * we do not need to take pbc into account for the bonded interactions.
2713 return (pbcType != PbcType::No && dd.comm->systemInfo.haveInterDomainBondeds
2714 && !(dd.numCells[XX] > 1 && dd.numCells[YY] > 1
2715 && (dd.numCells[ZZ] > 1 || pbcType == PbcType::XY)));
2718 /*! \brief Sets grid size limits and PP-PME setup, prints settings to log */
2719 static void set_ddgrid_parameters(const gmx::MDLogger& mdlog,
2722 const gmx_mtop_t& mtop,
2723 const t_inputrec& inputrec,
2724 const gmx_ddbox_t* ddbox)
2726 gmx_domdec_comm_t* comm = dd->comm;
2727 DDRankSetup& ddRankSetup = comm->ddRankSetup;
2729 if (EEL_PME(inputrec.coulombtype) || EVDW_PME(inputrec.vdwtype))
2731 init_ddpme(dd, &ddRankSetup.ddpme[0], 0);
2732 if (ddRankSetup.npmedecompdim >= 2)
2734 init_ddpme(dd, &ddRankSetup.ddpme[1], 1);
2739 ddRankSetup.numRanksDoingPme = 0;
2740 if (dd->pme_nodeid >= 0)
2742 gmx_fatal_collective(FARGS,
2745 "Can not have separate PME ranks without PME electrostatics");
2751 fprintf(debug, "The DD cut-off is %f\n", comm->systemInfo.cutoff);
2753 if (!isDlbDisabled(comm))
2755 set_cell_limits_dlb(mdlog, dd, dlb_scale, inputrec, ddbox);
2758 logSettings(mdlog, dd, mtop, inputrec, dlb_scale, ddbox);
2760 const real vol_frac = (inputrec.pbcType == PbcType::No)
2761 ? (1 - 1 / static_cast<double>(dd->nnodes))
2762 : ((1 + comm_box_frac(dd->numCells, comm->systemInfo.cutoff, *ddbox))
2763 / static_cast<double>(dd->nnodes));
2766 fprintf(debug, "Volume fraction for all DD zones: %f\n", vol_frac);
2768 int natoms_tot = mtop.natoms;
2770 dd->ga2la = new gmx_ga2la_t(natoms_tot, static_cast<int>(vol_frac * natoms_tot));
2773 /*! \brief Get some important DD parameters which can be modified by env.vars */
2774 static DDSettings getDDSettings(const gmx::MDLogger& mdlog,
2775 const DomdecOptions& options,
2776 const gmx::MdrunOptions& mdrunOptions,
2777 const t_inputrec& ir)
2779 DDSettings ddSettings;
2781 ddSettings.useSendRecv2 = (dd_getenv(mdlog, "GMX_DD_USE_SENDRECV2", 0) != 0);
2782 ddSettings.dlb_scale_lim = dd_getenv(mdlog, "GMX_DLB_MAX_BOX_SCALING", 10);
2783 ddSettings.useDDOrderZYX = bool(dd_getenv(mdlog, "GMX_DD_ORDER_ZYX", 0));
2784 ddSettings.useCartesianReorder = bool(dd_getenv(mdlog, "GMX_NO_CART_REORDER", 1));
2785 ddSettings.eFlop = dd_getenv(mdlog, "GMX_DLB_BASED_ON_FLOPS", 0);
2786 const int recload = dd_getenv(mdlog, "GMX_DD_RECORD_LOAD", 1);
2787 ddSettings.nstDDDump = dd_getenv(mdlog, "GMX_DD_NST_DUMP", 0);
2788 ddSettings.nstDDDumpGrid = dd_getenv(mdlog, "GMX_DD_NST_DUMP_GRID", 0);
2789 ddSettings.DD_debug = dd_getenv(mdlog, "GMX_DD_DEBUG", 0);
2791 if (ddSettings.useSendRecv2)
2795 "Will use two sequential MPI_Sendrecv calls instead of two simultaneous "
2796 "non-blocking MPI_Irecv and MPI_Isend pairs for constraint and vsite "
2800 if (ddSettings.eFlop)
2802 GMX_LOG(mdlog.info).appendText("Will load balance based on FLOP count");
2803 ddSettings.recordLoad = true;
2807 ddSettings.recordLoad = (wallcycle_have_counter() && recload > 0);
2810 ddSettings.initialDlbState =
2811 determineInitialDlbState(mdlog, options.dlbOption, ddSettings.recordLoad, mdrunOptions, ir);
2813 .appendTextFormatted("Dynamic load balancing: %s",
2814 enumValueToString(ddSettings.initialDlbState));
2819 gmx_domdec_t::gmx_domdec_t(const t_inputrec& ir) : unitCellInfo(ir) {}
2821 gmx_domdec_t::~gmx_domdec_t() = default;
2826 // TODO once the functionality stablizes, move this class and
2827 // supporting functionality into builder.cpp
2828 /*! \brief Impl class for DD builder */
2829 class DomainDecompositionBuilder::Impl
2833 Impl(const MDLogger& mdlog,
2835 const DomdecOptions& options,
2836 const MdrunOptions& mdrunOptions,
2837 const gmx_mtop_t& mtop,
2838 const t_inputrec& ir,
2840 ArrayRef<const RangePartitioning> updateGroupingPerMoleculeType,
2841 bool useUpdateGroups,
2842 real maxUpdateGroupRadius,
2843 ArrayRef<const RVec> xGlobal);
2845 //! Build the resulting DD manager
2846 gmx_domdec_t* build(LocalAtomSetManager* atomSets);
2848 //! Objects used in constructing and configuring DD
2851 const MDLogger& mdlog_;
2852 //! Communication object
2854 //! User-supplied options configuring DD behavior
2855 const DomdecOptions options_;
2856 //! Global system topology
2857 const gmx_mtop_t& mtop_;
2858 //! User input values from the tpr file
2859 const t_inputrec& ir_;
2862 //! Internal objects used in constructing DD
2864 //! Settings combined from the user input
2865 DDSettings ddSettings_;
2866 //! Information derived from the simulation system
2867 DDSystemInfo systemInfo_;
2869 gmx_ddbox_t ddbox_ = { 0 };
2870 //! Organization of the DD grids
2871 DDGridSetup ddGridSetup_;
2872 //! Organzation of the DD ranks
2873 DDRankSetup ddRankSetup_;
2874 //! Number of DD cells in each dimension
2875 ivec ddCellIndex_ = { 0, 0, 0 };
2876 //! IDs of PME-only ranks
2877 std::vector<int> pmeRanks_;
2878 //! Contains a valid Cartesian-communicator-based setup, or defaults.
2879 CartesianRankSetup cartSetup_;
2883 DomainDecompositionBuilder::Impl::Impl(const MDLogger& mdlog,
2885 const DomdecOptions& options,
2886 const MdrunOptions& mdrunOptions,
2887 const gmx_mtop_t& mtop,
2888 const t_inputrec& ir,
2890 ArrayRef<const RangePartitioning> updateGroupingPerMoleculeType,
2891 const bool useUpdateGroups,
2892 const real maxUpdateGroupRadius,
2893 ArrayRef<const RVec> xGlobal) :
2894 mdlog_(mdlog), cr_(cr), options_(options), mtop_(mtop), ir_(ir)
2896 GMX_LOG(mdlog_.info).appendTextFormatted("\nInitializing Domain Decomposition on %d ranks", cr_->sizeOfDefaultCommunicator);
2898 ddSettings_ = getDDSettings(mdlog_, options_, mdrunOptions, ir_);
2900 if (ddSettings_.eFlop > 1)
2902 /* Ensure that we have different random flop counts on different ranks */
2903 srand(1 + cr_->rankInDefaultCommunicator);
2906 systemInfo_ = getSystemInfo(mdlog_,
2907 MASTER(cr_) ? DDRole::Master : DDRole::Agent,
2908 cr->mpiDefaultCommunicator,
2913 updateGroupingPerMoleculeType,
2915 maxUpdateGroupRadius,
2918 const int numRanksRequested = cr_->sizeOfDefaultCommunicator;
2919 const bool checkForLargePrimeFactors = (options_.numCells[0] <= 0);
2920 checkForValidRankCountRequests(
2921 numRanksRequested, EEL_PME(ir_.coulombtype), options_.numPmeRanks, checkForLargePrimeFactors);
2923 // DD grid setup uses a more different cell size limit for
2924 // automated setup than the one in systemInfo_. The latter is used
2925 // in set_dd_limits() to configure DLB, for example.
2926 const real gridSetupCellsizeLimit =
2927 getDDGridSetupCellSizeLimit(mdlog_,
2928 !isDlbDisabled(ddSettings_.initialDlbState),
2929 options_.dlbScaling,
2931 systemInfo_.cellsizeLimit);
2932 ddGridSetup_ = getDDGridSetup(mdlog_,
2933 MASTER(cr_) ? DDRole::Master : DDRole::Agent,
2934 cr->mpiDefaultCommunicator,
2939 gridSetupCellsizeLimit,
2945 checkDDGridSetup(ddGridSetup_,
2946 MASTER(cr_) ? DDRole::Master : DDRole::Agent,
2947 cr->mpiDefaultCommunicator,
2948 cr->sizeOfDefaultCommunicator,
2952 gridSetupCellsizeLimit,
2955 cr_->npmenodes = ddGridSetup_.numPmeOnlyRanks;
2957 ddRankSetup_ = getDDRankSetup(mdlog_, cr_->sizeOfDefaultCommunicator, ddGridSetup_, ir_);
2959 /* Generate the group communicator, also decides the duty of each rank */
2960 cartSetup_ = makeGroupCommunicators(
2961 mdlog_, ddSettings_, options_.rankOrder, ddRankSetup_, cr_, ddCellIndex_, &pmeRanks_);
2964 gmx_domdec_t* DomainDecompositionBuilder::Impl::build(LocalAtomSetManager* atomSets)
2966 gmx_domdec_t* dd = new gmx_domdec_t(ir_);
2968 copy_ivec(ddCellIndex_, dd->ci);
2970 dd->comm = init_dd_comm();
2972 dd->comm->ddRankSetup = ddRankSetup_;
2973 dd->comm->cartesianRankSetup = cartSetup_;
2975 set_dd_limits(mdlog_,
2976 MASTER(cr_) ? DDRole::Master : DDRole::Agent,
2982 ddRankSetup_.numPPRanks,
2987 setupGroupCommunication(mdlog_, ddSettings_, pmeRanks_, cr_, mtop_.natoms, dd);
2989 if (thisRankHasDuty(cr_, DUTY_PP))
2991 set_ddgrid_parameters(mdlog_, dd, options_.dlbScaling, mtop_, ir_, &ddbox_);
2993 setup_neighbor_relations(dd);
2996 /* Set overallocation to avoid frequent reallocation of arrays */
2997 set_over_alloc_dd(true);
2999 dd->atomSets = atomSets;
3001 dd->localTopologyChecker = std::make_unique<LocalTopologyChecker>();
3006 DomainDecompositionBuilder::DomainDecompositionBuilder(const MDLogger& mdlog,
3008 const DomdecOptions& options,
3009 const MdrunOptions& mdrunOptions,
3010 const gmx_mtop_t& mtop,
3011 const t_inputrec& ir,
3013 ArrayRef<const RangePartitioning> updateGroupingPerMoleculeType,
3014 const bool useUpdateGroups,
3015 const real maxUpdateGroupRadius,
3016 ArrayRef<const RVec> xGlobal) :
3017 impl_(new Impl(mdlog, cr, options, mdrunOptions, mtop, ir, box, updateGroupingPerMoleculeType, useUpdateGroups, maxUpdateGroupRadius, xGlobal))
3021 gmx_domdec_t* DomainDecompositionBuilder::build(LocalAtomSetManager* atomSets)
3023 return impl_->build(atomSets);
3026 DomainDecompositionBuilder::~DomainDecompositionBuilder() = default;
3030 static gmx_bool test_dd_cutoff(const t_commrec* cr, const matrix box, gmx::ArrayRef<const gmx::RVec> x, real cutoffRequested)
3033 int LocallyLimited = 0;
3035 const auto* dd = cr->dd;
3037 set_ddbox(*dd, false, box, true, x, &ddbox);
3041 for (int d = 0; d < dd->ndim; d++)
3043 const int dim = dd->dim[d];
3045 real inv_cell_size = DD_CELL_MARGIN * dd->numCells[dim] / ddbox.box_size[dim];
3046 if (dd->unitCellInfo.ddBoxIsDynamic)
3048 inv_cell_size *= DD_PRES_SCALE_MARGIN;
3051 const int np = 1 + static_cast<int>(cutoffRequested * inv_cell_size * ddbox.skew_fac[dim]);
3053 if (!isDlbDisabled(dd->comm) && (dim < ddbox.npbcdim) && (dd->comm->cd[d].np_dlb > 0))
3055 if (np > dd->comm->cd[d].np_dlb)
3060 /* If a current local cell size is smaller than the requested
3061 * cut-off, we could still fix it, but this gets very complicated.
3062 * Without fixing here, we might actually need more checks.
3064 real cellSizeAlongDim =
3065 (dd->comm->cell_x1[dim] - dd->comm->cell_x0[dim]) * ddbox.skew_fac[dim];
3066 if (cellSizeAlongDim * dd->comm->cd[d].np_dlb < cutoffRequested)
3073 if (!isDlbDisabled(dd->comm))
3075 /* If DLB is not active yet, we don't need to check the grid jumps.
3076 * Actually we shouldn't, because then the grid jump data is not set.
3078 if (isDlbOn(dd->comm) && gmx::check_grid_jump(0, dd, cutoffRequested, &ddbox, FALSE))
3083 gmx_sumi(1, &LocallyLimited, cr);
3085 if (LocallyLimited > 0)
3094 bool change_dd_cutoff(t_commrec* cr, const matrix box, gmx::ArrayRef<const gmx::RVec> x, real cutoffRequested)
3096 bool bCutoffAllowed = test_dd_cutoff(cr, box, x, cutoffRequested);
3100 cr->dd->comm->systemInfo.cutoff = cutoffRequested;
3103 return bCutoffAllowed;
3106 void constructGpuHaloExchange(const gmx::MDLogger& mdlog,
3107 const t_commrec& cr,
3108 const gmx::DeviceStreamManager& deviceStreamManager,
3109 gmx_wallcycle* wcycle)
3111 GMX_RELEASE_ASSERT(deviceStreamManager.streamIsValid(gmx::DeviceStreamType::NonBondedLocal),
3112 "Local non-bonded stream should be valid when using"
3113 "GPU halo exchange.");
3114 GMX_RELEASE_ASSERT(deviceStreamManager.streamIsValid(gmx::DeviceStreamType::NonBondedNonLocal),
3115 "Non-local non-bonded stream should be valid when using "
3116 "GPU halo exchange.");
3118 if (cr.dd->gpuHaloExchange[0].empty())
3120 GMX_LOG(mdlog.warning)
3122 .appendTextFormatted(
3123 "NOTE: Activating the 'GPU halo exchange' feature, enabled "
3125 "GMX_GPU_DD_COMMS environment variable.");
3128 for (int d = 0; d < cr.dd->ndim; d++)
3130 for (int pulse = cr.dd->gpuHaloExchange[d].size(); pulse < cr.dd->comm->cd[d].numPulses(); pulse++)
3132 cr.dd->gpuHaloExchange[d].push_back(std::make_unique<gmx::GpuHaloExchange>(
3135 cr.mpi_comm_mygroup,
3136 deviceStreamManager.context(),
3137 deviceStreamManager.stream(gmx::DeviceStreamType::NonBondedLocal),
3138 deviceStreamManager.stream(gmx::DeviceStreamType::NonBondedNonLocal),
3145 void reinitGpuHaloExchange(const t_commrec& cr,
3146 const DeviceBuffer<gmx::RVec> d_coordinatesBuffer,
3147 const DeviceBuffer<gmx::RVec> d_forcesBuffer)
3149 for (int d = 0; d < cr.dd->ndim; d++)
3151 for (int pulse = 0; pulse < cr.dd->comm->cd[d].numPulses(); pulse++)
3153 cr.dd->gpuHaloExchange[d][pulse]->reinitHalo(d_coordinatesBuffer, d_forcesBuffer);
3158 void communicateGpuHaloCoordinates(const t_commrec& cr,
3160 GpuEventSynchronizer* coordinatesReadyOnDeviceEvent)
3162 for (int d = 0; d < cr.dd->ndim; d++)
3164 for (int pulse = 0; pulse < cr.dd->comm->cd[d].numPulses(); pulse++)
3166 cr.dd->gpuHaloExchange[d][pulse]->communicateHaloCoordinates(box, coordinatesReadyOnDeviceEvent);
3171 void communicateGpuHaloForces(const t_commrec& cr, bool accumulateForces)
3173 for (int d = cr.dd->ndim - 1; d >= 0; d--)
3175 for (int pulse = cr.dd->comm->cd[d].numPulses() - 1; pulse >= 0; pulse--)
3177 cr.dd->gpuHaloExchange[d][pulse]->communicateHaloForces(accumulateForces);