Skip to content

Commit

Permalink
Adding UnitTests for more degenerated subviews
Browse files Browse the repository at this point in the history
Exposing issue kokkos#2979
  • Loading branch information
crtrott committed Apr 27, 2020
1 parent 785d19f commit fd586b6
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 56 deletions.
26 changes: 15 additions & 11 deletions core/src/impl/Kokkos_ViewMapping.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1286,8 +1286,8 @@ struct ViewOffset<
/* Span of the range space */
KOKKOS_INLINE_FUNCTION
constexpr size_type span() const {
return m_stride * m_dim.N1 * m_dim.N2 * m_dim.N3 * m_dim.N4 * m_dim.N5 *
m_dim.N6 * m_dim.N7;
return (m_dim.N0 > size_type(0) ? m_stride : size_type(0)) * m_dim.N1 *
m_dim.N2 * m_dim.N3 * m_dim.N4 * m_dim.N5 * m_dim.N6 * m_dim.N7;
}

KOKKOS_INLINE_FUNCTION constexpr bool span_is_contiguous() const {
Expand Down Expand Up @@ -1882,7 +1882,9 @@ struct ViewOffset<

/* Span of the range space */
KOKKOS_INLINE_FUNCTION
constexpr size_type span() const { return m_dim.N0 * m_stride; }
constexpr size_type span() const {
return size() > 0 ? m_dim.N0 * m_stride : 0;
}

KOKKOS_INLINE_FUNCTION constexpr bool span_is_contiguous() const {
return m_stride == m_dim.N7 * m_dim.N6 * m_dim.N5 * m_dim.N4 * m_dim.N3 *
Expand Down Expand Up @@ -2398,14 +2400,16 @@ struct ViewOffset<Dimension, Kokkos::LayoutStride, void> {
/* Span of the range space, largest stride * dimension */
KOKKOS_INLINE_FUNCTION
constexpr size_type span() const {
return Max(m_dim.N0 * m_stride.S0,
Max(m_dim.N1 * m_stride.S1,
Max(m_dim.N2 * m_stride.S2,
Max(m_dim.N3 * m_stride.S3,
Max(m_dim.N4 * m_stride.S4,
Max(m_dim.N5 * m_stride.S5,
Max(m_dim.N6 * m_stride.S6,
m_dim.N7 * m_stride.S7)))))));
return size() == size_type(0)
? size_type(0)
: Max(m_dim.N0 * m_stride.S0,
Max(m_dim.N1 * m_stride.S1,
Max(m_dim.N2 * m_stride.S2,
Max(m_dim.N3 * m_stride.S3,
Max(m_dim.N4 * m_stride.S4,
Max(m_dim.N5 * m_stride.S5,
Max(m_dim.N6 * m_stride.S6,
m_dim.N7 * m_stride.S7)))))));
}

KOKKOS_INLINE_FUNCTION constexpr bool span_is_contiguous() const {
Expand Down
160 changes: 115 additions & 45 deletions core/unit_test/TestViewSubview.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,17 @@ void test_1d_strided_assignment() {
Space>(true, true, true, true, 17, 1);
}

template<class NewView, class OrigView, class ... Args>
void make_subview(bool use_constructor, NewView& v, OrigView org, Args ... args) {
if(use_constructor) {
v = NewView(org,args...);
} else {
v = Kokkos::subview(org,args...);
}
}

template <class Space>
void test_left_0() {
void test_left_0(bool constr = true) {
typedef Kokkos::View<int[2][3][4][5][2][3][4][5], Kokkos::LayoutLeft, Space>
view_static_8_type;

Expand All @@ -322,21 +331,41 @@ void test_left_0() {

ASSERT_TRUE(x_static_8.span_is_contiguous());

Kokkos::View<int, Kokkos::LayoutLeft, Space> x0 =
Kokkos::subview(x_static_8, 0, 0, 0, 0, 0, 0, 0, 0);
Kokkos::View<int, Kokkos::LayoutLeft, Space> x0;
make_subview(constr, x0, x_static_8, 0, 0, 0, 0, 0, 0, 0, 0);

ASSERT_TRUE(x0.span_is_contiguous());
ASSERT_EQ(x0.span(),1);
ASSERT_TRUE(&x0() == &x_static_8(0, 0, 0, 0, 0, 0, 0, 0));

Kokkos::View<int*, Kokkos::LayoutLeft, Space> x1 = Kokkos::subview(
Kokkos::View<int*, Kokkos::LayoutLeft, Space> x1;
make_subview(constr, x1,
x_static_8, Kokkos::pair<int, int>(0, 2), 1, 2, 3, 0, 1, 2, 3);

ASSERT_TRUE(x1.span_is_contiguous());
ASSERT_EQ(x1.span(),2);
ASSERT_TRUE(&x1(0) == &x_static_8(0, 1, 2, 3, 0, 1, 2, 3));
ASSERT_TRUE(&x1(1) == &x_static_8(1, 1, 2, 3, 0, 1, 2, 3));

Kokkos::View<int**, Kokkos::LayoutLeft, Space> x2 =
Kokkos::subview(x_static_8, Kokkos::pair<int, int>(0, 2), 1, 2, 3,

Kokkos::View<int*, Kokkos::LayoutLeft, Space> x_deg1;
make_subview(constr, x_deg1,
x_static_8, Kokkos::pair<int, int>(0, 0), 1, 2, 3, 0, 1, 2, 3);

ASSERT_TRUE(x_deg1.span_is_contiguous());
ASSERT_EQ(x_deg1.span(),0);
ASSERT_EQ(x_deg1.data(),&x_static_8(0,1,2,3,0,1,2,3));

Kokkos::View<int*, Kokkos::LayoutLeft, Space> x_deg2;
make_subview(constr, x_deg2,
x_static_8, Kokkos::pair<int, int>(2, 2), 2, 3, 4, 1, 2, 3, 4);

ASSERT_TRUE(x_deg2.span_is_contiguous());
ASSERT_EQ(x_deg2.span(),0);
ASSERT_EQ(x_deg2.data(),x_static_8.data()+x_static_8.span());

Kokkos::View<int**, Kokkos::LayoutLeft, Space> x2;
make_subview(constr, x2, x_static_8,
Kokkos::pair<int, int>(0, 2), 1, 2, 3,
Kokkos::pair<int, int>(0, 2), 1, 2, 3);

ASSERT_TRUE(!x2.span_is_contiguous());
Expand All @@ -346,8 +375,9 @@ void test_left_0() {
ASSERT_TRUE(&x2(1, 1) == &x_static_8(1, 1, 2, 3, 1, 1, 2, 3));

// Kokkos::View< int**, Kokkos::LayoutLeft, Space > error_2 =
Kokkos::View<int**, Kokkos::LayoutStride, Space> sx2 =
Kokkos::subview(x_static_8, 1, Kokkos::pair<int, int>(0, 2), 2, 3,
Kokkos::View<int**, Kokkos::LayoutStride, Space> sx2;
make_subview(constr, sx2, x_static_8,
1, Kokkos::pair<int, int>(0, 2), 2, 3,
Kokkos::pair<int, int>(0, 2), 1, 2, 3);

ASSERT_TRUE(!sx2.span_is_contiguous());
Expand All @@ -356,8 +386,9 @@ void test_left_0() {
ASSERT_TRUE(&sx2(0, 1) == &x_static_8(1, 0, 2, 3, 1, 1, 2, 3));
ASSERT_TRUE(&sx2(1, 1) == &x_static_8(1, 1, 2, 3, 1, 1, 2, 3));

Kokkos::View<int****, Kokkos::LayoutStride, Space> sx4 =
Kokkos::subview(x_static_8, 0, Kokkos::pair<int, int>(0, 2) /* of [3] */
Kokkos::View<int****, Kokkos::LayoutStride, Space> sx4;
make_subview(constr, sx4, x_static_8,
0, Kokkos::pair<int, int>(0, 2) /* of [3] */
,
1, Kokkos::pair<int, int>(1, 3) /* of [5] */
,
Expand All @@ -380,7 +411,7 @@ void test_left_0() {
}

template <class Space>
void test_left_1() {
void test_left_1(bool use_constr = true) {
typedef Kokkos::View<int*** * [2][3][4][5], Kokkos::LayoutLeft, Space>
view_type;

Expand All @@ -390,42 +421,67 @@ void test_left_1() {

ASSERT_TRUE(x8.span_is_contiguous());

Kokkos::View<int, Kokkos::LayoutLeft, Space> x0 =
Kokkos::subview(x8, 0, 0, 0, 0, 0, 0, 0, 0);
Kokkos::View<int, Kokkos::LayoutLeft, Space> x0;
make_subview(use_constr,x0, x8, 0, 0, 0, 0, 0, 0, 0, 0);

ASSERT_TRUE(x0.span_is_contiguous());
ASSERT_TRUE(&x0() == &x8(0, 0, 0, 0, 0, 0, 0, 0));

Kokkos::View<int*, Kokkos::LayoutLeft, Space> x1 =
Kokkos::subview(x8, Kokkos::pair<int, int>(0, 2), 1, 2, 3, 0, 1, 2, 3);
Kokkos::View<int*, Kokkos::LayoutLeft, Space> x1;
make_subview(use_constr,x1, x8, Kokkos::pair<int, int>(0, 2), 1, 2, 3, 0, 1, 2, 3);

ASSERT_TRUE(x1.span_is_contiguous());
ASSERT_TRUE(&x1(0) == &x8(0, 1, 2, 3, 0, 1, 2, 3));
ASSERT_TRUE(&x1(1) == &x8(1, 1, 2, 3, 0, 1, 2, 3));

Kokkos::View<int**, Kokkos::LayoutLeft, Space> x2 =
Kokkos::subview(x8, Kokkos::pair<int, int>(0, 2), 1, 2, 3,
Kokkos::View<int*, Kokkos::LayoutLeft, Space> x1_deg1;
make_subview(use_constr,x1_deg1, x8, Kokkos::pair<int, int>(0, 0), 1, 2, 3, 0, 1, 2, 3);

ASSERT_TRUE(x1_deg1.span_is_contiguous());
ASSERT_EQ(0,x1_deg1.span());
ASSERT_EQ(x1_deg1.data(),&x8(0,1,2,3,0,1,2,3));

Kokkos::View<int*, Kokkos::LayoutLeft, Space> x1_deg2;
make_subview(use_constr,x1_deg2,x8, Kokkos::pair<int, int>(2, 2), 2, 3, 4, 1, 2, 3, 4);

ASSERT_EQ(0,x1_deg2.span());
ASSERT_TRUE(x1_deg2.span_is_contiguous());
ASSERT_EQ(x1_deg2.data(),x8.data()+x8.span());

Kokkos::View<int**, Kokkos::LayoutLeft, Space> x2;
make_subview(use_constr,x2,x8, Kokkos::pair<int, int>(0, 2), 1, 2, 3,
Kokkos::pair<int, int>(0, 2), 1, 2, 3);

ASSERT_TRUE(!x2.span_is_contiguous());
ASSERT_TRUE(&x2(0, 0) == &x8(0, 1, 2, 3, 0, 1, 2, 3));
ASSERT_TRUE(&x2(1, 0) == &x8(1, 1, 2, 3, 0, 1, 2, 3));
ASSERT_TRUE(&x2(0, 1) == &x8(0, 1, 2, 3, 1, 1, 2, 3));
ASSERT_TRUE(&x2(1, 1) == &x8(1, 1, 2, 3, 1, 1, 2, 3));

Kokkos::View<int**, Kokkos::LayoutLeft, Space> x2_deg2;
make_subview(use_constr,x2_deg2,x8, Kokkos::pair<int, int>(2, 2), 2, 3, 4, 1, 2, Kokkos::pair<int,int>(2,3), 4);
ASSERT_EQ(0,x2_deg2.span());
ASSERT_EQ(x2_deg2.data(),x8.data()+x8.span());

// Kokkos::View< int**, Kokkos::LayoutLeft, Space > error_2 =
Kokkos::View<int**, Kokkos::LayoutStride, Space> sx2 =
Kokkos::subview(x8, 1, Kokkos::pair<int, int>(0, 2), 2, 3,
Kokkos::View<int**, Kokkos::LayoutStride, Space> sx2;
make_subview(use_constr,sx2,x8, 1, Kokkos::pair<int, int>(0, 2), 2, 3,
Kokkos::pair<int, int>(0, 2), 1, 2, 3);

ASSERT_TRUE(!sx2.span_is_contiguous());
ASSERT_TRUE(&sx2(0, 0) == &x8(1, 0, 2, 3, 0, 1, 2, 3));
ASSERT_TRUE(&sx2(1, 0) == &x8(1, 1, 2, 3, 0, 1, 2, 3));
ASSERT_TRUE(&sx2(0, 1) == &x8(1, 0, 2, 3, 1, 1, 2, 3));
ASSERT_TRUE(&sx2(1, 1) == &x8(1, 1, 2, 3, 1, 1, 2, 3));

Kokkos::View<int**, Kokkos::LayoutStride, Space> sx2_deg;
make_subview(use_constr,sx2,x8, 1, Kokkos::pair<int, int>(0, 0), 2, 3,
Kokkos::pair<int, int>(0, 2), 1, 2, 3);
ASSERT_EQ(0,sx2_deg.span());
ASSERT_EQ(&x8(1,0,2,3,0,1,2,3),sx2_deg.data());

Kokkos::View<int****, Kokkos::LayoutStride, Space> sx4 =
Kokkos::subview(x8, 0, Kokkos::pair<int, int>(0, 2) /* of [3] */
Kokkos::View<int****, Kokkos::LayoutStride, Space> sx4;
make_subview(use_constr,sx4,x8, 0, Kokkos::pair<int, int>(0, 2) /* of [3] */
,
1, Kokkos::pair<int, int>(1, 3) /* of [5] */
,
Expand Down Expand Up @@ -570,28 +626,28 @@ void test_left_3() {
//----------------------------------------------------------------------------

template <class Space>
void test_right_0() {
void test_right_0(bool use_constr = true) {
typedef Kokkos::View<int[2][3][4][5][2][3][4][5], Kokkos::LayoutRight, Space>
view_static_8_type;

if (Kokkos::Impl::SpaceAccessibility<
Kokkos::HostSpace, typename Space::memory_space>::accessible) {
view_static_8_type x_static_8("x_static_right_8");

Kokkos::View<int, Kokkos::LayoutRight, Space> x0 =
Kokkos::subview(x_static_8, 0, 0, 0, 0, 0, 0, 0, 0);
Kokkos::View<int, Kokkos::LayoutRight, Space> x0;
make_subview(use_constr,x0,x_static_8, 0, 0, 0, 0, 0, 0, 0, 0);

ASSERT_TRUE(&x0() == &x_static_8(0, 0, 0, 0, 0, 0, 0, 0));

Kokkos::View<int*, Kokkos::LayoutRight, Space> x1 = Kokkos::subview(
x_static_8, 0, 1, 2, 3, 0, 1, 2, Kokkos::pair<int, int>(1, 3));
Kokkos::View<int*, Kokkos::LayoutRight, Space> x1;
make_subview(use_constr,x1,x_static_8, 0, 1, 2, 3, 0, 1, 2, Kokkos::pair<int, int>(1, 3));

ASSERT_TRUE(x1.extent(0) == 2);
ASSERT_TRUE(&x1(0) == &x_static_8(0, 1, 2, 3, 0, 1, 2, 1));
ASSERT_TRUE(&x1(1) == &x_static_8(0, 1, 2, 3, 0, 1, 2, 2));

Kokkos::View<int**, Kokkos::LayoutRight, Space> x2 =
Kokkos::subview(x_static_8, 0, 1, 2, Kokkos::pair<int, int>(1, 3), 0, 1,
Kokkos::View<int**, Kokkos::LayoutRight, Space> x2;
make_subview(use_constr,x2,x_static_8, 0, 1, 2, Kokkos::pair<int, int>(1, 3), 0, 1,
2, Kokkos::pair<int, int>(1, 3));

ASSERT_TRUE(x2.extent(0) == 2);
Expand All @@ -602,8 +658,8 @@ void test_right_0() {
ASSERT_TRUE(&x2(1, 1) == &x_static_8(0, 1, 2, 2, 0, 1, 2, 2));

// Kokkos::View< int**, Kokkos::LayoutRight, Space > error_2 =
Kokkos::View<int**, Kokkos::LayoutStride, Space> sx2 =
Kokkos::subview(x_static_8, 1, Kokkos::pair<int, int>(0, 2), 2, 3,
Kokkos::View<int**, Kokkos::LayoutStride, Space> sx2;
make_subview(use_constr,sx2,x_static_8, 1, Kokkos::pair<int, int>(0, 2), 2, 3,
Kokkos::pair<int, int>(0, 2), 1, 2, 3);

ASSERT_TRUE(sx2.extent(0) == 2);
Expand All @@ -613,8 +669,8 @@ void test_right_0() {
ASSERT_TRUE(&sx2(0, 1) == &x_static_8(1, 0, 2, 3, 1, 1, 2, 3));
ASSERT_TRUE(&sx2(1, 1) == &x_static_8(1, 1, 2, 3, 1, 1, 2, 3));

Kokkos::View<int****, Kokkos::LayoutStride, Space> sx4 =
Kokkos::subview(x_static_8, 0, Kokkos::pair<int, int>(0, 2) /* of [3] */
Kokkos::View<int****, Kokkos::LayoutStride, Space> sx4;
make_subview(use_constr,sx4,x_static_8, 0, Kokkos::pair<int, int>(0, 2) /* of [3] */
,
1, Kokkos::pair<int, int>(1, 3) /* of [5] */
,
Expand All @@ -639,46 +695,60 @@ void test_right_0() {
}

template <class Space>
void test_right_1() {
void test_right_1(bool use_constr = true) {
typedef Kokkos::View<int*** * [2][3][4][5], Kokkos::LayoutRight, Space>
view_type;

if (Kokkos::Impl::SpaceAccessibility<
Kokkos::HostSpace, typename Space::memory_space>::accessible) {
view_type x8("x_right_8", 2, 3, 4, 5);

Kokkos::View<int, Kokkos::LayoutRight, Space> x0 =
Kokkos::subview(x8, 0, 0, 0, 0, 0, 0, 0, 0);
Kokkos::View<int, Kokkos::LayoutRight, Space> x0;
make_subview(use_constr, x0, x8, 0, 0, 0, 0, 0, 0, 0, 0);

ASSERT_TRUE(&x0() == &x8(0, 0, 0, 0, 0, 0, 0, 0));

Kokkos::View<int*, Kokkos::LayoutRight, Space> x1 =
Kokkos::subview(x8, 0, 1, 2, 3, 0, 1, 2, Kokkos::pair<int, int>(1, 3));
Kokkos::View<int*, Kokkos::LayoutRight, Space> x1;
make_subview(use_constr, x1, x8, 0, 1, 2, 3, 0, 1, 2, Kokkos::pair<int, int>(1, 3));

ASSERT_TRUE(&x1(0) == &x8(0, 1, 2, 3, 0, 1, 2, 1));
ASSERT_TRUE(&x1(1) == &x8(0, 1, 2, 3, 0, 1, 2, 2));

Kokkos::View<int*, Kokkos::LayoutRight, Space> x1_deg1;
make_subview(use_constr, x1_deg1, x8, 0, 1, 2, 3, 0, 1, 2, Kokkos::pair<int, int>(3, 3));
ASSERT_EQ(0,x1_deg1.span());

Kokkos::View<int**, Kokkos::LayoutRight, Space> x2 =
Kokkos::subview(x8, 0, 1, 2, Kokkos::pair<int, int>(1, 3), 0, 1, 2,
Kokkos::View<int**, Kokkos::LayoutRight, Space> x2;
make_subview(use_constr, x2, x8, 0, 1, 2, Kokkos::pair<int, int>(1, 3), 0, 1, 2,
Kokkos::pair<int, int>(1, 3));

ASSERT_TRUE(&x2(0, 0) == &x8(0, 1, 2, 1, 0, 1, 2, 1));
ASSERT_TRUE(&x2(1, 0) == &x8(0, 1, 2, 2, 0, 1, 2, 1));
ASSERT_TRUE(&x2(0, 1) == &x8(0, 1, 2, 1, 0, 1, 2, 2));
ASSERT_TRUE(&x2(1, 1) == &x8(0, 1, 2, 2, 0, 1, 2, 2));

Kokkos::View<int**, Kokkos::LayoutRight, Space> x2_deg2;
make_subview(use_constr, x2_deg2, x8, 0, 1, 2, Kokkos::pair<int, int>(1, 3), 0, 1, 2,
Kokkos::pair<int, int>(3, 3));
ASSERT_EQ(0,x2_deg2.span());

// Kokkos::View< int**, Kokkos::LayoutRight, Space > error_2 =
Kokkos::View<int**, Kokkos::LayoutStride, Space> sx2 =
Kokkos::subview(x8, 1, Kokkos::pair<int, int>(0, 2), 2, 3,
Kokkos::View<int**, Kokkos::LayoutStride, Space> sx2;
make_subview(use_constr,sx2,x8, 1, Kokkos::pair<int, int>(0, 2), 2, 3,
Kokkos::pair<int, int>(0, 2), 1, 2, 3);

ASSERT_TRUE(&sx2(0, 0) == &x8(1, 0, 2, 3, 0, 1, 2, 3));
ASSERT_TRUE(&sx2(1, 0) == &x8(1, 1, 2, 3, 0, 1, 2, 3));
ASSERT_TRUE(&sx2(0, 1) == &x8(1, 0, 2, 3, 1, 1, 2, 3));
ASSERT_TRUE(&sx2(1, 1) == &x8(1, 1, 2, 3, 1, 1, 2, 3));

Kokkos::View<int****, Kokkos::LayoutStride, Space> sx4 =
Kokkos::subview(x8, 0, Kokkos::pair<int, int>(0, 2) /* of [3] */

Kokkos::View<int**, Kokkos::LayoutStride, Space> sx2_deg;
make_subview(use_constr,sx2_deg,x8, 1, Kokkos::pair<int, int>(0, 2), 2, 3,
1, 1, 2, Kokkos::pair<int, int>(3,3));
ASSERT_EQ(0,sx2_deg.span());

Kokkos::View<int****, Kokkos::LayoutStride, Space> sx4;
make_subview(use_constr,sx4,x8, 0, Kokkos::pair<int, int>(0, 2) /* of [3] */
,
1, Kokkos::pair<int, int>(1, 3) /* of [5] */
,
Expand Down

0 comments on commit fd586b6

Please sign in to comment.