Implement alternating GPU wait
[alexxy/gromacs.git] / src / gromacs / gpu_utils / cudautils.cuh
index dae3548fdd950a8d5bb1dee5bf8392b61ce75df8..b4cca13bb546d5d098b29a2db9aca738242b4160 100644 (file)
@@ -266,7 +266,7 @@ static inline void rvec_inc(rvec a, const float3 b)
     rvec_inc(a, tmp);
 }
 
-/*! \brief Calls cudaStreamSynchronize() in the stream \p s.
+/*! \brief Wait for all taks in stream \p s to complete.
  *
  * \param[in] s stream to synchronize with
  */
@@ -276,4 +276,30 @@ static inline void gpuStreamSynchronize(cudaStream_t s)
     CU_RET_ERR(stat, "cudaStreamSynchronize failed");
 }
 
+/*! \brief  Returns true if all tasks in \p s have completed.
+ *
+ * \param[in] s stream to check
+ *
+ *  \returns     True if all tasks enqueued in the stream \p s (at the time of this call) have completed.
+ */
+static inline bool haveStreamTasksCompleted(cudaStream_t s)
+{
+    cudaError_t stat = cudaStreamQuery(s);
+
+    if (stat == cudaErrorNotReady)
+    {
+        // work is still in progress in the stream
+        return false;
+    }
+
+    GMX_ASSERT(stat !=  cudaErrorInvalidResourceHandle, "Stream idnetifier not valid");
+
+    // cudaSuccess and cudaErrorNotReady are the expected return values
+    CU_RET_ERR(stat, "Unexpected cudaStreamQuery failure");
+
+    GMX_ASSERT(stat == cudaSuccess, "Values other than cudaSuccess should have been explicitly handled");
+
+    return true;
+}
+
 #endif