Rework GPU halo and state propagator streams and dependencies to get better overlap
[alexxy/gromacs.git] / src / gromacs / domdec / gpuhaloexchange_impl.cuh
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 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 /*! \internal \file
36  *
37  * \brief Declares CUDA implementation of GPU Halo Exchange.
38  *
39  * This header file is needed to include from both the device-side
40  * kernels file, and the host-side management code.
41  *
42  * \author Alan Gray <alang@nvidia.com>
43  *
44  * \ingroup module_domdec
45  */
46 #ifndef GMX_DOMDEC_GPUHALOEXCHANGE_IMPL_H
47 #define GMX_DOMDEC_GPUHALOEXCHANGE_IMPL_H
48
49 #include "gromacs/domdec/gpuhaloexchange.h"
50 #include "gromacs/gpu_utils/device_context.h"
51 #include "gromacs/gpu_utils/gpueventsynchronizer.h"
52 #include "gromacs/gpu_utils/hostallocator.h"
53 #include "gromacs/utility/gmxmpi.h"
54
55 struct gmx_wallcycle;
56
57 namespace gmx
58 {
59
60 /*! \brief switch for whether coordinates or force halo is being applied */
61 enum class HaloQuantity
62 {
63     HaloCoordinates,
64     HaloForces
65 };
66
67 /*! \internal \brief Class with interfaces and data for GPU Halo Exchange */
68 class GpuHaloExchange::Impl
69 {
70
71 public:
72     /*! \brief Creates GPU Halo Exchange object.
73      *
74      * \param [inout] dd                       domdec structure
75      * \param [in]    dimIndex                 the dimension index for this instance
76      * \param [in]    mpi_comm_mysim           communicator used for simulation
77      * \param [in]    deviceContext            GPU device context
78      * \param [in]    pulse                    the communication pulse for this instance
79      * \param [in]    wcycle                   The wallclock counter
80      */
81     Impl(gmx_domdec_t*        dd,
82          int                  dimIndex,
83          MPI_Comm             mpi_comm_mysim,
84          const DeviceContext& deviceContext,
85          int                  pulse,
86          gmx_wallcycle*       wcycle);
87     ~Impl();
88
89     /*! \brief
90      * (Re-) Initialization for GPU halo exchange
91      * \param [in] d_coordinatesBuffer  pointer to coordinates buffer in GPU memory
92      * \param [in] d_forcesBuffer   pointer to forces buffer in GPU memory
93      */
94     void reinitHalo(float3* d_coordinatesBuffer, float3* d_forcesBuffer);
95
96
97     /*! \brief
98      * GPU halo exchange of coordinates buffer
99      * \param [in] box  Coordinate box (from which shifts will be constructed)
100      * \param [in] dependencyEvent   Dependency event for this operation
101      * \returns                      Event recorded when this operation has been launched
102      */
103     GpuEventSynchronizer* communicateHaloCoordinates(const matrix box, GpuEventSynchronizer* dependencyEvent);
104
105     /*! \brief  GPU halo exchange of force buffer
106      * \param [in] accumulateForces  True if forces should accumulate, otherwise they are set
107      * \param [in] dependencyEvents  Dependency events for this operation
108      */
109     void communicateHaloForces(bool                                           accumulateForces,
110                                FixedCapacityVector<GpuEventSynchronizer*, 2>* dependencyEvents);
111
112     /*! \brief Get the event synchronizer for the forces ready on device.
113      *  \returns  The event to synchronize the stream that consumes forces on device.
114      */
115     GpuEventSynchronizer* getForcesReadyOnDeviceEvent();
116
117 private:
118     /*! \brief Data transfer wrapper for GPU halo exchange
119      * \param [in] sendPtr      send buffer address
120      * \param [in] sendSize     number of elements to send
121      * \param [in] sendRank     rank of destination
122      * \param [in] recvPtr      receive buffer address
123      * \param [in] recvSize     number of elements to receive
124      * \param [in] recvRank     rank of source
125      */
126     void communicateHaloData(float3* sendPtr, int sendSize, int sendRank, float3* recvPtr, int recvSize, int recvRank);
127
128     /*! \brief Data transfer for GPU halo exchange using CUDA memcopies
129      * \param [inout] sendPtr    address to send data from
130      * \param [in] sendSize      number of atoms to be sent
131      * \param [in] sendRank      rank to send data to
132      * \param [in] remotePtr     remote address to recv data
133      * \param [in] recvRank      rank to recv data from
134      */
135     void communicateHaloDataWithCudaDirect(float3* sendPtr, int sendSize, int sendRank, float3* remotePtr, int recvRank);
136
137     /*! \brief Data transfer wrapper for GPU halo exchange using MPI_send and MPI_Recv
138      * \param [in] sendPtr      send buffer address
139      * \param [in] sendSize     number of elements to send
140      * \param [in] sendRank     rank of destination
141      * \param [in] recvPtr      receive buffer address
142      * \param [in] recvSize     number of elements to receive
143      * \param [in] recvRank     rank of source
144      */
145     void communicateHaloDataWithCudaMPI(float3* sendPtr,
146                                         int     sendSize,
147                                         int     sendRank,
148                                         float3* recvPtr,
149                                         int     recvSize,
150                                         int     recvRank);
151
152     /*! \brief Exchange coordinate-ready event with neighbor ranks and enqueue wait in halo stream
153      * \param [in] eventSync    event recorded when coordinates/forces are ready to device
154      */
155     void enqueueWaitRemoteCoordinatesReadyEvent(GpuEventSynchronizer* coordinatesReadyOnDeviceEvent);
156
157     //! Domain decomposition object
158     gmx_domdec_t* dd_ = nullptr;
159     //! map of indices to be sent from this rank
160     gmx::HostVector<int> h_indexMap_;
161     //! device copy of index map
162     int* d_indexMap_ = nullptr;
163     //! number of elements in index map array
164     int indexMapSize_ = -1;
165     //! number of elements allocated in index map array
166     int indexMapSizeAlloc_ = -1;
167     //! device buffer for sending packed data
168     float3* d_sendBuf_ = nullptr;
169     //! number of atoms in sendbuf array
170     int sendBufSize_ = -1;
171     //! number of atoms allocated in sendbuf array
172     int sendBufSizeAlloc_ = -1;
173     //! device buffer for receiving packed data
174     float3* d_recvBuf_ = nullptr;
175     //! maximum size of packed buffer
176     int maxPackedBufferSize_ = 0;
177     //! number of atoms in recvbuf array
178     int recvBufSize_ = -1;
179     //! number of atoms allocated in recvbuf array
180     int recvBufSizeAlloc_ = -1;
181     //! rank to send data to for X
182     int sendRankX_ = 0;
183     //! rank to recv data from for X
184     int recvRankX_ = 0;
185     //! rank to send data to for F
186     int sendRankF_ = 0;
187     //! rank to recv data from for F
188     int recvRankF_ = 0;
189     //! send copy size from this rank for X
190     int xSendSize_ = 0;
191     //! recv copy size to this rank for X
192     int xRecvSize_ = 0;
193     //! send copy size from this rank for F
194     int fSendSize_ = 0;
195     //! recv copy size to this rank for F
196     int fRecvSize_ = 0;
197     //! number of home atoms - offset of local halo region
198     int numHomeAtoms_ = 0;
199     //! remote GPU coordinates buffer pointer for pushing data
200     float3* remoteXPtr_ = nullptr;
201     //! remote GPU force buffer pointer for pushing data
202     float3* remoteFPtr_ = nullptr;
203     //! Periodic Boundary Conditions for this rank
204     bool usePBC_ = false;
205     //! force shift buffer on device
206     float3* d_fShift_ = nullptr;
207     //! Event triggered when halo transfer has been launched with direct CUD memory copy
208     GpuEventSynchronizer* haloDataTransferLaunched_ = nullptr;
209     //! MPI communicator used for simulation
210     MPI_Comm mpi_comm_mysim_;
211     //! GPU context object
212     const DeviceContext& deviceContext_;
213     //! CUDA stream for this halo exchange
214     DeviceStream* haloStream_;
215     //! full coordinates buffer in GPU memory
216     float3* d_x_ = nullptr;
217     //! full forces buffer in GPU memory
218     float3* d_f_ = nullptr;
219     //! An event recorded once the exchanged forces are ready on the GPU
220     GpuEventSynchronizer fReadyOnDevice_;
221     //! The dimension index corresponding to this halo exchange instance
222     int dimIndex_ = 0;
223     //! The pulse corresponding to this halo exchange instance
224     int pulse_ = 0;
225     //! The wallclock counter
226     gmx_wallcycle* wcycle_ = nullptr;
227     //! The atom offset for receive (x) or send (f) for dimension index and pulse corresponding to this halo exchange instance
228     int atomOffset_ = 0;
229     //! Event triggered when coordinate halo has been launched
230     GpuEventSynchronizer coordinateHaloLaunched_;
231 };
232
233 } // namespace gmx
234
235 #endif