From f8e2919e5ba3598474f967c7329cab9816464a50 Mon Sep 17 00:00:00 2001 From: Philip Mueller Date: Thu, 14 Nov 2024 15:00:01 +0100 Subject: [PATCH] Added tests for the NAN. --- dace/runtime/include/dace/nan.h | 36 ++++++++++++++++++--------------- tests/numpy/constants_test.py | 26 +++++++++++++++++++++++- 2 files changed, 45 insertions(+), 17 deletions(-) diff --git a/dace/runtime/include/dace/nan.h b/dace/runtime/include/dace/nan.h index c13f00a85c..e4bb8451a1 100644 --- a/dace/runtime/include/dace/nan.h +++ b/dace/runtime/include/dace/nan.h @@ -48,11 +48,11 @@ namespace dace }; template - DACE_CONSTEXPR DACE_HDFI std::enable_if_t::value, typeless_nan> + DACE_CONSTEXPR DACE_HDFI std::enable_if_t::value || std::is_integral::value, typeless_nan> operator*(const T&, const typeless_nan&) noexcept { return typeless_nan{}; } template - DACE_CONSTEXPR DACE_HDFI std::enable_if_t::value, typeless_nan> + DACE_CONSTEXPR DACE_HDFI std::enable_if_t::value || std::is_integral::value, typeless_nan> operator*(const typeless_nan&, const T&) noexcept { return typeless_nan{}; } DACE_CONSTEXPR DACE_HDFI typeless_nan @@ -60,11 +60,11 @@ namespace dace template - DACE_CONSTEXPR DACE_HDFI std::enable_if_t::value, typeless_nan> + DACE_CONSTEXPR DACE_HDFI std::enable_if_t::value || std::is_integral::value, typeless_nan> operator+(const T&, const typeless_nan&) noexcept { return typeless_nan{}; } template - DACE_CONSTEXPR DACE_HDFI std::enable_if_t::value, typeless_nan> + DACE_CONSTEXPR DACE_HDFI std::enable_if_t::value || std::is_integral::value, typeless_nan> operator+(const typeless_nan&, const T&) noexcept { return typeless_nan{}; } DACE_CONSTEXPR DACE_HDFI typeless_nan @@ -72,11 +72,11 @@ namespace dace template - DACE_CONSTEXPR DACE_HDFI std::enable_if_t::value, typeless_nan> + DACE_CONSTEXPR DACE_HDFI std::enable_if_t::value || std::is_integral::value, typeless_nan> operator-(const T&, const typeless_nan&) noexcept { return typeless_nan{}; } template - DACE_CONSTEXPR DACE_HDFI std::enable_if_t::value, typeless_nan> + DACE_CONSTEXPR DACE_HDFI std::enable_if_t::value || std::is_integral::value, typeless_nan> operator-(const typeless_nan&, const T&) noexcept { return typeless_nan{}; } DACE_CONSTEXPR DACE_HDFI typeless_nan @@ -84,11 +84,11 @@ namespace dace template - DACE_CONSTEXPR DACE_HDFI std::enable_if_t::value, typeless_nan> + DACE_CONSTEXPR DACE_HDFI std::enable_if_t::value || std::is_integral::value, typeless_nan> operator/(const T&, const typeless_nan&) noexcept { return typeless_nan{}; } template - DACE_CONSTEXPR DACE_HDFI std::enable_if_t::value, typeless_nan> + DACE_CONSTEXPR DACE_HDFI std::enable_if_t::value || std::is_integral::value, typeless_nan> operator/(const typeless_nan&, const T&) noexcept { return typeless_nan{}; } DACE_CONSTEXPR DACE_HDFI typeless_nan @@ -96,29 +96,33 @@ namespace dace template - DACE_CONSTEXPR DACE_HDFI std::enable_if_t::value, typeless_nan> + DACE_CONSTEXPR DACE_HDFI std::enable_if_t::value || std::is_integral::value, typeless_nan> operator%(const T&, const typeless_nan&) noexcept { return typeless_nan{}; } template - DACE_CONSTEXPR DACE_HDFI std::enable_if_t::value, typeless_nan> + DACE_CONSTEXPR DACE_HDFI std::enable_if_t::value || std::is_integral::value, typeless_nan> operator%(const typeless_nan&, const T&) noexcept { return typeless_nan{}; } DACE_CONSTEXPR DACE_HDFI typeless_nan operator%(const typeless_nan&, const typeless_nan&) noexcept { return typeless_nan{}; } - } -} + DACE_HDFI typeless_nan ipow(const typeless_nan&, const unsigned int&) { + return typeless_nan{}; + } //These functions allows to perfrom operations with `typeless_nan` instances. -# define FADAPT(F) DACE_CONSTEXPR DACE_HDFI ::dace::math::typeless_nan F (::dace::math::typeless_nan) noexcept { return ::dace::math::typeless_nan{}; } -# define FADAPT2(F) template DACE_CONSTEXPR DACE_HDFI dace::math::typeless_nan F (T1&&, dace::math::typeless_nan) noexcept { return ::dace::math::typeless_nan{}; }; \ - template DACE_CONSTEXPR DACE_HDFI dace::math::typeless_nan F (dace::math::typeless_nan, T2&&) noexcept { return ::dace::math::typeless_nan{}; }; \ - DACE_CONSTEXPR DACE_HDFI ::dace::math::typeless_nan F (dace::math::typeless_nan, dace::math::typeless_nan) noexcept { return ::dace::math::typeless_nan{}; } +# define FADAPT(F) DACE_CONSTEXPR DACE_HDFI typeless_nan F (const typeless_nan&) noexcept { return typeless_nan{}; } +# define FADAPT2(F) template DACE_CONSTEXPR DACE_HDFI typeless_nan F (T1&&, dace::math::typeless_nan) noexcept { return typeless_nan{}; }; \ + template DACE_CONSTEXPR DACE_HDFI typeless_nan F (const typeless_nan&, T2&&) noexcept { return typeless_nan{}; }; \ + DACE_CONSTEXPR DACE_HDFI typeless_nan F (const typeless_nan&, const typeless_nan&) noexcept { return typeless_nan{}; } FADAPT(tanh); FADAPT(cos); FADAPT(sin); FADAPT(sqrt); FADAPT(tan); FADAPT(acos); FADAPT(asin); FADAPT(atan); FADAPT(log); FADAPT(exp); FADAPT(floor); FADAPT(ceil); FADAPT(round); FADAPT(abs); FADAPT2(max); FADAPT2(min); # undef FADAPT2 # undef FADAPT + } +} + #endif // __DACE_NAN_H diff --git a/tests/numpy/constants_test.py b/tests/numpy/constants_test.py index 1fc159696b..c63ab08138 100644 --- a/tests/numpy/constants_test.py +++ b/tests/numpy/constants_test.py @@ -40,7 +40,7 @@ def _test_sdfg( out = np.zeros(10, dtype=dtype) sdfg.apply_gpu_transformations() sdfg(out=out) - assert np.allclose(out, expected), f"Expected {expected}, but got {out[0]}" + assert np.allclose(out, expected, equal_nan=True), f"Expected {expected}, but got {out[0]}" def _perform_test( @@ -137,9 +137,33 @@ def test_constant_pi_fun(): ) +@pytest.mark.gpu +def test_constant_nan(): + _perform_test( + code="math.nan", + expected=math.nan + ) + _perform_test( + code="math.nan + 2", + expected=math.nan + ) + _perform_test( + code="math.nan + 2.0", + expected=math.nan + ) + _perform_test( + code="math.sin(math.nan + 2.0)", + expected=math.nan + ) + _perform_test( + code="math.sin(math.nan + 2.0) ** 2", + expected=math.nan + ) + if __name__ == "__main__": test_constant_pi_simple() test_constant_pi_add() test_constant_pi_mult() test_constant_pi_fun() + test_constant_nan()