Clean up index handing in make_bondeds_zone
[alexxy/gromacs.git] / src / gromacs / domdec / domdec_struct.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2004, The GROMACS development team.
6  * Copyright (c) 2013,2014,2015,2018,2019,2020,2021, 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 /*! \libinternal \file
38  * \brief Declares structures related to domain decomposition.
39  *
40  * \author Berk Hess <hess@kth.se>
41  * \author David van der Spoel <david.vanderspoel@icm.uu.se>
42  * \inlibraryapi
43  * \ingroup module_domdec
44  */
45 #ifndef GMX_DOMDEC_DOMDEC_STRUCT_H
46 #define GMX_DOMDEC_DOMDEC_STRUCT_H
47
48 #include <cstddef>
49
50 #include <array>
51 #include <memory>
52 #include <vector>
53
54 #include "gromacs/math/vectypes.h"
55 #include "gromacs/topology/block.h"
56 #include "gromacs/utility/gmxmpi.h"
57 #include "gromacs/utility/range.h"
58 #include "gromacs/utility/real.h"
59
60 //! Max number of zones in domain decomposition
61 #define DD_MAXZONE 8
62 //! Max number of izones in domain decomposition
63 #define DD_MAXIZONE 4
64
65 struct AtomDistribution;
66 struct gmx_domdec_comm_t;
67 struct gmx_domdec_constraints_t;
68 struct gmx_domdec_specat_comm_t;
69 class gmx_ga2la_t;
70 struct gmx_pme_comm_n_box_t;
71 struct t_inputrec;
72 class gmx_reverse_top_t;
73 struct gmx_mtop_t;
74 struct ReverseTopOptions;
75
76 namespace gmx
77 {
78 template<typename T>
79 class HashedMap;
80 class LocalAtomSetManager;
81 class LocalTopologyChecker;
82 class GpuHaloExchange;
83 } // namespace gmx
84
85 /*! \internal
86  * \brief Pair interaction zone and atom range for an i-zone
87  */
88 struct DDPairInteractionRanges
89 {
90     //! The index of this i-zone in the i-zone list
91     int iZoneIndex = -1;
92     //! The range of j-zones
93     gmx::Range<int> jZoneRange;
94     //! The i-atom range
95     gmx::Range<int> iAtomRange;
96     //! The j-atom range
97     gmx::Range<int> jAtomRange;
98     //! Minimum shifts to consider
99     gmx::IVec shift0 = { 0, 0, 0 };
100     //! Maximum shifts to consider
101     gmx::IVec shift1 = { 0, 0, 0 };
102 };
103
104 typedef struct gmx_domdec_zone_size
105 {
106     /* Zone lower corner in triclinic coordinates         */
107     gmx::RVec x0 = { 0, 0, 0 };
108     /* Zone upper corner in triclinic coordinates         */
109     gmx::RVec x1 = { 0, 0, 0 };
110     /* Zone bounding box lower corner in Cartesian coords */
111     gmx::RVec bb_x0 = { 0, 0, 0 };
112     /* Zone bounding box upper corner in Cartesian coords */
113     gmx::RVec bb_x1 = { 0, 0, 0 };
114 } gmx_domdec_zone_size_t;
115
116 struct gmx_domdec_zones_t
117 {
118     /* The number of zones including the home zone */
119     int n = 0;
120     /* The shift of the zones with respect to the home zone */
121     std::array<ivec, DD_MAXZONE> shift;
122     /* The charge group boundaries for the zones */
123     std::array<int, DD_MAXZONE + 1> cg_range;
124     /* The pair interaction zone and atom ranges per each i-zone */
125     std::vector<DDPairInteractionRanges> iZones;
126     /* Boundaries of the zones */
127     std::array<gmx_domdec_zone_size_t, DD_MAXZONE> size;
128     /* The cg density of the home zone */
129     real dens_zone0 = 0;
130 };
131
132 struct gmx_ddbox_t
133 {
134     int       npbcdim;
135     int       nboundeddim;
136     gmx::RVec box0     = { 0, 0, 0 };
137     gmx::RVec box_size = { 0, 0, 0 };
138     /* Tells if the box is skewed for each of the three cartesian directions */
139     gmx::IVec tric_dir = { 0, 0, 0 };
140     gmx::RVec skew_fac = { 0, 0, 0 };
141     /* Orthogonal vectors for triclinic cells, Cartesian index */
142     rvec v[DIM][DIM];
143     /* Normal vectors for the cells walls */
144     rvec normal[DIM];
145 };
146
147 /*! \internal \brief Provides information about properties of the unit cell */
148 struct UnitCellInfo
149 {
150     //! Constructor
151     UnitCellInfo(const t_inputrec& ir);
152
153     //! We have PBC from dim 0 (X) up to npbcdim
154     int npbcdim;
155     //! The system is bounded from 0 (X) to numBoundedDimensions
156     int numBoundedDimensions;
157     //! Tells whether the box bounding the atoms is dynamic
158     bool ddBoxIsDynamic;
159     //! Screw PBC?
160     bool haveScrewPBC;
161 };
162
163 struct gmx_domdec_t
164 { //NOLINT(clang-analyzer-optin.performance.Padding)
165     //! Constructor, only partial for now
166     gmx_domdec_t(const t_inputrec& ir);
167     ~gmx_domdec_t();
168
169     /* The DD particle-particle nodes only */
170     /* The communication setup within the communicator all
171      * defined in dd->comm in domdec.c
172      */
173     int      nnodes       = 1;
174     MPI_Comm mpi_comm_all = MPI_COMM_NULL;
175     /* The local DD cell index and rank */
176     gmx::IVec ci         = { 0, 0, 0 };
177     int       rank       = 0;
178     gmx::IVec master_ci  = { 0, 0, 0 };
179     int       masterrank = 0;
180     /* Communication with the PME only nodes */
181     int                   pme_nodeid           = 0;
182     gmx_bool              pme_receive_vir_ener = false;
183     gmx_pme_comm_n_box_t* cnb                  = nullptr;
184     int                   nreq_pme             = 0;
185     MPI_Request           req_pme[8];
186
187     /* Properties of the unit cell */
188     UnitCellInfo unitCellInfo;
189
190     /* The communication setup, identical for each cell, cartesian index */
191     //! Todo: refactor nbnxm to not rely on this sometimes being a nullptr so this can be IVec
192     ivec      numCells = { 0, 0, 0 };
193     int       ndim     = 0;
194     gmx::IVec dim      = { 0, 0, 0 }; /* indexed by 0 to ndim */
195
196     /* Forward and backward neighboring cells, indexed by 0 to ndim */
197     int neighbor[DIM][2] = { { 0, 0 }, { 0, 0 }, { 0, 0 } };
198
199     /* Only available on the master node */
200     std::unique_ptr<AtomDistribution> ma;
201
202     /* Global atom number to interaction list */
203     std::unique_ptr<gmx_reverse_top_t> reverse_top;
204
205     /* Whether we have non-self exclusion */
206     bool haveExclusions = false;
207
208     /* Vsite stuff */
209     gmx::HashedMap<int>*      ga2la_vsite = nullptr;
210     gmx_domdec_specat_comm_t* vsite_comm  = nullptr;
211     std::vector<int>          vsite_requestedGlobalAtomIndices;
212
213     /* Constraint stuff */
214     gmx_domdec_constraints_t* constraints     = nullptr;
215     gmx_domdec_specat_comm_t* constraint_comm = nullptr;
216
217     /* The number of home atoms */
218     int numHomeAtoms = 0;
219     /* Global atom group indices for the home and all non-home groups */
220     std::vector<int> globalAtomGroupIndices;
221
222     /* Index from the local atoms to the global atoms, covers home and received zones */
223     std::vector<int> globalAtomIndices;
224
225     /* Global atom number to local atom number list */
226     gmx_ga2la_t* ga2la = nullptr;
227
228     /* Communication stuff */
229     gmx_domdec_comm_t* comm = nullptr;
230
231     /* The partioning count, to keep track of the state */
232     int64_t ddp_count = 0;
233
234     /* The managed atom sets that are updated in domain decomposition */
235     gmx::LocalAtomSetManager* atomSets = nullptr;
236
237     //! The handler for checking whether the local topology is missing interactions
238     std::unique_ptr<gmx::LocalTopologyChecker> localTopologyChecker;
239
240     /* gmx_pme_recv_f buffer */
241     std::vector<gmx::RVec> pmeForceReceiveBuffer;
242
243     /* GPU halo exchange objects: this structure supports a vector of pulses for each dimension */
244     std::vector<std::unique_ptr<gmx::GpuHaloExchange>> gpuHaloExchange[DIM];
245 };
246
247 //! Are we the master node for domain decomposition
248 static inline bool DDMASTER(const gmx_domdec_t& dd)
249 {
250     return dd.rank == dd.masterrank;
251 };
252
253 //! Are we the master node for domain decomposition, deprecated
254 static inline bool DDMASTER(const gmx_domdec_t* dd)
255 {
256     return dd->rank == dd->masterrank;
257 };
258
259 #endif