Skip to content

Commit

Permalink
Adding Kokkos::to_array (#6375)
Browse files Browse the repository at this point in the history
* Added Kokkos::to_Array for Kokkos::Array (analogous to std::to_array for
std::array)

* Changed KOKKOS_INLINE_FUNCTION into KOKKOS_FUNCTION (for inline template
functions)

* Added back to_Array tests

* Fixed surperfluous KOKKOS_INLINE_FUNCTION

* Fix for "set but not used" error for variables in a static_assert under
nvcc

* Guard against Kokkos::to_Array<long>({0, 1, 3}), as that is not
supported under gcc8

* Renamed to_Array to to_array and to_Array_impl to to_array_impl
  • Loading branch information
nliber committed May 24, 2024
1 parent 0410363 commit cf791bc
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
26 changes: 26 additions & 0 deletions core/src/Kokkos_Array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,32 @@ struct KOKKOS_DEPRECATED
template <typename T, typename... Us>
Array(T, Us...)->Array<T, 1 + sizeof...(Us)>;

namespace Impl {

template <typename T, size_t N, size_t... I>
KOKKOS_FUNCTION constexpr Array<std::remove_cv_t<T>, N> to_array_impl(
T (&a)[N], std::index_sequence<I...>) {
return {{a[I]...}};
}

template <typename T, size_t N, size_t... I>
KOKKOS_FUNCTION constexpr Array<std::remove_cv_t<T>, N> to_array_impl(
T(&&a)[N], std::index_sequence<I...>) {
return {{std::move(a[I])...}};
}

} // namespace Impl

template <typename T, size_t N>
KOKKOS_FUNCTION constexpr auto to_array(T (&a)[N]) {
return Impl::to_array_impl(a, std::make_index_sequence<N>{});
}

template <typename T, size_t N>
KOKKOS_FUNCTION constexpr auto to_array(T(&&a)[N]) {
return Impl::to_array_impl(std::move(a), std::make_index_sequence<N>{});
}

} // namespace Kokkos

//<editor-fold desc="Support for structured binding">
Expand Down
30 changes: 30 additions & 0 deletions core/unit_test/TestArray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@

namespace {

// nvcc errors on variables only used in static_asserts
// Passing those variables to this function should eliminate the warning
template <typename... Ts>
KOKKOS_FUNCTION constexpr void maybe_unused(Ts&&...) {}

KOKKOS_FUNCTION constexpr bool test_array() {
constexpr Kokkos::Array<int, 3> a{{1, 2}};

Expand Down Expand Up @@ -160,4 +165,29 @@ constexpr bool test_array_specialization_kokkos_swap() {

static_assert(test_array_specialization_kokkos_swap());

constexpr bool test_to_array() {
// copies a string literal
[[maybe_unused]] auto a1 = Kokkos::to_array("foo");
static_assert(a1.size() == 4);
maybe_unused(a1);

// deduces both element type and length
[[maybe_unused]] auto a2 = Kokkos::to_array({0, 2, 1, 3});
static_assert(std::is_same_v<decltype(a2), Kokkos::Array<int, 4>>);
maybe_unused(a2);

// gcc8 doesn't support the implicit conversion
#if !defined(KOKKOS_COMPILER_GNU) || (KOKKOS_COMPILER_GNU >= 910)
// deduces length with element type specified
// implicit conversion happens
[[maybe_unused]] auto a3 = Kokkos::to_array<long>({0, 1, 3});
static_assert(std::is_same_v<decltype(a3), Kokkos::Array<long, 3>>);
maybe_unused(a3);
#endif

return true;
}

static_assert(test_to_array());

} // namespace

0 comments on commit cf791bc

Please sign in to comment.