95558805bde83a9788b6adb7d8359a6548e382ab
[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 Data structure shared between the OpenCL device code and OpenCL host code
79  *
80  * Must not contain OpenCL objects (buffers)
81  * TODO: review, improve */
82 typedef struct cl_nbparam_params
83 {
84
85     //! type of electrostatics
86     enum Nbnxm::ElecType elecType;
87     //! type of VdW impl.
88     enum Nbnxm::VdwType vdwType;
89
90     //! charge multiplication factor
91     float epsfac;
92     //! Reaction-field/plain cutoff electrostatics const.
93     float c_rf;
94     //! Reaction-field electrostatics constant
95     float two_k_rf;
96     //! Ewald/PME parameter
97     float ewald_beta;
98     //! Ewald/PME correction term subtracted from the direct-space potential
99     float sh_ewald;
100     //! LJ-Ewald/PME correction term added to the correction potential
101     float sh_lj_ewald;
102     //! LJ-Ewald/PME coefficient
103     float ewaldcoeff_lj;
104
105     //! Coulomb cut-off squared
106     float rcoulomb_sq;
107
108     //! VdW cut-off squared
109     float rvdw_sq;
110     //! VdW switched cut-off
111     float rvdw_switch;
112     //! Full, outer pair-list cut-off squared
113     float rlistOuter_sq;
114     //! Inner, dynamic pruned pair-list cut-off squared
115     float rlistInner_sq;
116
117     //! VdW shift dispersion constants
118     shift_consts_t dispersion_shift;
119     //! VdW shift repulsion constants
120     shift_consts_t repulsion_shift;
121     //! VdW switch constants
122     switch_consts_t vdw_switch;
123
124     /* Ewald Coulomb force table data - accessed through texture memory */
125     //! table scale/spacing
126     float coulomb_tab_scale;
127 } cl_nbparam_params_t;
128
129
130 /*! \internal
131  * \brief Main data structure for OpenCL nonbonded force calculations.
132  */
133 struct NbnxmGpu
134 {
135     /* \brief OpenCL device context
136      *
137      * \todo Make it constant reference, once NbnxmGpu is a proper class.
138      */
139     const DeviceContext* deviceContext_;
140     //! OpenCL runtime data (context, kernels)
141     struct gmx_device_runtime_data_t* dev_rundata = nullptr;
142
143     /**< Pointers to non-bonded kernel functions
144      * organized similar with nb_kfunc_xxx arrays in nbnxn_ocl.cpp */
145     ///@{
146     cl_kernel kernel_noener_noprune_ptr[Nbnxm::c_numElecTypes][Nbnxm::c_numVdwTypes] = { { nullptr } };
147     cl_kernel kernel_ener_noprune_ptr[Nbnxm::c_numElecTypes][Nbnxm::c_numVdwTypes] = { { nullptr } };
148     cl_kernel kernel_noener_prune_ptr[Nbnxm::c_numElecTypes][Nbnxm::c_numVdwTypes] = { { nullptr } };
149     cl_kernel kernel_ener_prune_ptr[Nbnxm::c_numElecTypes][Nbnxm::c_numVdwTypes] = { { nullptr } };
150     ///@}
151     //! prune kernels, ePruneKind defined the kernel kinds
152     cl_kernel kernel_pruneonly[ePruneNR] = { nullptr };
153
154     //! true if prefetching fg i-atom LJ parameters should be used in the kernels
155     bool bPrefetchLjParam = false;
156
157     /**< auxiliary kernels implementing memset-like functions */
158     ///@{
159     cl_kernel kernel_memset_f  = nullptr;
160     cl_kernel kernel_memset_f2 = nullptr;
161     cl_kernel kernel_memset_f3 = nullptr;
162     ///@}
163
164     //! true if doing both local/non-local NB work on GPU
165     bool bUseTwoStreams = false;
166     //! true indicates that the nonlocal_done event was marked
167     bool bNonLocalStreamDoneMarked = false;
168
169     //! atom data
170     NBAtomData* atdat = nullptr;
171     //! parameters required for the non-bonded calc.
172     NBParamGpu* nbparam = nullptr;
173     //! pair-list data structures (local and non-local)
174     gmx::EnumerationArray<Nbnxm::InteractionLocality, Nbnxm::gpu_plist*> plist = { nullptr };
175     //! staging area where fshift/energies get downloaded
176     NBStagingData nbst;
177
178     // Data for GPU-side coordinate conversion between integrator and NBNXM
179     /*! \brief array of atom indices */
180     DeviceBuffer<int> atomIndices;
181     /*! \brief size of atom indices */
182     int atomIndicesSize = 0;
183     /*! \brief size of atom indices allocated in device buffer */
184     int atomIndicesSize_alloc = 0;
185     /*! \brief x buf ops num of atoms */
186     DeviceBuffer<int> cxy_na;
187     /*! \brief number of elements in cxy_na */
188     int ncxy_na = 0;
189     /*! \brief number of elements allocated allocated in device buffer */
190     int ncxy_na_alloc = 0;
191     /*! \brief x buf ops cell index mapping */
192     DeviceBuffer<int> cxy_ind;
193     /*! \brief number of elements in cxy_ind */
194     int ncxy_ind = 0;
195     /*! \brief number of elements allocated allocated in device buffer */
196     int ncxy_ind_alloc = 0;
197
198     //! local and non-local GPU queues
199     gmx::EnumerationArray<Nbnxm::InteractionLocality, const DeviceStream*> deviceStreams;
200
201     /*! \brief Events used for synchronization */
202     /*! \{ */
203     /*! \brief Event triggered when the non-local non-bonded
204      * kernel is done (and the local transfer can proceed) */
205     GpuEventSynchronizer nonlocal_done;
206     /*! \brief Event triggered when the tasks issued in the local
207      * stream that need to precede the non-local force or buffer
208      * operation calculations are done (e.g. f buffer 0-ing, local
209      * x/q H2D, buffer op initialization in local stream that is
210      * required also by nonlocal stream ) */
211     GpuEventSynchronizer misc_ops_and_local_H2D_done;
212     /*! \} */
213
214     //! True if there has been local/nonlocal GPU work, either bonded or nonbonded, scheduled
215     //  to be executed in the current domain. As long as bonded work is not split up into
216     //  local/nonlocal, if there is bonded GPU work, both flags will be true.
217     gmx::EnumerationArray<Nbnxm::InteractionLocality, bool> haveWork;
218
219
220     //! True if event-based timing is enabled.
221     bool bDoTime = false;
222     //! OpenCL event-based timers.
223     Nbnxm::GpuTimers* timers = nullptr;
224     //! Timing data. TODO: deprecate this and query timers for accumulated data instead
225     gmx_wallclock_gpu_nbnxn_t* timings = nullptr;
226 };
227
228 #endif /* NBNXN_OPENCL_TYPES_H */