From 48fb95de1e456569deed5096924e617d511cf109 Mon Sep 17 00:00:00 2001 From: Andrey Alekseenko Date: Fri, 18 Dec 2020 13:39:51 +0000 Subject: [PATCH] Disable FPE for SYCL builds 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 | 17 ++++++++++++++++- docs/dev-manual/tools.rst | 14 ++++++++++++++ src/gromacs/math/utilities.cpp | 15 ++++++++++++++- src/gromacs/mdrun/runner.cpp | 11 +---------- src/testutils/testinit.cpp | 8 ++++---- 5 files changed, 49 insertions(+), 16 deletions(-) diff --git a/api/legacy/include/gromacs/math/utilities.h b/api/legacy/include/gromacs/math/utilities.h index 5ff601d97c..b0647ddfe8 100644 --- a/api/legacy/include/gromacs/math/utilities.h +++ b/api/legacy/include/gromacs/math/utilities.h @@ -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 diff --git a/docs/dev-manual/tools.rst b/docs/dev-manual/tools.rst index c6f7d6f3c5..1c4d7efdaa 100644 --- a/docs/dev-manual/tools.rst +++ b/docs/dev-manual/tools.rst @@ -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 diff --git a/src/gromacs/math/utilities.cpp b/src/gromacs/math/utilities.cpp index 067cc32b25..3e0c33ec4d 100644 --- a/src/gromacs/math/utilities.cpp +++ b/src/gromacs/math/utilities.cpp @@ -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 +} diff --git a/src/gromacs/mdrun/runner.cpp b/src/gromacs/mdrun/runner.cpp index 802b5bf327..d42675f9fa 100644 --- a/src/gromacs/mdrun/runner.cpp +++ b/src/gromacs/mdrun/runner.cpp @@ -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) { diff --git a/src/testutils/testinit.cpp b/src/testutils/testinit.cpp index 1081a18143..e88d74c92a 100644 --- a/src/testutils/testinit.cpp +++ b/src/testutils/testinit.cpp @@ -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 { -- 2.22.0