Skip to content

Commit

Permalink
Make submdspan accept Kokkos::pair
Browse files Browse the repository at this point in the history
  • Loading branch information
crtrott committed Jul 10, 2024
1 parent 58ea2f3 commit 8fc1b8d
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 4 deletions.
32 changes: 32 additions & 0 deletions core/src/View/MDSpan/Kokkos_MDSpan_Header.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,38 @@ using std::layout_stride;
using std::mdspan;
} // namespace Kokkos
#else

// Opt in for Kokkos::pair to submdspan/subview
// submdspan does only take index_pair_like which is derived from tuple_like
// tuple_like is an enumerated list: tuple, pair, array, complex, ranges::subrange

#include <Kokkos_Pair.hpp>

namespace Kokkos {
namespace detail {
template<class IdxT1, class IdxT2>
constexpr auto first_of(const pair<IdxT1, IdxT2> &slice) {
return slice.first;
}
template<class IdxT1, class IdxT2, class Extents, size_t k>
KOKKOS_INLINE_FUNCTION
constexpr auto last_of(std::integral_constant<size_t, k>, const Extents &,
const pair<IdxT1, IdxT2> &slice) {
return slice.second;
}

template<class T, class IndexType>
struct index_pair_like;

template <class IdxT1, class IdxT2, class IndexType>
struct index_pair_like<Kokkos::pair<IdxT1, IdxT2>, IndexType> {
static constexpr bool value = std::is_convertible_v<IdxT1, IndexType> &&
std::is_convertible_v<IdxT2, IndexType>;
};

}
}

#include <mdspan/mdspan.hpp>
#endif

Expand Down
7 changes: 7 additions & 0 deletions core/unit_test/default/TestDefaultDeviceDevelop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@

#include <gtest/gtest.h>

#include <Kokkos_Pair.hpp>

#include <Kokkos_Core.hpp>

#include <TestDefaultDeviceType_Category.hpp>


namespace Test {

TEST(defaultdevicetype, development_test) {
Expand All @@ -31,5 +34,9 @@ TEST(defaultdevicetype, development_test) {
auto prop = Kokkos::view_alloc("C");
Kokkos::View<float*, Kokkos::LayoutRight> c(prop, Kokkos::LayoutRight(5));
Kokkos::View<int*> b_um(b.data(), 5);
Kokkos::mdspan<int, Kokkos::dextents<int, 1>> mds(b.data(), 5);
auto sub_a = Kokkos::submdspan(mds, std::pair{1,3});
auto sub_b = Kokkos::submdspan(mds, std::array{1,3});
auto sub_c = Kokkos::submdspan(mds, Kokkos::pair{1,3});
}
} // namespace Test
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#pragma once

#include <tuple>
#include <complex>

#include "strided_slice.hpp"
namespace MDSPAN_IMPL_STANDARD_NAMESPACE {
Expand Down Expand Up @@ -52,6 +53,33 @@ template <class OffsetType, class ExtentType, class StrideType>
struct is_strided_slice<
strided_slice<OffsetType, ExtentType, StrideType>> : std::true_type {};

// Helper for identifying valid pair like things
template <class T, class IndexType> struct index_pair_like : std::false_type {};

template <class IdxT1, class IdxT2, class IndexType>
struct index_pair_like<std::pair<IdxT1, IdxT2>, IndexType> {
static constexpr bool value = std::is_convertible_v<IdxT1, IndexType> &&
std::is_convertible_v<IdxT2, IndexType>;
};

template <class IdxT1, class IdxT2, class IndexType>
struct index_pair_like<std::tuple<IdxT1, IdxT2>, IndexType> {
static constexpr bool value = std::is_convertible_v<IdxT1, IndexType> &&
std::is_convertible_v<IdxT2, IndexType>;
};

template <class IdxT, class IndexType>
struct index_pair_like<std::complex<IdxT>, IndexType> {
static constexpr bool value = std::is_convertible_v<IdxT, IndexType>;
};

template <class IdxT, class IndexType>
struct index_pair_like<std::array<IdxT, 2>, IndexType> {
static constexpr bool value = std::is_convertible_v<IdxT, IndexType>;
};

// FIXME: we actually need to pass IndexType into all of these

// first_of(slice): getting begin of slice specifier range
MDSPAN_TEMPLATE_REQUIRES(
class Integral,
Expand All @@ -70,7 +98,7 @@ first_of(const ::MDSPAN_IMPL_STANDARD_NAMESPACE::full_extent_t &) {

MDSPAN_TEMPLATE_REQUIRES(
class Slice,
/* requires */(std::is_convertible_v<Slice, std::tuple<size_t, size_t>>)
/* requires */(index_pair_like<Slice, size_t>::value)
)
MDSPAN_INLINE_FUNCTION
constexpr auto first_of(const Slice &i) {
Expand Down Expand Up @@ -100,7 +128,7 @@ constexpr Integral

MDSPAN_TEMPLATE_REQUIRES(
size_t k, class Extents, class Slice,
/* requires */(std::is_convertible_v<Slice, std::tuple<size_t, size_t>>)
/* requires */(index_pair_like<Slice, size_t>::value)
)
MDSPAN_INLINE_FUNCTION
constexpr auto last_of(std::integral_constant<size_t, k>, const Extents &,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ template<class SliceSpecifier, class IndexType>
struct is_range_slice {
constexpr static bool value =
std::is_same_v<SliceSpecifier, full_extent_t> ||
std::is_convertible_v<SliceSpecifier,
std::tuple<IndexType, IndexType>>;
index_pair_like<SliceSpecifier, IndexType>::value;
};

template<class SliceSpecifier, class IndexType>
Expand Down

0 comments on commit 8fc1b8d

Please sign in to comment.