976e064cb2aa610ef79613a78a232a6c191de1da
[alexxy/gromacs.git] / src / gromacs / ewald / pme_gpu_types_host_impl.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 /*! \internal \file
37  * \brief Defines the host-side PME GPU data structure, which is dependent on the GPU types.
38  * It's included by pointer in the general PmeGpu host structure in pme_gpu_types_host.h.
39  *
40  * \author Aleksei Iupinov <a.yupinov@gmail.com>
41  * \ingroup module_ewald
42  */
43
44 #ifndef PMEGPUTYPESHOSTIMPL_H
45 #define PMEGPUTYPESHOSTIMPL_H
46
47 #include "config.h"
48
49 #include <array>
50 #include <set>
51 #include <vector>
52
53 #if GMX_GPU_CUDA
54 #    include "gromacs/gpu_utils/gpueventsynchronizer.cuh"
55 #    include "gromacs/gpu_utils/gpuregiontimer.cuh"
56 #elif GMX_GPU_OPENCL
57 #    include "gromacs/gpu_utils/gpueventsynchronizer_ocl.h"
58 #    include "gromacs/gpu_utils/gpuregiontimer_ocl.h"
59 #elif GMX_GPU_SYCL
60 #    include "gromacs/gpu_utils/gpueventsynchronizer_sycl.h"
61 #    include "gromacs/gpu_utils/gpuregiontimer_sycl.h"
62 #endif
63
64 #include "gromacs/timing/gpu_timing.h" // for gtPME_EVENT_COUNT
65
66 #include "pme_gpu_3dfft.h"
67
68 #ifndef NUMFEPSTATES
69 //! Number of FEP states.
70 #    define NUMFEPSTATES 2
71 #endif
72
73 class GpuParallel3dFft;
74
75 /*! \internal \brief
76  * The main PME CUDA/OpenCL-specific host data structure, included in the PME GPU structure by the archSpecific pointer.
77  */
78 struct PmeGpuSpecific
79 {
80     /*! \brief Constructor
81      *
82      * \param[in] deviceContext  GPU device context
83      * \param[in] pmeStream      GPU pme stream.
84      */
85     PmeGpuSpecific(const DeviceContext& deviceContext, const DeviceStream& pmeStream) :
86         deviceContext_(deviceContext),
87         pmeStream_(pmeStream)
88     {
89     }
90
91     /*! \brief
92      * A handle to the GPU context.
93      * TODO: this is currently extracted from the implementation of pmeGpu->programHandle_,
94      * but should be a constructor parameter to PmeGpu, as well as PmeGpuProgram,
95      * managed by high-level code.
96      */
97     const DeviceContext& deviceContext_;
98
99     /*! \brief The GPU stream where everything related to the PME happens. */
100     const DeviceStream& pmeStream_;
101
102     /* Synchronization events */
103     /*! \brief Triggered after the PME Force Calculations have been completed */
104     GpuEventSynchronizer pmeForcesReady;
105     /*! \brief Triggered after the grid has been copied to the host (after the spreading stage). */
106     GpuEventSynchronizer syncSpreadGridD2H;
107
108     /* Settings which are set at the start of the run */
109     /*! \brief A boolean which tells whether the complex and real grids for cu/clFFT are different or same. Currenty true. */
110     bool performOutOfPlaceFFT = false;
111     /*! \brief A boolean which tells if the GPU timing events are enabled.
112      *  False by default, can be enabled by setting the environment variable GMX_ENABLE_GPU_TIMING.
113      *  Note: will not be reliable when multiple GPU tasks are running concurrently on the same
114      * device context, as CUDA events on multiple streams are untrustworthy.
115      */
116     bool useTiming = false;
117
118     //! Vector of FFT setups
119     std::vector<std::unique_ptr<GpuParallel3dFft>> fftSetup;
120
121     //! All the timers one might use
122     std::array<GpuRegionTimer, gtPME_EVENT_COUNT> timingEvents;
123
124     //! Indices of timingEvents actually used
125     std::set<size_t> activeTimers;
126
127     /* GPU arrays element counts (not the arrays sizes in bytes!).
128      * They might be larger than the actual meaningful data sizes.
129      * These are paired: the actual element count + the maximum element count that can fit in the current allocated memory.
130      * These integer pairs are mostly meaningful for the reallocateDeviceBuffer calls.
131      * As such, if DeviceBuffer is refactored into a class, they can be freely changed, too.
132      * The only exceptions are realGridSize and complexGridSize which are also used for grid clearing/copying.
133      * TODO: these should live in a clean buffered container type, and be refactored in the NB/cudautils as well.
134      */
135     /*! \brief The kernelParams.atoms.coordinates float element count (actual)*/
136     int coordinatesSize = 0;
137     /*! \brief The kernelParams.atoms.coordinates float element count (reserved) */
138     int coordinatesSizeAlloc = 0;
139     /*! \brief The kernelParams.atoms.forces float element count (actual) */
140     int forcesSize = 0;
141     /*! \brief The kernelParams.atoms.forces float element count (reserved) */
142     int forcesSizeAlloc = 0;
143     /*! \brief The kernelParams.atoms.gridlineIndices int element count (actual) */
144     int gridlineIndicesSize = 0;
145     /*! \brief The kernelParams.atoms.gridlineIndices int element count (reserved) */
146     int gridlineIndicesSizeAlloc = 0;
147     /*! \brief Both the kernelParams.atoms.theta and kernelParams.atoms.dtheta float element count (actual) */
148     int splineDataSize = 0;
149     /*! \brief Both the kernelParams.atoms.theta and kernelParams.atoms.dtheta float element count (reserved) */
150     int splineDataSizeAlloc = 0;
151     /*! \brief The kernelParams.atoms.coefficients float element count (actual) */
152     int coefficientsSize[NUMFEPSTATES] = { 0, 0 };
153     /*! \brief The kernelParams.atoms.coefficients float element count (reserved) */
154     int coefficientsCapacity[NUMFEPSTATES] = { 0, 0 };
155     /*! \brief The kernelParams.grid.splineValuesArray float element count (actual) */
156     int splineValuesSize[NUMFEPSTATES] = { 0, 0 };
157     /*! \brief The kernelParams.grid.splineValuesArray float element count (reserved) */
158     int splineValuesCapacity[NUMFEPSTATES] = { 0, 0 };
159     /*! \brief The kernelParams.grid.realGrid float element count (actual) */
160     int realGridSize[NUMFEPSTATES] = { 0, 0 };
161     /*! \brief The kernelParams.grid.realGrid float element count (reserved) */
162     int realGridCapacity[NUMFEPSTATES] = { 0, 0 };
163     /*! \brief The kernelParams.grid.fourierGrid float (not float2!) element count (actual) */
164     int complexGridSize[NUMFEPSTATES] = { 0, 0 };
165     /*! \brief The kernelParams.grid.fourierGrid float (not float2!) element count (reserved) */
166     int complexGridCapacity[NUMFEPSTATES] = { 0, 0 };
167 };
168
169 #endif