Add GPU event to mark that coordinate update is done
authorSzilárd Páll <pall.szilard@gmail.com>
Mon, 30 Sep 2019 12:47:11 +0000 (14:47 +0200)
committerMagnus Lundborg <magnus.lundborg@scilifelab.se>
Wed, 9 Oct 2019 06:49:20 +0000 (08:49 +0200)
Also added related getter of a pointer to the GpuEventSynchronizer
object that allows syncing the dependent tasks.

Added temporary CPU blocking wait that ensures previous behaviour is
maintained while the GPU update integration is pending.

Change-Id: Iaae03d4f1f4083863a359e66a35f44d178b9382d

src/gromacs/mdlib/update_constrain_cuda.h
src/gromacs/mdlib/update_constrain_cuda_impl.cpp
src/gromacs/mdlib/update_constrain_cuda_impl.cu
src/gromacs/mdlib/update_constrain_cuda_impl.h
src/gromacs/mdrun/md.cpp

index f2035c687f4adf71ea8ed5bd8de42bf473e0dc39..9ab79720f6b7e65ccf32d0879a2815e664bb0831 100644 (file)
@@ -128,14 +128,20 @@ class UpdateConstrainCuda
          */
         void setPbc(const t_pbc *pbc);
 
-        /*! \brief Synchronize the device stream.
+        /*! \brief Blocking wait on the update of coordinates being ready.
+         *
+         * \todo Remove when the "stitching" is done.
          */
-        void synchronizeStream();
+        void waitCoordinatesReadyOnDevice();
+
+
+        /*! \brief Return the synchronizer associated with the event indicated that the coordinates are ready on the device.
+         */
+        void *getCoordinatesReadySync();
 
     private:
         class Impl;
         gmx::PrivateImplPointer<Impl> impl_;
-
 };
 
 } //namespace gmx
index f8f9c45a522ad71a98371f65cd2d6c00aa4950e5..9e9f56e80fcaaea2ad8102fa4756285d8a29e854 100644 (file)
@@ -60,7 +60,7 @@ UpdateConstrainCuda::UpdateConstrainCuda(gmx_unused const t_inputrec     &ir,
                                          gmx_unused const void           *commandStream)
     : impl_(nullptr)
 {
-    GMX_ASSERT(false, "A CPU stub for UpdateConstrain was called insted of the correct implementation.");
+    GMX_ASSERT(false, "A CPU stub for UpdateConstrain was called instead of the correct implementation.");
 }
 
 UpdateConstrainCuda::~UpdateConstrainCuda() = default;
@@ -75,7 +75,7 @@ void UpdateConstrainCuda::integrate(gmx_unused const real
                                     gmx_unused const float                       dtPressureCouple,
                                     gmx_unused const matrix                      velocityScalingMatrix)
 {
-    GMX_ASSERT(false, "A CPU stub for UpdateConstrain was called insted of the correct implementation.");
+    GMX_ASSERT(false, "A CPU stub for UpdateConstrain was called instead of the correct implementation.");
 }
 
 void UpdateConstrainCuda::set(gmx_unused DeviceBuffer<float>        d_x,
@@ -85,17 +85,23 @@ void UpdateConstrainCuda::set(gmx_unused DeviceBuffer<float>        d_x,
                               gmx_unused const t_mdatoms           &md,
                               gmx_unused const int                  numTempScaleValues)
 {
-    GMX_ASSERT(false, "A CPU stub for UpdateConstrain was called insted of the correct implementation.");
+    GMX_ASSERT(false, "A CPU stub for UpdateConstrain was called instead of the correct implementation.");
 }
 
 void UpdateConstrainCuda::setPbc(gmx_unused const t_pbc *pbc)
 {
-    GMX_ASSERT(false, "A CPU stub for UpdateConstrain was called insted of the correct implementation.");
+    GMX_ASSERT(false, "A CPU stub for UpdateConstrain was called instead of the correct implementation.");
 }
 
-void UpdateConstrainCuda::synchronizeStream()
+void UpdateConstrainCuda::waitCoordinatesReadyOnDevice()
 {
-    GMX_ASSERT(false, "A CPU stub for UpdateConstrain was called insted of the correct implementation.");
+    GMX_ASSERT(false, "A CPU stub for UpdateConstrain was called instead of the correct implementation.");
+}
+
+void* UpdateConstrainCuda::getCoordinatesReadySync()
+{
+    GMX_ASSERT(false, "A CPU stub for UpdateConstrain was called instead of the correct implementation.");
+    return nullptr;
 }
 
 }      // namespace gmx
index e4667720947eb63fc93ce77a5f3442495a435558..a58657b0e85dad99346126db743e9dff226e86d5 100644 (file)
@@ -107,6 +107,8 @@ void UpdateConstrainCuda::Impl::integrate(const real                        dt,
         }
     }
 
+    coordinatesReady_.markEvent(commandStream_);
+
     return;
 }
 
@@ -162,9 +164,14 @@ void UpdateConstrainCuda::Impl::setPbc(const t_pbc *pbc)
     settleCuda_->setPbc(pbc);
 }
 
-void UpdateConstrainCuda::Impl::synchronizeStream()
+void UpdateConstrainCuda::Impl::waitCoordinatesReadyOnDevice()
+{
+    coordinatesReady_.waitForEvent();
+}
+
+void *UpdateConstrainCuda::Impl::getCoordinatesReadySync()
 {
-    gpuStreamSynchronize(commandStream_);
+    return static_cast<void*> (&coordinatesReady_);
 }
 
 UpdateConstrainCuda::UpdateConstrainCuda(const t_inputrec  &ir,
@@ -206,9 +213,14 @@ void UpdateConstrainCuda::setPbc(const t_pbc *pbc)
     impl_->setPbc(pbc);
 }
 
-void UpdateConstrainCuda::synchronizeStream()
+void UpdateConstrainCuda::waitCoordinatesReadyOnDevice()
+{
+    impl_->waitCoordinatesReadyOnDevice();
+}
+
+void* UpdateConstrainCuda::getCoordinatesReadySync()
 {
-    impl_->synchronizeStream();
+    return impl_->getCoordinatesReadySync();
 }
 
 } //namespace gmx
index 85c6fb0dc4c29e6059dc4c2782b44d10ca5d71d6..31a05961e8af17fdac0d9dc4887ad31a4a7eaedc 100644 (file)
@@ -48,6 +48,7 @@
 
 #include "gmxpre.h"
 
+#include "gromacs/gpu_utils/gpueventsynchronizer.cuh"
 #include "gromacs/mdlib/leapfrog_cuda.cuh"
 #include "gromacs/mdlib/lincs_cuda.cuh"
 #include "gromacs/mdlib/settle_cuda.cuh"
@@ -132,9 +133,16 @@ class UpdateConstrainCuda::Impl
          */
         void setPbc(const t_pbc *pbc);
 
-        /*! \brief Synchronize the device stream.
+        /*! \brief Blocking wait on the update of coordinates being ready.
+         *
+         * \todo Remove when the "stitching" is done.
+         */
+        void waitCoordinatesReadyOnDevice();
+
+
+        /*! \brief Return the synchronizer associated with the event indicated that the coordinates are ready on the device.
          */
-        void synchronizeStream();
+        void *getCoordinatesReadySync();
 
     private:
 
@@ -176,6 +184,8 @@ class UpdateConstrainCuda::Impl
         //! SETTLE CUDA object for water constrains
         std::unique_ptr<SettleCuda>          settleCuda_;
 
+        //! An event to indicate when the update of coordinates is complete
+        GpuEventSynchronizer                 coordinatesReady_;
 };
 
 } // namespace gmx
index 2cebfd79206bb9aacad6223e2cc5211fe91aca19..7655cc6f022d8a950219197a17df6db2b36caaeb 100644 (file)
@@ -1241,9 +1241,9 @@ void gmx::LegacySimulator::do_md()
                                   doPressureCouple, ir->nstpcouple*ir->delta_t, M);
             stateGpu->copyCoordinatesFromGpu(ArrayRef<RVec>(state->x), StatePropagatorDataGpu::AtomLocality::All);
             stateGpu->copyVelocitiesFromGpu(state->v, StatePropagatorDataGpu::AtomLocality::All);
-            // Synchronize the update stream.
-            // TODO: Replace with event-based synchronization.
-            integrator->synchronizeStream();
+
+            // TODO: replace with stateGpu->waitForCopyCoordinatesFromGpu(...)
+            integrator->waitCoordinatesReadyOnDevice();
         }
         else
         {