2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
39 * Data types used internally in the nbnxm_ocl module.
41 * \author Anca Hamuraru <anca@streamcomputing.eu>
42 * \author Szilárd Páll <pszilard@kth.se>
43 * \ingroup module_nbnxm
46 #ifndef GMX_NBNXM_NBNXM_OPENCL_TYPES_H
47 #define GMX_NBNXM_NBNXM_OPENCL_TYPES_H
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/nbnxm_gpu.h"
57 #include "gromacs/nbnxm/pairlist.h"
58 #include "gromacs/utility/enumerationhelpers.h"
59 #include "gromacs/utility/fatalerror.h"
60 #include "gromacs/utility/real.h"
62 #include "nbnxm_ocl_consts.h"
64 struct gmx_wallclock_gpu_nbnxn_t;
66 /* kernel does #include "gromacs/math/utilities.h" */
67 /* Move the actual useful stuff here: */
70 #define M_FLOAT_1_SQRTPI 0.564189583547756f
73 /*! \brief Constants for platform-dependent defaults for the prune kernel's j4 processing concurrency.
75 * Initialized using macros that can be overridden at compile-time (using #GMX_NBNXN_PRUNE_KERNEL_J4_CONCURRENCY).
78 const int c_oclPruneKernelJ4ConcurrencyDEFAULT = GMX_NBNXN_PRUNE_KERNEL_J4_CONCURRENCY_DEFAULT;
81 /*! \brief Pruning kernel flavors.
83 * The values correspond to the first call of the pruning post-list generation
84 * and the rolling pruning, respectively.
94 * \brief Staging area for temporary data downloaded from the GPU.
96 * The energies/shift forces get downloaded here first, before getting added
97 * to the CPU-side aggregate values.
102 float* e_lj = nullptr;
103 //! electrostatic energy
104 float* e_el = nullptr;
105 //! float3 buffer with shift forces
106 float (*fshift)[3] = nullptr;
110 * \brief Nonbonded atom data - both inputs and outputs.
112 typedef struct cl_atomdata
116 //! number of local atoms
118 //! allocation size for the atom data (xq, f)
121 //! float4 buffer with atom coordinates + charges, size natoms
122 DeviceBuffer<float> xq;
124 //! float3 buffer with force output array, size natoms
125 DeviceBuffer<float> f;
127 //! LJ energy output, size 1
128 DeviceBuffer<float> e_lj;
129 //! Electrostatics energy input, size 1
130 DeviceBuffer<float> e_el;
132 //! float3 buffer with shift forces
133 DeviceBuffer<float> fshift;
135 //! number of atom types
137 //! int buffer with atom type indices, size natoms
138 DeviceBuffer<int> atom_types;
139 //! float2 buffer with sqrt(c6),sqrt(c12), size natoms
140 DeviceBuffer<float> lj_comb;
142 //! float3 buffer with shifts values
143 DeviceBuffer<float> shift_vec;
145 //! true if the shift vector has been uploaded
146 bool bShiftVecUploaded;
150 * \brief Parameters required for the OpenCL nonbonded calculations.
152 typedef struct cl_nbparam
155 //! type of electrostatics, takes values from #eelType
157 //! type of VdW impl., takes values from #evdwType
160 //! charge multiplication factor
162 //! Reaction-field/plain cutoff electrostatics const.
164 //! Reaction-field electrostatics constant
166 //! Ewald/PME parameter
168 //! Ewald/PME correction term substracted from the direct-space potential
170 //! LJ-Ewald/PME correction term added to the correction potential
172 //! LJ-Ewald/PME coefficient
175 //! Coulomb cut-off squared
178 //! VdW cut-off squared
180 //! VdW switched cut-off
182 //! Full, outer pair-list cut-off squared
184 //! Inner, dynamic pruned pair-list cut-off squared
186 //! True if we use dynamic pair-list pruning
187 bool useDynamicPruning;
189 //! VdW shift dispersion constants
190 shift_consts_t dispersion_shift;
191 //! VdW shift repulsion constants
192 shift_consts_t repulsion_shift;
193 //! VdW switch constants
194 switch_consts_t vdw_switch;
196 /* LJ non-bonded parameters - accessed through texture memory */
197 //! nonbonded parameter table with C6/C12 pairs per atom type-pair, 2*ntype^2 elements
199 //! nonbonded parameter table per atom type, 2*ntype elements
200 cl_mem nbfp_comb_climg2d;
202 /* Ewald Coulomb force table data - accessed through texture memory */
203 //! table scale/spacing
204 float coulomb_tab_scale;
205 //! pointer to the table in the device memory
206 DeviceBuffer<float> coulomb_tab_climg2d;
210 * \brief Data structure shared between the OpenCL device code and OpenCL host code
212 * Must not contain OpenCL objects (buffers)
213 * TODO: review, improve */
214 typedef struct cl_nbparam_params
217 //! type of electrostatics, takes values from #eelType
219 //! type of VdW impl., takes values from #evdwType
222 //! charge multiplication factor
224 //! Reaction-field/plain cutoff electrostatics const.
226 //! Reaction-field electrostatics constant
228 //! Ewald/PME parameter
230 //! Ewald/PME correction term substracted from the direct-space potential
232 //! LJ-Ewald/PME correction term added to the correction potential
234 //! LJ-Ewald/PME coefficient
237 //! Coulomb cut-off squared
240 //! VdW cut-off squared
242 //! VdW switched cut-off
244 //! Full, outer pair-list cut-off squared
246 //! Inner, dynamic pruned pair-list cut-off squared
249 //! VdW shift dispersion constants
250 shift_consts_t dispersion_shift;
251 //! VdW shift repulsion constants
252 shift_consts_t repulsion_shift;
253 //! VdW switch constants
254 switch_consts_t vdw_switch;
256 /* Ewald Coulomb force table data - accessed through texture memory */
257 //! table scale/spacing
258 float coulomb_tab_scale;
259 } cl_nbparam_params_t;
263 * \brief Pair list data.
265 using cl_plist_t = Nbnxm::gpu_plist;
268 * \brief Typedef of actual timer type.
270 typedef struct Nbnxm::gpu_timers_t cl_timers_t;
273 * \brief Main data structure for OpenCL nonbonded force calculations.
277 /* \brief OpenCL device context
279 * \todo Make it constant reference, once NbnxmGpu is a proper class.
281 const DeviceContext* deviceContext_;
282 //! OpenCL runtime data (context, kernels)
283 struct gmx_device_runtime_data_t* dev_rundata = nullptr;
285 /**< Pointers to non-bonded kernel functions
286 * organized similar with nb_kfunc_xxx arrays in nbnxn_ocl.cpp */
288 cl_kernel kernel_noener_noprune_ptr[eelTypeNR][evdwTypeNR] = { { nullptr } };
289 cl_kernel kernel_ener_noprune_ptr[eelTypeNR][evdwTypeNR] = { { nullptr } };
290 cl_kernel kernel_noener_prune_ptr[eelTypeNR][evdwTypeNR] = { { nullptr } };
291 cl_kernel kernel_ener_prune_ptr[eelTypeNR][evdwTypeNR] = { { nullptr } };
293 //! prune kernels, ePruneKind defined the kernel kinds
294 cl_kernel kernel_pruneonly[ePruneNR] = { nullptr };
296 //! true if prefetching fg i-atom LJ parameters should be used in the kernels
297 bool bPrefetchLjParam = false;
299 /**< auxiliary kernels implementing memset-like functions */
301 cl_kernel kernel_memset_f = nullptr;
302 cl_kernel kernel_memset_f2 = nullptr;
303 cl_kernel kernel_memset_f3 = nullptr;
304 cl_kernel kernel_zero_e_fshift = nullptr;
307 //! true if doing both local/non-local NB work on GPU
308 bool bUseTwoStreams = false;
309 //! true indicates that the nonlocal_done event was enqueued
310 bool bNonLocalStreamActive = false;
313 cl_atomdata_t* atdat = nullptr;
314 //! parameters required for the non-bonded calc.
315 cl_nbparam_t* nbparam = nullptr;
316 //! pair-list data structures (local and non-local)
317 gmx::EnumerationArray<Nbnxm::InteractionLocality, cl_plist_t*> plist = { nullptr };
318 //! staging area where fshift/energies get downloaded
321 //! local and non-local GPU queues
322 gmx::EnumerationArray<Nbnxm::InteractionLocality, const DeviceStream*> deviceStreams;
324 /*! \brief Events used for synchronization */
326 /*! \brief Event triggered when the non-local non-bonded
327 * kernel is done (and the local transfer can proceed) */
328 cl_event nonlocal_done = nullptr;
329 /*! \brief Event triggered when the tasks issued in the local
330 * stream that need to precede the non-local force or buffer
331 * operation calculations are done (e.g. f buffer 0-ing, local
332 * x/q H2D, buffer op initialization in local stream that is
333 * required also by nonlocal stream ) */
334 cl_event misc_ops_and_local_H2D_done = nullptr;
337 //! True if there has been local/nonlocal GPU work, either bonded or nonbonded, scheduled
338 // to be executed in the current domain. As long as bonded work is not split up into
339 // local/nonlocal, if there is bonded GPU work, both flags will be true.
340 gmx::EnumerationArray<Nbnxm::InteractionLocality, bool> haveWork;
343 //! True if event-based timing is enabled.
344 bool bDoTime = false;
345 //! OpenCL event-based timers.
346 cl_timers_t* timers = nullptr;
347 //! Timing data. TODO: deprecate this and query timers for accumulated data instead
348 gmx_wallclock_gpu_nbnxn_t* timings = nullptr;
351 #endif /* NBNXN_OPENCL_TYPES_H */