Make nbnxm headers more self-contained
[alexxy/gromacs.git] / src / gromacs / nbnxm / opencl / nbnxm_ocl_types.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2013,2014,2015,2016 by the GROMACS development team.
5  * Copyright (c) 2017,2018,2019,2020, by the GROMACS development team, led by
6  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7  * and including many others, as listed in the AUTHORS file in the
8  * top-level source directory and at http://www.gromacs.org.
9  *
10  * GROMACS is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public License
12  * as published by the Free Software Foundation; either version 2.1
13  * of the License, or (at your option) any later version.
14  *
15  * GROMACS is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with GROMACS; if not, see
22  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
24  *
25  * If you want to redistribute modifications to GROMACS, please
26  * consider that scientific software is very special. Version
27  * control is crucial - bugs must be traceable. We will be happy to
28  * consider code for inclusion in the official distribution, but
29  * derived work must not be called official GROMACS. Details are found
30  * in the README & COPYING files - if they are missing, get the
31  * official version at http://www.gromacs.org.
32  *
33  * To help us fund GROMACS development, we humbly ask that you cite
34  * the research papers on the package. Check out http://www.gromacs.org.
35  */
36
37 /*! \internal \file
38  *  \brief
39  *  Data types used internally in the nbnxm_ocl module.
40  *
41  *  \author Anca Hamuraru <anca@streamcomputing.eu>
42  *  \author Szilárd Páll <pszilard@kth.se>
43  *  \ingroup module_nbnxm
44  */
45
46 #ifndef GMX_NBNXM_NBNXM_OPENCL_TYPES_H
47 #define GMX_NBNXM_NBNXM_OPENCL_TYPES_H
48
49 #include "gromacs/gpu_utils/devicebuffer.h"
50 #include "gromacs/gpu_utils/gmxopencl.h"
51 #include "gromacs/gpu_utils/gputraits_ocl.h"
52 #include "gromacs/gpu_utils/oclutils.h"
53 #include "gromacs/mdtypes/interaction_const.h"
54 #include "gromacs/nbnxm/gpu_types_common.h"
55 #include "gromacs/nbnxm/nbnxm.h"
56 #include "gromacs/nbnxm/pairlist.h"
57 #include "gromacs/utility/enumerationhelpers.h"
58 #include "gromacs/utility/fatalerror.h"
59 #include "gromacs/utility/real.h"
60
61 #include "nbnxm_ocl_consts.h"
62
63 /* kernel does #include "gromacs/math/utilities.h" */
64 /* Move the actual useful stuff here: */
65
66 //! Define 1/sqrt(pi)
67 #define M_FLOAT_1_SQRTPI 0.564189583547756f
68
69 /*! @} */
70 /*! \brief Constants for platform-dependent defaults for the prune kernel's j4 processing concurrency.
71  *
72  *  Initialized using macros that can be overridden at compile-time (using #GMX_NBNXN_PRUNE_KERNEL_J4_CONCURRENCY).
73  */
74 /*! @{ */
75 const int c_oclPruneKernelJ4ConcurrencyDEFAULT = GMX_NBNXN_PRUNE_KERNEL_J4_CONCURRENCY_DEFAULT;
76 /*! @} */
77
78 /*! \brief Returns the j4 processing concurrency parameter for the vendor \p vendorId
79  *  \param vendorId takes values from #ocl_vendor_id_t.
80  */
81 static inline int getOclPruneKernelJ4Concurrency(int vendorId)
82 {
83     switch (vendorId)
84     {
85         default: return c_oclPruneKernelJ4ConcurrencyDEFAULT;
86     }
87 }
88
89
90 /*! \brief Electrostatic OpenCL kernel flavors.
91  *
92  *  Types of electrostatics implementations available in the OpenCL non-bonded
93  *  force kernels. These represent both the electrostatics types implemented
94  *  by the kernels (cut-off, RF, and Ewald - a subset of what's defined in
95  *  enums.h) as well as encode implementation details analytical/tabulated
96  *  and single or twin cut-off (for Ewald kernels).
97  *  Note that the cut-off and RF kernels have only analytical flavor and unlike
98  *  in the CPU kernels, the tabulated kernels are ATM Ewald-only.
99  *
100  *  The row-order of pointers to different electrostatic kernels defined in
101  *  nbnxn_cuda.cu by the nb_*_kfunc_ptr function pointer table
102  *  should match the order of enumerated types below.
103  */
104 enum eelOcl
105 {
106     eelOclCUT,
107     eelOclRF,
108     eelOclEWALD_TAB,
109     eelOclEWALD_TAB_TWIN,
110     eelOclEWALD_ANA,
111     eelOclEWALD_ANA_TWIN,
112     eelOclNR
113 };
114
115 /*! \brief VdW OpenCL kernel flavors.
116  *
117  * The enumerates values correspond to the LJ implementations in the OpenCL non-bonded
118  * kernels.
119  *
120  * The column-order of pointers to different electrostatic kernels defined in
121  * nbnxn_cuda.cu by the nb_*_kfunc_ptr function pointer table
122  * should match the order of enumerated types below.
123  */
124 enum evdwOcl
125 {
126     evdwOclCUT,
127     evdwOclCUTCOMBGEOM,
128     evdwOclCUTCOMBLB,
129     evdwOclFSWITCH,
130     evdwOclPSWITCH,
131     evdwOclEWALDGEOM,
132     evdwOclEWALDLB,
133     evdwOclNR
134 };
135
136 /*! \brief Pruning kernel flavors.
137  *
138  * The values correspond to the first call of the pruning post-list generation
139  * and the rolling pruning, respectively.
140  */
141 enum ePruneKind
142 {
143     epruneFirst,
144     epruneRolling,
145     ePruneNR
146 };
147
148 /*! \internal
149  * \brief Staging area for temporary data downloaded from the GPU.
150  *
151  *  The energies/shift forces get downloaded here first, before getting added
152  *  to the CPU-side aggregate values.
153  */
154 typedef struct cl_nb_staging
155 {
156     float* e_lj;        /**< LJ energy                       */
157     float* e_el;        /**< electrostatic energy            */
158     float (*fshift)[3]; /**< float3 buffer with shift forces */
159 } cl_nb_staging_t;
160
161 /*! \internal
162  * \brief Nonbonded atom data - both inputs and outputs.
163  */
164 typedef struct cl_atomdata
165 {
166     int natoms;       /**< number of atoms                              */
167     int natoms_local; /**< number of local atoms                        */
168     int nalloc;       /**< allocation size for the atom data (xq, f)    */
169
170     cl_mem xq; /**< float4 buffer with atom coordinates + charges, size natoms */
171
172     cl_mem f;           /**< float3 buffer with force output array, size natoms         */
173     size_t f_elem_size; /**< Size in bytes for one element of f buffer      */
174
175     cl_mem e_lj; /**< LJ energy output, size 1                       */
176     cl_mem e_el; /**< Electrostatics energy input, size 1            */
177
178     cl_mem fshift;           /**< float3 buffer with shift forces                */
179     size_t fshift_elem_size; /**< Size in bytes for one element of fshift buffer */
180
181     int    ntypes;     /**< number of atom types                           */
182     cl_mem atom_types; /**< int buffer with atom type indices, size natoms */
183     cl_mem lj_comb;    /**< float2 buffer with sqrt(c6),sqrt(c12), size natoms */
184
185     cl_mem shift_vec;           /**< float3 buffer with shifts values               */
186     size_t shift_vec_elem_size; /**< Size in bytes for one element of shift_vec buffer */
187
188     cl_bool bShiftVecUploaded; /**< true if the shift vector has been uploaded  */
189 } cl_atomdata_t;
190
191 /*! \internal
192  * \brief Parameters required for the OpenCL nonbonded calculations.
193  */
194 typedef struct cl_nbparam
195 {
196
197     int eeltype; /**< type of electrostatics, takes values from #eelOcl */
198     int vdwtype; /**< type of VdW impl., takes values from #evdwOcl     */
199
200     float epsfac;      /**< charge multiplication factor                      */
201     float c_rf;        /**< Reaction-field/plain cutoff electrostatics const. */
202     float two_k_rf;    /**< Reaction-field electrostatics constant            */
203     float ewald_beta;  /**< Ewald/PME parameter                               */
204     float sh_ewald;    /**< Ewald/PME correction term substracted from the direct-space potential */
205     float sh_lj_ewald; /**< LJ-Ewald/PME correction term added to the correction potential        */
206     float ewaldcoeff_lj; /**< LJ-Ewald/PME coefficient                          */
207
208     float rcoulomb_sq; /**< Coulomb cut-off squared                           */
209
210     float rvdw_sq;           /**< VdW cut-off squared                               */
211     float rvdw_switch;       /**< VdW switched cut-off                              */
212     float rlistOuter_sq;     /**< Full, outer pair-list cut-off squared             */
213     float rlistInner_sq;     /**< Inner, dynamic pruned pair-list cut-off squared   */
214     bool  useDynamicPruning; /**< True if we use dynamic pair-list pruning          */
215
216     shift_consts_t  dispersion_shift; /**< VdW shift dispersion constants           */
217     shift_consts_t  repulsion_shift;  /**< VdW shift repulsion constants            */
218     switch_consts_t vdw_switch;       /**< VdW switch constants                     */
219
220     /* LJ non-bonded parameters - accessed through texture memory */
221     cl_mem nbfp_climg2d; /**< nonbonded parameter table with C6/C12 pairs per atom type-pair, 2*ntype^2 elements */
222     cl_mem nbfp_comb_climg2d; /**< nonbonded parameter table per atom type, 2*ntype elements */
223
224     /* Ewald Coulomb force table data - accessed through texture memory */
225     float  coulomb_tab_scale;   /**< table scale/spacing                        */
226     cl_mem coulomb_tab_climg2d; /**< pointer to the table in the device memory  */
227 } cl_nbparam_t;
228
229 /*! \internal
230  * \brief Data structure shared between the OpenCL device code and OpenCL host code
231  *
232  * Must not contain OpenCL objects (buffers)
233  * TODO: review, improve */
234 typedef struct cl_nbparam_params
235 {
236
237     int eeltype; /**< type of electrostatics, takes values from #eelCu */
238     int vdwtype; /**< type of VdW impl., takes values from #evdwCu     */
239
240     float epsfac;      /**< charge multiplication factor                      */
241     float c_rf;        /**< Reaction-field/plain cutoff electrostatics const. */
242     float two_k_rf;    /**< Reaction-field electrostatics constant            */
243     float ewald_beta;  /**< Ewald/PME parameter                               */
244     float sh_ewald;    /**< Ewald/PME correction term substracted from the direct-space potential */
245     float sh_lj_ewald; /**< LJ-Ewald/PME correction term added to the correction potential        */
246     float ewaldcoeff_lj; /**< LJ-Ewald/PME coefficient                          */
247
248     float rcoulomb_sq; /**< Coulomb cut-off squared                           */
249
250     float rvdw_sq;       /**< VdW cut-off squared                               */
251     float rvdw_switch;   /**< VdW switched cut-off                              */
252     float rlistOuter_sq; /**< Full, outer pair-list cut-off squared             */
253     float rlistInner_sq; /**< Inner, dynamic pruned pair-list cut-off squared   */
254
255     shift_consts_t  dispersion_shift; /**< VdW shift dispersion constants           */
256     shift_consts_t  repulsion_shift;  /**< VdW shift repulsion constants            */
257     switch_consts_t vdw_switch;       /**< VdW switch constants                     */
258
259     /* Ewald Coulomb force table data - accessed through texture memory */
260     float coulomb_tab_scale; /**< table scale/spacing                        */
261 } cl_nbparam_params_t;
262
263
264 /*! \internal
265  * \brief Pair list data.
266  */
267 using cl_plist_t = Nbnxm::gpu_plist;
268
269 /** \internal
270  * \brief Typedef of actual timer type.
271  */
272 typedef struct Nbnxm::gpu_timers_t cl_timers_t;
273
274 /*! \internal
275  * \brief Main data structure for OpenCL nonbonded force calculations.
276  */
277 struct gmx_nbnxm_gpu_t
278 {
279     const gmx_device_info_t*          dev_info;    /**< OpenCL device information    */
280     struct gmx_device_runtime_data_t* dev_rundata; /**< OpenCL runtime data (context, kernels) */
281
282     /**< Pointers to non-bonded kernel functions
283      * organized similar with nb_kfunc_xxx arrays in nbnxn_ocl.cpp */
284     ///@{
285     cl_kernel kernel_noener_noprune_ptr[eelOclNR][evdwOclNR];
286     cl_kernel kernel_ener_noprune_ptr[eelOclNR][evdwOclNR];
287     cl_kernel kernel_noener_prune_ptr[eelOclNR][evdwOclNR];
288     cl_kernel kernel_ener_prune_ptr[eelOclNR][evdwOclNR];
289     ///@}
290     cl_kernel kernel_pruneonly[ePruneNR]; /**< prune kernels, ePruneKind defined the kernel kinds */
291
292     bool bPrefetchLjParam; /**< true if prefetching fg i-atom LJ parameters should be used in the kernels */
293
294     /**< auxiliary kernels implementing memset-like functions */
295     ///@{
296     cl_kernel kernel_memset_f;
297     cl_kernel kernel_memset_f2;
298     cl_kernel kernel_memset_f3;
299     cl_kernel kernel_zero_e_fshift;
300     ///@}
301
302     cl_bool bUseTwoStreams; /**< true if doing both local/non-local NB work on GPU          */
303     cl_bool bNonLocalStreamActive; /**< true indicates that the nonlocal_done event was enqueued */
304
305     cl_atomdata_t* atdat;   /**< atom data                                                  */
306     cl_nbparam_t*  nbparam; /**< parameters required for the non-bonded calc.               */
307     gmx::EnumerationArray<Nbnxm::InteractionLocality, cl_plist_t*> plist; /**< pair-list data structures (local and non-local)            */
308     cl_nb_staging_t nbst; /**< staging area where fshift/energies get downloaded          */
309
310     gmx::EnumerationArray<Nbnxm::InteractionLocality, cl_command_queue> stream; /**< local and non-local GPU queues                             */
311
312     /** events used for synchronization */
313     cl_event nonlocal_done;               /**< event triggered when the non-local non-bonded kernel
314                                              is done (and the local transfer can proceed) */
315     cl_event misc_ops_and_local_H2D_done; /**< event triggered when the tasks issued in
316                                              the local stream that need to precede the
317                                              non-local force calculations are done
318                                              (e.g. f buffer 0-ing, local x/q H2D) */
319
320     //! True if there has been local/nonlocal GPU work, either bonded or nonbonded, scheduled
321     //  to be executed in the current domain. As long as bonded work is not split up into
322     //  local/nonlocal, if there is bonded GPU work, both flags will be true.
323     gmx::EnumerationArray<Nbnxm::InteractionLocality, bool> haveWork;
324
325
326     cl_bool      bDoTime; /**< True if event-based timing is enabled.                     */
327     cl_timers_t* timers;  /**< OpenCL event-based timers.                                 */
328     struct gmx_wallclock_gpu_nbnxn_t* timings; /**< Timing data. TODO: deprecate this and query timers for accumulated data instead */
329 };
330
331 #endif /* NBNXN_OPENCL_TYPES_H */