Avoid allocating SYCL buffer on each call to PME solve
[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,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
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/clfftinitializer.h"
59 #include "gromacs/gpu_utils/hostallocator.h"
60 #include "gromacs/math/vectypes.h"
61
62 #include "pme_gpu_settings.h"
63 #include "pme_gpu_staging.h"
64
65 #if GMX_GPU
66 struct PmeGpuSpecific;
67 #else
68 /*! \brief A dummy typedef for the GPU host data placeholder on non-GPU builds */
69 typedef int PmeGpuSpecific;
70 #endif
71
72 #if GMX_GPU_CUDA
73 struct PmeGpuCudaKernelParams;
74 /*! \brief A typedef for including the GPU kernel arguments data by pointer */
75 typedef PmeGpuCudaKernelParams PmeGpuKernelParams;
76 #elif GMX_GPU_OPENCL || GMX_GPU_SYCL
77 struct PmeGpuKernelParamsBase;
78 /*! \brief A typedef for including the GPU kernel arguments data by pointer */
79 typedef PmeGpuKernelParamsBase PmeGpuKernelParams;
80 #else
81 /*! \brief A dummy typedef for the GPU kernel arguments data placeholder on non-GPU builds */
82 typedef int PmeGpuKernelParams;
83 #endif
84
85 struct DeviceInformation;
86
87 /*! \internal \brief
88  * The PME GPU structure for all the data copied directly from the CPU PME structure.
89  * The copying is done when the CPU PME structure is already (re-)initialized
90  * (pme_gpu_reinit is called at the end of gmx_pme_init).
91  * All the variables here are named almost the same way as in gmx_pme_t.
92  * The types are different: pointers are replaced by vectors.
93  * TODO: use the shared data with the PME CPU.
94  * Included in the main PME GPU structure by value.
95  */
96 struct PmeShared
97 {
98     /*! \brief Grid count */
99     int ngrids;
100     /*! \brief Grid dimensions - nkx, nky, nkz */
101     int nk[DIM];
102     /*! \brief PME interpolation order */
103     int pme_order;
104     /*! \brief Ewald splitting coefficient for Coulomb */
105     real ewaldcoeff_q;
106     /*! \brief Electrostatics parameter */
107     real epsilon_r;
108     /*! \brief Gridline indices - nnx, nny, nnz */
109     std::vector<int> nn;
110     /*! \brief Fractional shifts - fshx, fshy, fshz */
111     std::vector<real> fsh;
112     /*! \brief Precomputed B-spline values */
113     std::vector<real> bsp_mod[DIM];
114     /*! \brief The PME codepath being taken */
115     PmeRunMode runMode;
116     /*! \brief  Whether PME execution is happening on a PME-only rank (from gmx_pme_t.bPPnode). */
117     bool isRankPmeOnly;
118     /*! \brief The box scaler based on inputrec - created in pme_init and managed by CPU structure */
119     class EwaldBoxZScaler* boxScaler;
120     /*! \brief The previous computation box to know if we even need to update the current box params.
121      * \todo Manage this on higher level.
122      * \todo Alternatively, when this structure is used by CPU PME code, make use of this field there as well.
123      */
124     matrix previousBox;
125 };
126
127 /*! \internal \brief
128  * The main PME GPU host structure, included in the PME CPU structure by pointer.
129  */
130 struct PmeGpu
131 {
132     /*! \brief The information copied once per reinit from the CPU structure. */
133     std::shared_ptr<PmeShared> common; // TODO: make the CPU structure use the same type
134
135     //! A handle to the program created by buildPmeGpuProgram()
136     const PmeGpuProgram* programHandle_;
137
138     //! Handle that ensures the clFFT library has been initialized once per process.
139     std::unique_ptr<gmx::ClfftInitializer> initializedClfftLibrary_;
140
141     /*! \brief The settings. */
142     PmeGpuSettings settings;
143
144     /*! \brief The host-side buffers.
145      * The device-side buffers are buried in kernelParams, but that will have to change.
146      */
147     PmeGpuStaging staging;
148
149     /*! \brief Number of local atoms, padded to be divisible by c_pmeAtomDataAlignment.
150      *
151      * Used only as a basic size for almost all the atom data allocations
152      * (spline parameter data is also aligned by PME_SPREADGATHER_PARTICLES_PER_WARP).
153      * kernelParams.atoms.nAtoms is the actual atom count to be used for most data copying.
154      *
155      * TODO: memory allocation/padding properties should be handled by
156      * something like a container
157      */
158     int nAtomsAlloc;
159
160     /*! \brief Kernel scheduling grid width limit in X - derived from deviceinfo compute capability in CUDA.
161      * Declared as very large int to make it useful in computations with type promotion, to avoid overflows.
162      * OpenCL seems to not have readily available global work size limit, so we just assign a large arbitrary constant to this instead.
163      * TODO: this should be in PmeGpuProgram(Impl)
164      */
165     std::intmax_t maxGridWidthX;
166
167     /*! \brief A single structure encompassing all the PME data used on GPU.
168      * Its value is the only argument to all the PME GPU kernels.
169      * \todo Test whether this should be copied to the constant GPU memory once for each computation
170      * (or even less often with no box updates) instead of being an argument.
171      */
172     std::shared_ptr<PmeGpuKernelParams> kernelParams;
173
174     /*! \brief The pointer to GPU-framework specific host-side data, such as CUDA streams and events. */
175     std::shared_ptr<PmeGpuSpecific> archSpecific; /* FIXME: make it an unique_ptr */
176 };
177
178 #endif