ee551d152d15c8626b94545098e632cc994d07d8
[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, 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 "gromacs/utility/classhelpers.h"
48
49 #if GMX_GPU == GMX_GPU_CUDA
50 // TODO uncomment when we learn to compile .cpp with CUDA compiler
51 //! include "gromacs/gpu_utils/gputraits.cuh"
52 using Context = void *;
53 #elif GMX_GPU == GMX_GPU_OPENCL
54 #include "gromacs/gpu_utils/gputraits_ocl.h"
55 #elif GMX_GPU == GMX_GPU_NONE
56 // TODO place in gputraits_stub.h
57 using Context = void *;
58 #endif
59
60 struct gmx_device_info_t;
61
62 /*! \internal
63  * \brief
64  * PME GPU persistent host program/kernel data, which should be initialized once for the whole execution.
65  *
66  * Primary purpose of this is to not recompile GPU kernels for each OpenCL unit test,
67  * while the relevant GPU context (e.g. cl_context) instance persists.
68  * In CUDA, this just assigns the kernel function pointers.
69  * This also implicitly relies on the fact that reasonable share of the kernels are always used.
70  * If there were more template parameters, even smaller share of all possible kernels would be used.
71  *
72  * \todo In future if we would need to react to either user input or
73  * auto-tuning to compile different kernels, then we might wish to
74  * revisit the number of kernels we pre-compile, and/or the management
75  * of their lifetime.
76  *
77  * This also doesn't manage cuFFT/clFFT kernels, which depend on the PME grid dimensions.
78  *
79  * TODO: pass cl_context to the constructor and not create it inside.
80  * See also Redmine #2522.
81  */
82 struct PmeGpuProgramImpl
83 {
84     /*! \brief
85      * This is a handle to the GPU context, which is just a dummy in CUDA,
86      * but is created/destroyed by this class in OpenCL.
87      * TODO: Later we want to be able to own the context at a higher level and not here,
88      * but this class would still need the non-owning context handle to build the kernels.
89      */
90     Context context;
91
92     //! Conveniently all the PME kernels use the same single argument type
93 #if GMX_GPU == GMX_GPU_CUDA
94     using PmeKernelHandle = void(*)(const struct PmeGpuCudaKernelParams);
95 #elif GMX_GPU == GMX_GPU_OPENCL
96     using PmeKernelHandle = cl_kernel;
97 #else
98     using PmeKernelHandle = void *;
99 #endif
100
101     //@{
102     /**
103      * Spread/spline kernels are compiled only for order of 4.
104      * Spreading kernels also have hardcoded X/Y indices wrapping parameters,
105      * as a placeholder for implementing 1/2D decomposition.
106      */
107     PmeKernelHandle splineKernel;
108     PmeKernelHandle spreadKernel;
109     PmeKernelHandle splineAndSpreadKernel;
110     //@}
111
112     //@{
113     /** Same for gather: hardcoded X/Y unwrap parameters, order of 4, plus
114      * it can either reduce with previous forces in the host buffer, or ignore them.
115      */
116     PmeKernelHandle gatherReduceWithInputKernel;
117     PmeKernelHandle gatherKernel;
118     //@}
119
120     //@{
121     /** Solve kernel doesn't care about the interpolation order, but can optionally
122      * compute energy and virial, and supports XYZ and YZX grid orderings.
123      */
124     PmeKernelHandle solveYZXKernel;
125     PmeKernelHandle solveXYZKernel;
126     PmeKernelHandle solveYZXEnergyKernel;
127     PmeKernelHandle solveXYZEnergyKernel;
128     //@}
129
130     PmeGpuProgramImpl() = delete;
131     //! Constructor for the given device
132     explicit PmeGpuProgramImpl(const gmx_device_info_t *deviceInfo);
133     ~PmeGpuProgramImpl();
134     GMX_DISALLOW_COPY_AND_ASSIGN(PmeGpuProgramImpl);
135 };
136
137 #endif