kokkos-utils 0.0.1
 
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_Core.hpp"
5
8
9using execution_space = Kokkos::DefaultExecutionSpace;
10
22
24{
25
26#define CHECK_THAT(__slice__, __rank__, __extents__) \
27 { \
28 const auto tmp = __slice__; \
29 ASSERT_EQ(tmp.rank(), __rank__); \
30 ASSERT_EQ(utils::view::extents(tmp), __extents__); \
31 }
32
33constexpr size_t dim_1 = 5, dim_2 = 3, dim_3 = 6, dim_4 = 9;
34
36TEST(view, slice_rank_1)
37{
38 const Kokkos::View<double[dim_1], execution_space> view_1_static("rank-1 view with static extent");
39 const Kokkos::View<double* , execution_space> view_1_dynami("rank-1 view with dynamic extent", dim_1);
40
41 CHECK_THAT(utils::view::slice<1>(view_1_static), 1, Kokkos::Array{dim_1});
42 CHECK_THAT(utils::view::slice<1>(view_1_dynami), 1, Kokkos::Array{dim_1});
43
44 CHECK_THAT(utils::view::slice(view_1_static, 0), 0, (Kokkos::Array<size_t, 0>{}));
45 CHECK_THAT(utils::view::slice(view_1_dynami, 0), 0, (Kokkos::Array<size_t, 0>{}));
46}
47
49TEST(view, slice_rank_2)
50{
51 const Kokkos::View<double[dim_1][dim_2], execution_space> view_2_static("rank-2 view with static extents");
52 const Kokkos::View<double** , execution_space> view_2_dynami("rank-2 view with dynamic extents", dim_1, dim_2);
53 const Kokkos::View<double*[dim_2] , execution_space> view_2_mixed ("rank-2 view with mixed extents" , dim_1);
54
55 CHECK_THAT(utils::view::slice<2>(view_2_static, 0), 1, Kokkos::Array{dim_2});
56 CHECK_THAT(utils::view::slice<2>(view_2_dynami, 0), 1, Kokkos::Array{dim_2});
57 CHECK_THAT(utils::view::slice<2>(view_2_mixed , 0), 1, Kokkos::Array{dim_2});
58}
59
61TEST(view, slice_rank_3)
62{
63 const Kokkos::View<double[dim_1][dim_2][dim_3], execution_space> view_3_static("rank-3 view of static extents");
64 const Kokkos::View<double*** , execution_space> view_3_dynami("rank-3 view of dynamic extents", dim_1, dim_2, dim_3);
65 const Kokkos::View<double*[dim_2][dim_3] , execution_space> view_3_mixed ("rank-3 view of mixed extents" , dim_1);
66
67 CHECK_THAT(utils::view::slice<3>(view_3_static, 0), 2, (Kokkos::Array{dim_2, dim_3}));
68 CHECK_THAT(utils::view::slice<3>(view_3_dynami, 0), 2, (Kokkos::Array{dim_2, dim_3}));
69 CHECK_THAT(utils::view::slice<3>(view_3_mixed , 0), 2, (Kokkos::Array{dim_2, dim_3}));
70
71 CHECK_THAT(utils::view::slice<3>(view_3_mixed , 0, Kokkos::ALL), 2, (Kokkos::Array{dim_2, dim_3}));
72}
73
75TEST(view, slice_rank_4)
76{
77 const Kokkos::View<double[dim_1][dim_2][dim_3][dim_4], execution_space> view_4_static("rank-4 view of static extents");
78
79 CHECK_THAT(utils::view::slice<4>(view_4_static, 0, Kokkos::ALL), 3, (Kokkos::Array{dim_2, dim_3, dim_4}));
80
81 CHECK_THAT(utils::view::slice(view_4_static, 0, 0, 0, 0), 0, (Kokkos::Array<size_t, 0>{}));
82
83 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}));
84}
85
86template <concepts::ViewOfRank<4> ViewType>
87bool test_slice_on_device(const ViewType& view, const size_t expected_size)
88{
89 bool result = false;
90
91 Kokkos::parallel_reduce(
92 Kokkos::RangePolicy<execution_space>(0, 1),
93 KOKKOS_LAMBDA(const int, bool& result)
94 {
95 const auto subview = Kokkos::subview (view, 0, Kokkos::ALL, Kokkos::ALL, Kokkos::ALL);
96 const auto slice = utils::view::slice<4>(view, 0);
97 result = (
98 subview.size() == expected_size && subview.rank() == 3 &&
99 slice .size() == expected_size && slice .rank() == 3 &&
100 slice.extent(0) == dim_2 &&
101 slice.extent(1) == dim_3 &&
102 slice.extent(2) == dim_4
103 );
104 },
105 Kokkos::LAnd<bool>(result)
106 );
107
108 return result;
109}
110
112TEST(view, slice_on_device)
113{
114 const Kokkos::View<double[dim_1][dim_2][dim_3][dim_4], execution_space> view_4_static("rank-4 view of static extents");
115
116 ASSERT_TRUE(test_slice_on_device(view_4_static, dim_2 * dim_3 * dim_4));
117}
118
119} // namespace Kokkos::utils::tests::view
bool test_slice_on_device(const ViewType &view, const size_t expected_size)
TEST(view, extents_rank_0)
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
Kokkos::DefaultExecutionSpace execution_space
#define CHECK_THAT(__slice__, __rank__, __extents__)