From 1ef0492763fb5fce3c6490cc829266a209fa36fc Mon Sep 17 00:00:00 2001 From: Arne Morten Kvarving Date: Thu, 9 Mar 2023 09:43:01 +0100 Subject: [PATCH 1/4] sync FindQuadMath with dune while at it, change to FindQuadMath, not FindQuadmath --- cmake/Modules/FindQuadMath.cmake | 49 ++++++++++++++++++++++++++++ cmake/Modules/FindQuadmath.cmake | 55 -------------------------------- cmake/Modules/Finddune-fem.cmake | 2 +- 3 files changed, 50 insertions(+), 56 deletions(-) create mode 100644 cmake/Modules/FindQuadMath.cmake delete mode 100644 cmake/Modules/FindQuadmath.cmake diff --git a/cmake/Modules/FindQuadMath.cmake b/cmake/Modules/FindQuadMath.cmake new file mode 100644 index 000000000..04ccd4b59 --- /dev/null +++ b/cmake/Modules/FindQuadMath.cmake @@ -0,0 +1,49 @@ +# Module that checks whether the compiler supports the +# quadruple precision floating point math +# +# Adds target QuadMath::QuadMath if found +# +# perform tests +include(CheckCXXSourceCompiles) +include(CMakePushCheckState) +cmake_push_check_state() +set(CMAKE_REQUIRED_LIBRARIES quadmath) +if(${CMAKE_CXX_COMPILER_ID} STREQUAL GNU) + set(CMAKE_REQUIRED_FLAGS "-fext-numeric-literals") +endif() +check_cxx_source_compiles(" +#include + +int main () +{ + __float128 r = 1.0q; + r = strtoflt128(\"1.2345678\", NULL); + return 0; +}" QuadMath_COMPILES) +cmake_pop_check_state() # Reset CMAKE_REQUIRED_XXX variables + +if(QuadMath_COMPILES) + # Use additional variable for better report message + set(QuadMath_VAR "(Supported by compiler)") +endif() + +# Report that package was found +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(QuadMath + DEFAULT_MSG + QuadMath_VAR QuadMath_COMPILES +) + +# add imported target for quadmath +if(QuadMath_FOUND AND NOT TARGET QuadMath::QuadMath) + # Compiler supports QuadMath: Add appropriate linker flag + add_library(QuadMath::QuadMath INTERFACE IMPORTED) + target_link_libraries(QuadMath::QuadMath INTERFACE quadmath) + + target_compile_definitions(QuadMath::QuadMath INTERFACE + _GLIBCXX_USE_FLOAT128 HAVE_QUAD=1 + ) + target_compile_options(QuadMath::QuadMath INTERFACE + $<$:-fext-numeric-literals> + ) +endif() diff --git a/cmake/Modules/FindQuadmath.cmake b/cmake/Modules/FindQuadmath.cmake deleted file mode 100644 index eef710422..000000000 --- a/cmake/Modules/FindQuadmath.cmake +++ /dev/null @@ -1,55 +0,0 @@ -# Module that checks whether the compiler supports the -# quadruple precision floating point math -# -# Sets the following variables: -# HAVE_QUAD -# QUADMATH_LIBRARIES -# -# perform tests -include(CheckCSourceCompiles) -include(CheckCXXSourceCompiles) -include(CMakePushCheckState) -include(CheckCXXCompilerFlag) - -if(NOT DEFINED USE_QUADMATH OR USE_QUADMATH) - if(NOT DEFINED HAVE_EXTENDED_NUMERIC_LITERALS) - check_cxx_compiler_flag("-Werror -fext-numeric-literals" HAVE_EXTENDED_NUMERIC_LITERALS) - endif() - - cmake_push_check_state(RESET) - list(APPEND CMAKE_REQUIRED_LIBRARIES "quadmath") - CHECK_CXX_SOURCE_COMPILES(" -#include - -int main(void){ - __float128 foo = sqrtq(123.456); - foo = FLT128_MIN; -}" QUADMATH_FOUND) - cmake_pop_check_state() - - if (QUADMATH_FOUND) - set(QUADMATH_LIBRARIES "quadmath") - set(HAVE_QUAD "${QUADMATH_FOUND}") - if (NOT TARGET QuadMath::QuadMath) - add_library(QuadMath::QuadMath INTERFACE IMPORTED) - set_target_properties(QuadMath::QuadMath PROPERTIES - INTERFACE_LINK_LIBRARIES quadmath - INTERFACE_COMPILE_DEFINITIONS _GLIBCXX_USE_FLOAT128) - if(HAVE_EXTENDED_NUMERIC_LITERALS) - set_target_properties(QuadMath::QuadMath PROPERTIES - INTERFACE_COMPILE_OPTIONS $<$:-fext-numeric-literals>) - endif() - endif() - endif() -endif() - -if (USE_QUADMATH AND NOT QUADMATH_FOUND) - message(STATUS "Quadruple precision math support is unavailable! Skipping it.") -endif() - -include(FindPackageHandleStandardArgs) -find_package_handle_standard_args(Quadmath - DEFAULT_MSG - QUADMATH_LIBRARIES - HAVE_QUAD - ) diff --git a/cmake/Modules/Finddune-fem.cmake b/cmake/Modules/Finddune-fem.cmake index f0929848c..749238c1e 100644 --- a/cmake/Modules/Finddune-fem.cmake +++ b/cmake/Modules/Finddune-fem.cmake @@ -25,7 +25,7 @@ find_opm_package ( ZLIB; ZOLTAN; METIS; - Quadmath + QuadMath " # header to search for "dune/fem/space/shapefunctionset/legendre.hh" From 27ecbe5c98b2ae654e4b09bfcdfbf9217c03df5f Mon Sep 17 00:00:00 2001 From: Arne Morten Kvarving Date: Thu, 9 Mar 2023 12:28:53 +0100 Subject: [PATCH 2/4] fixed: instance CO2Tables for quad if QuadMath was found --- CMakeLists.txt | 8 ++++++++ opm-common-prereqs.cmake | 1 + src/opm/material/components/CO2.cpp | 15 +++++++++++++++ 3 files changed, 24 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2a891c4d4..7dd6bf4ab 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -151,6 +151,14 @@ macro (sources_hook) endif() set_source_files_properties(src/opm/input/eclipse/Python/Python.cpp PROPERTIES COMPILE_FLAGS -Wno-shadow) + if(QuadMath_FOUND) + get_target_property(qm_defs QuadMath::QuadMath INTERFACE_COMPILE_DEFINITIONS) + get_target_property(qm_options QuadMath::QuadMath INTERFACE_COMPILE_OPTIONS) + set_source_files_properties(src/opm/material/components/CO2.cpp + PROPERTIES COMPILE_DEFINITIONS "${qm_defs}" + COMPILE_OPTIONS "${qm_options}") + endif() + endmacro (sources_hook) macro (fortran_hook) diff --git a/opm-common-prereqs.cmake b/opm-common-prereqs.cmake index 57975dd1b..1de029441 100644 --- a/opm-common-prereqs.cmake +++ b/opm-common-prereqs.cmake @@ -28,5 +28,6 @@ list(APPEND opm-common_DEPS # cannot format UDQVarType. Hence we use the same version # as the embedded one. "fmt 7.0.3" + "QuadMath" ) find_package_deps(opm-common) diff --git a/src/opm/material/components/CO2.cpp b/src/opm/material/components/CO2.cpp index bd5426d38..53464c506 100644 --- a/src/opm/material/components/CO2.cpp +++ b/src/opm/material/components/CO2.cpp @@ -23,6 +23,9 @@ #include #include +#if HAVE_QUAD +#include +#endif #include "co2tables.inc" @@ -36,6 +39,7 @@ const UniformTabulated2DFunction& CO2::tabulatedDensity = CO2Tables::tabulatedDensity; template<> const double CO2::brineSalinity = CO2Tables::brineSalinity; + template<> const UniformTabulated2DFunction& CO2::tabulatedEnthalpy = CO2Tables::tabulatedEnthalpy; @@ -45,4 +49,15 @@ CO2::tabulatedDensity = CO2Tables::tabulatedDensity; template<> const float CO2::brineSalinity = CO2Tables::brineSalinity; +#if HAVE_QUAD +template<> +const UniformTabulated2DFunction& +CO2::tabulatedEnthalpy = CO2Tables::tabulatedEnthalpy; +template<> +const UniformTabulated2DFunction& +CO2::tabulatedDensity = CO2Tables::tabulatedDensity; +template<> +const quad CO2::brineSalinity = CO2Tables::brineSalinity; +#endif + } // namespace Opm From 509a018ae790e822dbc7d7802d2ece8dbfcfcd83 Mon Sep 17 00:00:00 2001 From: Arne Morten Kvarving Date: Thu, 9 Mar 2023 12:29:32 +0100 Subject: [PATCH 3/4] fixed: fix debug printing with quad std::to_string(__float128) is not defined we need to instance the evaluation formatter for __float128 --- CMakeLists.txt | 1 + opm/material/components/H2O.hpp | 19 +++++++++++++++---- .../CompositionFromFugacities.hpp | 18 ++++++++++++++---- opm/material/constraintsolvers/NcpFlash.hpp | 14 ++++++++++++-- .../fluidsystems/BrineCO2FluidSystem.hpp | 16 +++++++++++++--- src/opm/material/densead/Evaluation.cpp | 18 ++++++++++++++++++ 6 files changed, 73 insertions(+), 13 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7dd6bf4ab..b1b4f317f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -155,6 +155,7 @@ macro (sources_hook) get_target_property(qm_defs QuadMath::QuadMath INTERFACE_COMPILE_DEFINITIONS) get_target_property(qm_options QuadMath::QuadMath INTERFACE_COMPILE_OPTIONS) set_source_files_properties(src/opm/material/components/CO2.cpp + src/opm/material/densead/Evaluation.cpp PROPERTIES COMPILE_DEFINITIONS "${qm_defs}" COMPILE_OPTIONS "${qm_options}") endif() diff --git a/opm/material/components/H2O.hpp b/opm/material/components/H2O.hpp index 8db65a3e0..ad72e54be 100644 --- a/opm/material/components/H2O.hpp +++ b/opm/material/components/H2O.hpp @@ -981,13 +981,24 @@ private: const Evaluation& temperature, const Evaluation& pressure) { - auto tostring = [](const auto& val) -> std::string + auto cast = [](const auto d) { - if constexpr (DenseAd::is_evaluation::value) - return std::to_string(getValue(val.value())); +#if HAVE_QUAD + if constexpr (std::is_same_v) + return static_cast(d); else - return std::to_string(getValue(val)); +#endif + return d; }; + auto tostring = [cast](const auto& val) -> std::string + { + if constexpr (DenseAd::is_evaluation::value) { + return std::to_string(cast(getValue(val.value()))); + } + else + return std::to_string(cast(getValue(val))); + }; + return type + " is only implemented for temperatures " "below 623.15K and pressures below 100MPa. (T = " + tostring(temperature) + ", p=" + tostring(pressure); diff --git a/opm/material/constraintsolvers/CompositionFromFugacities.hpp b/opm/material/constraintsolvers/CompositionFromFugacities.hpp index 135f0d2c9..74f9edb97 100644 --- a/opm/material/constraintsolvers/CompositionFromFugacities.hpp +++ b/opm/material/constraintsolvers/CompositionFromFugacities.hpp @@ -166,17 +166,27 @@ public: } } + auto cast = [](const auto d) + { +#if HAVE_QUAD + if constexpr (std::is_same_v) + return static_cast(d); + else +#endif + return d; + }; + std::string msg = std::string("Calculating the ") + FluidSystem::phaseName(phaseIdx) + "Phase composition failed. Initial {x} = {"; for (const auto& v : xInit) - msg += " " + std::to_string(getValue(v)); + msg += " " + std::to_string(cast(getValue(v))); msg += " }, {fug_t} = {"; for (const auto& v : targetFug) - msg += " " + std::to_string(getValue(v)); - msg += " }, p = " + std::to_string(getValue(fluidState.pressure(phaseIdx))) - + ", T = " + std::to_string(getValue(fluidState.temperature(phaseIdx))); + msg += " " + std::to_string(cast(getValue(v))); + msg += " }, p = " + std::to_string(cast(getValue(fluidState.pressure(phaseIdx)))) + + ", T = " + std::to_string(cast(getValue(fluidState.temperature(phaseIdx)))); throw NumericalProblem(msg); } diff --git a/opm/material/constraintsolvers/NcpFlash.hpp b/opm/material/constraintsolvers/NcpFlash.hpp index a4d56fdb1..15d1ca0ac 100644 --- a/opm/material/constraintsolvers/NcpFlash.hpp +++ b/opm/material/constraintsolvers/NcpFlash.hpp @@ -232,12 +232,22 @@ public: } } + auto cast = [](const auto d) + { +#if HAVE_QUAD + if constexpr (std::is_same_v) + return static_cast(d); + else +#endif + return d; + }; + std::string msg = "NcpFlash solver failed: " "{c_alpha^kappa} = {"; for (const auto& v : globalMolarities) - msg += " " + std::to_string(getValue(v)); + msg += " " + std::to_string(cast(getValue(v))); msg += " }, T = "; - msg += std::to_string(getValue(fluidState.temperature(/*phaseIdx=*/0))); + msg += std::to_string(cast(getValue(fluidState.temperature(/*phaseIdx=*/0)))); throw NumericalProblem(msg); } diff --git a/opm/material/fluidsystems/BrineCO2FluidSystem.hpp b/opm/material/fluidsystems/BrineCO2FluidSystem.hpp index bb170710d..d71814808 100644 --- a/opm/material/fluidsystems/BrineCO2FluidSystem.hpp +++ b/opm/material/fluidsystems/BrineCO2FluidSystem.hpp @@ -490,12 +490,22 @@ private: Valgrind::CheckDefined(xlH2O); Valgrind::CheckDefined(xlCO2); - auto tostring = [](const auto& val) -> std::string + auto cast = [](const auto d) + { +#if HAVE_QUAD + if constexpr (std::is_same_v) + return static_cast(d); + else +#endif + return d; + }; + + auto tostring = [cast](const auto& val) -> std::string { if constexpr (DenseAd::is_evaluation::value) { - return std::to_string(getValue(val.value())); + return std::to_string(cast(getValue(val.value()))); } else { - return std::to_string(val); + return std::to_string(cast(val)); } }; diff --git a/src/opm/material/densead/Evaluation.cpp b/src/opm/material/densead/Evaluation.cpp index 705626965..60e2d2580 100644 --- a/src/opm/material/densead/Evaluation.cpp +++ b/src/opm/material/densead/Evaluation.cpp @@ -24,6 +24,10 @@ #include #include +#if HAVE_QUAD +#include +#endif + #include namespace Opm { @@ -52,13 +56,27 @@ void printEvaluation(std::ostream& os, const Evaluation&, \ bool); +#if HAVE_QUAD +#define INSTANCE(size) \ + INSTANCE_IMPL(double,size,0u) \ + INSTANCE_IMPL(quad,size,0u) \ + INSTANCE_IMPL(float,size,0u) +#else #define INSTANCE(size) \ INSTANCE_IMPL(double,size,0u) \ INSTANCE_IMPL(float,size,0u) +#endif +#if HAVE_QUAD +#define INSTANCE_DYN(size) \ + INSTANCE_IMPL(double,-1,size) \ + INSTANCE_IMPL(quad,-1,size) \ + INSTANCE_IMPL(float,-1,size) +#else #define INSTANCE_DYN(size) \ INSTANCE_IMPL(double,-1,size) \ INSTANCE_IMPL(float,-1,size) +#endif INSTANCE(1) INSTANCE(2) From 215fdae47d29584772330fc9efb1b7fb67a5a21c Mon Sep 17 00:00:00 2001 From: Arne Morten Kvarving Date: Thu, 9 Mar 2023 13:02:09 +0100 Subject: [PATCH 4/4] changed: remove explicit option to enable/disable quadmath not necessary as it is now only used for relevant targets you can still use the generic -DCMAKE_DISABLE_FIND_PACKAGE_QuadMath=1 if you so desire --- cmake/Modules/OpmInit.cmake | 7 ------- 1 file changed, 7 deletions(-) diff --git a/cmake/Modules/OpmInit.cmake b/cmake/Modules/OpmInit.cmake index c2ddbd5c3..612aad930 100644 --- a/cmake/Modules/OpmInit.cmake +++ b/cmake/Modules/OpmInit.cmake @@ -142,13 +142,6 @@ set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS OFF) -# quadmath must be explicitly enabled -# This needs to be in OpmInit as prereqs is called before OpmLibMain is included. -option (USE_QUADMATH "Search for high precision floating point library (normally not used)" ON) -if (NOT USE_QUADMATH) - set (CMAKE_DISABLE_FIND_PACKAGE_QuadMath TRUE) -endif () - option (USE_SUPERLU "Use SuperLU direct solvers for AMG (if umfpack is not found)" ON) if (NOT USE_SUPERLU) set (CMAKE_DISABLE_FIND_PACKAGE_SuperLU TRUE)