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