kokkos-utils
0.0.6
Toggle main menu visibility
Loading...
Searching...
No Matches
test_slice.cpp
Go to the documentation of this file.
1
#include "gmock/gmock.h"
2
#include "gtest/gtest.h"
3
4
#include "Kokkos_Macros.hpp"
5
#ifdef KOKKOS_ENABLE_EXPERIMENTAL_CXX20_MODULES
6
import
kokkos.core;
7
#else
8
#include "Kokkos_Core.hpp"
9
#endif
10
11
#include "
kokkos-utils/view/extents.hpp
"
12
#include "
kokkos-utils/view/slice.hpp
"
13
14
using
execution_space
= Kokkos::DefaultExecutionSpace;
15
27
28
namespace
Kokkos::utils::tests::view
29
{
30
31
#define CHECK_THAT(__slice__, __rank__, __extents__) \
32
{ \
33
const auto tmp = __slice__; \
34
ASSERT_EQ(tmp.rank(), __rank__); \
35
ASSERT_EQ(utils::view::extents(tmp), __extents__); \
36
}
37
38
constexpr
size_t
dim_1
= 5,
dim_2
= 3,
dim_3
= 6,
dim_4
= 9;
39
41
TEST
(
view
, slice_rank_1)
42
{
43
const
Kokkos::View<double[dim_1], execution_space> view_1_static(
"rank-1 view with static extent"
);
44
const
Kokkos::View<double* , execution_space> view_1_dynami(
"rank-1 view with dynamic extent"
,
dim_1
);
45
46
CHECK_THAT
(
utils::view::slice<1>
(view_1_static), 1, Kokkos::Array{
dim_1
});
47
CHECK_THAT
(
utils::view::slice<1>
(view_1_dynami), 1, Kokkos::Array{
dim_1
});
48
49
CHECK_THAT
(
utils::view::slice
(view_1_static, 0), 0, (Kokkos::Array<size_t, 0>{}));
50
CHECK_THAT
(
utils::view::slice
(view_1_dynami, 0), 0, (Kokkos::Array<size_t, 0>{}));
51
}
52
54
TEST
(
view
, slice_rank_2)
55
{
56
const
Kokkos::View<double[dim_1][dim_2], execution_space> view_2_static(
"rank-2 view with static extents"
);
57
const
Kokkos::View<double** , execution_space> view_2_dynami(
"rank-2 view with dynamic extents"
,
dim_1
,
dim_2
);
58
const
Kokkos::View<double*[dim_2] , execution_space> view_2_mixed (
"rank-2 view with mixed extents"
,
dim_1
);
59
60
CHECK_THAT
(
utils::view::slice<2>
(view_2_static, 0), 1, Kokkos::Array{
dim_2
});
61
CHECK_THAT
(
utils::view::slice<2>
(view_2_dynami, 0), 1, Kokkos::Array{
dim_2
});
62
CHECK_THAT
(
utils::view::slice<2>
(view_2_mixed , 0), 1, Kokkos::Array{
dim_2
});
63
}
64
66
TEST
(
view
, slice_rank_3)
67
{
68
const
Kokkos::View<double[dim_1][dim_2][dim_3], execution_space> view_3_static(
"rank-3 view of static extents"
);
69
const
Kokkos::View<double*** , execution_space> view_3_dynami(
"rank-3 view of dynamic extents"
,
dim_1
,
dim_2
,
dim_3
);
70
const
Kokkos::View<double*[dim_2][dim_3] , execution_space> view_3_mixed (
"rank-3 view of mixed extents"
,
dim_1
);
71
72
CHECK_THAT
(
utils::view::slice<3>
(view_3_static, 0), 2, (Kokkos::Array{
dim_2
,
dim_3
}));
73
CHECK_THAT
(
utils::view::slice<3>
(view_3_dynami, 0), 2, (Kokkos::Array{
dim_2
,
dim_3
}));
74
CHECK_THAT
(
utils::view::slice<3>
(view_3_mixed , 0), 2, (Kokkos::Array{
dim_2
,
dim_3
}));
75
76
CHECK_THAT
(
utils::view::slice<3>
(view_3_mixed , 0, Kokkos::ALL), 2, (Kokkos::Array{
dim_2
,
dim_3
}));
77
}
78
80
TEST
(
view
, slice_rank_4)
81
{
82
const
Kokkos::View<double[dim_1][dim_2][dim_3][dim_4], execution_space> view_4_static(
"rank-4 view of static extents"
);
83
84
CHECK_THAT
(
utils::view::slice<4>
(view_4_static, 0, Kokkos::ALL), 3, (Kokkos::Array{
dim_2
,
dim_3
,
dim_4
}));
85
86
CHECK_THAT
(
utils::view::slice
(view_4_static, 0, 0, 0, 0), 0, (Kokkos::Array<size_t, 0>{}));
87
88
CHECK_THAT
(
utils::view::slice<4>
(view_4_static, Kokkos::make_pair(0, 2)), 4, (Kokkos::Array<size_t, 4>{2,
dim_2
,
dim_3
,
dim_4
}));
89
}
90
91
template
<concepts::ViewOfRank<4> ViewType>
92
bool
test_slice_on_device
(
const
ViewType&
view
,
const
size_t
expected_size)
93
{
94
bool
result =
false
;
95
96
Kokkos::parallel_reduce(
97
Kokkos::RangePolicy<execution_space>(0, 1),
98
KOKKOS_LAMBDA(
const
int
,
bool
& current)
99
{
100
const
auto
subview = Kokkos::subview (
view
, 0, Kokkos::ALL, Kokkos::ALL, Kokkos::ALL);
101
const
auto
slice =
utils::view::slice<4>
(
view
, 0);
102
current = (
103
subview.size() == expected_size && subview.rank() == 3 &&
104
slice .size() == expected_size && slice .rank() == 3 &&
105
slice.extent(0) ==
dim_2
&&
106
slice.extent(1) ==
dim_3
&&
107
slice.extent(2) ==
dim_4
108
);
109
},
110
Kokkos::LAnd<bool>(result)
111
);
112
113
return
result;
114
}
115
117
TEST
(
view
, slice_on_device)
118
{
119
const
Kokkos::View<double[dim_1][dim_2][dim_3][dim_4], execution_space> view_4_static(
"rank-4 view of static extents"
);
120
121
ASSERT_TRUE(
test_slice_on_device
(view_4_static,
dim_2
*
dim_3
*
dim_4
));
122
}
123
124
}
// namespace Kokkos::utils::tests::view
extents.hpp
Kokkos::utils::tests::view
Definition
test_extents.cpp:28
Kokkos::utils::tests::view::dim_3
constexpr size_t dim_3
Definition
test_extents.cpp:30
Kokkos::utils::tests::view::test_slice_on_device
bool test_slice_on_device(const ViewType &view, const size_t expected_size)
Definition
test_slice.cpp:92
Kokkos::utils::tests::view::dim_2
constexpr size_t dim_2
Definition
test_extents.cpp:30
Kokkos::utils::tests::view::dim_4
constexpr size_t dim_4
Definition
test_extents.cpp:30
Kokkos::utils::tests::view::TEST
TEST(view, extents_rank_0)
Definition
test_extents.cpp:33
Kokkos::utils::tests::view::dim_1
constexpr size_t dim_1
Definition
test_extents.cpp:30
Kokkos::utils::view::slice
KOKKOS_FUNCTION constexpr auto slice(ViewType &&view, Indices &&... indices)
Get a subview, given the first indices. The rest is filled with Kokkos::ALL.
Definition
slice.hpp:54
slice.hpp
execution_space
Kokkos::DefaultExecutionSpace execution_space
Definition
test_InsertOp.cpp:13
CHECK_THAT
#define CHECK_THAT(__slice__, __rank__, __extents__)
Definition
test_slice.cpp:31
tests
view
test_slice.cpp
Generated on
for kokkos-utils by
1.18.0