Fix free_gpu
authorMark Abraham <mark.j.abraham@gmail.com>
Mon, 4 Dec 2017 10:56:46 +0000 (21:56 +1100)
committerMark Abraham <mark.j.abraham@gmail.com>
Mon, 4 Dec 2017 12:37:20 +0000 (13:37 +0100)
If a device context was not used, CUDA gives an error if we attempt to
clear it, so we must avoid clearing it.

Refs #2322

Change-Id: I67b8b2d263eaed9c7489a6de6f612b27496cc6c2

src/gromacs/gpu_utils/gpu_utils.cu
src/gromacs/gpu_utils/gpu_utils.h

index 4e2200794089385cc0310117b7e24ca7064de543..273c3baba17bd3c76f90a916035361f1ed61d334 100644 (file)
@@ -536,6 +536,14 @@ void init_gpu(const gmx::MDLogger &mdlog,
 
 void free_gpu(const gmx_device_info_t *deviceInfo)
 {
+    // One should only attempt to clear the device context when
+    // it has been used, but currently the only way to know that a GPU
+    // device was used is that deviceInfo will be non-null.
+    if (deviceInfo == nullptr)
+    {
+        return;
+    }
+
     cudaError_t  stat;
 
     if (debug)
@@ -546,12 +554,9 @@ void free_gpu(const gmx_device_info_t *deviceInfo)
         fprintf(stderr, "Cleaning up context on GPU ID #%d\n", gpuid);
     }
 
-    if (deviceInfo != nullptr)
+    if (!reset_gpu_application_clocks(deviceInfo))
     {
-        if (!reset_gpu_application_clocks(deviceInfo))
-        {
-            gmx_warning("Failed to reset GPU application clocks on GPU #%d", deviceInfo->id);
-        }
+        gmx_warning("Failed to reset GPU application clocks on GPU #%d", deviceInfo->id);
     }
 
     stat = cudaDeviceReset();
index b1cc07b51f9b656dcd9d46f6575537638ae33d93..a3254bd467cdc9cf9226459d570cc326ec333ef1 100644 (file)
@@ -125,8 +125,12 @@ void init_gpu(const gmx::MDLogger &GPU_FUNC_ARGUMENT(mdlog),
 
 /*! \brief Frees up the CUDA GPU used by the active context at the time of calling.
  *
- * The context is explicitly destroyed and therefore all data uploaded to the GPU
- * is lost. This should only be called when none of this data is required anymore.
+ * If \c deviceInfo is nullptr, then it is understood that no device
+ * was selected so no context is active to be freed. Otherwise, the
+ * context is explicitly destroyed and therefore all data uploaded to
+ * the GPU is lost. This must only be called when none of this data is
+ * required anymore, because subsequent attempts to free memory
+ * associated with the context will otherwise fail.
  *
  * Calls gmx_warning upon errors.
  *