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