9a033ed54f96d88e38f06c0f2864f8a0a9b7029f
[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, 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.cuh"
52 #include "gromacs/gpu_utils/hostallocator.h"
53 #include "gromacs/utility/gmxmpi.h"
54
55 namespace gmx
56 {
57
58 /*! \brief switch for whether coordinates or force halo is being applied */
59 enum class HaloQuantity
60 {
61     HaloCoordinates,
62     HaloForces
63 };
64
65 /*! \internal \brief Class with interfaces and data for GPU Halo Exchange */
66 class GpuHaloExchange::Impl
67 {
68
69 public:
70     /*! \brief Creates GPU Halo Exchange object.
71      *
72      * \param [inout] dd                       domdec structure
73      * \param [in]    mpi_comm_mysim           communicator used for simulation
74      * \param [in]    deviceContext            GPU device context
75      * \param [in]    localStream              local NB CUDA stream
76      * \param [in]    nonLocalStream           non-local NB CUDA stream
77      * \param [in]    pulse                    the communication pulse for this instance
78      */
79     Impl(gmx_domdec_t*        dd,
80          MPI_Comm             mpi_comm_mysim,
81          const DeviceContext& deviceContext,
82          const DeviceStream&  localStream,
83          const DeviceStream&  nonLocalStream,
84          int                  pulse);
85     ~Impl();
86
87     /*! \brief
88      * (Re-) Initialization for GPU halo exchange
89      * \param [in] d_coordinatesBuffer  pointer to coordinates buffer in GPU memory
90      * \param [in] d_forcesBuffer   pointer to forces buffer in GPU memory
91      */
92     void reinitHalo(float3* d_coordinatesBuffer, float3* d_forcesBuffer);
93
94
95     /*! \brief
96      * GPU halo exchange of coordinates buffer
97      * \param [in] box  Coordinate box (from which shifts will be constructed)
98      * \param [in] coordinatesReadyOnDeviceEvent event recorded when coordinates have been copied to device
99      */
100     void communicateHaloCoordinates(const matrix box, GpuEventSynchronizer* coordinatesReadyOnDeviceEvent);
101
102     /*! \brief  GPU halo exchange of force buffer
103      * \param[in] accumulateForces  True if forces should accumulate, otherwise they are set
104      */
105     void communicateHaloForces(bool accumulateForces);
106
107     /*! \brief Get the event synchronizer for the forces ready on device.
108      *  \returns  The event to synchronize the stream that consumes forces on device.
109      */
110     GpuEventSynchronizer* getForcesReadyOnDeviceEvent();
111
112 private:
113     /*! \brief Data transfer wrapper for GPU halo exchange
114      * \param [inout] d_ptr      pointer to coordinates or force buffer in GPU memory
115      * \param [in] haloQuantity  switch on whether X or F halo exchange is being performed
116      * \param [in] coordinatesReadyOnDeviceEvent event recorded when coordinates have been copied to device
117      */
118     void communicateHaloData(float3*               d_ptr,
119                              HaloQuantity          haloQuantity,
120                              GpuEventSynchronizer* coordinatesReadyOnDeviceEvent);
121
122     /*! \brief Data transfer for GPU halo exchange using CUDA memcopies
123      * \param [inout] sendPtr    address to send data from
124      * \param [in] sendSize      number of atoms to be sent
125      * \param [in] sendRank      rank to send data to
126      * \param [inout] remotePtr  remote address to recv data
127      * \param [in] recvRank      rank to recv data from
128      */
129     void communicateHaloDataWithCudaDirect(void* sendPtr, int sendSize, int sendRank, void* remotePtr, int recvRank);
130
131     //! Domain decomposition object
132     gmx_domdec_t* dd_ = nullptr;
133     //! map of indices to be sent from this rank
134     gmx::HostVector<int> h_indexMap_;
135     //! device copy of index map
136     int* d_indexMap_ = nullptr;
137     //! number of elements in index map array
138     int indexMapSize_ = -1;
139     //! number of elements allocated in index map array
140     int indexMapSizeAlloc_ = -1;
141     //! device buffer for sending packed data
142     float3* d_sendBuf_ = nullptr;
143     //! number of atoms in sendbuf array
144     int sendBufSize_ = -1;
145     //! number of atoms allocated in sendbuf array
146     int sendBufSizeAlloc_ = -1;
147     //! device buffer for receiving packed data
148     float3* d_recvBuf_ = nullptr;
149     //! maximum size of packed buffer
150     int maxPackedBufferSize_ = 0;
151     //! number of atoms in recvbuf array
152     int recvBufSize_ = -1;
153     //! number of atoms allocated in recvbuf array
154     int recvBufSizeAlloc_ = -1;
155     //! rank to send data to for X
156     int sendRankX_ = 0;
157     //! rank to recv data from for X
158     int recvRankX_ = 0;
159     //! rank to send data to for F
160     int sendRankF_ = 0;
161     //! rank to recv data from for F
162     int recvRankF_ = 0;
163     //! send copy size from this rank for X
164     int xSendSize_ = 0;
165     //! recv copy size to this rank for X
166     int xRecvSize_ = 0;
167     //! send copy size from this rank for F
168     int fSendSize_ = 0;
169     //! recv copy size to this rank for F
170     int fRecvSize_ = 0;
171     //! number of home atoms - offset of local halo region
172     int numHomeAtoms_ = 0;
173     //! remote GPU coordinates buffer pointer for pushing data
174     void* remoteXPtr_ = nullptr;
175     //! remote GPU force buffer pointer for pushing data
176     void* remoteFPtr_ = nullptr;
177     //! Periodic Boundary Conditions for this rank
178     bool usePBC_ = false;
179     //! force shift buffer on device
180     float3* d_fShift_ = nullptr;
181     //! Event triggered when halo transfer has been launched with direct CUD memory copy
182     GpuEventSynchronizer* haloDataTransferLaunched_ = nullptr;
183     //! MPI communicator used for simulation
184     MPI_Comm mpi_comm_mysim_;
185     //! GPU context object
186     const DeviceContext& deviceContext_;
187     //! CUDA stream for local non-bonded calculations
188     const DeviceStream& localStream_;
189     //! CUDA stream for non-local non-bonded calculations
190     const DeviceStream& nonLocalStream_;
191     //! full coordinates buffer in GPU memory
192     float3* d_x_ = nullptr;
193     //! full forces buffer in GPU memory
194     float3* d_f_ = nullptr;
195     //! An event recorded once the exchanged forces are ready on the GPU
196     GpuEventSynchronizer fReadyOnDevice_;
197     //! The pulse corresponding to this halo exchange instance
198     int pulse_ = 0;
199     //! Number of zones. Always 1 for 1-D case.
200     const int nzone_ = 1;
201 };
202
203 } // namespace gmx
204
205 #endif