Skip to content

Commit

Permalink
kokkos#6805: fix formatting and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nmm0 committed Mar 6, 2024
1 parent 6fa3de9 commit 7960ee0
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 18 deletions.
9 changes: 5 additions & 4 deletions core/src/Kokkos_View.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1786,15 +1786,16 @@ class View : public ViewTraits<DataType, Properties...> {
}

template <class OtherAccessorType,
typename = std::enable_if_t<
std::is_assignable_v<typename traits::value_type*&,
typename OtherAccessorType::data_handle_type>>>
typename = std::enable_if_t<std::is_assignable_v<
typename traits::value_type*&,
typename OtherAccessorType::data_handle_type>>>
KOKKOS_INLINE_FUNCTION constexpr auto to_mdspan(
const OtherAccessorType& other_accessor) {
using mdspan_type =
typename Experimental::Impl::MDSpanViewTraits<traits>::mdspan_type;
using ret_mdspan_type =
mdspan<typename mdspan_type::element_type, typename mdspan_type::extents_type,
mdspan<typename mdspan_type::element_type,
typename mdspan_type::extents_type,
typename mdspan_type::layout_type, OtherAccessorType>;
return ret_mdspan_type{
data(),
Expand Down
130 changes: 116 additions & 14 deletions core/unit_test/view/TestMDSpanConversion.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,36 @@
#include <gtest/gtest.h>
#include <type_traits>

#define KOKKOS_IMPL_TEST_ACCESS

#include <Kokkos_Core.hpp>
#include "experimental/__p0009_bits/default_accessor.hpp"
#include "experimental/__p0009_bits/dynamic_extent.hpp"
#include "experimental/__p2642_bits/layout_padded_fwd.hpp"

#ifdef KOKKOS_ENABLE_IMPL_MDSPAN

template <class T, class ExecutionSpace>
struct TestViewMDSpanConversion {
using value_type = T;

struct test_accessor
{
using offset_policy = test_accessor;
using element_type = value_type;
using reference = element_type &;
using data_handle_type = element_type *;

constexpr test_accessor() noexcept = default;
constexpr reference access(data_handle_type p, std::size_t i) {
return p[i];
}
constexpr data_handle_type offset(data_handle_type p, std::size_t i) {
return p + i;
}
};

template <class MDSpanLayout, class KokkosLayout, class DataType,
class MDSpanExtents>
static void test_conversion_from_mdspan(Kokkos::View<DataType> ref,
class MDSpanExtents, class... RefViewProps>
static void test_conversion_from_mdspan(Kokkos::View<DataType, RefViewProps...> ref,
const MDSpanExtents &exts) {
using view_type = Kokkos::View<DataType, KokkosLayout,
Kokkos::MemoryTraits<Kokkos::Unmanaged>>;
Expand All @@ -46,13 +63,38 @@ struct TestViewMDSpanConversion {

ASSERT_EQ(test_view.data(), ref.data());
ASSERT_EQ(test_view.data(), mds.data_handle());
ASSERT_EQ(test_view.layout(), ref.layout());
for (std::size_t r = 0; r < mdspan_type::rank(); ++r) {
ASSERT_EQ(test_view.extent(r), ref.extent(r));
ASSERT_EQ(test_view.extent(r), exts.extent(r));
}
}

template <class MDSpanLayoutMapping, class ViewType>
static void test_conversion_to_mdspan(
const MDSpanLayoutMapping &ref_layout_mapping, ViewType v) {
using view_type = ViewType;
using natural_mdspan_type =
typename Kokkos::Experimental::Impl::MDSpanViewTraits<
typename view_type::traits>::mdspan_type;

natural_mdspan_type cvt = v;
ASSERT_EQ(cvt.data_handle(), v.data());
ASSERT_EQ(cvt.mapping(), ref_layout_mapping);
}

natural_mdspan_type cvt = test_view;
auto cvt2 = test_view.to_mdspan(Kokkos::default_accessor<typename view_type::value_type>{});
template <class MDSpanLayoutMapping, class ViewType, class AccessorType>
static void test_conversion_to_mdspan(
const MDSpanLayoutMapping &ref_layout_mapping, ViewType v,
const AccessorType &a) {
using view_type = ViewType;
using natural_mdspan_type =
typename Kokkos::Experimental::Impl::MDSpanViewTraits<
typename view_type::traits>::mdspan_type;

auto cvt = v.to_mdspan(a);
ASSERT_EQ(cvt.data_handle(), v.data());
ASSERT_EQ(cvt.mapping(), ref_layout_mapping);
}

static void run_test() {
Expand All @@ -67,42 +109,102 @@ struct TestViewMDSpanConversion {
value_type)>>::type,
Kokkos::LayoutRight>);

// LayoutLeft
test_conversion_from_mdspan<
Kokkos::Experimental::layout_left_padded<sizeof(value_type)>,
Kokkos::LayoutLeft>(
Kokkos::View<double *>("ref", 7),
Kokkos::View<value_type *, Kokkos::LayoutLeft>("ref", 7),
Kokkos::extents<std::size_t, Kokkos::dynamic_extent>(7));
test_conversion_from_mdspan<
Kokkos::Experimental::layout_left_padded<sizeof(value_type)>,
Kokkos::LayoutLeft>(Kokkos::View<double[7]>("ref"),
Kokkos::LayoutLeft>(Kokkos::View<value_type[7], Kokkos::LayoutLeft>("ref"),
Kokkos::extents<std::size_t, 7>());
test_conversion_from_mdspan<
Kokkos::Experimental::layout_left_padded<sizeof(value_type)>,
Kokkos::LayoutLeft>(
Kokkos::View<double[7]>("ref"),
Kokkos::View<value_type[7], Kokkos::LayoutLeft>("ref"),
Kokkos::extents<std::size_t, Kokkos::dynamic_extent>(7));
test_conversion_from_mdspan<
Kokkos::Experimental::layout_left_padded<sizeof(value_type)>,
Kokkos::LayoutLeft>(Kokkos::View<double *>("ref", 7),
Kokkos::LayoutLeft>(Kokkos::View<value_type *, Kokkos::LayoutLeft>("ref", 7),
Kokkos::extents<std::size_t, 7>());

test_conversion_from_mdspan<
Kokkos::Experimental::layout_left_padded<sizeof(value_type)>,
Kokkos::LayoutLeft>(Kokkos::View<double **>("ref", 7, 3),
Kokkos::LayoutLeft>(Kokkos::View<value_type **, Kokkos::LayoutLeft>("ref", 7, 3),
Kokkos::dextents<std::size_t, 2>(7, 3));
test_conversion_from_mdspan<
Kokkos::Experimental::layout_left_padded<sizeof(value_type)>,
Kokkos::LayoutLeft>(Kokkos::View<double[7][3]>("ref"),
Kokkos::LayoutLeft>(Kokkos::View<value_type[7][3], Kokkos::LayoutLeft>("ref"),
Kokkos::extents<std::size_t, 7, 3>());
test_conversion_from_mdspan<
Kokkos::Experimental::layout_left_padded<sizeof(value_type)>,
Kokkos::LayoutLeft>(Kokkos::View<double[7][3]>("ref"),
Kokkos::LayoutLeft>(Kokkos::View<value_type[7][3], Kokkos::LayoutLeft>("ref"),
Kokkos::extents<std::size_t, Kokkos::dynamic_extent,
Kokkos::dynamic_extent>(7, 3));
test_conversion_from_mdspan<
Kokkos::Experimental::layout_left_padded<sizeof(value_type)>,
Kokkos::LayoutLeft>(Kokkos::View<double **>("ref", 7, 3),
Kokkos::LayoutLeft>(Kokkos::View<value_type **, Kokkos::LayoutLeft>("ref", 7, 3),
Kokkos::extents<std::size_t, 7, 3>());

// LayoutRight
test_conversion_from_mdspan<
Kokkos::Experimental::layout_right_padded<sizeof(value_type)>,
Kokkos::LayoutRight>(
Kokkos::View<value_type *, Kokkos::LayoutRight>("ref", 7),
Kokkos::extents<std::size_t, Kokkos::dynamic_extent>(7));
test_conversion_from_mdspan<
Kokkos::Experimental::layout_right_padded<sizeof(value_type)>,
Kokkos::LayoutRight>(Kokkos::View<value_type[7], Kokkos::LayoutRight>("ref"),
Kokkos::extents<std::size_t, 7>());
test_conversion_from_mdspan<
Kokkos::Experimental::layout_right_padded<sizeof(value_type)>,
Kokkos::LayoutRight>(
Kokkos::View<value_type[7], Kokkos::LayoutRight>("ref"),
Kokkos::extents<std::size_t, Kokkos::dynamic_extent>(7));
test_conversion_from_mdspan<
Kokkos::Experimental::layout_right_padded<sizeof(value_type)>,
Kokkos::LayoutRight>(Kokkos::View<value_type *, Kokkos::LayoutRight>("ref", 7),
Kokkos::extents<std::size_t, 7>());

test_conversion_from_mdspan<
Kokkos::Experimental::layout_right_padded<sizeof(value_type)>,
Kokkos::LayoutRight>(Kokkos::View<value_type **, Kokkos::LayoutRight>("ref", 7, 3),
Kokkos::dextents<std::size_t, 2>(7, 3));
test_conversion_from_mdspan<
Kokkos::Experimental::layout_right_padded<sizeof(value_type)>,
Kokkos::LayoutRight>(Kokkos::View<value_type[7][3], Kokkos::LayoutRight>("ref"),
Kokkos::extents<std::size_t, 7, 3>());
test_conversion_from_mdspan<
Kokkos::Experimental::layout_right_padded<sizeof(value_type)>,
Kokkos::LayoutRight>(Kokkos::View<value_type[7][3], Kokkos::LayoutRight>("ref"),
Kokkos::extents<std::size_t, Kokkos::dynamic_extent,
Kokkos::dynamic_extent>(7, 3));
test_conversion_from_mdspan<
Kokkos::Experimental::layout_right_padded<sizeof(value_type)>,
Kokkos::LayoutRight>(Kokkos::View<value_type **, Kokkos::LayoutRight>("ref", 7, 3),
Kokkos::extents<std::size_t, 7, 3>());

// Conversion to mdspan
using layout_left_padded = Kokkos::Experimental::layout_left_padded<Kokkos::dynamic_extent>;
using layout_right_padded = Kokkos::Experimental::layout_right_padded<Kokkos::dynamic_extent>;
test_conversion_to_mdspan(layout_left_padded::mapping<Kokkos::extents<std::size_t, 4>>({}, 4), Kokkos::View<value_type *, Kokkos::LayoutLeft>("v", 4));
test_conversion_to_mdspan(layout_left_padded::mapping<Kokkos::extents<std::size_t, 4, 7>>({}, 4), Kokkos::View<value_type **, Kokkos::LayoutLeft>("v", 4, 7));

test_conversion_to_mdspan(layout_right_padded::mapping<Kokkos::extents<std::size_t, 4>>({}, 4), Kokkos::View<value_type *, Kokkos::LayoutRight>("v", 4));
test_conversion_to_mdspan(layout_right_padded::mapping<Kokkos::extents<std::size_t, 4, 7>>({}, 7), Kokkos::View<value_type **, Kokkos::LayoutRight>("v", 4, 7));

test_conversion_to_mdspan(layout_left_padded::mapping<Kokkos::extents<std::size_t, 4>>({}, 4), Kokkos::View<value_type *, Kokkos::LayoutLeft>("v", 4), Kokkos::default_accessor<value_type>{});
test_conversion_to_mdspan(layout_left_padded::mapping<Kokkos::extents<std::size_t, 4, 7>>({}, 4), Kokkos::View<value_type **, Kokkos::LayoutLeft>("v", 4, 7), Kokkos::default_accessor<value_type>{});

test_conversion_to_mdspan(layout_right_padded::mapping<Kokkos::extents<std::size_t, 4>>({}, 4), Kokkos::View<value_type *, Kokkos::LayoutRight>("v", 4), Kokkos::default_accessor<value_type>{});
test_conversion_to_mdspan(layout_right_padded::mapping<Kokkos::extents<std::size_t, 4, 7>>({}, 7), Kokkos::View<value_type **, Kokkos::LayoutRight>("v", 4, 7), Kokkos::default_accessor<value_type>{});

test_conversion_to_mdspan(layout_left_padded::mapping<Kokkos::extents<std::size_t, 4>>({}, 4), Kokkos::View<value_type *, Kokkos::LayoutLeft>("v", 4), test_accessor{});
test_conversion_to_mdspan(layout_left_padded::mapping<Kokkos::extents<std::size_t, 4, 7>>({}, 4), Kokkos::View<value_type **, Kokkos::LayoutLeft>("v", 4, 7), test_accessor{});

test_conversion_to_mdspan(layout_right_padded::mapping<Kokkos::extents<std::size_t, 4>>({}, 4), Kokkos::View<value_type *, Kokkos::LayoutRight>("v", 4), test_accessor{});
test_conversion_to_mdspan(layout_right_padded::mapping<Kokkos::extents<std::size_t, 4, 7>>({}, 7), Kokkos::View<value_type **, Kokkos::LayoutRight>("v", 4, 7), test_accessor{});
}
};

Expand Down

0 comments on commit 7960ee0

Please sign in to comment.