kokkos-utils 0.0.6
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
6import kokkos.core;
7#else
8#include "Kokkos_Core.hpp"
9#endif
10
13
14using execution_space = Kokkos::DefaultExecutionSpace;
15
27
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
38constexpr size_t dim_1 = 5, dim_2 = 3, dim_3 = 6, dim_4 = 9;
39
41TEST(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
54TEST(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
66TEST(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
80TEST(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
91template <concepts::ViewOfRank<4> ViewType>
92bool 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
117TEST(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
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__)