Skip to content

Commit

Permalink
Merge pull request #4677 from ye-luo/custom-isnan
Browse files Browse the repository at this point in the history
Introduce qmcplusplus::isnan
  • Loading branch information
prckent authored Jul 19, 2023
2 parents 1e365df + c805656 commit 86d77a8
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/Platforms/CPU/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
#// This file is distributed under the University of Illinois/NCSA Open Source License.
#// See LICENSE file in top directory for details.
#//
#// Copyright (c) 2020 QMCPACK developers.
#// Copyright (c) 2023 QMCPACK developers.
#//
#// File developed by: Ye Luo, yeluo@anl.gov, Argonne National Laboratory
#//
#// File created by: Ye Luo, yeluo@anl.gov, Argonne National Laboratory
#//////////////////////////////////////////////////////////////////////////////////////

add_library(platform_cpu_runtime INTERFACE)
# To enable a custom qmcplusplus::isnan, remove fast-math. Only the file scope CMAKE_CXX_FLAGS gets affected.
string(REPLACE " -ffast-math" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})

add_library(platform_cpu_runtime math.cpp)
target_link_libraries(platform_cpu_runtime INTERFACE Math::scalar_vector_functions)

set(CPU_SRCS BlasThreadingEnv.cpp OMPThreadCountProtectorLA.cpp)
Expand Down
19 changes: 19 additions & 0 deletions src/Platforms/CPU/math.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//////////////////////////////////////////////////////////////////////////////////////
// This file is distributed under the University of Illinois/NCSA Open Source License.
// See LICENSE file in top directory for details.
//
// Copyright (c) 2023 QMCPACK developers.
//
// File developed by: Ye Luo, yeluo@anl.gov, Argonne National Laboratory
//
// File created by: Ye Luo, yeluo@anl.gov, Argonne National Laboratory
//////////////////////////////////////////////////////////////////////////////////////

#include <cmath>

namespace qmcplusplus
{
// To make the customized qmcplusplus::isnan always effective, this file must be compiled without -ffast-math.
bool isnan(float a) { return a != a; }
bool isnan(double a) { return a != a; }
} // namespace qmcplusplus
8 changes: 8 additions & 0 deletions src/Platforms/CPU/math.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ inline bool iszero(T a)
return std::fpclassify(a) == FP_ZERO;
}

/** return true if the value is NaN.
* std::isnan can be affected by compiler the -ffast-math option and return true constantly.
* The customized qmcplusplus::isnan should be always effective.
* This requires its definition compiled without -ffast-math.
*/
bool isnan(float);
bool isnan(double);

}

#endif
2 changes: 1 addition & 1 deletion src/Platforms/tests/CPU/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ set(SRC_DIR cpu)
set(UTEST_EXE test_${SRC_DIR})
set(UTEST_NAME deterministic-unit_test_${SRC_DIR})

add_executable(${UTEST_EXE} test_aligned_allocator.cpp test_e2iphi.cpp)
add_executable(${UTEST_EXE} test_aligned_allocator.cpp test_e2iphi.cpp test_math.cpp)
target_link_libraries(${UTEST_EXE} platform_runtime catch_main)

add_unit_test(${UTEST_NAME} 1 1 $<TARGET_FILE:${UTEST_EXE}>)
38 changes: 38 additions & 0 deletions src/Platforms/tests/CPU/test_math.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//////////////////////////////////////////////////////////////////////////////////////
// This file is distributed under the University of Illinois/NCSA Open Source License.
// See LICENSE file in top directory for details.
//
// Copyright (c) 2023 QMCPACK developers.
//
// File developed by: Ye Luo, yeluo@anl.gov, Argonne National Laboratory
//
// File created by: Ye Luo, yeluo@anl.gov, Argonne National Laboratory
//////////////////////////////////////////////////////////////////////////////////////


#include "catch.hpp"

#include <cmath>
#include "CPU/math.hpp"

namespace qmcplusplus
{

template<typename T>
void test_isnan()
{
const T one(1);
T a = std::sqrt(-one);
T b = -a;
CHECK(!qmcplusplus::isnan(one));
CHECK(qmcplusplus::isnan(a));
CHECK(qmcplusplus::isnan(b));
}

TEST_CASE("isnan", "[numerics]")
{
test_isnan<float>();
test_isnan<double>();
}

} // namespace qmcplusplus
2 changes: 1 addition & 1 deletion src/QMCTools/ppconvert/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ if(CMAKE_CXX_FLAGS_RELEASE)
string(REPLACE "-DNDEBUG" "" CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE})
endif()

string(REPLACE "-ffast-math" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
string(REPLACE " -ffast-math" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})

if(CMAKE_CROSSCOMPILING)
message(STATUS "Cannot run std::isnan tests when cross compiling. Setting ISNAN_WORKS to FALSE.")
Expand Down

0 comments on commit 86d77a8

Please sign in to comment.