forked from kokkos/kokkos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestMDSpanConversion.hpp
408 lines (371 loc) · 19.3 KB
/
TestMDSpanConversion.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//@HEADER
// ************************************************************************
//
// Kokkos v. 4.0
// Copyright (2022) National Technology & Engineering
// Solutions of Sandia, LLC (NTESS).
//
// Under the terms of Contract DE-NA0003525 with NTESS,
// the U.S. Government retains certain rights in this software.
//
// Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions.
// See https://kokkos.org/LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//@HEADER
#include <gtest/gtest.h>
#include <type_traits>
#include <Kokkos_Core.hpp>
#ifdef KOKKOS_ENABLE_IMPL_MDSPAN
namespace {
template <class T, class ExecutionSpace>
struct TestViewMDSpanConversion {
using value_type = T;
template <std::size_t Padding>
using layout_left_padded = Kokkos::Experimental::layout_left_padded<Padding>;
template <std::size_t Padding>
using layout_right_padded =
Kokkos::Experimental::layout_right_padded<Padding>;
struct TestAccessor {
using offset_policy = TestAccessor;
using element_type = value_type;
using reference = element_type &;
using data_handle_type = element_type *;
constexpr TestAccessor() noexcept = default;
constexpr reference access(data_handle_type p, std::size_t i) noexcept {
return p[i];
}
constexpr data_handle_type offset(data_handle_type p,
std::size_t i) noexcept {
return p + i;
}
};
template <class KokkosLayout, class DataType, class MDSpanLayoutMapping,
class... RefViewProps>
static void test_conversion_from_mdspan(
Kokkos::View<DataType, RefViewProps...> ref,
const MDSpanLayoutMapping &mapping) {
using unmanaged_view_view_type =
Kokkos::View<DataType, KokkosLayout, ExecutionSpace,
Kokkos::MemoryTraits<Kokkos::Unmanaged>>;
using natural_mdspan_type = typename Kokkos::Impl::MDSpanViewTraits<
typename unmanaged_view_view_type::traits>::mdspan_type;
using mapping_type = MDSpanLayoutMapping;
using mdspan_layout_type = typename MDSpanLayoutMapping::layout_type;
using extents_type = typename mapping_type::extents_type;
using mdspan_type =
Kokkos::mdspan<value_type, extents_type, mdspan_layout_type>;
static_assert(std::is_constructible_v<natural_mdspan_type, mdspan_type>);
static_assert(std::is_convertible_v<mdspan_type, natural_mdspan_type> ==
std::is_convertible_v<mdspan_type, unmanaged_view_view_type>);
// Manually create an mdspan from ref so we have a valid pointer to play
// with
const auto &exts = mapping.extents();
auto mds = mdspan_type{ref.data(), mapping};
auto test_view = unmanaged_view_view_type(mds);
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::Impl::MDSpanViewTraits<
typename view_type::traits>::mdspan_type;
static_assert(natural_mdspan_type::rank() == view_type::rank);
static_assert(std::is_same_v<typename natural_mdspan_type::value_type,
typename view_type::value_type>);
// test conversion operator to natural mdspan
{
natural_mdspan_type cvt = v;
ASSERT_EQ(cvt.data_handle(), v.data());
ASSERT_EQ(cvt.mapping(), ref_layout_mapping);
}
// test to_mdspan() returning natural mdspan
{
auto cvt = v.to_mdspan();
static_assert(std::is_same_v<natural_mdspan_type, decltype(cvt)>);
ASSERT_EQ(cvt.data_handle(), v.data());
ASSERT_EQ(cvt.mapping(), ref_layout_mapping);
}
// test conversion operator to different mdspan type
{
using mdspan_type = Kokkos::mdspan<
const typename natural_mdspan_type::element_type,
Kokkos::dextents<typename natural_mdspan_type::index_type,
natural_mdspan_type::rank()>,
typename natural_mdspan_type::layout_type,
typename natural_mdspan_type::accessor_type>;
mdspan_type cvt = v;
ASSERT_EQ(cvt.data_handle(), v.data());
ASSERT_EQ(cvt.mapping(), ref_layout_mapping);
}
}
template <class MDSpanLayoutMapping, class ViewType, class AccessorType>
static void test_conversion_to_mdspan_with_accessor(
const MDSpanLayoutMapping &ref_layout_mapping, ViewType v,
const AccessorType &a) {
auto cvt = v.to_mdspan(a);
static_assert(decltype(cvt)::rank() == ViewType::rank);
static_assert(std::is_same_v<typename decltype(cvt)::value_type,
typename ViewType::value_type>);
ASSERT_EQ(cvt.data_handle(), v.data());
ASSERT_EQ(cvt.mapping(), ref_layout_mapping);
}
static void run_test() {
// nvcc doesn't do CTAD properly here, making this way more verbose..
// LayoutLeft
test_conversion_from_mdspan<Kokkos::LayoutLeft>(
Kokkos::View<value_type *, Kokkos::LayoutLeft, ExecutionSpace>("ref",
7),
typename layout_left_padded<sizeof(
value_type)>::template mapping<Kokkos::dextents<std::size_t, 1>>{
Kokkos::dextents<std::size_t, 1>(7)});
test_conversion_from_mdspan<Kokkos::LayoutLeft>(
Kokkos::View<value_type[7], Kokkos::LayoutLeft, ExecutionSpace>("ref"),
typename layout_left_padded<sizeof(
value_type)>::template mapping<Kokkos::extents<std::size_t, 7>>{
Kokkos::extents<std::size_t, 7>()});
test_conversion_from_mdspan<Kokkos::LayoutLeft>(
Kokkos::View<value_type[7], Kokkos::LayoutLeft, ExecutionSpace>("ref"),
typename layout_left_padded<sizeof(
value_type)>::template mapping<Kokkos::dextents<std::size_t, 1>>{
Kokkos::dextents<std::size_t, 1>(7)});
test_conversion_from_mdspan<Kokkos::LayoutLeft>(
Kokkos::View<value_type *, Kokkos::LayoutLeft, ExecutionSpace>("ref",
7),
typename layout_left_padded<sizeof(
value_type)>::template mapping<Kokkos::extents<std::size_t, 7>>{
Kokkos::extents<std::size_t, 7>()});
test_conversion_from_mdspan<Kokkos::LayoutLeft>(
Kokkos::View<value_type **, Kokkos::LayoutLeft, ExecutionSpace>("ref",
7, 3),
typename layout_left_padded<sizeof(
value_type)>::template mapping<Kokkos::dextents<std::size_t, 2>>{
Kokkos::dextents<std::size_t, 2>(7, 3)});
test_conversion_from_mdspan<Kokkos::LayoutLeft>(
Kokkos::View<value_type[7][3], Kokkos::LayoutLeft, ExecutionSpace>(
"ref"),
typename layout_left_padded<sizeof(
value_type)>::template mapping<Kokkos::extents<std::size_t, 7, 3>>{
Kokkos::extents<std::size_t, 7, 3>()});
test_conversion_from_mdspan<Kokkos::LayoutLeft>(
Kokkos::View<value_type[7][3], Kokkos::LayoutLeft, ExecutionSpace>(
"ref"),
typename layout_left_padded<sizeof(
value_type)>::template mapping<Kokkos::dextents<std::size_t, 2>>{
Kokkos::dextents<std::size_t, 2>(7, 3)});
test_conversion_from_mdspan<Kokkos::LayoutLeft>(
Kokkos::View<value_type **, Kokkos::LayoutLeft, ExecutionSpace>("ref",
7, 3),
typename layout_left_padded<sizeof(
value_type)>::template mapping<Kokkos::extents<std::size_t, 7, 3>>{
Kokkos::extents<std::size_t, 7, 3>()});
// LayoutRight
test_conversion_from_mdspan<Kokkos::LayoutRight>(
Kokkos::View<value_type *, Kokkos::LayoutRight, ExecutionSpace>("ref",
7),
typename layout_right_padded<sizeof(
value_type)>::template mapping<Kokkos::dextents<std::size_t, 1>>{
Kokkos::dextents<std::size_t, 1>(7)});
test_conversion_from_mdspan<Kokkos::LayoutRight>(
Kokkos::View<value_type[7], Kokkos::LayoutRight, ExecutionSpace>("ref"),
typename layout_right_padded<sizeof(
value_type)>::template mapping<Kokkos::extents<std::size_t, 7>>{
Kokkos::extents<std::size_t, 7>()});
test_conversion_from_mdspan<Kokkos::LayoutRight>(
Kokkos::View<value_type[7], Kokkos::LayoutRight, ExecutionSpace>("ref"),
typename layout_right_padded<sizeof(
value_type)>::template mapping<Kokkos::dextents<std::size_t, 1>>{
Kokkos::dextents<std::size_t, 1>(7)});
test_conversion_from_mdspan<Kokkos::LayoutRight>(
Kokkos::View<value_type *, Kokkos::LayoutRight, ExecutionSpace>("ref",
7),
typename layout_right_padded<sizeof(
value_type)>::template mapping<Kokkos::extents<std::size_t, 7>>{
Kokkos::extents<std::size_t, 7>()});
test_conversion_from_mdspan<Kokkos::LayoutRight>(
Kokkos::View<value_type **, Kokkos::LayoutRight, ExecutionSpace>("ref",
7, 3),
typename layout_right_padded<sizeof(
value_type)>::template mapping<Kokkos::dextents<std::size_t, 2>>{
Kokkos::dextents<std::size_t, 2>(7, 3)});
test_conversion_from_mdspan<Kokkos::LayoutRight>(
Kokkos::View<value_type[7][3], Kokkos::LayoutRight, ExecutionSpace>(
"ref"),
typename layout_right_padded<sizeof(
value_type)>::template mapping<Kokkos::extents<std::size_t, 7, 3>>{
Kokkos::extents<std::size_t, 7, 3>()});
test_conversion_from_mdspan<Kokkos::LayoutRight>(
Kokkos::View<value_type[7][3], Kokkos::LayoutRight, ExecutionSpace>(
"ref"),
typename layout_right_padded<sizeof(
value_type)>::template mapping<Kokkos::dextents<std::size_t, 2>>{
Kokkos::dextents<std::size_t, 2>(7, 3)});
test_conversion_from_mdspan<Kokkos::LayoutRight>(
Kokkos::View<value_type **, Kokkos::LayoutRight, ExecutionSpace>("ref",
7, 3),
typename layout_right_padded<sizeof(
value_type)>::template mapping<Kokkos::extents<std::size_t, 7, 3>>{
Kokkos::extents<std::size_t, 7, 3>()});
// LayoutStride
test_conversion_from_mdspan<Kokkos::LayoutStride>(
Kokkos::View<value_type *, Kokkos::LayoutStride, ExecutionSpace>(
"ref", Kokkos::LayoutStride{7, 2}),
Kokkos::layout_stride::mapping<Kokkos::dextents<std::size_t, 1>>{
Kokkos::dextents<std::size_t, 1>{7},
std::array<std::size_t, 1>{2}});
test_conversion_from_mdspan<Kokkos::LayoutStride>(
Kokkos::View<value_type[7], Kokkos::LayoutStride, ExecutionSpace>(
"ref", Kokkos::LayoutStride{7, 2}),
Kokkos::layout_stride::mapping<Kokkos::extents<std::size_t, 7>>{
{}, std::array<std::size_t, 1>{2}});
test_conversion_from_mdspan<Kokkos::LayoutStride>(
Kokkos::View<value_type[7], Kokkos::LayoutStride, ExecutionSpace>(
"ref", Kokkos::LayoutStride{7, 2}),
Kokkos::layout_stride::mapping<Kokkos::dextents<std::size_t, 1>>{
Kokkos::dextents<std::size_t, 1>{7},
std::array<std::size_t, 1>{2}});
test_conversion_from_mdspan<Kokkos::LayoutStride>(
Kokkos::View<value_type *, Kokkos::LayoutStride, ExecutionSpace>(
"ref", Kokkos::LayoutStride{7, 2}),
Kokkos::layout_stride::mapping<Kokkos::extents<std::size_t, 7>>{
Kokkos::extents<std::size_t, 7>(), std::array<std::size_t, 1>{2}});
test_conversion_from_mdspan<Kokkos::LayoutStride>(
Kokkos::View<value_type **, Kokkos::LayoutStride, ExecutionSpace>(
"ref", Kokkos::LayoutStride{7, 2, 3, 4}),
Kokkos::layout_stride::mapping<Kokkos::dextents<std::size_t, 2>>{
Kokkos::dextents<std::size_t, 2>(7, 3),
std::array<std::size_t, 2>{2, 4}});
test_conversion_from_mdspan<Kokkos::LayoutStride>(
Kokkos::View<value_type[7][3], Kokkos::LayoutStride, ExecutionSpace>(
"ref", Kokkos::LayoutStride{7, 2, 3, 4}),
Kokkos::layout_stride::mapping<Kokkos::extents<std::size_t, 7, 3>>{
Kokkos::extents<std::size_t, 7, 3>(),
std::array<std::size_t, 2>{2, 4}});
test_conversion_from_mdspan<Kokkos::LayoutStride>(
Kokkos::View<value_type[7][3], Kokkos::LayoutStride, ExecutionSpace>(
"ref", Kokkos::LayoutStride{7, 2, 3, 4}),
Kokkos::layout_stride::mapping<Kokkos::dextents<std::size_t, 2>>{
Kokkos::dextents<std::size_t, 2>(7, 3),
std::array<std::size_t, 2>{2, 4}});
test_conversion_from_mdspan<Kokkos::LayoutStride>(
Kokkos::View<value_type **, Kokkos::LayoutStride, ExecutionSpace>(
"ref", Kokkos::LayoutStride{7, 2, 3, 4}),
Kokkos::layout_stride::mapping<Kokkos::extents<std::size_t, 7, 3>>{
Kokkos::extents<std::size_t, 7, 3>(),
std::array<std::size_t, 2>{2, 4}});
// Conversion to mdspan
test_conversion_to_mdspan(
layout_left_padded<Kokkos::dynamic_extent>::mapping<
Kokkos::extents<std::size_t, 4>>({}, 4),
Kokkos::View<value_type *, Kokkos::LayoutLeft, ExecutionSpace>("v", 4));
test_conversion_to_mdspan(
layout_left_padded<Kokkos::dynamic_extent>::mapping<
Kokkos::extents<std::size_t, 4, 7>>({}, 4),
Kokkos::View<value_type **, Kokkos::LayoutLeft, ExecutionSpace>("v", 4,
7));
test_conversion_to_mdspan(
layout_right_padded<Kokkos::dynamic_extent>::mapping<
Kokkos::extents<std::size_t, 4>>({}, 4),
Kokkos::View<value_type *, Kokkos::LayoutRight, ExecutionSpace>("v",
4));
test_conversion_to_mdspan(
layout_right_padded<Kokkos::dynamic_extent>::mapping<
Kokkos::extents<std::size_t, 4, 7>>({}, 7),
Kokkos::View<value_type **, Kokkos::LayoutRight, ExecutionSpace>("v", 4,
7));
test_conversion_to_mdspan(
Kokkos::layout_stride::mapping<Kokkos::extents<std::size_t, 4>>(
{}, std::array<std::size_t, 1>{5}),
Kokkos::View<value_type *, Kokkos::LayoutStride, ExecutionSpace>(
"v", Kokkos::LayoutStride{4, 5}));
test_conversion_to_mdspan(
Kokkos::layout_stride::mapping<Kokkos::extents<std::size_t, 4, 7>>(
{}, std::array<std::size_t, 2>{5, 9}),
Kokkos::View<value_type **, Kokkos::LayoutStride, ExecutionSpace>(
"v", Kokkos::LayoutStride{4, 5, 7, 9}));
// Conversion with standard default_accessor
test_conversion_to_mdspan_with_accessor(
layout_left_padded<Kokkos::dynamic_extent>::mapping<
Kokkos::extents<std::size_t, 4>>({}, 4),
Kokkos::View<value_type *, Kokkos::LayoutLeft, ExecutionSpace>("v", 4),
Kokkos::default_accessor<value_type>{});
test_conversion_to_mdspan_with_accessor(
layout_left_padded<Kokkos::dynamic_extent>::mapping<
Kokkos::extents<std::size_t, 4, 7>>({}, 4),
Kokkos::View<value_type **, Kokkos::LayoutLeft, ExecutionSpace>("v", 4,
7),
Kokkos::default_accessor<value_type>{});
test_conversion_to_mdspan_with_accessor(
layout_right_padded<Kokkos::dynamic_extent>::mapping<
Kokkos::extents<std::size_t, 4>>({}, 4),
Kokkos::View<value_type *, Kokkos::LayoutRight, ExecutionSpace>("v", 4),
Kokkos::default_accessor<value_type>{});
test_conversion_to_mdspan_with_accessor(
layout_right_padded<Kokkos::dynamic_extent>::mapping<
Kokkos::extents<std::size_t, 4, 7>>({}, 7),
Kokkos::View<value_type **, Kokkos::LayoutRight, ExecutionSpace>("v", 4,
7),
Kokkos::default_accessor<value_type>{});
test_conversion_to_mdspan_with_accessor(
Kokkos::layout_stride::mapping<Kokkos::extents<std::size_t, 4>>(
{}, std::array<std::size_t, 1>{5}),
Kokkos::View<value_type *, Kokkos::LayoutStride, ExecutionSpace>(
"v", Kokkos::LayoutStride{4, 5}),
Kokkos::default_accessor<value_type>{});
test_conversion_to_mdspan_with_accessor(
Kokkos::layout_stride::mapping<Kokkos::extents<std::size_t, 4, 7>>(
{}, std::array<std::size_t, 2>{5, 9}),
Kokkos::View<value_type **, Kokkos::LayoutStride, ExecutionSpace>(
"v", Kokkos::LayoutStride{4, 5, 7, 9}),
Kokkos::default_accessor<value_type>{});
// Conversion with a test accessor
test_conversion_to_mdspan_with_accessor(
layout_left_padded<Kokkos::dynamic_extent>::mapping<
Kokkos::extents<std::size_t, 4>>({}, 4),
Kokkos::View<value_type *, Kokkos::LayoutLeft, ExecutionSpace>("v", 4),
TestAccessor{});
test_conversion_to_mdspan_with_accessor(
layout_left_padded<Kokkos::dynamic_extent>::mapping<
Kokkos::extents<std::size_t, 4, 7>>({}, 4),
Kokkos::View<value_type **, Kokkos::LayoutLeft, ExecutionSpace>("v", 4,
7),
TestAccessor{});
test_conversion_to_mdspan_with_accessor(
layout_right_padded<Kokkos::dynamic_extent>::mapping<
Kokkos::extents<std::size_t, 4>>({}, 4),
Kokkos::View<value_type *, Kokkos::LayoutRight, ExecutionSpace>("v", 4),
TestAccessor{});
test_conversion_to_mdspan_with_accessor(
layout_right_padded<Kokkos::dynamic_extent>::mapping<
Kokkos::extents<std::size_t, 4, 7>>({}, 7),
Kokkos::View<value_type **, Kokkos::LayoutRight, ExecutionSpace>("v", 4,
7),
TestAccessor{});
test_conversion_to_mdspan_with_accessor(
Kokkos::layout_stride::mapping<Kokkos::extents<std::size_t, 4>>(
{}, std::array<std::size_t, 1>{5}),
Kokkos::View<value_type *, Kokkos::LayoutStride, ExecutionSpace>(
"v", Kokkos::LayoutStride{4, 5}),
TestAccessor{});
test_conversion_to_mdspan_with_accessor(
Kokkos::layout_stride::mapping<Kokkos::extents<std::size_t, 4, 7>>(
{}, std::array<std::size_t, 2>{5, 9}),
Kokkos::View<value_type **, Kokkos::LayoutStride, ExecutionSpace>(
"v", Kokkos::LayoutStride{4, 5, 7, 9}),
TestAccessor{});
}
};
TEST(TEST_CATEGORY, view_mdspan_conversion) {
TestViewMDSpanConversion<double, TEST_EXECSPACE>::run_test();
TestViewMDSpanConversion<float, TEST_EXECSPACE>::run_test();
TestViewMDSpanConversion<int, TEST_EXECSPACE>::run_test();
}
} // namespace
#endif // KOKKOS_ENABLE_IMPL_MDSPAN