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