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