2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2019, 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/gpueventsynchronizer.cuh"
51 #include "gromacs/gpu_utils/hostallocator.h"
56 /*! \brief switch for whether coordinates or force halo is being applied */
57 enum class HaloQuantity
59 HaloCoordinates, HaloForces
62 /*! \internal \brief Class with interfaces and data for GPU Halo Exchange */
63 class GpuHaloExchange::Impl
67 /*! \brief Creates GPU Halo Exchange object.
69 * \param [inout] dd domdec structure
70 * \param [in] mpi_comm_mysim communicator used for simulation
71 * \param [in] localStream local NB CUDA stream
72 * \param [in] nonLocalStream non-local NB CUDA stream
73 * \param [in] coordinatesOnDeviceEvent event recorded when coordinates have been copied to device
75 Impl(gmx_domdec_t *dd,
76 MPI_Comm mpi_comm_mysim,
79 void *coordinatesOnDeviceEvent);
83 * (Re-) Initialization for GPU halo exchange
84 * \param [in] d_coordinatesBuffer pointer to coordinates buffer in GPU memory
85 * \param [in] d_forcesBuffer pointer to forces buffer in GPU memory
87 void reinitHalo(float3 *d_coordinatesBuffer,
88 float3 *d_forcesBuffer);
92 * GPU halo exchange of coordinates buffer
93 * \param [in] box Coordinate box (from which shifts will be constructed)
95 void communicateHaloCoordinates(const matrix box);
97 /*! \brief GPU halo exchange of force buffer
98 * \param[in] accumulateForces True if forces should accumulate, otherwise they are set
100 void communicateHaloForces(bool accumulateForces);
104 /*! \brief Data transfer wrapper for GPU halo exchange
105 * \param [inout] d_ptr pointer to coordinates or force buffer in GPU memory
106 * \param [in] haloQuantity switch on whether X or F halo exchange is being performed
108 void communicateHaloData(float3 *d_ptr,
109 HaloQuantity haloQuantity);
111 /*! \brief Data transfer for GPU halo exchange using CUDA memcopies
112 * \param [inout] sendPtr address to send data from
113 * \param [in] sendSize number of atoms to be sent
114 * \param [in] sendRank rank to send data to
115 * \param [inout] remotePtr remote address to recv data
116 * \param [in] recvRank rank to recv data from
118 void communicateHaloDataWithCudaDirect(void *sendPtr,
124 //! Domain decomposition object
125 gmx_domdec_t *dd_ = nullptr;
126 //! map of indices to be sent from this rank
127 gmx::HostVector<int> h_indexMap_;
128 //! device copy of index map
129 int *d_indexMap_ = nullptr;
130 //! number of elements in index map array
131 int indexMapSize_ = -1;
132 //! number of elements allocated in index map array
133 int indexMapSizeAlloc_ = -1;
134 //! device buffer for sending packed data
135 float3 *d_sendBuf_ = nullptr;
136 //! number of atoms in sendbuf array
137 int sendBufSize_ = -1;
138 //! number of atoms allocated in sendbuf array
139 int sendBufSizeAlloc_ = -1;
140 //! device buffer for receiving packed data
141 float3 *d_recvBuf_ = nullptr;
142 //! maximum size of packed buffer
143 int maxPackedBufferSize_ = 0;
144 //! number of atoms in recvbuf array
145 int recvBufSize_ = -1;
146 //! number of atoms allocated in recvbuf array
147 int recvBufSizeAlloc_ = -1;
148 //! rank to send data to for X
150 //! rank to recv data from for X
152 //! rank to send data to for F
154 //! rank to recv data from for F
156 //! send copy size from this rank for X
158 //! recv copy size to this rank for X
160 //! send copy size from this rank for F
162 //! recv copy size to this rank for F
164 //! number of home atoms - offset of local halo region
165 int numHomeAtoms_ = 0;
166 //! remote GPU coordinates buffer pointer for pushing data
167 void *remoteXPtr_ = 0;
168 //! remote GPU force buffer pointer for pushing data
169 void *remoteFPtr_ = 0;
170 //! Periodic Boundary Conditions for this rank
171 bool usePBC_ = false;
172 //! force shift buffer on device
173 float3 * d_fShift_ = nullptr;
174 //! Event triggered when halo transfer has been launched with direct CUD memory copy
175 GpuEventSynchronizer *haloDataTransferLaunched_ = nullptr;
176 //! MPI communicator used for simulation
177 MPI_Comm mpi_comm_mysim_;
178 //! CUDA stream for local non-bonded calculations
179 cudaStream_t localStream_ = nullptr;
180 //! CUDA stream for non-local non-bonded calculations
181 cudaStream_t nonLocalStream_ = nullptr;
182 //! Event triggered when coordinates have been copied to device
183 GpuEventSynchronizer *coordinatesOnDeviceEvent_ = nullptr;
184 //! full coordinates buffer in GPU memory
185 float3 *d_x_ = nullptr;
186 //! full forces buffer in GPU memory
187 float3 *d_f_ = nullptr;