Merge branch release-2020 into merge-2020-into-2021
[alexxy/gromacs.git] / src / gromacs / mdtypes / state_propagator_data_gpu_impl_gpu.cpp
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 Definitions of interfaces for GPU state data propagator object.
38  *
39  * \author Artem Zhmurov <zhmurov@gmail.com>
40  *
41  * \ingroup module_mdtypes
42  */
43 #include "gmxpre.h"
44
45 #include "config.h"
46
47 #if GMX_GPU
48
49 #    include "gromacs/gpu_utils/device_stream_manager.h"
50 #    include "gromacs/gpu_utils/devicebuffer.h"
51 #    include "gromacs/math/vectypes.h"
52 #    include "gromacs/mdtypes/state_propagator_data_gpu.h"
53 #    include "gromacs/timing/wallcycle.h"
54 #    include "gromacs/utility/classhelpers.h"
55
56 #    include "state_propagator_data_gpu_impl.h"
57
58
59 namespace gmx
60 {
61
62 StatePropagatorDataGpu::Impl::Impl(const DeviceStreamManager& deviceStreamManager,
63                                    GpuApiCallBehavior         transferKind,
64                                    int                        allocationBlockSizeDivisor,
65                                    gmx_wallcycle*             wcycle) :
66     deviceContext_(deviceStreamManager.context()),
67     transferKind_(transferKind),
68     allocationBlockSizeDivisor_(allocationBlockSizeDivisor),
69     wcycle_(wcycle)
70 {
71     static_assert(
72             GMX_GPU,
73             "GPU state propagator data object should only be constructed on the GPU code-paths.");
74
75     // We need to keep local copies for re-initialization.
76     pmeStream_      = &deviceStreamManager.stream(DeviceStreamType::Pme);
77     localStream_    = &deviceStreamManager.stream(DeviceStreamType::NonBondedLocal);
78     nonLocalStream_ = &deviceStreamManager.stream(DeviceStreamType::NonBondedNonLocal);
79     // PME stream is used in OpenCL for H2D coordinate transfer
80     updateStream_ = &deviceStreamManager.stream(
81             GMX_GPU_OPENCL ? DeviceStreamType::Pme : DeviceStreamType::UpdateAndConstraints);
82
83     // Map the atom locality to the stream that will be used for coordinates,
84     // velocities and forces transfers. Same streams are used for H2D and D2H copies.
85     // Note, that nullptr stream is used here to indicate that the copy is not supported.
86     xCopyStreams_[AtomLocality::Local]    = updateStream_;
87     xCopyStreams_[AtomLocality::NonLocal] = nonLocalStream_;
88     xCopyStreams_[AtomLocality::All]      = updateStream_;
89
90     vCopyStreams_[AtomLocality::Local]    = updateStream_;
91     vCopyStreams_[AtomLocality::NonLocal] = nullptr;
92     vCopyStreams_[AtomLocality::All]      = updateStream_;
93
94     fCopyStreams_[AtomLocality::Local]    = localStream_;
95     fCopyStreams_[AtomLocality::NonLocal] = nonLocalStream_;
96     fCopyStreams_[AtomLocality::All]      = updateStream_;
97 }
98
99 StatePropagatorDataGpu::Impl::Impl(const DeviceStream*  pmeStream,
100                                    const DeviceContext& deviceContext,
101                                    GpuApiCallBehavior   transferKind,
102                                    int                  allocationBlockSizeDivisor,
103                                    gmx_wallcycle*       wcycle) :
104     deviceContext_(deviceContext),
105     transferKind_(transferKind),
106     allocationBlockSizeDivisor_(allocationBlockSizeDivisor),
107     wcycle_(wcycle)
108 {
109     static_assert(
110             GMX_GPU,
111             "GPU state propagator data object should only be constructed on the GPU code-paths.");
112
113     GMX_ASSERT(pmeStream->isValid(), "GPU PME stream should be valid.");
114     pmeStream_      = pmeStream;
115     localStream_    = pmeStream; // For clearing the force buffer
116     nonLocalStream_ = nullptr;
117     updateStream_   = nullptr;
118
119
120     // Only local/all coordinates are allowed to be copied in PME-only rank/ PME tests.
121     // This it temporary measure to make it safe to use this class in those cases.
122     xCopyStreams_[AtomLocality::Local]    = pmeStream_;
123     xCopyStreams_[AtomLocality::NonLocal] = nullptr;
124     xCopyStreams_[AtomLocality::All]      = pmeStream_;
125
126     vCopyStreams_[AtomLocality::Local]    = nullptr;
127     vCopyStreams_[AtomLocality::NonLocal] = nullptr;
128     vCopyStreams_[AtomLocality::All]      = nullptr;
129
130     fCopyStreams_[AtomLocality::Local]    = nullptr;
131     fCopyStreams_[AtomLocality::NonLocal] = nullptr;
132     fCopyStreams_[AtomLocality::All]      = nullptr;
133 }
134
135 StatePropagatorDataGpu::Impl::~Impl() {}
136
137 void StatePropagatorDataGpu::Impl::reinit(int numAtomsLocal, int numAtomsAll)
138 {
139     wallcycle_start_nocount(wcycle_, ewcLAUNCH_GPU);
140     wallcycle_sub_start_nocount(wcycle_, ewcsLAUNCH_STATE_PROPAGATOR_DATA);
141
142     numAtomsLocal_ = numAtomsLocal;
143     numAtomsAll_   = numAtomsAll;
144
145     int numAtomsPadded;
146     if (allocationBlockSizeDivisor_ > 0)
147     {
148         numAtomsPadded = ((numAtomsAll_ + allocationBlockSizeDivisor_ - 1) / allocationBlockSizeDivisor_)
149                          * allocationBlockSizeDivisor_;
150     }
151     else
152     {
153         numAtomsPadded = numAtomsAll_;
154     }
155
156     reallocateDeviceBuffer(&d_x_, numAtomsPadded, &d_xSize_, &d_xCapacity_, deviceContext_);
157
158     const size_t paddingAllocationSize = numAtomsPadded - numAtomsAll_;
159     if (paddingAllocationSize > 0)
160     {
161         // The PME stream is used here because the padding region of d_x_ is only in the PME task.
162         clearDeviceBufferAsync(&d_x_, numAtomsAll_, paddingAllocationSize, *pmeStream_);
163     }
164
165     reallocateDeviceBuffer(&d_v_, numAtomsAll_, &d_vSize_, &d_vCapacity_, deviceContext_);
166     const int d_fOldCapacity = d_fCapacity_;
167     reallocateDeviceBuffer(&d_f_, numAtomsAll_, &d_fSize_, &d_fCapacity_, deviceContext_);
168
169     // Clearing of the forces can be done in local stream since the nonlocal stream cannot reach
170     // the force accumulation stage before syncing with the local stream. Only done in CUDA,
171     // since the force buffer ops are not implemented in OpenCL.
172     if (GMX_GPU_CUDA && d_fCapacity_ != d_fOldCapacity)
173     {
174         clearDeviceBufferAsync(&d_f_, 0, d_fCapacity_, *localStream_);
175     }
176
177     wallcycle_sub_stop(wcycle_, ewcsLAUNCH_STATE_PROPAGATOR_DATA);
178     wallcycle_stop(wcycle_, ewcLAUNCH_GPU);
179 }
180
181 std::tuple<int, int> StatePropagatorDataGpu::Impl::getAtomRangesFromAtomLocality(AtomLocality atomLocality)
182 {
183     int atomsStartAt   = 0;
184     int numAtomsToCopy = 0;
185     switch (atomLocality)
186     {
187         case AtomLocality::All:
188             atomsStartAt   = 0;
189             numAtomsToCopy = numAtomsAll_;
190             break;
191         case AtomLocality::Local:
192             atomsStartAt   = 0;
193             numAtomsToCopy = numAtomsLocal_;
194             break;
195         case AtomLocality::NonLocal:
196             atomsStartAt   = numAtomsLocal_;
197             numAtomsToCopy = numAtomsAll_ - numAtomsLocal_;
198             break;
199         default:
200             GMX_RELEASE_ASSERT(false,
201                                "Wrong range of atoms requested in GPU state data manager. Should "
202                                "be All, Local or NonLocal.");
203     }
204     GMX_ASSERT(atomsStartAt >= 0,
205                "The first elemtnt to copy has negative index. Probably, the GPU propagator state "
206                "was not initialized.");
207     GMX_ASSERT(numAtomsToCopy >= 0,
208                "Number of atoms to copy is negative. Probably, the GPU propagator state was not "
209                "initialized.");
210     return std::make_tuple(atomsStartAt, numAtomsToCopy);
211 }
212
213 void StatePropagatorDataGpu::Impl::copyToDevice(DeviceBuffer<RVec>                   d_data,
214                                                 const gmx::ArrayRef<const gmx::RVec> h_data,
215                                                 int                                  dataSize,
216                                                 AtomLocality                         atomLocality,
217                                                 const DeviceStream&                  deviceStream)
218 {
219     GMX_UNUSED_VALUE(dataSize);
220
221     GMX_ASSERT(atomLocality < AtomLocality::Count, "Wrong atom locality.");
222
223     GMX_ASSERT(dataSize >= 0, "Trying to copy to device buffer before it was allocated.");
224
225     GMX_ASSERT(deviceStream.isValid(), "No stream is valid for copying with given atom locality.");
226
227     int atomsStartAt, numAtomsToCopy;
228     std::tie(atomsStartAt, numAtomsToCopy) = getAtomRangesFromAtomLocality(atomLocality);
229
230     if (numAtomsToCopy != 0)
231     {
232         GMX_ASSERT(atomsStartAt + numAtomsToCopy <= dataSize,
233                    "The device allocation is smaller than requested copy range.");
234         GMX_ASSERT(atomsStartAt + numAtomsToCopy <= h_data.ssize(),
235                    "The host buffer is smaller than the requested copy range.");
236
237         copyToDeviceBuffer(&d_data, reinterpret_cast<const RVec*>(&h_data.data()[atomsStartAt]),
238                            atomsStartAt, numAtomsToCopy, deviceStream, transferKind_, nullptr);
239     }
240 }
241
242 void StatePropagatorDataGpu::Impl::copyFromDevice(gmx::ArrayRef<gmx::RVec> h_data,
243                                                   DeviceBuffer<RVec>       d_data,
244                                                   int                      dataSize,
245                                                   AtomLocality             atomLocality,
246                                                   const DeviceStream&      deviceStream)
247 {
248     GMX_UNUSED_VALUE(dataSize);
249
250     GMX_ASSERT(atomLocality < AtomLocality::Count, "Wrong atom locality.");
251
252     GMX_ASSERT(dataSize >= 0, "Trying to copy from device buffer before it was allocated.");
253
254     GMX_ASSERT(deviceStream.isValid(), "No stream is valid for copying with given atom locality.");
255
256     int atomsStartAt, numAtomsToCopy;
257     std::tie(atomsStartAt, numAtomsToCopy) = getAtomRangesFromAtomLocality(atomLocality);
258
259     if (numAtomsToCopy != 0)
260     {
261         GMX_ASSERT(atomsStartAt + numAtomsToCopy <= dataSize,
262                    "The device allocation is smaller than requested copy range.");
263         GMX_ASSERT(atomsStartAt + numAtomsToCopy <= h_data.ssize(),
264                    "The host buffer is smaller than the requested copy range.");
265
266         copyFromDeviceBuffer(reinterpret_cast<RVec*>(&h_data.data()[atomsStartAt]), &d_data,
267                              atomsStartAt, numAtomsToCopy, deviceStream, transferKind_, nullptr);
268     }
269 }
270
271 DeviceBuffer<RVec> StatePropagatorDataGpu::Impl::getCoordinates()
272 {
273     return d_x_;
274 }
275
276 void StatePropagatorDataGpu::Impl::copyCoordinatesToGpu(const gmx::ArrayRef<const gmx::RVec> h_x,
277                                                         AtomLocality atomLocality)
278 {
279     GMX_ASSERT(atomLocality < AtomLocality::Count, "Wrong atom locality.");
280     const DeviceStream* deviceStream = xCopyStreams_[atomLocality];
281     GMX_ASSERT(deviceStream != nullptr,
282                "No stream is valid for copying positions with given atom locality.");
283
284     wallcycle_start_nocount(wcycle_, ewcLAUNCH_GPU);
285     wallcycle_sub_start(wcycle_, ewcsLAUNCH_STATE_PROPAGATOR_DATA);
286
287     copyToDevice(d_x_, h_x, d_xSize_, atomLocality, *deviceStream);
288
289     // markEvent is skipped in OpenCL as:
290     //   - it's not needed, copy is done in the same stream as the only consumer task (PME)
291     //   - we don't consume the events in OpenCL which is not allowed by GpuEventSynchronizer (would leak memory).
292     // TODO: remove this by adding an event-mark free flavor of this function
293     if (GMX_GPU_CUDA)
294     {
295         xReadyOnDevice_[atomLocality].markEvent(*deviceStream);
296     }
297
298     wallcycle_sub_stop(wcycle_, ewcsLAUNCH_STATE_PROPAGATOR_DATA);
299     wallcycle_stop(wcycle_, ewcLAUNCH_GPU);
300 }
301
302 GpuEventSynchronizer*
303 StatePropagatorDataGpu::Impl::getCoordinatesReadyOnDeviceEvent(AtomLocality atomLocality,
304                                                                const SimulationWorkload& simulationWork,
305                                                                const StepWorkload&       stepWork)
306 {
307     // The provider of the coordinates may be different for local atoms. If the update is offloaded
308     // and this is not a neighbor search step, then the consumer needs to wait for the update
309     // to complete. Otherwise, the coordinates are copied from the host and we need to wait for
310     // the copy event. Non-local coordinates are always provided by the H2D copy.
311     //
312     // TODO: This should be reconsidered to support the halo exchange.
313     //
314     // In OpenCL no events are used as coordinate sync is not necessary
315     if (GMX_GPU_OPENCL)
316     {
317         return nullptr;
318     }
319     if (atomLocality == AtomLocality::Local && simulationWork.useGpuUpdate && !stepWork.doNeighborSearch)
320     {
321         return &xUpdatedOnDevice_;
322     }
323     else
324     {
325         return &xReadyOnDevice_[atomLocality];
326     }
327 }
328
329 void StatePropagatorDataGpu::Impl::waitCoordinatesCopiedToDevice(AtomLocality atomLocality)
330 {
331     wallcycle_start(wcycle_, ewcWAIT_GPU_STATE_PROPAGATOR_DATA);
332     GMX_ASSERT(atomLocality < AtomLocality::Count, "Wrong atom locality.");
333     xReadyOnDevice_[atomLocality].waitForEvent();
334     wallcycle_stop(wcycle_, ewcWAIT_GPU_STATE_PROPAGATOR_DATA);
335 }
336
337 GpuEventSynchronizer* StatePropagatorDataGpu::Impl::xUpdatedOnDevice()
338 {
339     return &xUpdatedOnDevice_;
340 }
341
342 void StatePropagatorDataGpu::Impl::copyCoordinatesFromGpu(gmx::ArrayRef<gmx::RVec> h_x, AtomLocality atomLocality)
343 {
344     GMX_ASSERT(atomLocality < AtomLocality::Count, "Wrong atom locality.");
345     const DeviceStream* deviceStream = xCopyStreams_[atomLocality];
346     GMX_ASSERT(deviceStream != nullptr,
347                "No stream is valid for copying positions with given atom locality.");
348
349     wallcycle_start_nocount(wcycle_, ewcLAUNCH_GPU);
350     wallcycle_sub_start(wcycle_, ewcsLAUNCH_STATE_PROPAGATOR_DATA);
351
352     copyFromDevice(h_x, d_x_, d_xSize_, atomLocality, *deviceStream);
353     // Note: unlike copyCoordinatesToGpu this is not used in OpenCL, and the conditional is not needed.
354     xReadyOnHost_[atomLocality].markEvent(*deviceStream);
355
356     wallcycle_sub_stop(wcycle_, ewcsLAUNCH_STATE_PROPAGATOR_DATA);
357     wallcycle_stop(wcycle_, ewcLAUNCH_GPU);
358 }
359
360 void StatePropagatorDataGpu::Impl::waitCoordinatesReadyOnHost(AtomLocality atomLocality)
361 {
362     wallcycle_start(wcycle_, ewcWAIT_GPU_STATE_PROPAGATOR_DATA);
363     xReadyOnHost_[atomLocality].waitForEvent();
364     wallcycle_stop(wcycle_, ewcWAIT_GPU_STATE_PROPAGATOR_DATA);
365 }
366
367
368 DeviceBuffer<RVec> StatePropagatorDataGpu::Impl::getVelocities()
369 {
370     return d_v_;
371 }
372
373 void StatePropagatorDataGpu::Impl::copyVelocitiesToGpu(const gmx::ArrayRef<const gmx::RVec> h_v,
374                                                        AtomLocality atomLocality)
375 {
376     GMX_ASSERT(atomLocality < AtomLocality::Count, "Wrong atom locality.");
377     const DeviceStream* deviceStream = vCopyStreams_[atomLocality];
378     GMX_ASSERT(deviceStream != nullptr,
379                "No stream is valid for copying velocities with given atom locality.");
380
381     wallcycle_start_nocount(wcycle_, ewcLAUNCH_GPU);
382     wallcycle_sub_start(wcycle_, ewcsLAUNCH_STATE_PROPAGATOR_DATA);
383
384     copyToDevice(d_v_, h_v, d_vSize_, atomLocality, *deviceStream);
385     vReadyOnDevice_[atomLocality].markEvent(*deviceStream);
386
387     wallcycle_sub_stop(wcycle_, ewcsLAUNCH_STATE_PROPAGATOR_DATA);
388     wallcycle_stop(wcycle_, ewcLAUNCH_GPU);
389 }
390
391 GpuEventSynchronizer* StatePropagatorDataGpu::Impl::getVelocitiesReadyOnDeviceEvent(AtomLocality atomLocality)
392 {
393     return &vReadyOnDevice_[atomLocality];
394 }
395
396
397 void StatePropagatorDataGpu::Impl::copyVelocitiesFromGpu(gmx::ArrayRef<gmx::RVec> h_v, AtomLocality atomLocality)
398 {
399     GMX_ASSERT(atomLocality < AtomLocality::Count, "Wrong atom locality.");
400     const DeviceStream* deviceStream = vCopyStreams_[atomLocality];
401     GMX_ASSERT(deviceStream != nullptr,
402                "No stream is valid for copying velocities with given atom locality.");
403
404     wallcycle_start_nocount(wcycle_, ewcLAUNCH_GPU);
405     wallcycle_sub_start(wcycle_, ewcsLAUNCH_STATE_PROPAGATOR_DATA);
406
407     copyFromDevice(h_v, d_v_, d_vSize_, atomLocality, *deviceStream);
408     vReadyOnHost_[atomLocality].markEvent(*deviceStream);
409
410     wallcycle_sub_stop(wcycle_, ewcsLAUNCH_STATE_PROPAGATOR_DATA);
411     wallcycle_stop(wcycle_, ewcLAUNCH_GPU);
412 }
413
414 void StatePropagatorDataGpu::Impl::waitVelocitiesReadyOnHost(AtomLocality atomLocality)
415 {
416     wallcycle_start(wcycle_, ewcWAIT_GPU_STATE_PROPAGATOR_DATA);
417     vReadyOnHost_[atomLocality].waitForEvent();
418     wallcycle_stop(wcycle_, ewcWAIT_GPU_STATE_PROPAGATOR_DATA);
419 }
420
421
422 DeviceBuffer<RVec> StatePropagatorDataGpu::Impl::getForces()
423 {
424     return d_f_;
425 }
426
427 void StatePropagatorDataGpu::Impl::copyForcesToGpu(const gmx::ArrayRef<const gmx::RVec> h_f,
428                                                    AtomLocality atomLocality)
429 {
430     GMX_ASSERT(atomLocality < AtomLocality::Count, "Wrong atom locality.");
431     const DeviceStream* deviceStream = fCopyStreams_[atomLocality];
432     GMX_ASSERT(deviceStream != nullptr,
433                "No stream is valid for copying forces with given atom locality.");
434
435     wallcycle_start_nocount(wcycle_, ewcLAUNCH_GPU);
436     wallcycle_sub_start(wcycle_, ewcsLAUNCH_STATE_PROPAGATOR_DATA);
437
438     copyToDevice(d_f_, h_f, d_fSize_, atomLocality, *deviceStream);
439     fReadyOnDevice_[atomLocality].markEvent(*deviceStream);
440
441     wallcycle_sub_stop(wcycle_, ewcsLAUNCH_STATE_PROPAGATOR_DATA);
442     wallcycle_stop(wcycle_, ewcLAUNCH_GPU);
443 }
444
445 GpuEventSynchronizer* StatePropagatorDataGpu::Impl::getForcesReadyOnDeviceEvent(AtomLocality atomLocality,
446                                                                                 bool useGpuFBufferOps)
447 {
448     if ((atomLocality == AtomLocality::Local || atomLocality == AtomLocality::NonLocal) && useGpuFBufferOps)
449     {
450         return &fReducedOnDevice_;
451     }
452     else
453     {
454         return &fReadyOnDevice_[atomLocality];
455     }
456 }
457
458 GpuEventSynchronizer* StatePropagatorDataGpu::Impl::fReducedOnDevice()
459 {
460     return &fReducedOnDevice_;
461 }
462
463 void StatePropagatorDataGpu::Impl::copyForcesFromGpu(gmx::ArrayRef<gmx::RVec> h_f, AtomLocality atomLocality)
464 {
465     GMX_ASSERT(atomLocality < AtomLocality::Count, "Wrong atom locality.");
466     const DeviceStream* deviceStream = fCopyStreams_[atomLocality];
467     GMX_ASSERT(deviceStream != nullptr,
468                "No stream is valid for copying forces with given atom locality.");
469
470     wallcycle_start_nocount(wcycle_, ewcLAUNCH_GPU);
471     wallcycle_sub_start(wcycle_, ewcsLAUNCH_STATE_PROPAGATOR_DATA);
472
473     copyFromDevice(h_f, d_f_, d_fSize_, atomLocality, *deviceStream);
474     fReadyOnHost_[atomLocality].markEvent(*deviceStream);
475
476     wallcycle_sub_stop(wcycle_, ewcsLAUNCH_STATE_PROPAGATOR_DATA);
477     wallcycle_stop(wcycle_, ewcLAUNCH_GPU);
478 }
479
480 void StatePropagatorDataGpu::Impl::waitForcesReadyOnHost(AtomLocality atomLocality)
481 {
482     wallcycle_start(wcycle_, ewcWAIT_GPU_STATE_PROPAGATOR_DATA);
483     fReadyOnHost_[atomLocality].waitForEvent();
484     wallcycle_stop(wcycle_, ewcWAIT_GPU_STATE_PROPAGATOR_DATA);
485 }
486
487 const DeviceStream* StatePropagatorDataGpu::Impl::getUpdateStream()
488 {
489     return updateStream_;
490 }
491
492 int StatePropagatorDataGpu::Impl::numAtomsLocal()
493 {
494     return numAtomsLocal_;
495 }
496
497 int StatePropagatorDataGpu::Impl::numAtomsAll()
498 {
499     return numAtomsAll_;
500 }
501
502
503 StatePropagatorDataGpu::StatePropagatorDataGpu(const DeviceStreamManager& deviceStreamManager,
504                                                GpuApiCallBehavior         transferKind,
505                                                int            allocationBlockSizeDivisor,
506                                                gmx_wallcycle* wcycle) :
507     impl_(new Impl(deviceStreamManager, transferKind, allocationBlockSizeDivisor, wcycle))
508 {
509 }
510
511 StatePropagatorDataGpu::StatePropagatorDataGpu(const DeviceStream*  pmeStream,
512                                                const DeviceContext& deviceContext,
513                                                GpuApiCallBehavior   transferKind,
514                                                int                  allocationBlockSizeDivisor,
515                                                gmx_wallcycle*       wcycle) :
516     impl_(new Impl(pmeStream, deviceContext, transferKind, allocationBlockSizeDivisor, wcycle))
517 {
518 }
519
520 StatePropagatorDataGpu::StatePropagatorDataGpu(StatePropagatorDataGpu&& /* other */) noexcept = default;
521
522 StatePropagatorDataGpu& StatePropagatorDataGpu::operator=(StatePropagatorDataGpu&& /* other */) noexcept = default;
523
524 StatePropagatorDataGpu::~StatePropagatorDataGpu() = default;
525
526
527 void StatePropagatorDataGpu::reinit(int numAtomsLocal, int numAtomsAll)
528 {
529     return impl_->reinit(numAtomsLocal, numAtomsAll);
530 }
531
532 std::tuple<int, int> StatePropagatorDataGpu::getAtomRangesFromAtomLocality(AtomLocality atomLocality)
533 {
534     return impl_->getAtomRangesFromAtomLocality(atomLocality);
535 }
536
537
538 DeviceBuffer<RVec> StatePropagatorDataGpu::getCoordinates()
539 {
540     return impl_->getCoordinates();
541 }
542
543 void StatePropagatorDataGpu::copyCoordinatesToGpu(const gmx::ArrayRef<const gmx::RVec> h_x,
544                                                   AtomLocality                         atomLocality)
545 {
546     return impl_->copyCoordinatesToGpu(h_x, atomLocality);
547 }
548
549 GpuEventSynchronizer*
550 StatePropagatorDataGpu::getCoordinatesReadyOnDeviceEvent(AtomLocality              atomLocality,
551                                                          const SimulationWorkload& simulationWork,
552                                                          const StepWorkload&       stepWork)
553 {
554     return impl_->getCoordinatesReadyOnDeviceEvent(atomLocality, simulationWork, stepWork);
555 }
556
557 void StatePropagatorDataGpu::waitCoordinatesCopiedToDevice(AtomLocality atomLocality)
558 {
559     return impl_->waitCoordinatesCopiedToDevice(atomLocality);
560 }
561
562 GpuEventSynchronizer* StatePropagatorDataGpu::xUpdatedOnDevice()
563 {
564     return impl_->xUpdatedOnDevice();
565 }
566
567 void StatePropagatorDataGpu::copyCoordinatesFromGpu(gmx::ArrayRef<RVec> h_x, AtomLocality atomLocality)
568 {
569     return impl_->copyCoordinatesFromGpu(h_x, atomLocality);
570 }
571
572 void StatePropagatorDataGpu::waitCoordinatesReadyOnHost(AtomLocality atomLocality)
573 {
574     return impl_->waitCoordinatesReadyOnHost(atomLocality);
575 }
576
577
578 DeviceBuffer<RVec> StatePropagatorDataGpu::getVelocities()
579 {
580     return impl_->getVelocities();
581 }
582
583 void StatePropagatorDataGpu::copyVelocitiesToGpu(const gmx::ArrayRef<const gmx::RVec> h_v,
584                                                  AtomLocality                         atomLocality)
585 {
586     return impl_->copyVelocitiesToGpu(h_v, atomLocality);
587 }
588
589 GpuEventSynchronizer* StatePropagatorDataGpu::getVelocitiesReadyOnDeviceEvent(AtomLocality atomLocality)
590 {
591     return impl_->getVelocitiesReadyOnDeviceEvent(atomLocality);
592 }
593
594 void StatePropagatorDataGpu::copyVelocitiesFromGpu(gmx::ArrayRef<RVec> h_v, AtomLocality atomLocality)
595 {
596     return impl_->copyVelocitiesFromGpu(h_v, atomLocality);
597 }
598
599 void StatePropagatorDataGpu::waitVelocitiesReadyOnHost(AtomLocality atomLocality)
600 {
601     return impl_->waitVelocitiesReadyOnHost(atomLocality);
602 }
603
604
605 DeviceBuffer<RVec> StatePropagatorDataGpu::getForces()
606 {
607     return impl_->getForces();
608 }
609
610 void StatePropagatorDataGpu::copyForcesToGpu(const gmx::ArrayRef<const gmx::RVec> h_f, AtomLocality atomLocality)
611 {
612     return impl_->copyForcesToGpu(h_f, atomLocality);
613 }
614
615 GpuEventSynchronizer* StatePropagatorDataGpu::getForcesReadyOnDeviceEvent(AtomLocality atomLocality,
616                                                                           bool useGpuFBufferOps)
617 {
618     return impl_->getForcesReadyOnDeviceEvent(atomLocality, useGpuFBufferOps);
619 }
620
621 GpuEventSynchronizer* StatePropagatorDataGpu::fReducedOnDevice()
622 {
623     return impl_->fReducedOnDevice();
624 }
625
626 void StatePropagatorDataGpu::copyForcesFromGpu(gmx::ArrayRef<RVec> h_f, AtomLocality atomLocality)
627 {
628     return impl_->copyForcesFromGpu(h_f, atomLocality);
629 }
630
631 void StatePropagatorDataGpu::waitForcesReadyOnHost(AtomLocality atomLocality)
632 {
633     return impl_->waitForcesReadyOnHost(atomLocality);
634 }
635
636
637 const DeviceStream* StatePropagatorDataGpu::getUpdateStream()
638 {
639     return impl_->getUpdateStream();
640 }
641
642 int StatePropagatorDataGpu::numAtomsLocal()
643 {
644     return impl_->numAtomsLocal();
645 }
646
647 int StatePropagatorDataGpu::numAtomsAll()
648 {
649     return impl_->numAtomsAll();
650 }
651
652 } // namespace gmx
653
654 #endif // GMX_GPU