Remove NVML support
[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, 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 /*! \internal \brief
109  * The PME GPU intermediate buffers structure, included in the main PME GPU structure by value.
110  * Buffers are managed by the PME GPU module.
111  */
112 struct PmeGpuStaging
113 {
114     //! Host-side force buffer
115     std::vector < gmx::RVec, gmx::HostAllocator < gmx::RVec>> h_forces;
116
117     /*! \brief Virial and energy intermediate host-side buffer. Size is PME_GPU_VIRIAL_AND_ENERGY_COUNT. */
118     float  *h_virialAndEnergy;
119     /*! \brief B-spline values intermediate host-side buffer. */
120     float  *h_splineModuli;
121
122     /*! \brief Pointer to the host memory with B-spline values. Only used for host-side gather, or unit tests */
123     float  *h_theta;
124     /*! \brief Pointer to the host memory with B-spline derivative values. Only used for host-side gather, or unit tests */
125     float  *h_dtheta;
126     /*! \brief Pointer to the host memory with ivec atom gridline indices. Only used for host-side gather, or unit tests */
127     int    *h_gridlineIndices;
128 };
129
130 /*! \internal \brief
131  * The PME GPU structure for all the data copied directly from the CPU PME structure.
132  * The copying is done when the CPU PME structure is already (re-)initialized
133  * (pme_gpu_reinit is called at the end of gmx_pme_init).
134  * All the variables here are named almost the same way as in gmx_pme_t.
135  * The types are different: pointers are replaced by vectors.
136  * TODO: use the shared data with the PME CPU.
137  * Included in the main PME GPU structure by value.
138  */
139 struct PmeShared
140 {
141     /*! \brief Grid count - currently always 1 on GPU */
142     int                    ngrids;
143     /*! \brief Grid dimensions - nkx, nky, nkz */
144     int                    nk[DIM];
145     /*! \brief PME interpolation order */
146     int                    pme_order;
147     /*! \brief Ewald splitting coefficient for Coulomb */
148     real                   ewaldcoeff_q;
149     /*! \brief Electrostatics parameter */
150     real                   epsilon_r;
151     /*! \brief Gridline indices - nnx, nny, nnz */
152     std::vector<int>       nn;
153     /*! \brief Fractional shifts - fshx, fshy, fshz */
154     std::vector<real>      fsh;
155     /*! \brief Precomputed B-spline values */
156     std::vector<real>      bsp_mod[DIM];
157     /*! \brief The PME codepath being taken */
158     PmeRunMode             runMode;
159     /*! \brief The box scaler based on inputrec - created in pme_init and managed by CPU structure */
160     class EwaldBoxZScaler *boxScaler;
161     /*! \brief The previous computation box to know if we even need to update the current box params.
162      * \todo Manage this on higher level.
163      * \todo Alternatively, when this structure is used by CPU PME code, make use of this field there as well.
164      */
165     matrix previousBox;
166 };
167
168 /*! \internal \brief
169  * The main PME GPU host structure, included in the PME CPU structure by pointer.
170  */
171 struct PmeGpu
172 {
173     /*! \brief The information copied once per reinit from the CPU structure. */
174     std::shared_ptr<PmeShared> common; // TODO: make the CPU structure use the same type
175
176     //! A handle to the program created by buildPmeGpuProgram()
177     PmeGpuProgramHandle programHandle_;
178
179     /*! \brief The settings. */
180     PmeGpuSettings settings;
181
182     /*! \brief The host-side buffers.
183      * The device-side buffers are buried in kernelParams, but that will have to change.
184      */
185     PmeGpuStaging staging;
186
187     /*! \brief Number of local atoms, padded to be divisible by PME_ATOM_DATA_ALIGNMENT.
188      * Used for kernel scheduling.
189      * kernelParams.atoms.nAtoms is the actual atom count to be used for data copying.
190      * TODO: this and the next member represent a memory allocation/padding properties -
191      * what a container type should do ideally.
192      */
193     int nAtomsPadded;
194     /*! \brief Number of local atoms, padded to be divisible by PME_ATOM_DATA_ALIGNMENT
195      * if c_usePadding is true.
196      * Used only as a basic size for almost all the atom data allocations
197      * (spline parameter data is also aligned by PME_SPREADGATHER_PARTICLES_PER_WARP).
198      * This should be the same as (c_usePadding ? nAtomsPadded : kernelParams.atoms.nAtoms).
199      * kernelParams.atoms.nAtoms is the actual atom count to be used for most data copying.
200      */
201     int nAtomsAlloc;
202
203     /*! \brief A pointer to the device used during the execution. */
204     const gmx_device_info_t *deviceInfo;
205
206     /*! \brief Kernel scheduling grid width limit in X - derived from deviceinfo compute capability in CUDA.
207      * Declared as very large int to make it useful in computations with type promotion, to avoid overflows.
208      */
209     std::intmax_t maxGridWidthX;
210
211     /*! \brief A single structure encompassing all the PME data used on GPU.
212      * Its value is the only argument to all the PME GPU kernels.
213      * \todo Test whether this should be copied to the constant GPU memory once for each computation
214      * (or even less often with no box updates) instead of being an argument.
215      */
216     std::shared_ptr<PmeGpuKernelParams> kernelParams;
217
218     /*! \brief The pointer to GPU-framework specific host-side data, such as CUDA streams and events. */
219     std::shared_ptr<PmeGpuSpecific> archSpecific; /* FIXME: make it an unique_ptr */
220 };
221
222 #endif