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