Add FloatN aliases to OpenCL and use them in NBNXM
[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,2021, 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/gpueventsynchronizer_ocl.h"
52 #include "gromacs/gpu_utils/gputraits_ocl.h"
53 #include "gromacs/gpu_utils/oclutils.h"
54 #include "gromacs/mdtypes/interaction_const.h"
55 #include "gromacs/nbnxm/gpu_types_common.h"
56 #include "gromacs/nbnxm/nbnxm.h"
57 #include "gromacs/nbnxm/nbnxm_gpu.h"
58 #include "gromacs/nbnxm/pairlist.h"
59 #include "gromacs/utility/enumerationhelpers.h"
60 #include "gromacs/utility/fatalerror.h"
61 #include "gromacs/utility/real.h"
62
63 struct gmx_wallclock_gpu_nbnxn_t;
64
65 /*! \brief Pruning kernel flavors.
66  *
67  * The values correspond to the first call of the pruning post-list generation
68  * and the rolling pruning, respectively.
69  */
70 enum ePruneKind
71 {
72     epruneFirst,
73     epruneRolling,
74     ePruneNR
75 };
76
77 /*! \internal
78  * \brief Staging area for temporary data downloaded from the GPU.
79  *
80  *  The energies/shift forces get downloaded here first, before getting added
81  *  to the CPU-side aggregate values.
82  */
83 struct nb_staging_t
84 {
85     //! LJ energy
86     float* e_lj = nullptr;
87     //! electrostatic energy
88     float* e_el = nullptr;
89     //! float3 buffer with shift forces
90     Float3* fshift = nullptr;
91 };
92
93 /*! \internal
94  * \brief Nonbonded atom data - both inputs and outputs.
95  */
96 typedef struct cl_atomdata
97 {
98     //! number of atoms
99     int natoms;
100     //! number of local atoms
101     int natoms_local;
102     //! allocation size for the atom data (xq, f)
103     int nalloc;
104
105     //! float4 buffer with atom coordinates + charges, size natoms
106     DeviceBuffer<Float4> xq;
107
108     //! float3 buffer with force output array, size natoms
109     DeviceBuffer<Float3> f;
110
111     //! LJ energy output, size 1
112     DeviceBuffer<float> e_lj;
113     //! Electrostatics energy input, size 1
114     DeviceBuffer<float> e_el;
115
116     //! float3 buffer with shift forces
117     DeviceBuffer<Float3> fshift;
118
119     //! number of atom types
120     int ntypes;
121     //! int buffer with atom type indices, size natoms
122     DeviceBuffer<int> atom_types;
123     //! float2 buffer with sqrt(c6),sqrt(c12), size natoms
124     DeviceBuffer<Float2> lj_comb;
125
126     //! float3 buffer with shifts values
127     DeviceBuffer<Float3> shift_vec;
128
129     //! true if the shift vector has been uploaded
130     bool bShiftVecUploaded;
131 } cl_atomdata_t;
132
133 /*! \internal
134  * \brief Data structure shared between the OpenCL device code and OpenCL host code
135  *
136  * Must not contain OpenCL objects (buffers)
137  * TODO: review, improve */
138 typedef struct cl_nbparam_params
139 {
140
141     //! type of electrostatics
142     enum Nbnxm::ElecType elecType;
143     //! type of VdW impl.
144     enum Nbnxm::VdwType vdwType;
145
146     //! charge multiplication factor
147     float epsfac;
148     //! Reaction-field/plain cutoff electrostatics const.
149     float c_rf;
150     //! Reaction-field electrostatics constant
151     float two_k_rf;
152     //! Ewald/PME parameter
153     float ewald_beta;
154     //! Ewald/PME correction term subtracted from the direct-space potential
155     float sh_ewald;
156     //! LJ-Ewald/PME correction term added to the correction potential
157     float sh_lj_ewald;
158     //! LJ-Ewald/PME coefficient
159     float ewaldcoeff_lj;
160
161     //! Coulomb cut-off squared
162     float rcoulomb_sq;
163
164     //! VdW cut-off squared
165     float rvdw_sq;
166     //! VdW switched cut-off
167     float rvdw_switch;
168     //! Full, outer pair-list cut-off squared
169     float rlistOuter_sq;
170     //! Inner, dynamic pruned pair-list cut-off squared
171     float rlistInner_sq;
172
173     //! VdW shift dispersion constants
174     shift_consts_t dispersion_shift;
175     //! VdW shift repulsion constants
176     shift_consts_t repulsion_shift;
177     //! VdW switch constants
178     switch_consts_t vdw_switch;
179
180     /* Ewald Coulomb force table data - accessed through texture memory */
181     //! table scale/spacing
182     float coulomb_tab_scale;
183 } cl_nbparam_params_t;
184
185
186 /** \internal
187  * \brief Typedef of actual timer type.
188  */
189 typedef struct Nbnxm::gpu_timers_t cl_timers_t;
190
191 /*! \internal
192  * \brief Main data structure for OpenCL nonbonded force calculations.
193  */
194 struct NbnxmGpu
195 {
196     /* \brief OpenCL device context
197      *
198      * \todo Make it constant reference, once NbnxmGpu is a proper class.
199      */
200     const DeviceContext* deviceContext_;
201     //! OpenCL runtime data (context, kernels)
202     struct gmx_device_runtime_data_t* dev_rundata = nullptr;
203
204     /**< Pointers to non-bonded kernel functions
205      * organized similar with nb_kfunc_xxx arrays in nbnxn_ocl.cpp */
206     ///@{
207     cl_kernel kernel_noener_noprune_ptr[Nbnxm::c_numElecTypes][Nbnxm::c_numVdwTypes] = { { nullptr } };
208     cl_kernel kernel_ener_noprune_ptr[Nbnxm::c_numElecTypes][Nbnxm::c_numVdwTypes] = { { nullptr } };
209     cl_kernel kernel_noener_prune_ptr[Nbnxm::c_numElecTypes][Nbnxm::c_numVdwTypes] = { { nullptr } };
210     cl_kernel kernel_ener_prune_ptr[Nbnxm::c_numElecTypes][Nbnxm::c_numVdwTypes] = { { nullptr } };
211     ///@}
212     //! prune kernels, ePruneKind defined the kernel kinds
213     cl_kernel kernel_pruneonly[ePruneNR] = { nullptr };
214
215     //! true if prefetching fg i-atom LJ parameters should be used in the kernels
216     bool bPrefetchLjParam = false;
217
218     /**< auxiliary kernels implementing memset-like functions */
219     ///@{
220     cl_kernel kernel_memset_f      = nullptr;
221     cl_kernel kernel_memset_f2     = nullptr;
222     cl_kernel kernel_memset_f3     = nullptr;
223     cl_kernel kernel_zero_e_fshift = nullptr;
224     ///@}
225
226     //! true if doing both local/non-local NB work on GPU
227     bool bUseTwoStreams = false;
228     //! true indicates that the nonlocal_done event was marked
229     bool bNonLocalStreamDoneMarked = false;
230
231     //! atom data
232     cl_atomdata_t* atdat = nullptr;
233     //! parameters required for the non-bonded calc.
234     NBParamGpu* nbparam = nullptr;
235     //! pair-list data structures (local and non-local)
236     gmx::EnumerationArray<Nbnxm::InteractionLocality, Nbnxm::gpu_plist*> plist = { nullptr };
237     //! staging area where fshift/energies get downloaded
238     nb_staging_t nbst;
239
240     //! local and non-local GPU queues
241     gmx::EnumerationArray<Nbnxm::InteractionLocality, const DeviceStream*> deviceStreams;
242
243     /*! \brief Events used for synchronization */
244     /*! \{ */
245     /*! \brief Event triggered when the non-local non-bonded
246      * kernel is done (and the local transfer can proceed) */
247     GpuEventSynchronizer nonlocal_done;
248     /*! \brief Event triggered when the tasks issued in the local
249      * stream that need to precede the non-local force or buffer
250      * operation calculations are done (e.g. f buffer 0-ing, local
251      * x/q H2D, buffer op initialization in local stream that is
252      * required also by nonlocal stream ) */
253     GpuEventSynchronizer misc_ops_and_local_H2D_done;
254     /*! \} */
255
256     //! True if there has been local/nonlocal GPU work, either bonded or nonbonded, scheduled
257     //  to be executed in the current domain. As long as bonded work is not split up into
258     //  local/nonlocal, if there is bonded GPU work, both flags will be true.
259     gmx::EnumerationArray<Nbnxm::InteractionLocality, bool> haveWork;
260
261
262     //! True if event-based timing is enabled.
263     bool bDoTime = false;
264     //! OpenCL event-based timers.
265     cl_timers_t* timers = nullptr;
266     //! Timing data. TODO: deprecate this and query timers for accumulated data instead
267     gmx_wallclock_gpu_nbnxn_t* timings = nullptr;
268 };
269
270 #endif /* NBNXN_OPENCL_TYPES_H */