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/pairlist.h"
57 #include "gromacs/utility/enumerationhelpers.h"
58 #include "gromacs/utility/fatalerror.h"
59 #include "gromacs/utility/real.h"
61 #include "nbnxm_ocl_consts.h"
63 /* kernel does #include "gromacs/math/utilities.h" */
64 /* Move the actual useful stuff here: */
67 #define M_FLOAT_1_SQRTPI 0.564189583547756f
70 /*! \brief Constants for platform-dependent defaults for the prune kernel's j4 processing concurrency.
72 * Initialized using macros that can be overridden at compile-time (using #GMX_NBNXN_PRUNE_KERNEL_J4_CONCURRENCY).
75 const int c_oclPruneKernelJ4ConcurrencyDEFAULT = GMX_NBNXN_PRUNE_KERNEL_J4_CONCURRENCY_DEFAULT;
78 /*! \brief Returns the j4 processing concurrency parameter for the vendor \p vendorId
79 * \param vendorId takes values from #ocl_vendor_id_t.
81 static inline int getOclPruneKernelJ4Concurrency(int vendorId)
85 default: return c_oclPruneKernelJ4ConcurrencyDEFAULT;
90 /*! \brief Electrostatic OpenCL kernel flavors.
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.
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.
109 eelOclEWALD_TAB_TWIN,
111 eelOclEWALD_ANA_TWIN,
115 /*! \brief VdW OpenCL kernel flavors.
117 * The enumerates values correspond to the LJ implementations in the OpenCL non-bonded
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.
136 /*! \brief Pruning kernel flavors.
138 * The values correspond to the first call of the pruning post-list generation
139 * and the rolling pruning, respectively.
149 * \brief Staging area for temporary data downloaded from the GPU.
151 * The energies/shift forces get downloaded here first, before getting added
152 * to the CPU-side aggregate values.
154 typedef struct cl_nb_staging
158 //! electrostatic energy
160 //! float3 buffer with shift forces
165 * \brief Nonbonded atom data - both inputs and outputs.
167 typedef struct cl_atomdata
171 //! number of local atoms
173 //! allocation size for the atom data (xq, f)
176 //! float4 buffer with atom coordinates + charges, size natoms
179 //! float3 buffer with force output array, size natoms
181 //! Size in bytes for one element of f buffer
184 //! LJ energy output, size 1
186 //! Electrostatics energy input, size 1
189 //! float3 buffer with shift forces
191 //! Size in bytes for one element of fshift buffer
192 size_t fshift_elem_size;
194 //! number of atom types
196 //! int buffer with atom type indices, size natoms
198 //! float2 buffer with sqrt(c6),sqrt(c12), size natoms
201 //! float3 buffer with shifts values
203 //! Size in bytes for one element of shift_vec buffer
204 size_t shift_vec_elem_size;
206 //! true if the shift vector has been uploaded
207 cl_bool bShiftVecUploaded;
211 * \brief Parameters required for the OpenCL nonbonded calculations.
213 typedef struct cl_nbparam
216 //! type of electrostatics, takes values from #eelOcl
218 //! type of VdW impl., takes values from #evdwOcl
221 //! charge multiplication factor
223 //! Reaction-field/plain cutoff electrostatics const.
225 //! Reaction-field electrostatics constant
227 //! Ewald/PME parameter
229 //! Ewald/PME correction term substracted from the direct-space potential
231 //! LJ-Ewald/PME correction term added to the correction potential
233 //! LJ-Ewald/PME coefficient
236 //! Coulomb cut-off squared
239 //! VdW cut-off squared
241 //! VdW switched cut-off
243 //! Full, outer pair-list cut-off squared
245 //! Inner, dynamic pruned pair-list cut-off squared
247 //! True if we use dynamic pair-list pruning
248 bool useDynamicPruning;
250 //! VdW shift dispersion constants
251 shift_consts_t dispersion_shift;
252 //! VdW shift repulsion constants
253 shift_consts_t repulsion_shift;
254 //! VdW switch constants
255 switch_consts_t vdw_switch;
257 /* LJ non-bonded parameters - accessed through texture memory */
258 //! nonbonded parameter table with C6/C12 pairs per atom type-pair, 2*ntype^2 elements
260 //! nonbonded parameter table per atom type, 2*ntype elements
261 cl_mem nbfp_comb_climg2d;
263 /* Ewald Coulomb force table data - accessed through texture memory */
264 //! table scale/spacing
265 float coulomb_tab_scale;
266 //! pointer to the table in the device memory
267 cl_mem coulomb_tab_climg2d;
271 * \brief Data structure shared between the OpenCL device code and OpenCL host code
273 * Must not contain OpenCL objects (buffers)
274 * TODO: review, improve */
275 typedef struct cl_nbparam_params
278 //! type of electrostatics, takes values from #eelCu
280 //! type of VdW impl., takes values from #evdwCu
283 //! charge multiplication factor
285 //! Reaction-field/plain cutoff electrostatics const.
287 //! Reaction-field electrostatics constant
289 //! Ewald/PME parameter
291 //! Ewald/PME correction term substracted from the direct-space potential
293 //! LJ-Ewald/PME correction term added to the correction potential
295 //! LJ-Ewald/PME coefficient
298 //! Coulomb cut-off squared
301 //! VdW cut-off squared
303 //! VdW switched cut-off
305 //! Full, outer pair-list cut-off squared
307 //! Inner, dynamic pruned pair-list cut-off squared
310 //! VdW shift dispersion constants
311 shift_consts_t dispersion_shift;
312 //! VdW shift repulsion constants
313 shift_consts_t repulsion_shift;
314 //! VdW switch constants
315 switch_consts_t vdw_switch;
317 /* Ewald Coulomb force table data - accessed through texture memory */
318 //! table scale/spacing
319 float coulomb_tab_scale;
320 } cl_nbparam_params_t;
324 * \brief Pair list data.
326 using cl_plist_t = Nbnxm::gpu_plist;
329 * \brief Typedef of actual timer type.
331 typedef struct Nbnxm::gpu_timers_t cl_timers_t;
334 * \brief Main data structure for OpenCL nonbonded force calculations.
336 struct gmx_nbnxm_gpu_t
338 //! OpenCL device information
339 const gmx_device_info_t* dev_info;
340 //! OpenCL runtime data (context, kernels)
341 struct gmx_device_runtime_data_t* dev_rundata;
343 /**< Pointers to non-bonded kernel functions
344 * organized similar with nb_kfunc_xxx arrays in nbnxn_ocl.cpp */
346 cl_kernel kernel_noener_noprune_ptr[eelOclNR][evdwOclNR];
347 cl_kernel kernel_ener_noprune_ptr[eelOclNR][evdwOclNR];
348 cl_kernel kernel_noener_prune_ptr[eelOclNR][evdwOclNR];
349 cl_kernel kernel_ener_prune_ptr[eelOclNR][evdwOclNR];
351 //! prune kernels, ePruneKind defined the kernel kinds
352 cl_kernel kernel_pruneonly[ePruneNR];
354 //! true if prefetching fg i-atom LJ parameters should be used in the kernels
355 bool bPrefetchLjParam;
357 /**< auxiliary kernels implementing memset-like functions */
359 cl_kernel kernel_memset_f;
360 cl_kernel kernel_memset_f2;
361 cl_kernel kernel_memset_f3;
362 cl_kernel kernel_zero_e_fshift;
365 //! true if doing both local/non-local NB work on GPU
366 cl_bool bUseTwoStreams;
367 //! true indicates that the nonlocal_done event was enqueued
368 cl_bool bNonLocalStreamActive;
371 cl_atomdata_t* atdat;
372 //! parameters required for the non-bonded calc.
373 cl_nbparam_t* nbparam;
374 //! pair-list data structures (local and non-local)
375 gmx::EnumerationArray<Nbnxm::InteractionLocality, cl_plist_t*> plist;
376 //! staging area where fshift/energies get downloaded
377 cl_nb_staging_t nbst;
379 //! local and non-local GPU queues
380 gmx::EnumerationArray<Nbnxm::InteractionLocality, cl_command_queue> stream;
382 /*! \brief Events used for synchronization */
384 /*! \brief Event triggered when the non-local non-bonded
385 * kernel is done (and the local transfer can proceed) */
386 cl_event nonlocal_done;
387 /*! \brief Event triggered when the tasks issued in the local
388 * stream that need to precede the non-local force or buffer
389 * operation calculations are done (e.g. f buffer 0-ing, local
390 * x/q H2D, buffer op initialization in local stream that is
391 * required also by nonlocal stream ) */
392 cl_event misc_ops_and_local_H2D_done;
395 //! True if there has been local/nonlocal GPU work, either bonded or nonbonded, scheduled
396 // to be executed in the current domain. As long as bonded work is not split up into
397 // local/nonlocal, if there is bonded GPU work, both flags will be true.
398 gmx::EnumerationArray<Nbnxm::InteractionLocality, bool> haveWork;
401 //! True if event-based timing is enabled.
403 //! OpenCL event-based timers.
405 //! Timing data. TODO: deprecate this and query timers for accumulated data instead
406 struct gmx_wallclock_gpu_nbnxn_t* timings;
409 #endif /* NBNXN_OPENCL_TYPES_H */