2 * This file is part of the GROMACS molecular simulation package.
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.
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
49 # include "gromacs/gpu_utils/device_stream_manager.h"
50 # include "gromacs/gpu_utils/devicebuffer.h"
51 # include "gromacs/gpu_utils/gpueventsynchronizer.h"
52 # include "gromacs/math/vectypes.h"
53 # include "gromacs/mdtypes/state_propagator_data_gpu.h"
54 # include "gromacs/timing/wallcycle.h"
55 # include "gromacs/utility/classhelpers.h"
57 # include "state_propagator_data_gpu_impl.h"
63 StatePropagatorDataGpu::Impl::Impl(const DeviceStreamManager& deviceStreamManager,
64 GpuApiCallBehavior transferKind,
65 int allocationBlockSizeDivisor,
66 gmx_wallcycle* wcycle) :
67 deviceContext_(deviceStreamManager.context()),
68 transferKind_(transferKind),
69 allocationBlockSizeDivisor_(allocationBlockSizeDivisor),
74 "GPU state propagator data object should only be constructed on the GPU code-paths.");
76 // We need to keep local copies for re-initialization.
77 pmeStream_ = &deviceStreamManager.stream(DeviceStreamType::Pme);
78 localStream_ = &deviceStreamManager.stream(DeviceStreamType::NonBondedLocal);
79 nonLocalStream_ = &deviceStreamManager.stream(DeviceStreamType::NonBondedNonLocal);
80 updateStream_ = &deviceStreamManager.stream(DeviceStreamType::UpdateAndConstraints);
82 // Map the atom locality to the stream that will be used for coordinates,
83 // velocities and forces transfers. Same streams are used for H2D and D2H copies.
84 // Note, that nullptr stream is used here to indicate that the copy is not supported.
85 xCopyStreams_[AtomLocality::Local] = updateStream_;
86 xCopyStreams_[AtomLocality::NonLocal] = nonLocalStream_;
87 xCopyStreams_[AtomLocality::All] = nullptr;
89 vCopyStreams_[AtomLocality::Local] = updateStream_;
90 vCopyStreams_[AtomLocality::NonLocal] = nullptr;
91 vCopyStreams_[AtomLocality::All] = nullptr;
93 fCopyStreams_[AtomLocality::Local] = localStream_;
94 fCopyStreams_[AtomLocality::NonLocal] = nonLocalStream_;
95 fCopyStreams_[AtomLocality::All] = updateStream_;
97 copyInStream_ = std::make_unique<DeviceStream>(deviceContext_, DeviceStreamPriority::Normal, false);
98 memsetStream_ = std::make_unique<DeviceStream>(deviceContext_, DeviceStreamPriority::Normal, false);
101 StatePropagatorDataGpu::Impl::Impl(const DeviceStream* pmeStream,
102 const DeviceContext& deviceContext,
103 GpuApiCallBehavior transferKind,
104 int allocationBlockSizeDivisor,
105 gmx_wallcycle* wcycle) :
106 deviceContext_(deviceContext),
107 transferKind_(transferKind),
108 allocationBlockSizeDivisor_(allocationBlockSizeDivisor),
113 "GPU state propagator data object should only be constructed on the GPU code-paths.");
115 GMX_ASSERT(pmeStream->isValid(), "GPU PME stream should be valid.");
116 pmeStream_ = pmeStream;
117 localStream_ = pmeStream; // For clearing the force buffer
118 nonLocalStream_ = nullptr;
119 updateStream_ = nullptr;
123 // Only local/all coordinates are allowed to be copied in PME-only rank/ PME tests.
124 // This it temporary measure to make it safe to use this class in those cases.
125 xCopyStreams_[AtomLocality::Local] = pmeStream_;
126 xCopyStreams_[AtomLocality::NonLocal] = nullptr;
127 xCopyStreams_[AtomLocality::All] = nullptr;
129 vCopyStreams_[AtomLocality::Local] = nullptr;
130 vCopyStreams_[AtomLocality::NonLocal] = nullptr;
131 vCopyStreams_[AtomLocality::All] = nullptr;
133 fCopyStreams_[AtomLocality::Local] = nullptr;
134 fCopyStreams_[AtomLocality::NonLocal] = nullptr;
135 fCopyStreams_[AtomLocality::All] = nullptr;
138 StatePropagatorDataGpu::Impl::~Impl() {}
140 void StatePropagatorDataGpu::Impl::reinit(int numAtomsLocal, int numAtomsAll)
142 wallcycle_start_nocount(wcycle_, WallCycleCounter::LaunchGpu);
143 wallcycle_sub_start_nocount(wcycle_, WallCycleSubCounter::LaunchStatePropagatorData);
145 numAtomsLocal_ = numAtomsLocal;
146 numAtomsAll_ = numAtomsAll;
149 if (allocationBlockSizeDivisor_ > 0)
151 numAtomsPadded = ((numAtomsAll_ + allocationBlockSizeDivisor_ - 1) / allocationBlockSizeDivisor_)
152 * allocationBlockSizeDivisor_;
156 numAtomsPadded = numAtomsAll_;
159 reallocateDeviceBuffer(&d_x_, numAtomsPadded, &d_xSize_, &d_xCapacity_, deviceContext_);
161 const size_t paddingAllocationSize = numAtomsPadded - numAtomsAll_;
162 if (paddingAllocationSize > 0)
164 // The PME stream is used here because the padding region of d_x_ is only in the PME task.
165 clearDeviceBufferAsync(&d_x_, numAtomsAll_, paddingAllocationSize, *pmeStream_);
168 reallocateDeviceBuffer(&d_v_, numAtomsAll_, &d_vSize_, &d_vCapacity_, deviceContext_);
169 const int d_fOldCapacity = d_fCapacity_;
170 reallocateDeviceBuffer(&d_f_, numAtomsAll_, &d_fSize_, &d_fCapacity_, deviceContext_);
172 // Clearing of the forces can be done in local stream since the nonlocal stream cannot reach
173 // the force accumulation stage before syncing with the local stream. Only done in CUDA and
174 // SYCL, since the force buffer ops are not implemented in OpenCL.
175 static constexpr bool sc_haveGpuFBufferOps = ((GMX_GPU_CUDA != 0) || (GMX_GPU_SYCL != 0));
176 if (sc_haveGpuFBufferOps && d_fCapacity_ != d_fOldCapacity)
178 clearDeviceBufferAsync(&d_f_, 0, d_fCapacity_, *localStream_);
181 wallcycle_sub_stop(wcycle_, WallCycleSubCounter::LaunchStatePropagatorData);
182 wallcycle_stop(wcycle_, WallCycleCounter::LaunchGpu);
185 std::tuple<int, int> StatePropagatorDataGpu::Impl::getAtomRangesFromAtomLocality(AtomLocality atomLocality) const
187 int atomsStartAt = 0;
188 int numAtomsToCopy = 0;
189 switch (atomLocality)
191 case AtomLocality::All:
193 numAtomsToCopy = numAtomsAll_;
195 case AtomLocality::Local:
197 numAtomsToCopy = numAtomsLocal_;
199 case AtomLocality::NonLocal:
200 atomsStartAt = numAtomsLocal_;
201 numAtomsToCopy = numAtomsAll_ - numAtomsLocal_;
204 GMX_RELEASE_ASSERT(false,
205 "Wrong range of atoms requested in GPU state data manager. Should "
206 "be All, Local or NonLocal.");
208 GMX_ASSERT(atomsStartAt >= 0,
209 "The first element to copy has negative index. Probably, the GPU propagator state "
210 "was not initialized.");
211 GMX_ASSERT(numAtomsToCopy >= 0,
212 "Number of atoms to copy is negative. Probably, the GPU propagator state was not "
214 return std::make_tuple(atomsStartAt, numAtomsToCopy);
217 void StatePropagatorDataGpu::Impl::copyToDevice(DeviceBuffer<RVec> d_data,
218 const gmx::ArrayRef<const gmx::RVec> h_data,
220 AtomLocality atomLocality,
221 const DeviceStream& deviceStream)
223 GMX_UNUSED_VALUE(dataSize);
225 GMX_ASSERT(atomLocality < AtomLocality::Count, "Wrong atom locality.");
227 GMX_ASSERT(dataSize >= 0, "Trying to copy to device buffer before it was allocated.");
229 GMX_ASSERT(deviceStream.isValid(), "No stream is valid for copying with given atom locality.");
231 int atomsStartAt, numAtomsToCopy;
232 std::tie(atomsStartAt, numAtomsToCopy) = getAtomRangesFromAtomLocality(atomLocality);
234 if (numAtomsToCopy != 0)
236 GMX_ASSERT(atomsStartAt + numAtomsToCopy <= dataSize,
237 "The device allocation is smaller than requested copy range.");
238 GMX_ASSERT(atomsStartAt + numAtomsToCopy <= h_data.ssize(),
239 "The host buffer is smaller than the requested copy range.");
241 copyToDeviceBuffer(&d_data,
242 reinterpret_cast<const RVec*>(&h_data.data()[atomsStartAt]),
251 void StatePropagatorDataGpu::Impl::copyFromDevice(gmx::ArrayRef<gmx::RVec> h_data,
252 DeviceBuffer<RVec> d_data,
254 AtomLocality atomLocality,
255 const DeviceStream& deviceStream)
257 GMX_UNUSED_VALUE(dataSize);
259 GMX_ASSERT(atomLocality < AtomLocality::Count, "Wrong atom locality.");
261 GMX_ASSERT(dataSize >= 0, "Trying to copy from device buffer before it was allocated.");
263 GMX_ASSERT(deviceStream.isValid(), "No stream is valid for copying with given atom locality.");
265 int atomsStartAt, numAtomsToCopy;
266 std::tie(atomsStartAt, numAtomsToCopy) = getAtomRangesFromAtomLocality(atomLocality);
268 if (numAtomsToCopy != 0)
270 GMX_ASSERT(atomsStartAt + numAtomsToCopy <= dataSize,
271 "The device allocation is smaller than requested copy range.");
272 GMX_ASSERT(atomsStartAt + numAtomsToCopy <= h_data.ssize(),
273 "The host buffer is smaller than the requested copy range.");
275 copyFromDeviceBuffer(reinterpret_cast<RVec*>(&h_data.data()[atomsStartAt]),
285 void StatePropagatorDataGpu::Impl::clearOnDevice(DeviceBuffer<RVec> d_data,
287 AtomLocality atomLocality,
288 const DeviceStream& deviceStream) const
290 GMX_UNUSED_VALUE(dataSize);
292 GMX_ASSERT(atomLocality < AtomLocality::Count, "Wrong atom locality.");
294 GMX_ASSERT(dataSize >= 0, "Trying to clear to device buffer before it was allocated.");
296 GMX_ASSERT(deviceStream.isValid(), "No stream is valid for clearing with given atom locality.");
298 int atomsStartAt, numAtomsToClear;
299 std::tie(atomsStartAt, numAtomsToClear) = getAtomRangesFromAtomLocality(atomLocality);
301 if (numAtomsToClear != 0)
303 GMX_ASSERT(atomsStartAt + numAtomsToClear <= dataSize,
304 "The device allocation is smaller than requested clear range.");
306 clearDeviceBufferAsync(&d_data, atomsStartAt, numAtomsToClear, deviceStream);
310 DeviceBuffer<RVec> StatePropagatorDataGpu::Impl::getCoordinates()
315 void StatePropagatorDataGpu::Impl::copyCoordinatesToGpu(const gmx::ArrayRef<const gmx::RVec> h_x,
316 AtomLocality atomLocality)
318 GMX_ASSERT(atomLocality < AtomLocality::All,
319 formatString("Wrong atom locality. Only Local and NonLocal are allowed for "
320 "coordinate transfers, passed value is \"%s\"",
321 enumValueToString(atomLocality))
324 const DeviceStream* deviceStream = xCopyStreams_[atomLocality];
325 GMX_ASSERT(deviceStream != nullptr,
326 "No stream is valid for copying positions with given atom locality.");
328 wallcycle_start_nocount(wcycle_, WallCycleCounter::LaunchGpu);
329 wallcycle_sub_start(wcycle_, WallCycleSubCounter::LaunchStatePropagatorData);
331 copyToDevice(d_x_, h_x, d_xSize_, atomLocality, *deviceStream);
333 // marking is skipped on the PME-rank mode as everything is on the same stream
336 xReadyOnDevice_[atomLocality].markEvent(*deviceStream);
339 wallcycle_sub_stop(wcycle_, WallCycleSubCounter::LaunchStatePropagatorData);
340 wallcycle_stop(wcycle_, WallCycleCounter::LaunchGpu);
343 GpuEventSynchronizer* StatePropagatorDataGpu::Impl::getCoordinatesReadyOnDeviceEvent(
344 AtomLocality atomLocality,
345 const SimulationWorkload& simulationWork,
346 const StepWorkload& stepWork,
347 GpuEventSynchronizer* gpuCoordinateHaloLaunched)
349 // The provider of the coordinates may be different for local atoms. If the update is offloaded
350 // and this is not a neighbor search step, then the consumer needs to wait for the update
351 // to complete. Otherwise, the coordinates are copied from the host and we need to wait for
352 // the copy event. Non-local coordinates are provided by the GPU halo exchange (if active), otherwise by H2D copy.
354 if (atomLocality == AtomLocality::NonLocal && stepWork.useGpuXHalo)
356 GMX_ASSERT(gpuCoordinateHaloLaunched != nullptr,
357 "GPU halo exchange is active but its completion event is null.");
358 return gpuCoordinateHaloLaunched;
360 if (atomLocality == AtomLocality::Local && simulationWork.useGpuUpdate && !stepWork.doNeighborSearch)
362 GMX_ASSERT(xUpdatedOnDeviceEvent_ != nullptr, "The event synchronizer can not be nullptr.");
363 return xUpdatedOnDeviceEvent_;
367 if (stepWork.doNeighborSearch && xUpdatedOnDeviceEvent_)
369 /* On search steps, we do not consume the result of the GPU update
370 * but rather that of a H2D transfer. So, we reset the event triggered after
371 * update to avoid leaving it unconsumed.
372 * Unfortunately, we don't always have the event marked either (e.g., on the
373 * first step) so we just reset it here.
374 * See Issue #3988. */
375 xUpdatedOnDeviceEvent_->reset();
377 return &xReadyOnDevice_[atomLocality];
381 void StatePropagatorDataGpu::Impl::waitCoordinatesCopiedToDevice(AtomLocality atomLocality)
383 wallcycle_start(wcycle_, WallCycleCounter::WaitGpuStatePropagatorData);
384 GMX_ASSERT(atomLocality < AtomLocality::Count, "Wrong atom locality.");
385 xReadyOnDevice_[atomLocality].waitForEvent();
386 wallcycle_stop(wcycle_, WallCycleCounter::WaitGpuStatePropagatorData);
389 void StatePropagatorDataGpu::Impl::consumeCoordinatesCopiedToDeviceEvent(AtomLocality atomLocality)
391 GMX_ASSERT(atomLocality < AtomLocality::Count, "Wrong atom locality.");
392 xReadyOnDevice_[atomLocality].consume();
395 void StatePropagatorDataGpu::Impl::resetCoordinatesCopiedToDeviceEvent(AtomLocality atomLocality)
397 GMX_ASSERT(atomLocality < AtomLocality::Count, "Wrong atom locality.");
398 xReadyOnDevice_[atomLocality].reset();
401 void StatePropagatorDataGpu::Impl::setXUpdatedOnDeviceEvent(GpuEventSynchronizer* xUpdatedOnDeviceEvent)
403 GMX_ASSERT(xUpdatedOnDeviceEvent != nullptr, "The event synchronizer can not be nullptr.");
404 xUpdatedOnDeviceEvent_ = xUpdatedOnDeviceEvent;
407 void StatePropagatorDataGpu::Impl::copyCoordinatesFromGpu(gmx::ArrayRef<gmx::RVec> h_x,
408 AtomLocality atomLocality,
409 GpuEventSynchronizer* dependency)
411 GMX_ASSERT(atomLocality < AtomLocality::All,
412 formatString("Wrong atom locality. Only Local and NonLocal are allowed for "
413 "coordinate transfers, passed value is \"%s\"",
414 enumValueToString(atomLocality))
416 const DeviceStream* deviceStream = xCopyStreams_[atomLocality];
417 GMX_ASSERT(deviceStream != nullptr,
418 "No stream is valid for copying positions with given atom locality.");
420 if (dependency != nullptr)
422 dependency->enqueueWaitEvent(*deviceStream);
425 wallcycle_start_nocount(wcycle_, WallCycleCounter::LaunchGpu);
426 wallcycle_sub_start(wcycle_, WallCycleSubCounter::LaunchStatePropagatorData);
428 copyFromDevice(h_x, d_x_, d_xSize_, atomLocality, *deviceStream);
429 // Note: unlike copyCoordinatesToGpu this is not used in OpenCL, and the conditional is not needed.
430 xReadyOnHost_[atomLocality].markEvent(*deviceStream);
432 wallcycle_sub_stop(wcycle_, WallCycleSubCounter::LaunchStatePropagatorData);
433 wallcycle_stop(wcycle_, WallCycleCounter::LaunchGpu);
436 void StatePropagatorDataGpu::Impl::waitCoordinatesReadyOnHost(AtomLocality atomLocality)
438 wallcycle_start(wcycle_, WallCycleCounter::WaitGpuStatePropagatorData);
439 xReadyOnHost_[atomLocality].waitForEvent();
440 wallcycle_stop(wcycle_, WallCycleCounter::WaitGpuStatePropagatorData);
444 DeviceBuffer<RVec> StatePropagatorDataGpu::Impl::getVelocities()
449 void StatePropagatorDataGpu::Impl::copyVelocitiesToGpu(const gmx::ArrayRef<const gmx::RVec> h_v,
450 AtomLocality atomLocality)
452 GMX_ASSERT(atomLocality == AtomLocality::Local,
453 formatString("Wrong atom locality. Only Local is allowed for "
454 "velocity transfers, passed value is \"%s\"",
455 enumValueToString(atomLocality))
457 const DeviceStream* deviceStream = vCopyStreams_[atomLocality];
458 GMX_ASSERT(deviceStream != nullptr,
459 "No stream is valid for copying velocities with given atom locality.");
461 wallcycle_start_nocount(wcycle_, WallCycleCounter::LaunchGpu);
462 wallcycle_sub_start(wcycle_, WallCycleSubCounter::LaunchStatePropagatorData);
464 copyToDevice(d_v_, h_v, d_vSize_, atomLocality, *deviceStream);
465 /* Not marking the event, because it is not used anywhere.
466 * Since we only use velocities on the device for update, and we launch the copy in
467 * the "update" stream, that should be safe.
470 wallcycle_sub_stop(wcycle_, WallCycleSubCounter::LaunchStatePropagatorData);
471 wallcycle_stop(wcycle_, WallCycleCounter::LaunchGpu);
474 void StatePropagatorDataGpu::Impl::copyVelocitiesFromGpu(gmx::ArrayRef<gmx::RVec> h_v, AtomLocality atomLocality)
476 GMX_ASSERT(atomLocality == AtomLocality::Local,
477 formatString("Wrong atom locality. Only Local is allowed for "
478 "velocity transfers, passed value is \"%s\"",
479 enumValueToString(atomLocality))
481 const DeviceStream* deviceStream = vCopyStreams_[atomLocality];
482 GMX_ASSERT(deviceStream != nullptr,
483 "No stream is valid for copying velocities with given atom locality.");
485 wallcycle_start_nocount(wcycle_, WallCycleCounter::LaunchGpu);
486 wallcycle_sub_start(wcycle_, WallCycleSubCounter::LaunchStatePropagatorData);
488 copyFromDevice(h_v, d_v_, d_vSize_, atomLocality, *deviceStream);
489 vReadyOnHost_[atomLocality].markEvent(*deviceStream);
491 wallcycle_sub_stop(wcycle_, WallCycleSubCounter::LaunchStatePropagatorData);
492 wallcycle_stop(wcycle_, WallCycleCounter::LaunchGpu);
495 void StatePropagatorDataGpu::Impl::waitVelocitiesReadyOnHost(AtomLocality atomLocality)
497 wallcycle_start(wcycle_, WallCycleCounter::WaitGpuStatePropagatorData);
498 vReadyOnHost_[atomLocality].waitForEvent();
499 wallcycle_stop(wcycle_, WallCycleCounter::WaitGpuStatePropagatorData);
503 DeviceBuffer<RVec> StatePropagatorDataGpu::Impl::getForces()
508 // Copy CPU forces to GPU using stream internal to this module to allow overlap
509 // with GPU force calculations.
510 void StatePropagatorDataGpu::Impl::copyForcesToGpu(const gmx::ArrayRef<const gmx::RVec> h_f,
511 AtomLocality atomLocality)
513 GMX_ASSERT(atomLocality < AtomLocality::Count, "Wrong atom locality.");
514 DeviceStream* deviceStream = copyInStream_.get();
515 GMX_ASSERT(deviceStream != nullptr,
516 "No stream is valid for copying forces with given atom locality.");
518 wallcycle_start_nocount(wcycle_, WallCycleCounter::LaunchGpu);
519 wallcycle_sub_start(wcycle_, WallCycleSubCounter::LaunchStatePropagatorData);
521 copyToDevice(d_f_, h_f, d_fSize_, atomLocality, *deviceStream);
522 fReadyOnDevice_[atomLocality].markEvent(*deviceStream);
524 wallcycle_sub_stop(wcycle_, WallCycleSubCounter::LaunchStatePropagatorData);
525 wallcycle_stop(wcycle_, WallCycleCounter::LaunchGpu);
528 void StatePropagatorDataGpu::Impl::clearForcesOnGpu(AtomLocality atomLocality, GpuEventSynchronizer* dependency)
530 GMX_ASSERT(atomLocality < AtomLocality::Count, "Wrong atom locality.");
531 DeviceStream* deviceStream = memsetStream_.get();
533 GMX_ASSERT(dependency != nullptr, "Dependency is not valid for clearing forces.");
534 dependency->enqueueWaitEvent(*deviceStream);
536 GMX_ASSERT(deviceStream != nullptr,
537 "No stream is valid for clearing forces with given atom locality.");
539 wallcycle_start_nocount(wcycle_, WallCycleCounter::LaunchGpu);
540 wallcycle_sub_start(wcycle_, WallCycleSubCounter::LaunchStatePropagatorData);
542 clearOnDevice(d_f_, d_fSize_, atomLocality, *deviceStream);
544 fReadyOnDevice_[atomLocality].markEvent(*deviceStream);
546 wallcycle_sub_stop(wcycle_, WallCycleSubCounter::LaunchStatePropagatorData);
547 wallcycle_stop(wcycle_, WallCycleCounter::LaunchGpu);
550 GpuEventSynchronizer* StatePropagatorDataGpu::Impl::getLocalForcesReadyOnDeviceEvent(StepWorkload stepWork,
551 SimulationWorkload simulationWork)
553 if (stepWork.useGpuFBufferOps && !simulationWork.useCpuPmePpCommunication)
555 return &fReducedOnDevice_[AtomLocality::Local];
559 return &fReadyOnDevice_[AtomLocality::Local];
563 GpuEventSynchronizer* StatePropagatorDataGpu::Impl::fReducedOnDevice(AtomLocality atomLocality)
565 return &fReducedOnDevice_[atomLocality];
568 void StatePropagatorDataGpu::Impl::consumeForcesReducedOnDeviceEvent(AtomLocality atomLocality)
570 fReducedOnDevice_[atomLocality].consume();
573 GpuEventSynchronizer* StatePropagatorDataGpu::Impl::fReadyOnDevice(AtomLocality atomLocality)
575 return &fReadyOnDevice_[atomLocality];
578 void StatePropagatorDataGpu::Impl::copyForcesFromGpu(gmx::ArrayRef<gmx::RVec> h_f, AtomLocality atomLocality)
580 GMX_ASSERT(atomLocality < AtomLocality::Count, "Wrong atom locality.");
581 const DeviceStream* deviceStream = fCopyStreams_[atomLocality];
582 GMX_ASSERT(deviceStream != nullptr,
583 "No stream is valid for copying forces with given atom locality.");
585 wallcycle_start_nocount(wcycle_, WallCycleCounter::LaunchGpu);
586 wallcycle_sub_start(wcycle_, WallCycleSubCounter::LaunchStatePropagatorData);
588 copyFromDevice(h_f, d_f_, d_fSize_, atomLocality, *deviceStream);
589 fReadyOnHost_[atomLocality].markEvent(*deviceStream);
591 wallcycle_sub_stop(wcycle_, WallCycleSubCounter::LaunchStatePropagatorData);
592 wallcycle_stop(wcycle_, WallCycleCounter::LaunchGpu);
595 void StatePropagatorDataGpu::Impl::waitForcesReadyOnHost(AtomLocality atomLocality)
597 wallcycle_start(wcycle_, WallCycleCounter::WaitGpuStatePropagatorData);
598 fReadyOnHost_[atomLocality].waitForEvent();
599 wallcycle_stop(wcycle_, WallCycleCounter::WaitGpuStatePropagatorData);
602 const DeviceStream* StatePropagatorDataGpu::Impl::getUpdateStream()
604 return updateStream_;
607 int StatePropagatorDataGpu::Impl::numAtomsLocal() const
609 return numAtomsLocal_;
612 int StatePropagatorDataGpu::Impl::numAtomsAll() const
618 StatePropagatorDataGpu::StatePropagatorDataGpu(const DeviceStreamManager& deviceStreamManager,
619 GpuApiCallBehavior transferKind,
620 int allocationBlockSizeDivisor,
621 gmx_wallcycle* wcycle) :
622 impl_(new Impl(deviceStreamManager, transferKind, allocationBlockSizeDivisor, wcycle))
626 StatePropagatorDataGpu::StatePropagatorDataGpu(const DeviceStream* pmeStream,
627 const DeviceContext& deviceContext,
628 GpuApiCallBehavior transferKind,
629 int allocationBlockSizeDivisor,
630 gmx_wallcycle* wcycle) :
631 impl_(new Impl(pmeStream, deviceContext, transferKind, allocationBlockSizeDivisor, wcycle))
635 StatePropagatorDataGpu::StatePropagatorDataGpu(StatePropagatorDataGpu&& /* other */) noexcept = default;
637 StatePropagatorDataGpu& StatePropagatorDataGpu::operator=(StatePropagatorDataGpu&& /* other */) noexcept = default;
639 StatePropagatorDataGpu::~StatePropagatorDataGpu() = default;
642 void StatePropagatorDataGpu::reinit(int numAtomsLocal, int numAtomsAll)
644 return impl_->reinit(numAtomsLocal, numAtomsAll);
647 std::tuple<int, int> StatePropagatorDataGpu::getAtomRangesFromAtomLocality(AtomLocality atomLocality) const
649 return impl_->getAtomRangesFromAtomLocality(atomLocality);
653 DeviceBuffer<RVec> StatePropagatorDataGpu::getCoordinates()
655 return impl_->getCoordinates();
658 void StatePropagatorDataGpu::copyCoordinatesToGpu(const gmx::ArrayRef<const gmx::RVec> h_x,
659 AtomLocality atomLocality)
661 return impl_->copyCoordinatesToGpu(h_x, atomLocality);
664 GpuEventSynchronizer*
665 StatePropagatorDataGpu::getCoordinatesReadyOnDeviceEvent(AtomLocality atomLocality,
666 const SimulationWorkload& simulationWork,
667 const StepWorkload& stepWork,
668 GpuEventSynchronizer* gpuCoordinateHaloLaunched)
670 return impl_->getCoordinatesReadyOnDeviceEvent(
671 atomLocality, simulationWork, stepWork, gpuCoordinateHaloLaunched);
674 void StatePropagatorDataGpu::waitCoordinatesCopiedToDevice(AtomLocality atomLocality)
676 return impl_->waitCoordinatesCopiedToDevice(atomLocality);
679 void StatePropagatorDataGpu::consumeCoordinatesCopiedToDeviceEvent(AtomLocality atomLocality)
681 return impl_->consumeCoordinatesCopiedToDeviceEvent(atomLocality);
684 void StatePropagatorDataGpu::resetCoordinatesCopiedToDeviceEvent(AtomLocality atomLocality)
686 return impl_->resetCoordinatesCopiedToDeviceEvent(atomLocality);
689 void StatePropagatorDataGpu::setXUpdatedOnDeviceEvent(GpuEventSynchronizer* xUpdatedOnDeviceEvent)
691 impl_->setXUpdatedOnDeviceEvent(xUpdatedOnDeviceEvent);
694 void StatePropagatorDataGpu::copyCoordinatesFromGpu(gmx::ArrayRef<RVec> h_x,
695 AtomLocality atomLocality,
696 GpuEventSynchronizer* dependency)
698 return impl_->copyCoordinatesFromGpu(h_x, atomLocality, dependency);
701 void StatePropagatorDataGpu::waitCoordinatesReadyOnHost(AtomLocality atomLocality)
703 return impl_->waitCoordinatesReadyOnHost(atomLocality);
707 DeviceBuffer<RVec> StatePropagatorDataGpu::getVelocities()
709 return impl_->getVelocities();
712 void StatePropagatorDataGpu::copyVelocitiesToGpu(const gmx::ArrayRef<const gmx::RVec> h_v,
713 AtomLocality atomLocality)
715 return impl_->copyVelocitiesToGpu(h_v, atomLocality);
718 void StatePropagatorDataGpu::copyVelocitiesFromGpu(gmx::ArrayRef<RVec> h_v, AtomLocality atomLocality)
720 return impl_->copyVelocitiesFromGpu(h_v, atomLocality);
723 void StatePropagatorDataGpu::waitVelocitiesReadyOnHost(AtomLocality atomLocality)
725 return impl_->waitVelocitiesReadyOnHost(atomLocality);
729 DeviceBuffer<RVec> StatePropagatorDataGpu::getForces()
731 return impl_->getForces();
734 void StatePropagatorDataGpu::copyForcesToGpu(const gmx::ArrayRef<const gmx::RVec> h_f, AtomLocality atomLocality)
736 return impl_->copyForcesToGpu(h_f, atomLocality);
739 void StatePropagatorDataGpu::clearForcesOnGpu(AtomLocality atomLocality, GpuEventSynchronizer* dependency)
741 return impl_->clearForcesOnGpu(atomLocality, dependency);
744 GpuEventSynchronizer* StatePropagatorDataGpu::getLocalForcesReadyOnDeviceEvent(StepWorkload stepWork,
745 SimulationWorkload simulationWork)
747 return impl_->getLocalForcesReadyOnDeviceEvent(stepWork, simulationWork);
750 GpuEventSynchronizer* StatePropagatorDataGpu::fReducedOnDevice(AtomLocality atomLocality)
752 return impl_->fReducedOnDevice(atomLocality);
755 void StatePropagatorDataGpu::consumeForcesReducedOnDeviceEvent(AtomLocality atomLocality)
757 impl_->consumeForcesReducedOnDeviceEvent(atomLocality);
760 GpuEventSynchronizer* StatePropagatorDataGpu::fReadyOnDevice(AtomLocality atomLocality)
762 return impl_->fReadyOnDevice(atomLocality);
765 void StatePropagatorDataGpu::copyForcesFromGpu(gmx::ArrayRef<RVec> h_f, AtomLocality atomLocality)
767 return impl_->copyForcesFromGpu(h_f, atomLocality);
770 void StatePropagatorDataGpu::waitForcesReadyOnHost(AtomLocality atomLocality)
772 return impl_->waitForcesReadyOnHost(atomLocality);
776 const DeviceStream* StatePropagatorDataGpu::getUpdateStream()
778 return impl_->getUpdateStream();
781 int StatePropagatorDataGpu::numAtomsLocal() const
783 return impl_->numAtomsLocal();
786 int StatePropagatorDataGpu::numAtomsAll() const
788 return impl_->numAtomsAll();