Unify handling of GMX_ENABLE_GPU_TIMING and GMX_DISABLE_GPU_TIMING
[alexxy/gromacs.git] / src / gromacs / gpu_utils / gpu_utils.cpp
index eb26ce0a8e16a5e381363add1747f7f2c5169c39..2d8846caa334f827ad6c3ed6846b87694a0c4b17 100644 (file)
@@ -1,7 +1,8 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2014,2015,2017,2018,2019,2020, 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.
 
 #include "config.h"
 
-#include <cassert>
-
 #include "gromacs/utility/arrayref.h"
-#include "gromacs/utility/smalloc.h"
+#include "gromacs/utility/enumerationhelpers.h"
 #include "gromacs/utility/stringutil.h"
 
 #ifdef _MSC_VER
 #    pragma warning(disable : 6237)
 #endif
 
-/*! \brief Help build a descriptive message in \c error if there are
- * \c errorReasons why nonbondeds on a GPU are not supported.
- *
- * \returns Whether the lack of errorReasons indicate there is support. */
-static bool addMessageIfNotSupported(gmx::ArrayRef<const std::string> errorReasons, std::string* error)
+const char* enumValueToString(GpuApiCallBehavior enumValue)
 {
-    bool isSupported = errorReasons.empty();
-    if (!isSupported && error)
-    {
-        *error = "Nonbonded interactions cannot run on GPUs: ";
-        *error += joinStrings(errorReasons, "; ") + ".";
-    }
-    return isSupported;
+    static constexpr gmx::EnumerationArray<GpuApiCallBehavior, const char*> s_gpuApiCallBehaviorNames = {
+        "Synchronous", "Asynchronous"
+    };
+    return s_gpuApiCallBehaviorNames[enumValue];
 }
 
-bool buildSupportsNonbondedOnGpu(std::string* error)
+bool decideGpuTimingsUsage()
 {
-    std::vector<std::string> errorReasons;
-    if (GMX_DOUBLE)
+    if (GMX_GPU_CUDA || GMX_GPU_SYCL)
     {
-        errorReasons.emplace_back("double precision");
+        /* 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);
     }
-    if (!GMX_GPU)
+    else if (GMX_GPU_OPENCL)
     {
-        errorReasons.emplace_back("non-GPU build of GROMACS");
+        return (getenv("GMX_DISABLE_GPU_TIMING") == nullptr);
     }
-    if (GMX_GPU_SYCL)
+    else
     {
-        errorReasons.emplace_back("SYCL build of GROMACS");
+        // CPU-only build
+        return false;
     }
-    return addMessageIfNotSupported(errorReasons, error);
 }