59886a5f8bad21254a51eba3e447cb3d1bfa97a3
[alexxy/gromacs.git] / src / gromacs / nbnxm / nbnxm.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2013,2014,2015,2017,2018,2019, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35
36 // FIXME: remove the "__" prefix in front of the group def when we move the
37 //        nonbonded code into separate dir.
38
39 /*! \libinternal \defgroup __module_nbnxm Short-range non-bonded interaction module
40  * \ingroup group_mdrun
41  *
42  * \brief Computes forces and energies for short-range pair-interactions
43  * based on the Verlet algorithm. The algorithm uses pair-lists generated
44  * at fixed intervals as well as various flavors of pair interaction kernels
45  * implemented for a wide range of CPU and GPU architectures.
46  *
47  * The module includes support for flavors of Coulomb and Lennard-Jones interaction
48  * treatment implemented for a large range of SIMD instruction sets for CPU
49  * architectures as well as in CUDA and OpenCL for GPU architectures.
50  * Additionally there is a reference CPU non-SIMD and a reference CPU
51  * for GPU pair-list setup interaction kernel.
52  *
53  * The implementation of the kernels is based on the cluster non-bonded algorithm
54  * which in the code is referred to as the NxM algorithms ("nbnxm_" prefix);
55  * for details of the algorithm see DOI:10.1016/j.cpc.2013.06.003.
56  *
57  * Algorithmically, the non-bonded computation has two different modes:
58  * A "classical" mode: generate a list every nstlist steps containing at least
59  * all atom pairs up to a distance of rlistOuter and compute pair interactions
60  * for all pairs that are within the interaction cut-off.
61  * A "dynamic pruning" mode: generate an "outer-list" up to cut-off rlistOuter
62  * every nstlist steps and prune the outer-list using a cut-off of rlistInner
63  * every nstlistPrune steps to obtain a, smaller, "inner-list". This
64  * results in fewer interaction computations and allows for a larger nstlist.
65  * On a GPU, this dynamic pruning is performed in a rolling fashion, pruning
66  * only a sub-part of the list each (second) step. This way it can often
67  * overlap with integration and constraints on the CPU.
68  * Currently a simple heuristic determines which mode will be used.
69  *
70  * TODO: add a summary list and brief descriptions of the different submodules:
71  * search, CPU kernels, GPU glue code + kernels.
72  *
73  * \author Berk Hess <hess@kth.se>
74  * \author Szilárd Páll <pall.szilard@gmail.com>
75  * \author Mark Abraham <mark.j.abraham@gmail.com>
76  * \author Anca Hamuraru <anca@streamcomputing.eu>
77  * \author Teemu Virolainen <teemu@streamcomputing.eu>
78  * \author Dimitrios Karkoulis <dimitris.karkoulis@gmail.com>
79  *
80  * TODO: add more authors!
81  */
82
83 /*! \libinternal \file
84  *
85  * \brief This file contains the public interface of the nbnxm module
86  * that implements the NxM atom cluster non-bonded algorithm to efficiently
87  * compute pair forces.
88  *
89  *
90  * \author Berk Hess <hess@kth.se>
91  * \author Szilárd Páll <pall.szilard@gmail.com>
92  *
93  * \inlibraryapi
94  * \ingroup __module_nbnxm
95  */
96
97
98 #ifndef GMX_NBNXM_NBNXM_H
99 #define GMX_NBNXM_NBNXM_H
100
101 #include <memory>
102
103 #include "gromacs/math/vectypes.h"
104 #include "gromacs/nbnxm/pairlist.h"
105 #include "gromacs/nbnxm/pairlistset.h"
106 #include "gromacs/utility/arrayref.h"
107 #include "gromacs/utility/real.h"
108
109 // TODO: Remove this include and the two nbnxm includes above
110 #include "nbnxm_gpu.h"
111
112 struct gmx_device_info_t;
113 struct gmx_domdec_zones_t;
114 struct gmx_enerdata_t;
115 struct gmx_hw_info_t;
116 struct gmx_mtop_t;
117 struct interaction_const_t;
118 struct t_commrec;
119 struct t_nrnb;
120 struct t_forcerec;
121 struct t_inputrec;
122
123 namespace gmx
124 {
125 class MDLogger;
126 class UpdateGroupsCog;
127 }
128
129 //! Help pass GPU-emulation parameters with type safety.
130 enum class EmulateGpuNonbonded : bool
131 {
132     //! Do not emulate GPUs.
133     No,
134     //! Do emulate GPUs.
135     Yes
136 };
137
138
139 /*! \brief Nonbonded NxN kernel types: plain C, CPU SIMD, GPU, GPU emulation */
140 typedef enum
141 {
142     nbnxnkNotSet = 0,
143     nbnxnk4x4_PlainC,
144     nbnxnk4xN_SIMD_4xN,
145     nbnxnk4xN_SIMD_2xNN,
146     nbnxnk8x8x8_GPU,
147     nbnxnk8x8x8_PlainC,
148     nbnxnkNR
149 } nbnxn_kernel_type;
150
151 /*! \brief Return a string identifying the kernel type.
152  *
153  * \param [in] kernel_type   nonbonded kernel types, takes values from the nbnxn_kernel_type enum
154  * \returns                  a string identifying the kernel corresponding to the type passed as argument
155  */
156 const char *lookup_nbnxn_kernel_name(int kernel_type);
157
158 /*! \brief Ewald exclusion types */
159 enum {
160     ewaldexclTable, ewaldexclAnalytical
161 };
162
163 /*! \brief Atom locality indicator: local, non-local, all.
164  *
165  * Used for calls to:
166  * gridding, pair-search, force calculation, x/f buffer operations
167  * */
168 enum {
169     eatLocal = 0, eatNonlocal = 1, eatAll
170 };
171
172 /*! \brief Tests for local atom range */
173 #define LOCAL_A(x)               ((x) == eatLocal)
174 /*! \brief Tests for non-local atom range */
175 #define NONLOCAL_A(x)            ((x) == eatNonlocal)
176 /*! \brief Tests for either local or non-local atom range */
177 #define LOCAL_OR_NONLOCAL_A(x)   (LOCAL_A(x) || NONLOCAL_A(x))
178
179 /*! \brief Interaction locality indicator
180  *
181  * Used in pair-list search/calculations in the following manner:
182  *  - local interactions require local atom data and affect local output only;
183  *  - non-local interactions require both local and non-local atom data and
184  *    affect both local- and non-local output.
185  */
186 enum {
187     eintLocal = 0, eintNonlocal = 1
188 };
189
190 /*! \brief Tests for local interaction indicator */
191 #define LOCAL_I(x)               ((x) == eintLocal)
192 /*! \brief Tests for non-local interaction indicator */
193 #define NONLOCAL_I(x)            ((x) == eintNonlocal)
194
195 /*! \brief Flag to tell the nonbonded kernels whether to clear the force output buffers */
196 enum {
197     enbvClearFNo, enbvClearFYes
198 };
199
200 /*! \libinternal
201  *  \brief Non-bonded interaction group data structure. */
202 typedef struct nonbonded_verlet_group_t {
203     nbnxn_pairlist_set_t  nbl_lists;   /**< pair list(s)                       */
204     int                   kernel_type; /**< non-bonded kernel - see enum above */
205     int                   ewald_excl;  /**< Ewald exclusion - see enum above   */
206 } nonbonded_verlet_group_t;
207
208 /*! \libinternal
209  *  \brief Top-level non-bonded data structure for the Verlet-type cut-off scheme. */
210 typedef struct nonbonded_verlet_t {
211     std::unique_ptr<NbnxnListParameters> listParams;      /**< Parameters for the search and list pruning setup */
212     std::unique_ptr<nbnxn_search>        nbs;             /**< n vs n atom pair searching data       */
213     int                                  ngrp;            /**< number of interaction groups          */
214     nonbonded_verlet_group_t             grp[2];          /**< local and non-local interaction group */
215     nbnxn_atomdata_t                    *nbat;            /**< atom data                             */
216
217     gmx_bool                             bUseGPU;         /**< TRUE when non-bonded interactions are computed on a physical GPU */
218     EmulateGpuNonbonded                  emulateGpu;      /**< true when non-bonded interactions are computed on the CPU using GPU-style pair lists */
219     gmx_nbnxn_gpu_t                     *gpu_nbv;         /**< pointer to GPU nb verlet data     */
220     int                                  min_ci_balanced; /**< pair list balancing parameter
221                                                                used for the 8x8x8 GPU kernels    */
222 } nonbonded_verlet_t;
223
224 /*! \brief Initializes the nbnxn module */
225 void init_nb_verlet(const gmx::MDLogger     &mdlog,
226                     nonbonded_verlet_t     **nb_verlet,
227                     gmx_bool                 bFEP_NonBonded,
228                     const t_inputrec        *ir,
229                     const t_forcerec        *fr,
230                     const t_commrec         *cr,
231                     const gmx_hw_info_t     &hardwareInfo,
232                     const gmx_device_info_t *deviceInfo,
233                     const gmx_mtop_t        *mtop,
234                     matrix                   box);
235
236 /*! \brief Put the atoms on the pair search grid.
237  *
238  * Only atoms atomStart to atomEnd in x are put on the grid.
239  * The atom_density is used to determine the grid size.
240  * When atomDensity<=0, the density is determined from atomEnd-atomStart and the corners.
241  * With domain decomposition part of the n particles might have migrated,
242  * but have not been removed yet. This count is given by nmoved.
243  * When move[i] < 0 particle i has migrated and will not be put on the grid.
244  * Without domain decomposition move will be NULL.
245  */
246 void nbnxn_put_on_grid(nbnxn_search_t                  nbs,
247                        int                             ePBC,
248                        const matrix                    box,
249                        int                             ddZone,
250                        const rvec                      lowerCorner,
251                        const rvec                      upperCorner,
252                        const gmx::UpdateGroupsCog     *updateGroupsCog,
253                        int                             atomStart,
254                        int                             atomEnd,
255                        real                            atomDensity,
256                        const int                      *atinfo,
257                        gmx::ArrayRef<const gmx::RVec>  x,
258                        int                             numAtomsMoved,
259                        const int                      *move,
260                        int                             nb_kernel_type,
261                        nbnxn_atomdata_t               *nbat);
262
263 /*! \brief As nbnxn_put_on_grid, but for the non-local atoms
264  *
265  * with domain decomposition. Should be called after calling
266  * nbnxn_search_put_on_grid for the local atoms / home zone.
267  */
268 void nbnxn_put_on_grid_nonlocal(nbnxn_search_t                   nbs,
269                                 const struct gmx_domdec_zones_t *zones,
270                                 const int                       *atinfo,
271                                 gmx::ArrayRef<const gmx::RVec>   x,
272                                 int                              nb_kernel_type,
273                                 nbnxn_atomdata_t                *nbat);
274
275 /*! \brief Returns the number of x and y cells in the local grid */
276 void nbnxn_get_ncells(nbnxn_search_t nbs, int *ncx, int *ncy);
277
278 /*! \brief Returns the order indices of the atoms on the pairlist search grid */
279 gmx::ArrayRef<const int> nbnxn_get_atomorder(const nbnxn_search* nbs);
280
281 /*! \brief Renumbers the atom indices on the grid to consecutive order */
282 void nbnxn_set_atomorder(nbnxn_search_t nbs);
283
284 /*! \brief Returns the index position of the atoms on the pairlist search grid */
285 gmx::ArrayRef<const int> nbnxn_get_gridindices(const nbnxn_search* nbs);
286
287 /*! \brief Prune all pair-lists with given locality (currently CPU only)
288  *
289  * For all pair-lists with given locality, takes the outer list and prunes out
290  * pairs beyond the pairlist inner radius and writes the result to a list that is
291  * to be consumed by the non-bonded kernel.
292  */
293 void NbnxnDispatchPruneKernel(nonbonded_verlet_t *nbv,
294                               int                 ilocality,
295                               const rvec         *shift_vec);
296
297 /*! \brief Executes the non-bonded kernel of the GPU or launches it on the GPU */
298 void NbnxnDispatchKernel(nonbonded_verlet_t        *nbv,
299                          int                        ilocality,
300                          const interaction_const_t &ic,
301                          int                        forceFlags,
302                          int                        clearF,
303                          t_forcerec                *fr,
304                          gmx_enerdata_t            *enerd,
305                          t_nrnb                    *nrnb);
306
307 #endif // GMX_NBNXN_NBNXN_H