Disable FPE for SYCL builds
authorAndrey Alekseenko <al42and@gmail.com>
Fri, 18 Dec 2020 13:39:51 +0000 (13:39 +0000)
committerAndrey Alekseenko <al42and@gmail.com>
Mon, 8 Feb 2021 16:20:22 +0000 (19:20 +0300)
Intel Graphics Compiler triggers FPEs inside its JIT. We can more
selectively call gmx_fedisableexcept only iff we are running on
Intel's GPU, but, to me, a more predictable approach seems friendlier.

Also, refactored the two complex PP conditions into a separate function.
It's only called once at program launch, so function call overhead
should not be an issue.

api/legacy/include/gromacs/math/utilities.h
docs/dev-manual/tools.rst
src/gromacs/math/utilities.cpp
src/gromacs/mdrun/runner.cpp
src/testutils/testinit.cpp

index 5ff601d97cdfff969b386ae520fca04ec89be1fe..b0647ddfe8b5a9676fdf632c863a1107a8d8a390 100644 (file)
@@ -4,7 +4,7 @@
  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
  * Copyright (c) 2001-2004, The GROMACS development team.
  * Copyright (c) 2013,2014,2015,2016,2017 by the GROMACS development team.
- * Copyright (c) 2018,2019,2020, by the GROMACS development team, led by
+ * Copyright (c) 2018,2019,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.
@@ -157,4 +157,19 @@ int gmx_feenableexcept();
  */
 int gmx_fedisableexcept();
 
+/*! \brief Return true if the current build should enable floating-point exceptions by default.
+ *
+ * Currently, it returns true unless any of the following conditions are met:
+ * - release build,
+ * - SYCL build (Intel IGC, at least 1.0.5964, raises FP exceptions in JIT compilation),
+ * - - See https://github.com/intel/intel-graphics-compiler/issues/164
+ * - compilers with known buggy FP exception support (clang with any optimization)
+ *   or suspected buggy FP exception support (gcc 7.* with optimization).
+ *
+ * Note that this function does not check whether the build/OS supports FP exceptions.
+ *
+ * \returns true if we should enable FP exceptions by default.
+ */
+bool gmxShouldEnableFPExceptions();
+
 #endif
index c6f7d6f3c58a00f734453ab0dc8fb536a0d0c2c0..1c4d7efdaa6c33a961ae8806a0e6531e62286218 100644 (file)
@@ -77,6 +77,20 @@ coverage
 
 regression tests
 
+floating-point exceptions
+  In debug builds, floating-point exceptions (FPEs) are generated whenever one of the
+  following operations is encountered: division by zero, floating-point overflow,
+  invalid operation (e.g., taking sqrt of a negative number).
+  Such checks are *not* performed in the following configurations:
+
+  - release build,
+  - any build by GCC 7.x or Clang with optimizations,
+  - build with SYCL support.
+
+  In these configurations, FPEs can be enabled by adding ``-fpexcept`` flag to ``gmx``
+  invocation. However, FPEs are not supported on Windows and non-x86 Apple hardware.
+  See ``api/legacy/include/gromacs/math/utilities.h`` for more details.
+
 .. _dev-formatting-tools:
 
 Code formatting and style
index 067cc32b25b81476635397f0a0bba91371a8c210..3e0c33ec4d82961f3066369e01641c7308b27415 100644 (file)
@@ -4,7 +4,7 @@
  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
  * Copyright (c) 2001-2004, The GROMACS development team.
  * Copyright (c) 2013,2014,2015,2016,2018 by the GROMACS development team.
- * Copyright (c) 2019,2020, by the GROMACS development team, led by
+ * Copyright (c) 2019,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.
@@ -140,3 +140,16 @@ int gmx_fedisableexcept()
     return -1;
 #endif
 }
+
+bool gmxShouldEnableFPExceptions()
+{
+#if defined(NDEBUG)
+    return false; // Release build
+#elif ((defined __clang__ || (defined(__GNUC__) && __GNUC__ == 7)) && defined __OPTIMIZE__)
+    return false; // Buggy compiler
+#elif GMX_GPU_SYCL
+    return false; // avoid spurious FPE during SYCL JIT
+#else
+    return true;
+#endif
+}
index 802b5bf3276cd7c421aa104599104a524155ea96..d42675f9fa3edb4352056be769ab89f78b6396d7 100644 (file)
@@ -1478,16 +1478,7 @@ int Mdrunner::mdrunner()
                           hw_opt.nthreads_omp_pme,
                           !thisRankHasDuty(cr, DUTY_PP));
 
-// Enable FP exception detection, but not in
-// Release mode and not for compilers with known buggy FP
-// exception support (clang with any optimization) or suspected
-// buggy FP exception support (gcc 7.* with optimization).
-#if !defined NDEBUG \
-        && !((defined __clang__ || (defined(__GNUC__) && __GNUC__ == 7)) && defined __OPTIMIZE__)
-    const bool bEnableFPE = true;
-#else
-    const bool bEnableFPE = false;
-#endif
+    const bool bEnableFPE = gmxShouldEnableFPExceptions();
     // FIXME - reconcile with gmx_feenableexcept() call from CommandLineModuleManager::run()
     if (bEnableFPE)
     {
index 1081a181435409f896cfe7a40a255fa83d085648..e88d74c92a062fabfbb415c4b6e04fb1b971064e 100644 (file)
@@ -153,10 +153,10 @@ void initTestUtils(const char* dataPath,
                    int*        argc,
                    char***     argv)
 {
-#if !defined NDEBUG \
-        && !((defined __clang__ || (defined(__GNUC__) && __GNUC__ == 7)) && defined __OPTIMIZE__)
-    gmx_feenableexcept();
-#endif
+    if (gmxShouldEnableFPExceptions())
+    {
+        gmx_feenableexcept();
+    }
     const CommandLineProgramContext& context = initForCommandLine(argc, argv);
     try
     {