Fix cmake policy warning
[alexxy/gromacs.git] / src / gromacs / ewald / pme_gpu_types_host.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2018,2019, 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
36 /*! \libinternal \file
37  * \brief Defines the host-side PME GPU data structures.
38  * \todo Some renaming/refactoring, which does not impair the performance:
39  * -- bringing the function names up to guidelines
40  * -- PmeGpuSettings -> PmeGpuTasks
41  * -- refining GPU notation application (#2053)
42  * -- renaming coefficients to charges (?)
43  *
44  * \author Aleksei Iupinov <a.yupinov@gmail.com>
45  * \ingroup module_ewald
46  */
47
48 #ifndef GMX_EWALD_PME_GPU_TYPES_HOST_H
49 #define GMX_EWALD_PME_GPU_TYPES_HOST_H
50
51 #include "config.h"
52
53 #include <memory>
54 #include <vector>
55
56 #include "gromacs/ewald/pme.h"
57 #include "gromacs/ewald/pme_gpu_program.h"
58 #include "gromacs/gpu_utils/gpu_utils.h"      // for GpuApiCallBehavior
59 #include "gromacs/gpu_utils/hostallocator.h"
60 #include "gromacs/math/vectypes.h"
61
62 #if GMX_GPU != GMX_GPU_NONE
63 struct PmeGpuSpecific;
64 #else
65 /*! \brief A dummy typedef for the GPU host data placeholder on non-GPU builds */
66 typedef int PmeGpuSpecific;
67 #endif
68
69 #if GMX_GPU == GMX_GPU_CUDA
70 struct PmeGpuCudaKernelParams;
71 /*! \brief A typedef for including the GPU kernel arguments data by pointer */
72 typedef PmeGpuCudaKernelParams PmeGpuKernelParams;
73 #elif GMX_GPU == GMX_GPU_OPENCL
74 struct PmeGpuKernelParamsBase;
75 /*! \brief A typedef for including the GPU kernel arguments data by pointer */
76 typedef PmeGpuKernelParamsBase PmeGpuKernelParams;
77 #else
78 /*! \brief A dummy typedef for the GPU kernel arguments data placeholder on non-GPU builds */
79 typedef int PmeGpuKernelParams;
80 #endif
81
82 struct gmx_device_info_t;
83
84 /*! \internal \brief
85  * The PME GPU settings structure, included in the main PME GPU structure by value.
86  */
87 struct PmeGpuSettings
88 {
89     /* Permanent settings set on initialization */
90     /*! \brief A boolean which tells if the solving is performed on GPU. Currently always true */
91     bool performGPUSolve;
92     /*! \brief A boolean which tells if the gathering is performed on GPU. Currently always true */
93     bool performGPUGather;
94     /*! \brief A boolean which tells if the FFT is performed on GPU. Currently true for a single MPI rank. */
95     bool performGPUFFT;
96     /*! \brief A convenience boolean which tells if PME decomposition is used. */
97     bool useDecomposition;
98     /*! \brief A boolean which tells if any PME GPU stage should copy all of its outputs to the host.
99      * Only intended to be used by the test framework.
100      */
101     bool               copyAllOutputs;
102     /*! \brief An enum which tells whether most PME GPU D2H/H2D data transfers should be synchronous. */
103     GpuApiCallBehavior transferKind;
104     /*! \brief Various flags for the current PME computation, corresponding to the GMX_PME_ flags in pme.h. */
105     int                currentFlags;
106 };
107
108 // TODO There's little value in computing the Coulomb and LJ virial
109 // separately, so we should simplify that.
110 // TODO The matrices might be best as a view, but not currently
111 // possible. Use mdspan?
112 struct PmeOutput
113 {
114     gmx::ArrayRef<gmx::RVec> forces_;
115     real                     coulombEnergy_;
116     matrix                   coulombVirial_;
117     real                     lennardJonesEnergy_;
118     matrix                   lennardJonesVirial_;
119 };
120
121 /*! \internal \brief
122  * The PME GPU intermediate buffers structure, included in the main PME GPU structure by value.
123  * Buffers are managed by the PME GPU module.
124  */
125 struct PmeGpuStaging
126 {
127     //! Host-side force buffer
128     gmx::PaddedHostVector<gmx::RVec> h_forces;
129
130     /*! \brief Virial and energy intermediate host-side buffer. Size is PME_GPU_VIRIAL_AND_ENERGY_COUNT. */
131     float  *h_virialAndEnergy;
132     /*! \brief B-spline values intermediate host-side buffer. */
133     float  *h_splineModuli;
134
135     /*! \brief Pointer to the host memory with B-spline values. Only used for host-side gather, or unit tests */
136     float  *h_theta;
137     /*! \brief Pointer to the host memory with B-spline derivative values. Only used for host-side gather, or unit tests */
138     float  *h_dtheta;
139     /*! \brief Pointer to the host memory with ivec atom gridline indices. Only used for host-side gather, or unit tests */
140     int    *h_gridlineIndices;
141 };
142
143 /*! \internal \brief
144  * The PME GPU structure for all the data copied directly from the CPU PME structure.
145  * The copying is done when the CPU PME structure is already (re-)initialized
146  * (pme_gpu_reinit is called at the end of gmx_pme_init).
147  * All the variables here are named almost the same way as in gmx_pme_t.
148  * The types are different: pointers are replaced by vectors.
149  * TODO: use the shared data with the PME CPU.
150  * Included in the main PME GPU structure by value.
151  */
152 struct PmeShared
153 {
154     /*! \brief Grid count - currently always 1 on GPU */
155     int                    ngrids;
156     /*! \brief Grid dimensions - nkx, nky, nkz */
157     int                    nk[DIM];
158     /*! \brief PME interpolation order */
159     int                    pme_order;
160     /*! \brief Ewald splitting coefficient for Coulomb */
161     real                   ewaldcoeff_q;
162     /*! \brief Electrostatics parameter */
163     real                   epsilon_r;
164     /*! \brief Gridline indices - nnx, nny, nnz */
165     std::vector<int>       nn;
166     /*! \brief Fractional shifts - fshx, fshy, fshz */
167     std::vector<real>      fsh;
168     /*! \brief Precomputed B-spline values */
169     std::vector<real>      bsp_mod[DIM];
170     /*! \brief The PME codepath being taken */
171     PmeRunMode             runMode;
172     /*! \brief The box scaler based on inputrec - created in pme_init and managed by CPU structure */
173     class EwaldBoxZScaler *boxScaler;
174     /*! \brief The previous computation box to know if we even need to update the current box params.
175      * \todo Manage this on higher level.
176      * \todo Alternatively, when this structure is used by CPU PME code, make use of this field there as well.
177      */
178     matrix previousBox;
179 };
180
181 /*! \internal \brief
182  * The main PME GPU host structure, included in the PME CPU structure by pointer.
183  */
184 struct PmeGpu
185 {
186     /*! \brief The information copied once per reinit from the CPU structure. */
187     std::shared_ptr<PmeShared> common; // TODO: make the CPU structure use the same type
188
189     //! A handle to the program created by buildPmeGpuProgram()
190     PmeGpuProgramHandle programHandle_;
191
192     /*! \brief The settings. */
193     PmeGpuSettings settings;
194
195     /*! \brief The host-side buffers.
196      * The device-side buffers are buried in kernelParams, but that will have to change.
197      */
198     PmeGpuStaging staging;
199
200     /*! \brief Number of local atoms, padded to be divisible by c_pmeAtomDataAlignment.
201      * Used for kernel scheduling.
202      * kernelParams.atoms.nAtoms is the actual atom count to be used for data copying.
203      * TODO: this and the next member represent a memory allocation/padding properties -
204      * what a container type should do ideally.
205      */
206     int nAtomsPadded;
207     /*! \brief Number of local atoms, padded to be divisible by c_pmeAtomDataAlignment
208      * if c_usePadding is true.
209      * Used only as a basic size for almost all the atom data allocations
210      * (spline parameter data is also aligned by PME_SPREADGATHER_PARTICLES_PER_WARP).
211      * This should be the same as (c_usePadding ? nAtomsPadded : kernelParams.atoms.nAtoms).
212      * kernelParams.atoms.nAtoms is the actual atom count to be used for most data copying.
213      */
214     int nAtomsAlloc;
215
216     /*! \brief A pointer to the device used during the execution. */
217     const gmx_device_info_t *deviceInfo;
218
219     /*! \brief Kernel scheduling grid width limit in X - derived from deviceinfo compute capability in CUDA.
220      * Declared as very large int to make it useful in computations with type promotion, to avoid overflows.
221      * OpenCL seems to not have readily available global work size limit, so we just assign a large arbitrary constant to this instead.
222      * TODO: this should be in PmeGpuProgram(Impl)
223      */
224     std::intmax_t maxGridWidthX;
225
226     /*! \brief A single structure encompassing all the PME data used on GPU.
227      * Its value is the only argument to all the PME GPU kernels.
228      * \todo Test whether this should be copied to the constant GPU memory once for each computation
229      * (or even less often with no box updates) instead of being an argument.
230      */
231     std::shared_ptr<PmeGpuKernelParams> kernelParams;
232
233     /*! \brief The pointer to GPU-framework specific host-side data, such as CUDA streams and events. */
234     std::shared_ptr<PmeGpuSpecific> archSpecific; /* FIXME: make it an unique_ptr */
235 };
236
237 #endif