Add checking function for whether a buffer is pinned
[alexxy/gromacs.git] / src / gromacs / gpu_utils / gpu_utils.cu
index 5e0afd3f2bea342689a7c3ac1d0d660f42bbf81a..fb509f5d37e16b964b275d4a828796948d6274b7 100644 (file)
@@ -134,6 +134,31 @@ static void checkCompiledTargetCompatibility(const gmx_device_info_t *devInfo)
     }
 }
 
+bool isHostMemoryPinned(void *h_ptr)
+{
+    cudaPointerAttributes memoryAttributes;
+    cudaError_t           stat = cudaPointerGetAttributes(&memoryAttributes, h_ptr);
+
+    bool                  result = false;
+    switch (stat)
+    {
+        case cudaSuccess:
+            result = true;
+            break;
+
+        case cudaErrorInvalidValue:
+            // If the buffer was not pinned, then it will not be recognized by CUDA at all
+            result = false;
+            // Reset the last error status
+            cudaGetLastError();
+            break;
+
+        default:
+            CU_RET_ERR(stat, "Unexpected CUDA error");
+    }
+    return result;
+}
+
 /*!
  * \brief Runs GPU sanity checks.
  *