6608dc7d4e678cd36e0fd93192fa81efc45b90c9
[alexxy/gromacs.git] / src / gromacs / domdec / domdec.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2005 - 2014, The GROMACS development team.
5  * Copyright (c) 2015,2016,2017,2018,2019 by the GROMACS development team.
6  * Copyright (c) 2020, 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.
10  *
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.
15  *
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.
20  *
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.
25  *
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.
33  *
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.
36  */
37 /*! \defgroup module_domdec Spatial domain decomposition (for parallelization over MPI)
38  * \ingroup group_mdrun
39  *
40  * \brief Manages the decomposition of the simulation volume over MPI
41  * ranks to try to distribute work evenly with minimal communication
42  * overheads.
43  *
44  * \todo Get domdec stuff out of mdtypes/commrec.h
45  *
46  * \author Berk Hess <hess@kth.se>
47  *
48  */
49
50 /*! \libinternal \file
51  *
52  * \brief This file declares functions for mdrun to call to manage the
53  * details of its domain decomposition.
54  *
55  * \author Berk Hess <hess@kth.se>
56  * \inlibraryapi
57  * \ingroup module_domdec
58  */
59
60 #ifndef GMX_DOMDEC_DOMDEC_H
61 #define GMX_DOMDEC_DOMDEC_H
62
63 #include <vector>
64
65 #include "gromacs/math/vectypes.h"
66 #include "gromacs/utility/arrayref.h"
67 #include "gromacs/utility/basedefinitions.h"
68 #include "gromacs/utility/real.h"
69
70 struct cginfo_mb_t;
71 struct gmx_domdec_t;
72 struct gmx_ddbox_t;
73 struct gmx_domdec_zones_t;
74 struct gmx_localtop_t;
75 struct gmx_mtop_t;
76 struct gmx_vsite_t;
77 struct t_block;
78 struct t_blocka;
79 struct t_commrec;
80 struct t_forcerec;
81 struct t_inputrec;
82 struct t_mdatoms;
83 struct t_nrnb;
84 struct gmx_wallcycle;
85 class t_state;
86
87 namespace gmx
88 {
89 class ForceWithShiftForces;
90 class MDLogger;
91 class RangePartitioning;
92 } // namespace gmx
93
94 /*! \brief Returns the global topology atom number belonging to local atom index i.
95  *
96  * This function is intended for writing ASCII output
97  * and returns atom numbers starting at 1.
98  * When dd=NULL returns i+1.
99  */
100 int ddglatnr(const gmx_domdec_t* dd, int i);
101
102 /*! \brief Returns a list of update group partitioning for each molecule type or empty when update groups are not used */
103 gmx::ArrayRef<const gmx::RangePartitioning> getUpdateGroupingPerMoleculetype(const gmx_domdec_t& dd);
104
105 /*! \brief Store the global cg indices of the home cgs in state,
106  *
107  * This means it can be reset, even after a new DD partitioning.
108  */
109 void dd_store_state(struct gmx_domdec_t* dd, t_state* state);
110
111 /*! \brief Returns a pointer to the gmx_domdec_zones_t struct */
112 struct gmx_domdec_zones_t* domdec_zones(struct gmx_domdec_t* dd);
113
114 /*! \brief Returns the range for atoms in zones*/
115 int dd_numAtomsZones(const gmx_domdec_t& dd);
116
117 /*! \brief Returns the number of home atoms */
118 int dd_numHomeAtoms(const gmx_domdec_t& dd);
119
120 /*! \brief Returns the atom range in the local state for atoms that need to be present in mdatoms */
121 int dd_natoms_mdatoms(const gmx_domdec_t* dd);
122
123 /*! \brief Returns the atom range in the local state for atoms involved in virtual sites */
124 int dd_natoms_vsite(const gmx_domdec_t* dd);
125
126 /*! \brief Sets the atom range for atom in the local state for atoms received in constraints communication */
127 void dd_get_constraint_range(const gmx_domdec_t* dd, int* at_start, int* at_end);
128
129 /*! \libinternal \brief Struct for passing around the number of PME domains */
130 struct NumPmeDomains
131 {
132     int x; //!< The number of PME domains along dimension x
133     int y; //!< The number of PME domains along dimension y
134 };
135
136 /*! \brief Returns the number of PME domains, can be called with dd=NULL */
137 NumPmeDomains getNumPmeDomains(const gmx_domdec_t* dd);
138
139 /*! \brief Returns the set of DD ranks that communicate with pme node cr->nodeid */
140 std::vector<int> get_pme_ddranks(const t_commrec* cr, int pmenodeid);
141
142 /*! \brief Returns the maximum shift for coordinate communication in PME, dim x */
143 int dd_pme_maxshift_x(const gmx_domdec_t* dd);
144
145 /*! \brief Returns the maximum shift for coordinate communication in PME, dim y */
146 int dd_pme_maxshift_y(const gmx_domdec_t* dd);
147
148 /*! \brief Return whether constraints, not including settles, cross domain boundaries */
149 bool ddHaveSplitConstraints(const gmx_domdec_t& dd);
150
151 /*! \brief Return whether update groups are used */
152 bool ddUsesUpdateGroups(const gmx_domdec_t& dd);
153
154 /*! \brief Return whether the DD has a single dimension with a single pulse
155  *
156  * The GPU halo exchange code requires a 1D single-pulse DD, and its
157  * setup code can use the returned value to understand what it should
158  * do. */
159 bool is1DAnd1PulseDD(const gmx_domdec_t& dd);
160
161 /*! \brief Initialize data structures for bonded interactions */
162 void dd_init_bondeds(FILE*                      fplog,
163                      gmx_domdec_t*              dd,
164                      const gmx_mtop_t&          mtop,
165                      const gmx_vsite_t*         vsite,
166                      const t_inputrec*          ir,
167                      gmx_bool                   bBCheck,
168                      gmx::ArrayRef<cginfo_mb_t> cginfo_mb);
169
170 /*! \brief Returns whether molecules are always whole, i.e. not broken by PBC */
171 bool dd_moleculesAreAlwaysWhole(const gmx_domdec_t& dd);
172
173 /*! \brief Returns if we need to do pbc for calculating bonded interactions */
174 gmx_bool dd_bonded_molpbc(const gmx_domdec_t* dd, int ePBC);
175
176 /*! \brief Change the DD non-bonded communication cut-off.
177  *
178  * This could fail when trying to increase the cut-off,
179  * then FALSE will be returned and the cut-off is not modified.
180  *
181  * \param[in] cr               Communication recrod
182  * \param[in] box              Box matrix, used for computing the dimensions of the system
183  * \param[in] x                Position vector, used for computing the dimensions of the system
184  * \param[in] cutoffRequested  The requested atom to atom cut-off distance, usually the pair-list cutoff distance
185  */
186 gmx_bool change_dd_cutoff(t_commrec* cr, const matrix box, gmx::ArrayRef<const gmx::RVec> x, real cutoffRequested);
187
188 /*! \brief Set up communication for averaging GPU wait times over domains
189  *
190  * When domains (PP MPI ranks) share a GPU, the individual GPU wait times
191  * are meaningless, as it depends on the order in which tasks on the same
192  * GPU finish. Therefore there wait times need to be averaged over the ranks
193  * sharing the same GPU. This function sets up the communication for that.
194  */
195 void dd_setup_dlb_resource_sharing(const t_commrec* cr, int gpu_id);
196
197 /*! \brief Cycle counter indices used internally in the domain decomposition */
198 enum
199 {
200     ddCyclStep,
201     ddCyclPPduringPME,
202     ddCyclF,
203     ddCyclWaitGPU,
204     ddCyclPME,
205     ddCyclNr
206 };
207
208 /*! \brief Add the wallcycle count to the DD counter */
209 void dd_cycles_add(const gmx_domdec_t* dd, float cycles, int ddCycl);
210
211 /*! \brief Communicate the coordinates to the neighboring cells and do pbc. */
212 void dd_move_x(struct gmx_domdec_t* dd, const matrix box, gmx::ArrayRef<gmx::RVec> x, gmx_wallcycle* wcycle);
213
214 /*! \brief Sum the forces over the neighboring cells.
215  *
216  * When fshift!=NULL the shift forces are updated to obtain
217  * the correct virial from the single sum including f.
218  */
219 void dd_move_f(struct gmx_domdec_t* dd, gmx::ForceWithShiftForces* forceWithShiftForces, gmx_wallcycle* wcycle);
220
221 /*! \brief Communicate a real for each atom to the neighboring cells. */
222 void dd_atom_spread_real(struct gmx_domdec_t* dd, real v[]);
223
224 /*! \brief Sum the contributions to a real for each atom over the neighboring cells. */
225 void dd_atom_sum_real(struct gmx_domdec_t* dd, real v[]);
226
227 /*! \brief Reset all the statistics and counters for total run counting */
228 void reset_dd_statistics_counters(struct gmx_domdec_t* dd);
229
230 /* In domdec_con.c */
231
232 /*! \brief Communicates the virtual site forces, reduces the shift forces when \p fshift != NULL */
233 void dd_move_f_vsites(struct gmx_domdec_t* dd, rvec* f, rvec* fshift);
234
235 /*! \brief Clears the forces for virtual sites */
236 void dd_clear_f_vsites(struct gmx_domdec_t* dd, rvec* f);
237
238 /*! \brief Move x0 and also x1 if x1!=NULL. bX1IsCoord tells if to do PBC on x1 */
239 void dd_move_x_constraints(struct gmx_domdec_t* dd, const matrix box, rvec* x0, rvec* x1, gmx_bool bX1IsCoord);
240
241 /*! \brief Communicates the coordinates involved in virtual sites */
242 void dd_move_x_vsites(struct gmx_domdec_t* dd, const matrix box, rvec* x);
243
244 /*! \brief Returns the local atom count array for all constraints
245  *
246  * The local atom count for a constraint, possible values 2/1/0, is needed
247  * to avoid not/double-counting contributions linked to the Lagrange
248  * multiplier, such as the virial and free-energy derivatives.
249  *
250  * \note When \p dd = nullptr, an empty reference is returned.
251  */
252 gmx::ArrayRef<const int> dd_constraints_nlocalatoms(const gmx_domdec_t* dd);
253
254 /* In domdec_top.c */
255
256 /*! \brief Print error output when interactions are missing */
257 [[noreturn]] void dd_print_missing_interactions(const gmx::MDLogger&  mdlog,
258                                                 t_commrec*            cr,
259                                                 int                   local_count,
260                                                 const gmx_mtop_t*     top_global,
261                                                 const gmx_localtop_t* top_local,
262                                                 const rvec*           x,
263                                                 const matrix          box);
264
265 /*! \brief Generate and store the reverse topology */
266 void dd_make_reverse_top(FILE*              fplog,
267                          gmx_domdec_t*      dd,
268                          const gmx_mtop_t*  mtop,
269                          const gmx_vsite_t* vsite,
270                          const t_inputrec*  ir,
271                          gmx_bool           bBCheck);
272
273 /*! \brief Generate the local topology and virtual site data */
274 void dd_make_local_top(struct gmx_domdec_t*       dd,
275                        struct gmx_domdec_zones_t* zones,
276                        int                        npbcdim,
277                        matrix                     box,
278                        rvec                       cellsize_min,
279                        const ivec                 npulse,
280                        t_forcerec*                fr,
281                        rvec*                      cgcm_or_x,
282                        const gmx_mtop_t&          top,
283                        gmx_localtop_t*            ltop);
284
285 /*! \brief Sort ltop->ilist when we are doing free energy. */
286 void dd_sort_local_top(gmx_domdec_t* dd, const t_mdatoms* mdatoms, gmx_localtop_t* ltop);
287
288 /*! \brief Initialize local topology
289  *
290  * \param[in] top_global Reference to global topology.
291  * \param[in,out] top Pointer to new local topology
292  */
293 void dd_init_local_top(const gmx_mtop_t& top_global, gmx_localtop_t* top);
294
295 /*! \brief Construct local state */
296 void dd_init_local_state(struct gmx_domdec_t* dd, const t_state* state_global, t_state* local_state);
297
298 /*! \brief Generate a list of links between atoms that are linked by bonded interactions
299  *
300  * Also stores whether atoms are linked in \p cginfo_mb.
301  */
302 t_blocka* makeBondedLinks(const gmx_mtop_t& mtop, gmx::ArrayRef<cginfo_mb_t> cginfo_mb);
303
304 /*! \brief Calculate the maximum distance involved in 2-body and multi-body bonded interactions */
305 void dd_bonded_cg_distance(const gmx::MDLogger& mdlog,
306                            const gmx_mtop_t*    mtop,
307                            const t_inputrec*    ir,
308                            const rvec*          x,
309                            const matrix         box,
310                            gmx_bool             bBCheck,
311                            real*                r_2b,
312                            real*                r_mb);
313
314 #endif