Unify handling of GMX_ENABLE_GPU_TIMING and GMX_DISABLE_GPU_TIMING
[alexxy/gromacs.git] / src / gromacs / gpu_utils / gpu_utils.cpp
index fcaed80d22eb83c273d185b5d88d3c1456d3d255..2d8846caa334f827ad6c3ed6846b87694a0c4b17 100644 (file)
@@ -1,7 +1,8 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2014,2015,2017, by the GROMACS development team, led by
+ * Copyright (c) 2014,2015,2017,2018,2019, by the GROMACS development team.
+ * Copyright (c) 2020,2021, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
@@ -33,7 +34,7 @@
  * the research papers on the package. Check out http://www.gromacs.org.
  */
 /*! \internal \file
- *  \brief Stub functions for non-GPU builds
+ *  \brief Function definitions for non-GPU builds
  *
  *  \author Mark Abraham <mark.j.abraham@gmail.com>
  */
 
 #include "gpu_utils.h"
 
-/*! \brief Set allocation functions used by the GPU host
- *
- * Since GPU support is not configured, there is no host memory to
- * allocate. */
-void gpu_set_host_malloc_and_free(bool,
-                                  gmx_host_alloc_t **nb_alloc,
-                                  gmx_host_free_t  **nb_free)
+#include "config.h"
+
+#include "gromacs/utility/arrayref.h"
+#include "gromacs/utility/enumerationhelpers.h"
+#include "gromacs/utility/stringutil.h"
+
+#ifdef _MSC_VER
+#    pragma warning(disable : 6237)
+#endif
+
+const char* enumValueToString(GpuApiCallBehavior enumValue)
+{
+    static constexpr gmx::EnumerationArray<GpuApiCallBehavior, const char*> s_gpuApiCallBehaviorNames = {
+        "Synchronous", "Asynchronous"
+    };
+    return s_gpuApiCallBehaviorNames[enumValue];
+}
+
+bool decideGpuTimingsUsage()
 {
-    *nb_alloc = nullptr;
-    *nb_free  = nullptr;
+    if (GMX_GPU_CUDA || GMX_GPU_SYCL)
+    {
+        /* CUDA: timings are incorrect with multiple streams.
+         * This is the main reason why they are disabled by default.
+         * TODO: Consider turning on by default when we can detect nr of streams.
+         *
+         * SYCL: compilers and runtimes change rapidly, so we disable timings by default
+         * to avoid any possible overhead. */
+        return (getenv("GMX_ENABLE_GPU_TIMING") != nullptr);
+    }
+    else if (GMX_GPU_OPENCL)
+    {
+        return (getenv("GMX_DISABLE_GPU_TIMING") == nullptr);
+    }
+    else
+    {
+        // CPU-only build
+        return false;
+    }
 }