0d66883979cc4b90bea44e5ef7cc237c7017ae98
[alexxy/gromacs.git] / src / gromacs / ewald / pme_pp_comm_gpu_impl.cu
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 Implements PME-PP communication using CUDA
38  *
39  *
40  * \author Alan Gray <alang@nvidia.com>
41  *
42  * \ingroup module_ewald
43  */
44 #include "gmxpre.h"
45
46 #include "pme_pp_comm_gpu_impl.h"
47
48 #include "config.h"
49
50 #include "gromacs/gpu_utils/cudautils.cuh"
51 #include "gromacs/gpu_utils/device_context.h"
52 #include "gromacs/gpu_utils/device_stream.h"
53 #include "gromacs/gpu_utils/devicebuffer.h"
54 #include "gromacs/gpu_utils/gpueventsynchronizer.cuh"
55 #include "gromacs/utility/gmxmpi.h"
56
57 namespace gmx
58 {
59
60 PmePpCommGpu::Impl::Impl(MPI_Comm             comm,
61                          int                  pmeRank,
62                          const DeviceContext& deviceContext,
63                          const DeviceStream&  deviceStream) :
64     deviceContext_(deviceContext),
65     pmePpCommStream_(deviceStream),
66     comm_(comm),
67     pmeRank_(pmeRank)
68 {
69     GMX_RELEASE_ASSERT(
70             GMX_THREAD_MPI,
71             "PME-PP GPU Communication is currently only supported with thread-MPI enabled");
72 }
73
74 PmePpCommGpu::Impl::~Impl() = default;
75
76 void PmePpCommGpu::Impl::reinit(int size)
77 {
78     // This rank will access PME rank memory directly, so needs to receive the remote PME buffer addresses.
79 #if GMX_MPI
80     MPI_Recv(&remotePmeXBuffer_, sizeof(void**), MPI_BYTE, pmeRank_, 0, comm_, MPI_STATUS_IGNORE);
81     MPI_Recv(&remotePmeFBuffer_, sizeof(void**), MPI_BYTE, pmeRank_, 0, comm_, MPI_STATUS_IGNORE);
82
83     // Reallocate buffer used for staging PME force on GPU
84     reallocateDeviceBuffer(&d_pmeForces_, size, &d_pmeForcesSize_, &d_pmeForcesSizeAlloc_, deviceContext_);
85 #else
86     GMX_UNUSED_VALUE(size);
87 #endif
88     return;
89 }
90
91 // TODO make this asynchronous by splitting into this into
92 // launchRecvForceFromPmeCudaDirect() and sycnRecvForceFromPmeCudaDirect()
93 void PmePpCommGpu::Impl::receiveForceFromPmeCudaDirect(void* recvPtr, int recvSize, bool receivePmeForceToGpu)
94 {
95 #if GMX_MPI
96     // Receive event from PME task and add to stream, to ensure pull of data doesn't
97     // occur before PME force calc is completed
98     GpuEventSynchronizer* pmeSync;
99     MPI_Recv(&pmeSync, sizeof(GpuEventSynchronizer*), MPI_BYTE, pmeRank_, 0, comm_, MPI_STATUS_IGNORE);
100     pmeSync->enqueueWaitEvent(pmePpCommStream_);
101
102     // Pull force data from remote GPU
103     void*       pmeForcePtr = receivePmeForceToGpu ? static_cast<void*>(d_pmeForces_) : recvPtr;
104     cudaError_t stat        = cudaMemcpyAsync(pmeForcePtr,
105                                        remotePmeFBuffer_,
106                                        recvSize * DIM * sizeof(float),
107                                        cudaMemcpyDefault,
108                                        pmePpCommStream_.stream());
109     CU_RET_ERR(stat, "cudaMemcpyAsync on Recv from PME CUDA direct data transfer failed");
110
111     if (receivePmeForceToGpu)
112     {
113         // Record event to be enqueued in the GPU local buffer operations, to
114         // satisfy dependency on receiving the PME force data before
115         // reducing it with the other force contributions.
116         forcesReadySynchronizer_.markEvent(pmePpCommStream_);
117     }
118     else
119     {
120         // Ensure CPU waits for PME forces to be copied before reducing
121         // them with other forces on the CPU
122         cudaStreamSynchronize(pmePpCommStream_.stream());
123     }
124 #else
125     GMX_UNUSED_VALUE(recvPtr);
126     GMX_UNUSED_VALUE(recvSize);
127     GMX_UNUSED_VALUE(receivePmeForceToGpu);
128 #endif
129 }
130
131 void PmePpCommGpu::Impl::sendCoordinatesToPmeCudaDirect(void* sendPtr,
132                                                         int   sendSize,
133                                                         bool gmx_unused sendPmeCoordinatesFromGpu,
134                                                         GpuEventSynchronizer* coordinatesReadyOnDeviceEvent)
135 {
136 #if GMX_MPI
137     // ensure stream waits until coordinate data is available on device
138     coordinatesReadyOnDeviceEvent->enqueueWaitEvent(pmePpCommStream_);
139
140     cudaError_t stat = cudaMemcpyAsync(remotePmeXBuffer_,
141                                        sendPtr,
142                                        sendSize * DIM * sizeof(float),
143                                        cudaMemcpyDefault,
144                                        pmePpCommStream_.stream());
145     CU_RET_ERR(stat, "cudaMemcpyAsync on Send to PME CUDA direct data transfer failed");
146
147     // Record and send event to allow PME task to sync to above transfer before commencing force calculations
148     pmeCoordinatesSynchronizer_.markEvent(pmePpCommStream_);
149     GpuEventSynchronizer* pmeSync = &pmeCoordinatesSynchronizer_;
150     MPI_Send(&pmeSync, sizeof(GpuEventSynchronizer*), MPI_BYTE, pmeRank_, 0, comm_);
151 #else
152     GMX_UNUSED_VALUE(sendPtr);
153     GMX_UNUSED_VALUE(sendSize);
154     GMX_UNUSED_VALUE(sendPmeCoordinatesFromGpu);
155     GMX_UNUSED_VALUE(coordinatesReadyOnDeviceEvent);
156 #endif
157 }
158 DeviceBuffer<gmx::RVec> PmePpCommGpu::Impl::getGpuForceStagingPtr()
159 {
160     return d_pmeForces_;
161 }
162
163 GpuEventSynchronizer* PmePpCommGpu::Impl::getForcesReadySynchronizer()
164 {
165     return &forcesReadySynchronizer_;
166 }
167
168 PmePpCommGpu::PmePpCommGpu(MPI_Comm             comm,
169                            int                  pmeRank,
170                            const DeviceContext& deviceContext,
171                            const DeviceStream&  deviceStream) :
172     impl_(new Impl(comm, pmeRank, deviceContext, deviceStream))
173 {
174 }
175
176 PmePpCommGpu::~PmePpCommGpu() = default;
177
178 void PmePpCommGpu::reinit(int size)
179 {
180     impl_->reinit(size);
181 }
182
183 void PmePpCommGpu::receiveForceFromPmeCudaDirect(void* recvPtr, int recvSize, bool receivePmeForceToGpu)
184 {
185     impl_->receiveForceFromPmeCudaDirect(recvPtr, recvSize, receivePmeForceToGpu);
186 }
187
188 void PmePpCommGpu::sendCoordinatesToPmeCudaDirect(void*                 sendPtr,
189                                                   int                   sendSize,
190                                                   bool                  sendPmeCoordinatesFromGpu,
191                                                   GpuEventSynchronizer* coordinatesReadyOnDeviceEvent)
192 {
193     impl_->sendCoordinatesToPmeCudaDirect(
194             sendPtr, sendSize, sendPmeCoordinatesFromGpu, coordinatesReadyOnDeviceEvent);
195 }
196
197 DeviceBuffer<gmx::RVec> PmePpCommGpu::getGpuForceStagingPtr()
198 {
199     return impl_->getGpuForceStagingPtr();
200 }
201
202 GpuEventSynchronizer* PmePpCommGpu::getForcesReadySynchronizer()
203 {
204     return impl_->getForcesReadySynchronizer();
205 }
206
207 } // namespace gmx