37d00888d502e9a69f01f72b355cbb8f4528b28d
[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,
238                            reinterpret_cast<const RVec*>(&h_data.data()[atomsStartAt]),
239                            atomsStartAt,
240                            numAtomsToCopy,
241                            deviceStream,
242                            transferKind_,
243                            nullptr);
244     }
245 }
246
247 void StatePropagatorDataGpu::Impl::copyFromDevice(gmx::ArrayRef<gmx::RVec> h_data,
248                                                   DeviceBuffer<RVec>       d_data,
249                                                   int                      dataSize,
250                                                   AtomLocality             atomLocality,
251                                                   const DeviceStream&      deviceStream)
252 {
253     GMX_UNUSED_VALUE(dataSize);
254
255     GMX_ASSERT(atomLocality < AtomLocality::Count, "Wrong atom locality.");
256
257     GMX_ASSERT(dataSize >= 0, "Trying to copy from device buffer before it was allocated.");
258
259     GMX_ASSERT(deviceStream.isValid(), "No stream is valid for copying with given atom locality.");
260
261     int atomsStartAt, numAtomsToCopy;
262     std::tie(atomsStartAt, numAtomsToCopy) = getAtomRangesFromAtomLocality(atomLocality);
263
264     if (numAtomsToCopy != 0)
265     {
266         GMX_ASSERT(atomsStartAt + numAtomsToCopy <= dataSize,
267                    "The device allocation is smaller than requested copy range.");
268         GMX_ASSERT(atomsStartAt + numAtomsToCopy <= h_data.ssize(),
269                    "The host buffer is smaller than the requested copy range.");
270
271         copyFromDeviceBuffer(reinterpret_cast<RVec*>(&h_data.data()[atomsStartAt]),
272                              &d_data,
273                              atomsStartAt,
274                              numAtomsToCopy,
275                              deviceStream,
276                              transferKind_,
277                              nullptr);
278     }
279 }
280
281 DeviceBuffer<RVec> StatePropagatorDataGpu::Impl::getCoordinates()
282 {
283     return d_x_;
284 }
285
286 void StatePropagatorDataGpu::Impl::copyCoordinatesToGpu(const gmx::ArrayRef<const gmx::RVec> h_x,
287                                                         AtomLocality atomLocality)
288 {
289     GMX_ASSERT(atomLocality < AtomLocality::Count, "Wrong atom locality.");
290     const DeviceStream* deviceStream = xCopyStreams_[atomLocality];
291     GMX_ASSERT(deviceStream != nullptr,
292                "No stream is valid for copying positions with given atom locality.");
293
294     wallcycle_start_nocount(wcycle_, ewcLAUNCH_GPU);
295     wallcycle_sub_start(wcycle_, ewcsLAUNCH_STATE_PROPAGATOR_DATA);
296
297     copyToDevice(d_x_, h_x, d_xSize_, atomLocality, *deviceStream);
298
299     // markEvent is skipped in OpenCL as:
300     //   - it's not needed, copy is done in the same stream as the only consumer task (PME)
301     //   - we don't consume the events in OpenCL which is not allowed by GpuEventSynchronizer (would leak memory).
302     // TODO: remove this by adding an event-mark free flavor of this function
303     if (GMX_GPU_CUDA)
304     {
305         xReadyOnDevice_[atomLocality].markEvent(*deviceStream);
306     }
307
308     wallcycle_sub_stop(wcycle_, ewcsLAUNCH_STATE_PROPAGATOR_DATA);
309     wallcycle_stop(wcycle_, ewcLAUNCH_GPU);
310 }
311
312 GpuEventSynchronizer*
313 StatePropagatorDataGpu::Impl::getCoordinatesReadyOnDeviceEvent(AtomLocality atomLocality,
314                                                                const SimulationWorkload& simulationWork,
315                                                                const StepWorkload&       stepWork)
316 {
317     // The provider of the coordinates may be different for local atoms. If the update is offloaded
318     // and this is not a neighbor search step, then the consumer needs to wait for the update
319     // to complete. Otherwise, the coordinates are copied from the host and we need to wait for
320     // the copy event. Non-local coordinates are always provided by the H2D copy.
321     //
322     // TODO: This should be reconsidered to support the halo exchange.
323     //
324     // In OpenCL no events are used as coordinate sync is not necessary
325     if (GMX_GPU_OPENCL)
326     {
327         return nullptr;
328     }
329     if (atomLocality == AtomLocality::Local && simulationWork.useGpuUpdate && !stepWork.doNeighborSearch)
330     {
331         return &xUpdatedOnDevice_;
332     }
333     else
334     {
335         return &xReadyOnDevice_[atomLocality];
336     }
337 }
338
339 void StatePropagatorDataGpu::Impl::waitCoordinatesCopiedToDevice(AtomLocality atomLocality)
340 {
341     wallcycle_start(wcycle_, ewcWAIT_GPU_STATE_PROPAGATOR_DATA);
342     GMX_ASSERT(atomLocality < AtomLocality::Count, "Wrong atom locality.");
343     xReadyOnDevice_[atomLocality].waitForEvent();
344     wallcycle_stop(wcycle_, ewcWAIT_GPU_STATE_PROPAGATOR_DATA);
345 }
346
347 GpuEventSynchronizer* StatePropagatorDataGpu::Impl::xUpdatedOnDevice()
348 {
349     return &xUpdatedOnDevice_;
350 }
351
352 void StatePropagatorDataGpu::Impl::copyCoordinatesFromGpu(gmx::ArrayRef<gmx::RVec> h_x, AtomLocality atomLocality)
353 {
354     GMX_ASSERT(atomLocality < AtomLocality::Count, "Wrong atom locality.");
355     const DeviceStream* deviceStream = xCopyStreams_[atomLocality];
356     GMX_ASSERT(deviceStream != nullptr,
357                "No stream is valid for copying positions with given atom locality.");
358
359     wallcycle_start_nocount(wcycle_, ewcLAUNCH_GPU);
360     wallcycle_sub_start(wcycle_, ewcsLAUNCH_STATE_PROPAGATOR_DATA);
361
362     copyFromDevice(h_x, d_x_, d_xSize_, atomLocality, *deviceStream);
363     // Note: unlike copyCoordinatesToGpu this is not used in OpenCL, and the conditional is not needed.
364     xReadyOnHost_[atomLocality].markEvent(*deviceStream);
365
366     wallcycle_sub_stop(wcycle_, ewcsLAUNCH_STATE_PROPAGATOR_DATA);
367     wallcycle_stop(wcycle_, ewcLAUNCH_GPU);
368 }
369
370 void StatePropagatorDataGpu::Impl::waitCoordinatesReadyOnHost(AtomLocality atomLocality)
371 {
372     wallcycle_start(wcycle_, ewcWAIT_GPU_STATE_PROPAGATOR_DATA);
373     xReadyOnHost_[atomLocality].waitForEvent();
374     wallcycle_stop(wcycle_, ewcWAIT_GPU_STATE_PROPAGATOR_DATA);
375 }
376
377
378 DeviceBuffer<RVec> StatePropagatorDataGpu::Impl::getVelocities()
379 {
380     return d_v_;
381 }
382
383 void StatePropagatorDataGpu::Impl::copyVelocitiesToGpu(const gmx::ArrayRef<const gmx::RVec> h_v,
384                                                        AtomLocality atomLocality)
385 {
386     GMX_ASSERT(atomLocality < AtomLocality::Count, "Wrong atom locality.");
387     const DeviceStream* deviceStream = vCopyStreams_[atomLocality];
388     GMX_ASSERT(deviceStream != nullptr,
389                "No stream is valid for copying velocities with given atom locality.");
390
391     wallcycle_start_nocount(wcycle_, ewcLAUNCH_GPU);
392     wallcycle_sub_start(wcycle_, ewcsLAUNCH_STATE_PROPAGATOR_DATA);
393
394     copyToDevice(d_v_, h_v, d_vSize_, atomLocality, *deviceStream);
395     vReadyOnDevice_[atomLocality].markEvent(*deviceStream);
396
397     wallcycle_sub_stop(wcycle_, ewcsLAUNCH_STATE_PROPAGATOR_DATA);
398     wallcycle_stop(wcycle_, ewcLAUNCH_GPU);
399 }
400
401 GpuEventSynchronizer* StatePropagatorDataGpu::Impl::getVelocitiesReadyOnDeviceEvent(AtomLocality atomLocality)
402 {
403     return &vReadyOnDevice_[atomLocality];
404 }
405
406
407 void StatePropagatorDataGpu::Impl::copyVelocitiesFromGpu(gmx::ArrayRef<gmx::RVec> h_v, AtomLocality atomLocality)
408 {
409     GMX_ASSERT(atomLocality < AtomLocality::Count, "Wrong atom locality.");
410     const DeviceStream* deviceStream = vCopyStreams_[atomLocality];
411     GMX_ASSERT(deviceStream != nullptr,
412                "No stream is valid for copying velocities with given atom locality.");
413
414     wallcycle_start_nocount(wcycle_, ewcLAUNCH_GPU);
415     wallcycle_sub_start(wcycle_, ewcsLAUNCH_STATE_PROPAGATOR_DATA);
416
417     copyFromDevice(h_v, d_v_, d_vSize_, atomLocality, *deviceStream);
418     vReadyOnHost_[atomLocality].markEvent(*deviceStream);
419
420     wallcycle_sub_stop(wcycle_, ewcsLAUNCH_STATE_PROPAGATOR_DATA);
421     wallcycle_stop(wcycle_, ewcLAUNCH_GPU);
422 }
423
424 void StatePropagatorDataGpu::Impl::waitVelocitiesReadyOnHost(AtomLocality atomLocality)
425 {
426     wallcycle_start(wcycle_, ewcWAIT_GPU_STATE_PROPAGATOR_DATA);
427     vReadyOnHost_[atomLocality].waitForEvent();
428     wallcycle_stop(wcycle_, ewcWAIT_GPU_STATE_PROPAGATOR_DATA);
429 }
430
431
432 DeviceBuffer<RVec> StatePropagatorDataGpu::Impl::getForces()
433 {
434     return d_f_;
435 }
436
437 void StatePropagatorDataGpu::Impl::copyForcesToGpu(const gmx::ArrayRef<const gmx::RVec> h_f,
438                                                    AtomLocality atomLocality)
439 {
440     GMX_ASSERT(atomLocality < AtomLocality::Count, "Wrong atom locality.");
441     const DeviceStream* deviceStream = fCopyStreams_[atomLocality];
442     GMX_ASSERT(deviceStream != nullptr,
443                "No stream is valid for copying forces with given atom locality.");
444
445     wallcycle_start_nocount(wcycle_, ewcLAUNCH_GPU);
446     wallcycle_sub_start(wcycle_, ewcsLAUNCH_STATE_PROPAGATOR_DATA);
447
448     copyToDevice(d_f_, h_f, d_fSize_, atomLocality, *deviceStream);
449     fReadyOnDevice_[atomLocality].markEvent(*deviceStream);
450
451     wallcycle_sub_stop(wcycle_, ewcsLAUNCH_STATE_PROPAGATOR_DATA);
452     wallcycle_stop(wcycle_, ewcLAUNCH_GPU);
453 }
454
455 GpuEventSynchronizer* StatePropagatorDataGpu::Impl::getForcesReadyOnDeviceEvent(AtomLocality atomLocality,
456                                                                                 bool useGpuFBufferOps)
457 {
458     if ((atomLocality == AtomLocality::Local || atomLocality == AtomLocality::NonLocal) && useGpuFBufferOps)
459     {
460         return &fReducedOnDevice_;
461     }
462     else
463     {
464         return &fReadyOnDevice_[atomLocality];
465     }
466 }
467
468 GpuEventSynchronizer* StatePropagatorDataGpu::Impl::fReducedOnDevice()
469 {
470     return &fReducedOnDevice_;
471 }
472
473 void StatePropagatorDataGpu::Impl::copyForcesFromGpu(gmx::ArrayRef<gmx::RVec> h_f, AtomLocality atomLocality)
474 {
475     GMX_ASSERT(atomLocality < AtomLocality::Count, "Wrong atom locality.");
476     const DeviceStream* deviceStream = fCopyStreams_[atomLocality];
477     GMX_ASSERT(deviceStream != nullptr,
478                "No stream is valid for copying forces with given atom locality.");
479
480     wallcycle_start_nocount(wcycle_, ewcLAUNCH_GPU);
481     wallcycle_sub_start(wcycle_, ewcsLAUNCH_STATE_PROPAGATOR_DATA);
482
483     copyFromDevice(h_f, d_f_, d_fSize_, atomLocality, *deviceStream);
484     fReadyOnHost_[atomLocality].markEvent(*deviceStream);
485
486     wallcycle_sub_stop(wcycle_, ewcsLAUNCH_STATE_PROPAGATOR_DATA);
487     wallcycle_stop(wcycle_, ewcLAUNCH_GPU);
488 }
489
490 void StatePropagatorDataGpu::Impl::waitForcesReadyOnHost(AtomLocality atomLocality)
491 {
492     wallcycle_start(wcycle_, ewcWAIT_GPU_STATE_PROPAGATOR_DATA);
493     fReadyOnHost_[atomLocality].waitForEvent();
494     wallcycle_stop(wcycle_, ewcWAIT_GPU_STATE_PROPAGATOR_DATA);
495 }
496
497 const DeviceStream* StatePropagatorDataGpu::Impl::getUpdateStream()
498 {
499     return updateStream_;
500 }
501
502 int StatePropagatorDataGpu::Impl::numAtomsLocal()
503 {
504     return numAtomsLocal_;
505 }
506
507 int StatePropagatorDataGpu::Impl::numAtomsAll()
508 {
509     return numAtomsAll_;
510 }
511
512
513 StatePropagatorDataGpu::StatePropagatorDataGpu(const DeviceStreamManager& deviceStreamManager,
514                                                GpuApiCallBehavior         transferKind,
515                                                int            allocationBlockSizeDivisor,
516                                                gmx_wallcycle* wcycle) :
517     impl_(new Impl(deviceStreamManager, transferKind, allocationBlockSizeDivisor, wcycle))
518 {
519 }
520
521 StatePropagatorDataGpu::StatePropagatorDataGpu(const DeviceStream*  pmeStream,
522                                                const DeviceContext& deviceContext,
523                                                GpuApiCallBehavior   transferKind,
524                                                int                  allocationBlockSizeDivisor,
525                                                gmx_wallcycle*       wcycle) :
526     impl_(new Impl(pmeStream, deviceContext, transferKind, allocationBlockSizeDivisor, wcycle))
527 {
528 }
529
530 StatePropagatorDataGpu::StatePropagatorDataGpu(StatePropagatorDataGpu&& /* other */) noexcept = default;
531
532 StatePropagatorDataGpu& StatePropagatorDataGpu::operator=(StatePropagatorDataGpu&& /* other */) noexcept = default;
533
534 StatePropagatorDataGpu::~StatePropagatorDataGpu() = default;
535
536
537 void StatePropagatorDataGpu::reinit(int numAtomsLocal, int numAtomsAll)
538 {
539     return impl_->reinit(numAtomsLocal, numAtomsAll);
540 }
541
542 std::tuple<int, int> StatePropagatorDataGpu::getAtomRangesFromAtomLocality(AtomLocality atomLocality)
543 {
544     return impl_->getAtomRangesFromAtomLocality(atomLocality);
545 }
546
547
548 DeviceBuffer<RVec> StatePropagatorDataGpu::getCoordinates()
549 {
550     return impl_->getCoordinates();
551 }
552
553 void StatePropagatorDataGpu::copyCoordinatesToGpu(const gmx::ArrayRef<const gmx::RVec> h_x,
554                                                   AtomLocality                         atomLocality)
555 {
556     return impl_->copyCoordinatesToGpu(h_x, atomLocality);
557 }
558
559 GpuEventSynchronizer*
560 StatePropagatorDataGpu::getCoordinatesReadyOnDeviceEvent(AtomLocality              atomLocality,
561                                                          const SimulationWorkload& simulationWork,
562                                                          const StepWorkload&       stepWork)
563 {
564     return impl_->getCoordinatesReadyOnDeviceEvent(atomLocality, simulationWork, stepWork);
565 }
566
567 void StatePropagatorDataGpu::waitCoordinatesCopiedToDevice(AtomLocality atomLocality)
568 {
569     return impl_->waitCoordinatesCopiedToDevice(atomLocality);
570 }
571
572 GpuEventSynchronizer* StatePropagatorDataGpu::xUpdatedOnDevice()
573 {
574     return impl_->xUpdatedOnDevice();
575 }
576
577 void StatePropagatorDataGpu::copyCoordinatesFromGpu(gmx::ArrayRef<RVec> h_x, AtomLocality atomLocality)
578 {
579     return impl_->copyCoordinatesFromGpu(h_x, atomLocality);
580 }
581
582 void StatePropagatorDataGpu::waitCoordinatesReadyOnHost(AtomLocality atomLocality)
583 {
584     return impl_->waitCoordinatesReadyOnHost(atomLocality);
585 }
586
587
588 DeviceBuffer<RVec> StatePropagatorDataGpu::getVelocities()
589 {
590     return impl_->getVelocities();
591 }
592
593 void StatePropagatorDataGpu::copyVelocitiesToGpu(const gmx::ArrayRef<const gmx::RVec> h_v,
594                                                  AtomLocality                         atomLocality)
595 {
596     return impl_->copyVelocitiesToGpu(h_v, atomLocality);
597 }
598
599 GpuEventSynchronizer* StatePropagatorDataGpu::getVelocitiesReadyOnDeviceEvent(AtomLocality atomLocality)
600 {
601     return impl_->getVelocitiesReadyOnDeviceEvent(atomLocality);
602 }
603
604 void StatePropagatorDataGpu::copyVelocitiesFromGpu(gmx::ArrayRef<RVec> h_v, AtomLocality atomLocality)
605 {
606     return impl_->copyVelocitiesFromGpu(h_v, atomLocality);
607 }
608
609 void StatePropagatorDataGpu::waitVelocitiesReadyOnHost(AtomLocality atomLocality)
610 {
611     return impl_->waitVelocitiesReadyOnHost(atomLocality);
612 }
613
614
615 DeviceBuffer<RVec> StatePropagatorDataGpu::getForces()
616 {
617     return impl_->getForces();
618 }
619
620 void StatePropagatorDataGpu::copyForcesToGpu(const gmx::ArrayRef<const gmx::RVec> h_f, AtomLocality atomLocality)
621 {
622     return impl_->copyForcesToGpu(h_f, atomLocality);
623 }
624
625 GpuEventSynchronizer* StatePropagatorDataGpu::getForcesReadyOnDeviceEvent(AtomLocality atomLocality,
626                                                                           bool useGpuFBufferOps)
627 {
628     return impl_->getForcesReadyOnDeviceEvent(atomLocality, useGpuFBufferOps);
629 }
630
631 GpuEventSynchronizer* StatePropagatorDataGpu::fReducedOnDevice()
632 {
633     return impl_->fReducedOnDevice();
634 }
635
636 void StatePropagatorDataGpu::copyForcesFromGpu(gmx::ArrayRef<RVec> h_f, AtomLocality atomLocality)
637 {
638     return impl_->copyForcesFromGpu(h_f, atomLocality);
639 }
640
641 void StatePropagatorDataGpu::waitForcesReadyOnHost(AtomLocality atomLocality)
642 {
643     return impl_->waitForcesReadyOnHost(atomLocality);
644 }
645
646
647 const DeviceStream* StatePropagatorDataGpu::getUpdateStream()
648 {
649     return impl_->getUpdateStream();
650 }
651
652 int StatePropagatorDataGpu::numAtomsLocal()
653 {
654     return impl_->numAtomsLocal();
655 }
656
657 int StatePropagatorDataGpu::numAtomsAll()
658 {
659     return impl_->numAtomsAll();
660 }
661
662 } // namespace gmx
663
664 #endif // GMX_GPU