From 0d872ef819a33c04da1164b85422e3144a34ffdc Mon Sep 17 00:00:00 2001 From: Carl Pearson Date: Mon, 13 May 2024 10:55:22 -0600 Subject: [PATCH] Kokkos::nan() -> KokkosKernels::Impl::quiet_NaN_v --- common/impl/KokkosKernels_NaN.hpp | 43 +++++++++++++++++++++++ sparse/unit_test/Test_Sparse_spmv.hpp | 5 +-- sparse/unit_test/Test_Sparse_spmv_bsr.hpp | 8 ++--- 3 files changed, 50 insertions(+), 6 deletions(-) create mode 100644 common/impl/KokkosKernels_NaN.hpp diff --git a/common/impl/KokkosKernels_NaN.hpp b/common/impl/KokkosKernels_NaN.hpp new file mode 100644 index 0000000000..75d6a3ac8c --- /dev/null +++ b/common/impl/KokkosKernels_NaN.hpp @@ -0,0 +1,43 @@ +//@HEADER +// ************************************************************************ +// +// Kokkos v. 4.0 +// Copyright (2022) National Technology & Engineering +// Solutions of Sandia, LLC (NTESS). +// +// Under the terms of Contract DE-NA0003525 with NTESS, +// the U.S. Government retains certain rights in this software. +// +// Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions. +// See https://kokkos.org/LICENSE for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//@HEADER + +#ifndef KOKKOSKERNELS_NAN_HPP +#define KOKKOSKERNELS_NAN_HPP + +#include +#include + +namespace KokkosKernels::Impl { + +// This could be constexpr if Kokkos::complex ctor was +template +KOKKOS_INLINE_FUNCTION T quiet_NaN() { + if constexpr (std::is_same_v) { + return double(Kokkos::Experimental::quiet_NaN_v); // Kokkos::Experimetnal::quiet_NaN_v + // is undefined in + // device code + } else if constexpr (Kokkos::ArithTraits::is_complex) { + using value_type = typename T::value_type; + return T(quiet_NaN(), + quiet_NaN()); // Kokkos::complex ctor is not constexpr + } else { + return Kokkos::Experimental::quiet_NaN_v; + } +} + +} // namespace KokkosKernels::Impl + +#endif // KOKKOSKERNELS_NAN_HPP diff --git a/sparse/unit_test/Test_Sparse_spmv.hpp b/sparse/unit_test/Test_Sparse_spmv.hpp index 019e32a71f..2057a8ba14 100644 --- a/sparse/unit_test/Test_Sparse_spmv.hpp +++ b/sparse/unit_test/Test_Sparse_spmv.hpp @@ -23,6 +23,7 @@ #include #include #include +#include #include "KokkosKernels_default_types.hpp" @@ -451,13 +452,13 @@ void test_spmv(KokkosSparse::SPMVAlgorithm algo, lno_t numRows, size_type nnz, Kokkos::parallel_for( y_policy(0, input_y_nans.extent(0)), KOKKOS_LAMBDA(const size_t i) { if (0 == (i % 19)) { - input_y_nans(i) = NAN; + input_y_nans(i) = KokkosKernels::Impl::quiet_NaN(); } }); Kokkos::parallel_for( y_policy(0, input_yt_nans.extent(0)), KOKKOS_LAMBDA(const size_t i) { if (0 == (i % 23)) { - input_yt_nans(i) = Kokkos::nan(""); + input_yt_nans(i) = KokkosKernels::Impl::quiet_NaN(); } }); diff --git a/sparse/unit_test/Test_Sparse_spmv_bsr.hpp b/sparse/unit_test/Test_Sparse_spmv_bsr.hpp index 273d6f4636..7338e264c8 100644 --- a/sparse/unit_test/Test_Sparse_spmv_bsr.hpp +++ b/sparse/unit_test/Test_Sparse_spmv_bsr.hpp @@ -353,13 +353,13 @@ auto random_vecs_for_spmv(const char *mode, const Bsr &a, Kokkos::parallel_for( policy_type(0, x.extent(0)), KOKKOS_LAMBDA(size_t i) { if (0 == (i % 17)) { - x(i) = Kokkos::nan(""); + x(i) = KokkosKernels::Impl::quiet_NaN(); } }); Kokkos::parallel_for( policy_type(0, y.extent(0)), KOKKOS_LAMBDA(size_t i) { if (0 == (i % 17)) { - y(i) = Kokkos::nan(""); + y(i) = KokkosKernels::Impl::quiet_NaN(); } }); } @@ -615,7 +615,7 @@ auto random_multivecs_for_spm_mv(const char *mode, const Bsr &a, policy_type(0, x.extent(0)), KOKKOS_LAMBDA(size_t i) { for (size_t j = 0; j < x.extent(1); ++j) { if (0 == ((i * x.extent(1) + j) % 13)) { - x(i, j) = Kokkos::nan(""); + x(i, j) = KokkosKernels::Impl::quiet_NaN(); } } }); @@ -623,7 +623,7 @@ auto random_multivecs_for_spm_mv(const char *mode, const Bsr &a, policy_type(0, y.extent(0)), KOKKOS_LAMBDA(size_t i) { for (size_t j = 0; j < y.extent(1); ++j) { if (0 == ((i * y.extent(1) + j) % 17)) { - y(i, j) = Kokkos::nan(""); + y(i, j) = KokkosKernels::Impl::quiet_NaN(); } } });