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