8a1e8afaf110a84a6b06d96328225bd5ee35ca6a
[alexxy/gromacs.git] / src / gromacs / ewald / pme_gpu_program_impl.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2018,2019,2020,2021, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35 /*! \internal \file
36  * \brief
37  * Declares PmeGpuProgramImpl, which stores PME GPU (compiled) kernel handles.
38  *
39  * \author Aleksei Iupinov <a.yupinov@gmail.com>
40  * \ingroup module_ewald
41  */
42 #ifndef GMX_EWALD_PME_PME_GPU_PROGRAM_IMPL_H
43 #define GMX_EWALD_PME_PME_GPU_PROGRAM_IMPL_H
44
45 #include "config.h"
46
47 #include <memory>
48
49 #include "gromacs/gpu_utils/device_context.h"
50 #include "gromacs/utility/classhelpers.h"
51
52 class DeviceContext;
53 struct DeviceInformation;
54
55 /*! \internal
56  * \brief
57  * PME GPU persistent host program/kernel data, which should be initialized once for the whole execution.
58  *
59  * Primary purpose of this is to not recompile GPU kernels for each OpenCL unit test,
60  * while the relevant GPU context (e.g. cl_context) instance persists.
61  * In CUDA, this just assigns the kernel function pointers.
62  * This also implicitly relies on the fact that reasonable share of the kernels are always used.
63  * If there were more template parameters, even smaller share of all possible kernels would be used.
64  *
65  * \todo In future if we would need to react to either user input or
66  * auto-tuning to compile different kernels, then we might wish to
67  * revisit the number of kernels we pre-compile, and/or the management
68  * of their lifetime.
69  *
70  * This also doesn't manage cuFFT/clFFT kernels, which depend on the PME grid dimensions.
71  *
72  * TODO: pass cl_context to the constructor and not create it inside.
73  * See also Issue #2522.
74  */
75 struct PmeGpuProgramImpl
76 {
77     /*! \brief
78      * This is a handle to the GPU context, which is just a dummy in CUDA,
79      * but is created/destroyed by this class in OpenCL.
80      */
81     const DeviceContext& deviceContext_;
82
83     //! Conveniently all the PME kernels use the same single argument type
84 #if GMX_GPU_CUDA
85     using PmeKernelHandle = void (*)(const struct PmeGpuCudaKernelParams);
86 #elif GMX_GPU_OPENCL
87     using PmeKernelHandle = cl_kernel;
88 #else
89     using PmeKernelHandle = void*;
90 #endif
91
92     /*! \brief
93      * Maximum synchronous GPU thread group execution width.
94      * "Warp" is a CUDA term which we end up reusing in OpenCL kernels as well.
95      * For CUDA, this is a static value that comes from gromacs/gpu_utils/cuda_arch_utils.cuh;
96      * for OpenCL, we have to query it dynamically.
97      */
98     size_t warpSize_;
99
100     //@{
101     /**
102      * Spread/spline kernels are compiled only for order of 4.
103      * There are multiple versions of each kernel, paramaretized according to
104      *   Number of threads per atom. Using either order(4) or order*order (16) threads per atom is
105      * supported If the spline data is written in the spline/spread kernel and loaded in the gather
106      *   or recalculated in the gather.
107      * Spreading kernels also have hardcoded X/Y indices wrapping parameters,
108      * as a placeholder for implementing 1/2D decomposition.
109      * The kernels are templated separately for spreading on one grid (one or
110      * two sets of coefficients) or on two grids (required for energy and virial
111      * calculations).
112      */
113     size_t spreadWorkGroupSize;
114
115     PmeKernelHandle splineKernelSingle;
116     PmeKernelHandle splineKernelThPerAtom4Single;
117     PmeKernelHandle spreadKernelSingle;
118     PmeKernelHandle spreadKernelThPerAtom4Single;
119     PmeKernelHandle splineAndSpreadKernelSingle;
120     PmeKernelHandle splineAndSpreadKernelThPerAtom4Single;
121     PmeKernelHandle splineAndSpreadKernelWriteSplinesSingle;
122     PmeKernelHandle splineAndSpreadKernelWriteSplinesThPerAtom4Single;
123     PmeKernelHandle splineKernelDual;
124     PmeKernelHandle splineKernelThPerAtom4Dual;
125     PmeKernelHandle spreadKernelDual;
126     PmeKernelHandle spreadKernelThPerAtom4Dual;
127     PmeKernelHandle splineAndSpreadKernelDual;
128     PmeKernelHandle splineAndSpreadKernelThPerAtom4Dual;
129     PmeKernelHandle splineAndSpreadKernelWriteSplinesDual;
130     PmeKernelHandle splineAndSpreadKernelWriteSplinesThPerAtom4Dual;
131     //@}
132
133     //@{
134     /** Same for gather: hardcoded X/Y unwrap parameters, order of 4, plus
135      * it can either reduce with previous forces in the host buffer, or ignore them.
136      * Also similarly to the gather we can use either order(4) or order*order (16) threads per atom
137      * and either recalculate the splines or read the ones written by the spread
138      * The kernels are templated separately for using one or two grids (required for
139      * calculating energies and virial).
140      */
141     size_t gatherWorkGroupSize;
142
143     PmeKernelHandle gatherKernelSingle;
144     PmeKernelHandle gatherKernelThPerAtom4Single;
145     PmeKernelHandle gatherKernelReadSplinesSingle;
146     PmeKernelHandle gatherKernelReadSplinesThPerAtom4Single;
147     PmeKernelHandle gatherKernelDual;
148     PmeKernelHandle gatherKernelThPerAtom4Dual;
149     PmeKernelHandle gatherKernelReadSplinesDual;
150     PmeKernelHandle gatherKernelReadSplinesThPerAtom4Dual;
151     //@}
152
153     //@{
154     /** Solve kernel doesn't care about the interpolation order, but can optionally
155      * compute energy and virial, and supports XYZ and YZX grid orderings.
156      * The kernels are templated separately for grids in state A and B.
157      */
158     size_t solveMaxWorkGroupSize;
159
160     PmeKernelHandle solveYZXKernelA;
161     PmeKernelHandle solveXYZKernelA;
162     PmeKernelHandle solveYZXEnergyKernelA;
163     PmeKernelHandle solveXYZEnergyKernelA;
164     PmeKernelHandle solveYZXKernelB;
165     PmeKernelHandle solveXYZKernelB;
166     PmeKernelHandle solveYZXEnergyKernelB;
167     PmeKernelHandle solveXYZEnergyKernelB;
168     //@}
169
170     PmeGpuProgramImpl() = delete;
171     //! Constructor for the given device
172     explicit PmeGpuProgramImpl(const DeviceContext& deviceContext);
173     // NOLINTNEXTLINE(performance-trivially-destructible)
174     ~PmeGpuProgramImpl();
175     GMX_DISALLOW_COPY_AND_ASSIGN(PmeGpuProgramImpl);
176
177     //! Return the warp size for which the kernels were compiled
178     int warpSize() const { return warpSize_; }
179
180 private:
181     // Compiles kernels, if supported. Called by the constructor.
182     void compileKernels(const DeviceInformation& deviceInfo);
183 };
184
185 #endif