2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
37 * \brief Declares CUDA implementation of GPU Halo Exchange.
39 * This header file is needed to include from both the device-side
40 * kernels file, and the host-side management code.
42 * \author Alan Gray <alang@nvidia.com>
44 * \ingroup module_domdec
46 #ifndef GMX_DOMDEC_GPUHALOEXCHANGE_IMPL_H
47 #define GMX_DOMDEC_GPUHALOEXCHANGE_IMPL_H
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"
60 /*! \brief switch for whether coordinates or force halo is being applied */
61 enum class HaloQuantity
67 /*! \internal \brief Class with interfaces and data for GPU Halo Exchange */
68 class GpuHaloExchange::Impl
72 /*! \brief Creates GPU Halo Exchange object.
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] localStream local NB CUDA stream
79 * \param [in] nonLocalStream non-local NB CUDA stream
80 * \param [in] pulse the communication pulse for this instance
81 * \param [in] wcycle The wallclock counter
83 Impl(gmx_domdec_t* dd,
85 MPI_Comm mpi_comm_mysim,
86 const DeviceContext& deviceContext,
87 const DeviceStream& localStream,
88 const DeviceStream& nonLocalStream,
90 gmx_wallcycle* wcycle);
94 * (Re-) Initialization for GPU halo exchange
95 * \param [in] d_coordinatesBuffer pointer to coordinates buffer in GPU memory
96 * \param [in] d_forcesBuffer pointer to forces buffer in GPU memory
98 void reinitHalo(float3* d_coordinatesBuffer, float3* d_forcesBuffer);
102 * GPU halo exchange of coordinates buffer
103 * \param [in] box Coordinate box (from which shifts will be constructed)
104 * \param [in] coordinatesReadyOnDeviceEvent event recorded when coordinates have been copied to device
106 void communicateHaloCoordinates(const matrix box, GpuEventSynchronizer* coordinatesReadyOnDeviceEvent);
108 /*! \brief GPU halo exchange of force buffer
109 * \param[in] accumulateForces True if forces should accumulate, otherwise they are set
111 void communicateHaloForces(bool accumulateForces);
113 /*! \brief Get the event synchronizer for the forces ready on device.
114 * \returns The event to synchronize the stream that consumes forces on device.
116 GpuEventSynchronizer* getForcesReadyOnDeviceEvent();
119 /*! \brief Data transfer wrapper for GPU halo exchange
120 * \param [in] sendPtr send buffer address
121 * \param [in] sendSize number of elements to send
122 * \param [in] sendRank rank of destination
123 * \param [in] recvPtr receive buffer address
124 * \param [in] recvSize number of elements to receive
125 * \param [in] recvRank rank of source
127 void communicateHaloData(float3* sendPtr, int sendSize, int sendRank, float3* recvPtr, int recvSize, int recvRank);
129 /*! \brief Data transfer for GPU halo exchange using CUDA memcopies
130 * \param [inout] sendPtr address to send data from
131 * \param [in] sendSize number of atoms to be sent
132 * \param [in] sendRank rank to send data to
133 * \param [in] remotePtr remote address to recv data
134 * \param [in] recvRank rank to recv data from
136 void communicateHaloDataWithCudaDirect(float3* sendPtr, int sendSize, int sendRank, float3* remotePtr, int recvRank);
138 /*! \brief Data transfer wrapper for GPU halo exchange using MPI_send and MPI_Recv
139 * \param [in] sendPtr send buffer address
140 * \param [in] sendSize number of elements to send
141 * \param [in] sendRank rank of destination
142 * \param [in] recvPtr receive buffer address
143 * \param [in] recvSize number of elements to receive
144 * \param [in] recvRank rank of source
146 void communicateHaloDataWithCudaMPI(float3* sendPtr,
153 /*! \brief Exchange coordinate-ready event with neighbor ranks and enqueue wait in non-local
154 * stream \param [in] eventSync event recorded when coordinates/forces are ready to device
156 void enqueueWaitRemoteCoordinatesReadyEvent(GpuEventSynchronizer* coordinatesReadyOnDeviceEvent);
158 //! Domain decomposition object
159 gmx_domdec_t* dd_ = nullptr;
160 //! map of indices to be sent from this rank
161 gmx::HostVector<int> h_indexMap_;
162 //! device copy of index map
163 int* d_indexMap_ = nullptr;
164 //! number of elements in index map array
165 int indexMapSize_ = -1;
166 //! number of elements allocated in index map array
167 int indexMapSizeAlloc_ = -1;
168 //! device buffer for sending packed data
169 float3* d_sendBuf_ = nullptr;
170 //! number of atoms in sendbuf array
171 int sendBufSize_ = -1;
172 //! number of atoms allocated in sendbuf array
173 int sendBufSizeAlloc_ = -1;
174 //! device buffer for receiving packed data
175 float3* d_recvBuf_ = nullptr;
176 //! maximum size of packed buffer
177 int maxPackedBufferSize_ = 0;
178 //! number of atoms in recvbuf array
179 int recvBufSize_ = -1;
180 //! number of atoms allocated in recvbuf array
181 int recvBufSizeAlloc_ = -1;
182 //! rank to send data to for X
184 //! rank to recv data from for X
186 //! rank to send data to for F
188 //! rank to recv data from for F
190 //! send copy size from this rank for X
192 //! recv copy size to this rank for X
194 //! send copy size from this rank for F
196 //! recv copy size to this rank for F
198 //! number of home atoms - offset of local halo region
199 int numHomeAtoms_ = 0;
200 //! remote GPU coordinates buffer pointer for pushing data
201 float3* remoteXPtr_ = nullptr;
202 //! remote GPU force buffer pointer for pushing data
203 float3* remoteFPtr_ = nullptr;
204 //! Periodic Boundary Conditions for this rank
205 bool usePBC_ = false;
206 //! force shift buffer on device
207 float3* d_fShift_ = nullptr;
208 //! Event triggered when halo transfer has been launched with direct CUD memory copy
209 GpuEventSynchronizer* haloDataTransferLaunched_ = nullptr;
210 //! MPI communicator used for simulation
211 MPI_Comm mpi_comm_mysim_;
212 //! GPU context object
213 const DeviceContext& deviceContext_;
214 //! CUDA stream for local non-bonded calculations
215 const DeviceStream& localStream_;
216 //! CUDA stream for non-local non-bonded calculations
217 const DeviceStream& nonLocalStream_;
218 //! full coordinates buffer in GPU memory
219 float3* d_x_ = nullptr;
220 //! full forces buffer in GPU memory
221 float3* d_f_ = nullptr;
222 //! An event recorded once the exchanged forces are ready on the GPU
223 GpuEventSynchronizer fReadyOnDevice_;
224 //! The dimension index corresponding to this halo exchange instance
226 //! The pulse corresponding to this halo exchange instance
228 //! The wallclock counter
229 gmx_wallcycle* wcycle_ = nullptr;
230 //! The atom offset for receive (x) or send (f) for dimension index and pulse corresponding to this halo exchange instance