Skip to content

Commit

Permalink
Added tests for the NAN.
Browse files Browse the repository at this point in the history
  • Loading branch information
philip-paul-mueller committed Nov 14, 2024
1 parent 8143feb commit f8e2919
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 17 deletions.
36 changes: 20 additions & 16 deletions dace/runtime/include/dace/nan.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,77 +48,81 @@ namespace dace
};

template<typename T>
DACE_CONSTEXPR DACE_HDFI std::enable_if_t<std::is_floating_point<T>::value, typeless_nan>
DACE_CONSTEXPR DACE_HDFI std::enable_if_t<std::is_floating_point<T>::value || std::is_integral<T>::value, typeless_nan>
operator*(const T&, const typeless_nan&) noexcept { return typeless_nan{}; }

template<typename T>
DACE_CONSTEXPR DACE_HDFI std::enable_if_t<std::is_floating_point<T>::value, typeless_nan>
DACE_CONSTEXPR DACE_HDFI std::enable_if_t<std::is_floating_point<T>::value || std::is_integral<T>::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{}; }


template<typename T>
DACE_CONSTEXPR DACE_HDFI std::enable_if_t<std::is_floating_point<T>::value, typeless_nan>
DACE_CONSTEXPR DACE_HDFI std::enable_if_t<std::is_floating_point<T>::value || std::is_integral<T>::value, typeless_nan>
operator+(const T&, const typeless_nan&) noexcept { return typeless_nan{}; }

template<typename T>
DACE_CONSTEXPR DACE_HDFI std::enable_if_t<std::is_floating_point<T>::value, typeless_nan>
DACE_CONSTEXPR DACE_HDFI std::enable_if_t<std::is_floating_point<T>::value || std::is_integral<T>::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{}; }


template<typename T>
DACE_CONSTEXPR DACE_HDFI std::enable_if_t<std::is_floating_point<T>::value, typeless_nan>
DACE_CONSTEXPR DACE_HDFI std::enable_if_t<std::is_floating_point<T>::value || std::is_integral<T>::value, typeless_nan>
operator-(const T&, const typeless_nan&) noexcept { return typeless_nan{}; }

template<typename T>
DACE_CONSTEXPR DACE_HDFI std::enable_if_t<std::is_floating_point<T>::value, typeless_nan>
DACE_CONSTEXPR DACE_HDFI std::enable_if_t<std::is_floating_point<T>::value || std::is_integral<T>::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{}; }


template<typename T>
DACE_CONSTEXPR DACE_HDFI std::enable_if_t<std::is_floating_point<T>::value, typeless_nan>
DACE_CONSTEXPR DACE_HDFI std::enable_if_t<std::is_floating_point<T>::value || std::is_integral<T>::value, typeless_nan>
operator/(const T&, const typeless_nan&) noexcept { return typeless_nan{}; }

template<typename T>
DACE_CONSTEXPR DACE_HDFI std::enable_if_t<std::is_floating_point<T>::value, typeless_nan>
DACE_CONSTEXPR DACE_HDFI std::enable_if_t<std::is_floating_point<T>::value || std::is_integral<T>::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{}; }


template<typename T>
DACE_CONSTEXPR DACE_HDFI std::enable_if_t<std::is_floating_point<T>::value, typeless_nan>
DACE_CONSTEXPR DACE_HDFI std::enable_if_t<std::is_floating_point<T>::value || std::is_integral<T>::value, typeless_nan>
operator%(const T&, const typeless_nan&) noexcept { return typeless_nan{}; }

template<typename T>
DACE_CONSTEXPR DACE_HDFI std::enable_if_t<std::is_floating_point<T>::value, typeless_nan>
DACE_CONSTEXPR DACE_HDFI std::enable_if_t<std::is_floating_point<T>::value || std::is_integral<T>::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<typename T1> DACE_CONSTEXPR DACE_HDFI dace::math::typeless_nan F (T1&&, dace::math::typeless_nan) noexcept { return ::dace::math::typeless_nan{}; }; \
template<typename T2> 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<typename T1> DACE_CONSTEXPR DACE_HDFI typeless_nan F (T1&&, dace::math::typeless_nan) noexcept { return typeless_nan{}; }; \
template<typename T2> 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
26 changes: 25 additions & 1 deletion tests/numpy/constants_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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()

0 comments on commit f8e2919

Please sign in to comment.