Move PairlistSet declaration
[alexxy/gromacs.git] / src / gromacs / nbnxm / pairlist.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2013,2014,2015,2016,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 #ifndef GMX_NBNXM_PAIRLIST_H
37 #define GMX_NBNXM_PAIRLIST_H
38
39 #include "config.h"
40
41 #include <cstddef>
42
43 #include "gromacs/gpu_utils/hostallocator.h"
44 #include "gromacs/math/vectypes.h"
45 #include "gromacs/mdtypes/nblist.h"
46 #include "gromacs/utility/basedefinitions.h"
47 #include "gromacs/utility/defaultinitializationallocator.h"
48 #include "gromacs/utility/enumerationhelpers.h"
49 #include "gromacs/utility/real.h"
50
51 // This file with constants is separate from this file to be able
52 // to include it during OpenCL jitting without including config.h
53 #include "gromacs/nbnxm/constants.h"
54
55 #include "locality.h"
56
57 struct NbnxnPairlistCpuWork;
58 struct NbnxnPairlistGpuWork;
59
60
61 /* Convenience type for vector with aligned memory */
62 template<typename T>
63 using AlignedVector = std::vector < T, gmx::AlignedAllocator < T>>;
64
65 /* Convenience type for vector that avoids initialization at resize() */
66 template<typename T>
67 using FastVector = std::vector < T, gmx::DefaultInitializationAllocator < T>>;
68
69 /* With CPU kernels the i-cluster size is always 4 atoms. */
70 static constexpr int c_nbnxnCpuIClusterSize = 4;
71
72 /* With GPU kernels the i and j cluster size is 8 atoms for CUDA and can be set at compile time for OpenCL */
73 #if GMX_GPU == GMX_GPU_OPENCL
74 static constexpr int c_nbnxnGpuClusterSize = GMX_OPENCL_NB_CLUSTER_SIZE;
75 #else
76 static constexpr int c_nbnxnGpuClusterSize = 8;
77 #endif
78
79 /* The number of clusters in a pair-search cell, used for GPU */
80 static constexpr int c_gpuNumClusterPerCellZ = 2;
81 static constexpr int c_gpuNumClusterPerCellY = 2;
82 static constexpr int c_gpuNumClusterPerCellX = 2;
83 static constexpr int c_gpuNumClusterPerCell  = c_gpuNumClusterPerCellZ*c_gpuNumClusterPerCellY*c_gpuNumClusterPerCellX;
84
85
86 /* In CUDA the number of threads in a warp is 32 and we have cluster pairs
87  * of 8*8=64 atoms, so it's convenient to store data for cluster pair halves.
88  */
89 static constexpr int c_nbnxnGpuClusterpairSplit = 2;
90
91 /* The fixed size of the exclusion mask array for a half cluster pair */
92 static constexpr int c_nbnxnGpuExclSize = c_nbnxnGpuClusterSize*c_nbnxnGpuClusterSize/c_nbnxnGpuClusterpairSplit;
93
94 //! The available pair list types
95 enum class PairlistType : int
96 {
97     Simple4x2,
98     Simple4x4,
99     Simple4x8,
100     HierarchicalNxN,
101     Count
102 };
103
104 //! Gives the i-cluster size for each pairlist type
105 static constexpr gmx::EnumerationArray<PairlistType, int> IClusterSizePerListType =
106 { {
107       c_nbnxnCpuIClusterSize,
108       c_nbnxnCpuIClusterSize,
109       c_nbnxnCpuIClusterSize,
110       c_nbnxnGpuClusterSize
111   } };
112 //! Gives the j-cluster size for each pairlist type
113 static constexpr gmx::EnumerationArray<PairlistType, int> JClusterSizePerListType =
114 { {
115       2,
116       4,
117       8,
118       c_nbnxnGpuClusterSize
119   } };
120
121 /* A buffer data structure of 64 bytes
122  * to be placed at the beginning and end of structs
123  * to avoid cache invalidation of the real contents
124  * of the struct by writes to neighboring memory.
125  */
126 typedef struct {
127     int dummy[16];
128 } gmx_cache_protect_t;
129
130 /* This is the actual cluster-pair list j-entry.
131  * cj is the j-cluster.
132  * The interaction bits in excl are indexed i-major, j-minor.
133  * The cj entries are sorted such that ones with exclusions come first.
134  * This means that once a full mask (=NBNXN_INTERACTION_MASK_ALL)
135  * is found, all subsequent j-entries in the i-entry also have full masks.
136  */
137 struct nbnxn_cj_t
138 {
139     int          cj;    /* The j-cluster                    */
140     unsigned int excl;  /* The exclusion (interaction) bits */
141 };
142
143 /* In nbnxn_ci_t the integer shift contains the shift in the lower 7 bits.
144  * The upper bits contain information for non-bonded kernel optimization.
145  * Simply calculating LJ and Coulomb for all pairs in a cluster pair is fine.
146  * But three flags can be used to skip interactions, currently only for subc=0
147  * !(shift & NBNXN_CI_DO_LJ(subc))   => we can skip LJ for all pairs
148  * shift & NBNXN_CI_HALF_LJ(subc)    => we can skip LJ for the second half of i
149  * !(shift & NBNXN_CI_DO_COUL(subc)) => we can skip Coulomb for all pairs
150  */
151 #define NBNXN_CI_SHIFT          127
152 #define NBNXN_CI_DO_LJ(subc)    (1<<(7+3*(subc)))
153 #define NBNXN_CI_HALF_LJ(subc)  (1<<(8+3*(subc)))
154 #define NBNXN_CI_DO_COUL(subc)  (1<<(9+3*(subc)))
155
156 /* Cluster-pair Interaction masks
157  * Bit i*j-cluster-size + j tells if atom i and j interact.
158  */
159 // TODO: Rename according to convention when moving into Nbnxn namespace
160 /* All interaction mask is the same for all kernels */
161 constexpr unsigned int NBNXN_INTERACTION_MASK_ALL       = 0xffffffffU;
162 /* 4x4 kernel diagonal mask */
163 constexpr unsigned int NBNXN_INTERACTION_MASK_DIAG      = 0x08ceU;
164 /* 4x2 kernel diagonal masks */
165 constexpr unsigned int NBNXN_INTERACTION_MASK_DIAG_J2_0 = 0x0002U;
166 constexpr unsigned int NBNXN_INTERACTION_MASK_DIAG_J2_1 = 0x002fU;
167 /* 4x8 kernel diagonal masks */
168 constexpr unsigned int NBNXN_INTERACTION_MASK_DIAG_J8_0 = 0xf0f8fcfeU;
169 constexpr unsigned int NBNXN_INTERACTION_MASK_DIAG_J8_1 = 0x0080c0e0U;
170
171 /* Simple pair-list i-unit */
172 struct nbnxn_ci_t
173 {
174     int ci;             /* i-cluster             */
175     int shift;          /* Shift vector index plus possible flags, see above */
176     int cj_ind_start;   /* Start index into cj   */
177     int cj_ind_end;     /* End index into cj     */
178 };
179
180 /* Grouped pair-list i-unit */
181 typedef struct {
182     /* Returns the number of j-cluster groups in this entry */
183     int numJClusterGroups() const
184     {
185         return cj4_ind_end - cj4_ind_start;
186     }
187
188     int sci;            /* i-super-cluster       */
189     int shift;          /* Shift vector index plus possible flags */
190     int cj4_ind_start;  /* Start index into cj4  */
191     int cj4_ind_end;    /* End index into cj4    */
192 } nbnxn_sci_t;
193
194 /* Interaction data for a j-group for one warp */
195 struct nbnxn_im_ei_t
196 {
197     // The i-cluster interactions mask for 1 warp
198     unsigned int imask    = 0U;
199     // Index into the exclusion array for 1 warp, default index 0 which means no exclusions
200     int          excl_ind = 0;
201 };
202
203 typedef struct {
204     int           cj[c_nbnxnGpuJgroupSize];         /* The 4 j-clusters */
205     nbnxn_im_ei_t imei[c_nbnxnGpuClusterpairSplit]; /* The i-cluster mask data       for 2 warps   */
206 } nbnxn_cj4_t;
207
208 /* Struct for storing the atom-pair interaction bits for a cluster pair in a GPU pairlist */
209 struct nbnxn_excl_t
210 {
211     /* Constructor, sets no exclusions, so all atom pairs interacting */
212     nbnxn_excl_t()
213     {
214         for (unsigned int &pairEntry : pair)
215         {
216             pairEntry = NBNXN_INTERACTION_MASK_ALL;
217         }
218     }
219
220     /* Topology exclusion interaction bits per warp */
221     unsigned int pair[c_nbnxnGpuExclSize];
222 };
223
224 /* Cluster pairlist type for use on CPUs */
225 struct NbnxnPairlistCpu
226 {
227     NbnxnPairlistCpu();
228
229     gmx_cache_protect_t     cp0;
230
231     int                     na_ci;       /* The number of atoms per i-cluster        */
232     int                     na_cj;       /* The number of atoms per j-cluster        */
233     real                    rlist;       /* The radius for constructing the list     */
234     FastVector<nbnxn_ci_t>  ci;          /* The i-cluster list                       */
235     FastVector<nbnxn_ci_t>  ciOuter;     /* The outer, unpruned i-cluster list       */
236
237     FastVector<nbnxn_cj_t>  cj;          /* The j-cluster list, size ncj             */
238     FastVector<nbnxn_cj_t>  cjOuter;     /* The outer, unpruned j-cluster list       */
239     int                     ncjInUse;    /* The number of j-clusters that are used by ci entries in this list, will be <= cj.size() */
240
241     int                     nci_tot;     /* The total number of i clusters           */
242
243     /* Working data storage for list construction */
244     std::unique_ptr<NbnxnPairlistCpuWork> work;
245
246     gmx_cache_protect_t                   cp1;
247 };
248
249 /* Cluster pairlist type, with extra hierarchies, for on the GPU
250  *
251  * NOTE: for better performance when combining lists over threads,
252  *       all vectors should use default initialization. But when
253  *       changing this, excl should be intialized when adding entries.
254  */
255 struct NbnxnPairlistGpu
256 {
257     /* Constructor
258      *
259      * \param[in] pinningPolicy  Sets the pinning policy for all buffers used on the GPU
260      */
261     NbnxnPairlistGpu(gmx::PinningPolicy pinningPolicy);
262
263     gmx_cache_protect_t            cp0;
264
265     int                            na_ci; /* The number of atoms per i-cluster        */
266     int                            na_cj; /* The number of atoms per j-cluster        */
267     int                            na_sc; /* The number of atoms per super cluster    */
268     real                           rlist; /* The radius for constructing the list     */
269     // The i-super-cluster list, indexes into cj4;
270     gmx::HostVector<nbnxn_sci_t>   sci;
271     // The list of 4*j-cluster groups
272     gmx::HostVector<nbnxn_cj4_t>   cj4;
273     // Atom interaction bits (non-exclusions)
274     gmx::HostVector<nbnxn_excl_t>  excl;
275     // The total number of i-clusters
276     int                            nci_tot;
277
278     /* Working data storage for list construction */
279     std::unique_ptr<NbnxnPairlistGpuWork> work;
280
281     gmx_cache_protect_t                   cp1;
282 };
283
284 //! Initializes a free-energy pair-list
285 void nbnxn_init_pairlist_fep(t_nblist *nl);
286
287 #endif