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 Definitions of interfaces for GPU state data propagator object.
39 * \author Artem Zhmurov <zhmurov@gmail.com>
41 * \ingroup module_mdtypes
47 #if GMX_GPU != GMX_GPU_NONE
49 #if GMX_GPU == GMX_GPU_CUDA
50 #include "gromacs/gpu_utils/cudautils.cuh"
52 #include "gromacs/gpu_utils/devicebuffer.h"
53 #if GMX_GPU == GMX_GPU_OPENCL
54 #include "gromacs/gpu_utils/oclutils.h"
56 #include "gromacs/math/vectypes.h"
57 #include "gromacs/mdtypes/state_propagator_data_gpu.h"
58 #include "gromacs/utility/classhelpers.h"
60 #include "state_propagator_data_gpu_impl.h"
65 StatePropagatorDataGpu::Impl::Impl(const void *pmeStream,
66 const void *localStream,
67 const void *nonLocalStream,
68 const void *deviceContext,
69 GpuApiCallBehavior transferKind,
71 transferKind_(transferKind),
72 paddingSize_(paddingSize)
74 static_assert(GMX_GPU != GMX_GPU_NONE, "This object should only be constructed on the GPU code-paths.");
75 GMX_RELEASE_ASSERT(getenv("GMX_USE_GPU_BUFFER_OPS") == nullptr, "GPU buffer ops are not supported in this build.");
77 // TODO: Refactor when the StreamManager is introduced.
78 if (GMX_GPU == GMX_GPU_OPENCL)
80 GMX_ASSERT(deviceContext != nullptr, "GPU context should be set in OpenCL builds.");
81 GMX_ASSERT(pmeStream != nullptr, "GPU PME stream should be set in OpenCL builds.");
83 // The update stream is set to the PME stream in OpenCL, since PME stream is the only stream created in the PME context.
84 pmeStream_ = *static_cast<const CommandStream*>(pmeStream);
85 updateStream_ = *static_cast<const CommandStream*>(pmeStream);
86 deviceContext_ = *static_cast<const DeviceContext*>(deviceContext);
87 GMX_UNUSED_VALUE(localStream);
88 GMX_UNUSED_VALUE(nonLocalStream);
91 if (GMX_GPU == GMX_GPU_CUDA)
93 if (pmeStream != nullptr)
95 pmeStream_ = *static_cast<const CommandStream*>(pmeStream);
97 if (localStream != nullptr)
99 localStream_ = *static_cast<const CommandStream*>(localStream);
101 if (nonLocalStream != nullptr)
103 nonLocalStream_ = *static_cast<const CommandStream*>(nonLocalStream);
106 // TODO: The update stream should be created only when it is needed.
107 #if (GMX_GPU == GMX_GPU_CUDA)
109 stat = cudaStreamCreate(&updateStream_);
110 CU_RET_ERR(stat, "CUDA stream creation failed in StatePropagatorDataGpu");
112 GMX_UNUSED_VALUE(deviceContext);
115 // Map the atom locality to the stream that will be used for coordinates transfer.
116 // Same streams are used for H2D and D2H copies
117 xCopyStreams_[AtomLocality::Local] = updateStream_;
118 xCopyStreams_[AtomLocality::NonLocal] = nonLocalStream_;
119 xCopyStreams_[AtomLocality::All] = updateStream_;
122 StatePropagatorDataGpu::Impl::~Impl()
126 void StatePropagatorDataGpu::Impl::reinit(int numAtomsLocal, int numAtomsAll)
128 numAtomsLocal_ = numAtomsLocal;
129 numAtomsAll_ = numAtomsAll;
132 if (paddingSize_ > 0)
134 numAtomsPadded = ((numAtomsAll_ + paddingSize_ - 1 ) / paddingSize_ )*paddingSize_;
138 numAtomsPadded = numAtomsAll_;
141 reallocateDeviceBuffer(&d_x_, DIM*numAtomsPadded, &d_xSize_, &d_xCapacity_, deviceContext_);
143 const size_t paddingAllocationSize = numAtomsPadded - numAtomsAll_;
144 if (paddingAllocationSize > 0)
146 // The PME stream is used here because the padding region of d_x_ is only in the PME task.
147 clearDeviceBufferAsync(&d_x_, DIM*numAtomsAll_, DIM*paddingAllocationSize, pmeStream_);
150 reallocateDeviceBuffer(&d_v_, DIM*numAtomsAll_, &d_vSize_, &d_vCapacity_, deviceContext_);
151 reallocateDeviceBuffer(&d_f_, DIM*numAtomsAll_, &d_fSize_, &d_fCapacity_, deviceContext_);
155 std::tuple<int, int> StatePropagatorDataGpu::Impl::getAtomRangesFromAtomLocality(AtomLocality atomLocality)
157 int atomsStartAt = 0;
158 int numAtomsToCopy = 0;
159 switch (atomLocality)
161 case AtomLocality::All:
163 numAtomsToCopy = numAtomsAll_;
165 case AtomLocality::Local:
167 numAtomsToCopy = numAtomsLocal_;
169 case AtomLocality::NonLocal:
170 atomsStartAt = numAtomsLocal_;
171 numAtomsToCopy = numAtomsAll_ - numAtomsLocal_;
174 GMX_RELEASE_ASSERT(false, "Wrong range of atoms requested in GPU state data manager. Should be All, Local or NonLocal.");
176 GMX_ASSERT(atomsStartAt >= 0, "The first elemtnt to copy has negative index. Probably, the GPU propagator state was not initialized.");
177 GMX_ASSERT(numAtomsToCopy >= 0, "Number of atoms to copy is negative. Probably, the GPU propagator state was not initialized.");
178 return std::make_tuple(atomsStartAt, numAtomsToCopy);
181 void StatePropagatorDataGpu::Impl::copyToDevice(DeviceBuffer<float> d_data,
182 const gmx::ArrayRef<const gmx::RVec> h_data,
184 AtomLocality atomLocality,
185 CommandStream commandStream)
188 GMX_UNUSED_VALUE(dataSize);
190 GMX_ASSERT(dataSize >= 0, "Trying to copy to device buffer before it was allocated.");
192 int atomsStartAt, numAtomsToCopy;
193 std::tie(atomsStartAt, numAtomsToCopy) = getAtomRangesFromAtomLocality(atomLocality);
195 int elementsStartAt = atomsStartAt*DIM;
196 int numElementsToCopy = numAtomsToCopy*DIM;
198 if (numAtomsToCopy != 0)
200 GMX_ASSERT(elementsStartAt + numElementsToCopy <= dataSize, "The device allocation is smaller than requested copy range.");
201 GMX_ASSERT(atomsStartAt + numAtomsToCopy <= h_data.ssize(), "The host buffer is smaller than the requested copy range.");
203 copyToDeviceBuffer(&d_data, reinterpret_cast<const float *>(&h_data.data()[atomsStartAt]),
204 elementsStartAt, numElementsToCopy,
205 commandStream, transferKind_, nullptr);
209 void StatePropagatorDataGpu::Impl::copyFromDevice(gmx::ArrayRef<gmx::RVec> h_data,
210 DeviceBuffer<float> d_data,
212 AtomLocality atomLocality,
213 CommandStream commandStream)
216 GMX_UNUSED_VALUE(dataSize);
218 GMX_ASSERT(dataSize >= 0, "Trying to copy from device buffer before it was allocated.");
220 int atomsStartAt, numAtomsToCopy;
221 std::tie(atomsStartAt, numAtomsToCopy) = getAtomRangesFromAtomLocality(atomLocality);
223 int elementsStartAt = atomsStartAt*DIM;
224 int numElementsToCopy = numAtomsToCopy*DIM;
226 if (numAtomsToCopy != 0)
228 GMX_ASSERT(elementsStartAt + numElementsToCopy <= dataSize, "The device allocation is smaller than requested copy range.");
229 GMX_ASSERT(atomsStartAt + numAtomsToCopy <= h_data.ssize(), "The host buffer is smaller than the requested copy range.");
231 copyFromDeviceBuffer(reinterpret_cast<float*>(&h_data.data()[atomsStartAt]), &d_data,
232 elementsStartAt, numElementsToCopy,
233 commandStream, transferKind_, nullptr);
237 DeviceBuffer<float> StatePropagatorDataGpu::Impl::getCoordinates()
242 void StatePropagatorDataGpu::Impl::copyCoordinatesToGpu(const gmx::ArrayRef<const gmx::RVec> h_x,
243 AtomLocality atomLocality)
245 GMX_ASSERT(atomLocality < AtomLocality::Count, "Wrong atom locality.");
246 CommandStream commandStream = xCopyStreams_[atomLocality];
247 GMX_ASSERT(commandStream != nullptr, "No stream is valid for copying positions with given atom locality.");
249 copyToDevice(d_x_, h_x, d_xSize_, atomLocality, commandStream);
251 // markEvent is skipped in OpenCL as:
252 // - it's not needed, copy is done in the same stream as the only consumer task (PME)
253 // - we don't consume the events in OpenCL which is not allowed by GpuEventSynchronizer (would leak memory).
254 // TODO: remove this by adding an event-mark free flavor of this function
255 if (GMX_GPU == GMX_GPU_CUDA)
257 xReadyOnDevice_[atomLocality].markEvent(commandStream);
258 // TODO: Remove When event-based synchronization is introduced
259 gpuStreamSynchronize(commandStream);
263 GpuEventSynchronizer* StatePropagatorDataGpu::Impl::getCoordinatesReadyOnDeviceEvent(AtomLocality atomLocality)
265 return &xReadyOnDevice_[atomLocality];
268 void StatePropagatorDataGpu::Impl::copyCoordinatesFromGpu(gmx::ArrayRef<gmx::RVec> h_x,
269 AtomLocality atomLocality)
271 GMX_ASSERT(atomLocality < AtomLocality::Count, "Wrong atom locality.");
272 CommandStream commandStream = xCopyStreams_[atomLocality];
273 GMX_ASSERT(commandStream != nullptr, "No stream is valid for copying positions with given atom locality.");
275 copyFromDevice(h_x, d_x_, d_xSize_, atomLocality, commandStream);
276 // TODO: Remove When event-based synchronization is introduced
277 gpuStreamSynchronize(commandStream);
278 // Note: unlike copyCoordinatesToGpu this is not used in OpenCL, and the conditional is not needed.
279 xReadyOnHost_[atomLocality].markEvent(commandStream);
282 void StatePropagatorDataGpu::Impl::waitCoordinatesReadyOnHost(AtomLocality atomLocality)
284 xReadyOnHost_[atomLocality].waitForEvent();
288 DeviceBuffer<float> StatePropagatorDataGpu::Impl::getVelocities()
293 void StatePropagatorDataGpu::Impl::copyVelocitiesToGpu(const gmx::ArrayRef<const gmx::RVec> h_v,
294 AtomLocality atomLocality)
296 // TODO: Use the correct stream
297 copyToDevice(d_v_, h_v, d_vSize_, atomLocality, nullptr);
300 void StatePropagatorDataGpu::Impl::copyVelocitiesFromGpu(gmx::ArrayRef<gmx::RVec> h_v,
301 AtomLocality atomLocality)
303 // TODO: Use the correct stream
304 copyFromDevice(h_v, d_v_, d_vSize_, atomLocality, nullptr);
308 DeviceBuffer<float> StatePropagatorDataGpu::Impl::getForces()
313 void StatePropagatorDataGpu::Impl::copyForcesToGpu(const gmx::ArrayRef<const gmx::RVec> h_f,
314 AtomLocality atomLocality)
316 // TODO: Use the correct stream
317 copyToDevice(d_f_, h_f, d_fSize_, atomLocality, nullptr);
320 void StatePropagatorDataGpu::Impl::copyForcesFromGpu(gmx::ArrayRef<gmx::RVec> h_f,
321 AtomLocality atomLocality)
323 // TODO: Use the correct stream
324 copyFromDevice(h_f, d_f_, d_fSize_, atomLocality, nullptr);
327 void* StatePropagatorDataGpu::Impl::getUpdateStream()
329 return updateStream_;
332 int StatePropagatorDataGpu::Impl::numAtomsLocal()
334 return numAtomsLocal_;
337 int StatePropagatorDataGpu::Impl::numAtomsAll()
344 StatePropagatorDataGpu::StatePropagatorDataGpu(const void *pmeStream,
345 const void *localStream,
346 const void *nonLocalStream,
347 const void *deviceContext,
348 GpuApiCallBehavior transferKind,
350 : impl_(new Impl(pmeStream,
359 StatePropagatorDataGpu::StatePropagatorDataGpu(StatePropagatorDataGpu && /* other */) noexcept = default;
361 StatePropagatorDataGpu &StatePropagatorDataGpu::operator=(StatePropagatorDataGpu && /* other */) noexcept = default;
363 StatePropagatorDataGpu::~StatePropagatorDataGpu() = default;
366 void StatePropagatorDataGpu::reinit(int numAtomsLocal, int numAtomsAll)
368 return impl_->reinit(numAtomsLocal, numAtomsAll);
371 std::tuple<int, int> StatePropagatorDataGpu::getAtomRangesFromAtomLocality(AtomLocality atomLocality)
373 return impl_->getAtomRangesFromAtomLocality(atomLocality);
377 DeviceBuffer<float> StatePropagatorDataGpu::getCoordinates()
379 return impl_->getCoordinates();
382 void StatePropagatorDataGpu::copyCoordinatesToGpu(const gmx::ArrayRef<const gmx::RVec> h_x,
383 AtomLocality atomLocality)
385 return impl_->copyCoordinatesToGpu(h_x, atomLocality);
388 GpuEventSynchronizer* StatePropagatorDataGpu::getCoordinatesReadyOnDeviceEvent(AtomLocality atomLocality)
390 return impl_->getCoordinatesReadyOnDeviceEvent(atomLocality);
393 void StatePropagatorDataGpu::copyCoordinatesFromGpu(gmx::ArrayRef<RVec> h_x,
394 AtomLocality atomLocality)
396 return impl_->copyCoordinatesFromGpu(h_x, atomLocality);
399 void StatePropagatorDataGpu::waitCoordinatesReadyOnHost(AtomLocality atomLocality)
401 return impl_->waitCoordinatesReadyOnHost(atomLocality);
405 DeviceBuffer<float> StatePropagatorDataGpu::getVelocities()
407 return impl_->getVelocities();
410 void StatePropagatorDataGpu::copyVelocitiesToGpu(const gmx::ArrayRef<const gmx::RVec> h_v,
411 AtomLocality atomLocality)
413 return impl_->copyVelocitiesToGpu(h_v, atomLocality);
416 void StatePropagatorDataGpu::copyVelocitiesFromGpu(gmx::ArrayRef<RVec> h_v,
417 AtomLocality atomLocality)
419 return impl_->copyVelocitiesFromGpu(h_v, atomLocality);
423 DeviceBuffer<float> StatePropagatorDataGpu::getForces()
425 return impl_->getForces();
428 void StatePropagatorDataGpu::copyForcesToGpu(const gmx::ArrayRef<const gmx::RVec> h_f,
429 AtomLocality atomLocality)
431 return impl_->copyForcesToGpu(h_f, atomLocality);
434 void StatePropagatorDataGpu::copyForcesFromGpu(gmx::ArrayRef<RVec> h_f,
435 AtomLocality atomLocality)
437 return impl_->copyForcesFromGpu(h_f, atomLocality);
440 void* StatePropagatorDataGpu::getUpdateStream()
442 return impl_->getUpdateStream();
445 int StatePropagatorDataGpu::numAtomsLocal()
447 return impl_->numAtomsLocal();
450 int StatePropagatorDataGpu::numAtomsAll()
452 return impl_->numAtomsAll();
457 #endif // GMX_GPU == GMX_GPU_NONE