-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathmdarray_ctors.cpp
More file actions
338 lines (321 loc) · 8.74 KB
/
mdarray_ctors.cpp
File metadata and controls
338 lines (321 loc) · 8.74 KB
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
//@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
#define MDARRAY_SUPPORT_ALLOC
#define MDARRAY_SUPPORT_ITERATOR
#define MDARRAY_SUPPORT_INITLIST
#include <mdspan/mdspan.hpp>
#include <mdspan/mdarray.hpp>
#include <cassert>
template<class mda_t, class ext_t, class map_t, class Container>
void check(mda_t& a, ext_t ext, map_t map, Container& c) {
assert(a.extents() == ext);
assert(a.mapping() == map);
assert(a.container_size() == c.size());
}
// 11 ctors
template<class mda_t, class Container>
void test_minimal_set() {
using ext_t = typename mda_t::extents_type;
using map_t = typename mda_t::mapping_type;
ext_t ext(10, 15);
map_t map(ext);
Container c(map.required_span_size());
{ // default construction
mda_t a;
assert(a.extents() == ext_t());
assert(a.container_size() == map_t(ext_t()).required_span_size());
}
{ // copy constructor
mda_t a(10, 15);
auto ptr = a.data();
mda_t b(a);
check(b, ext, map, c);
assert(b.data() != a.data());
assert(a.data() == ptr);
}
{ // move constructor
mda_t a(10, 15);
auto ptr = a.data();
mda_t b(std::move(a));
check(b, ext, map, c);
assert(b.data() == ptr);
}
{ // integrals
mda_t a(10, 15);
check(a, ext, map, c);
}
{ // extents
mda_t a(ext);
check(a, ext, map, c);
}
{ // mapping
mda_t a(map);
check(a, ext, map, c);
}
{ // extents + container
mda_t a(ext, c);
check(a, ext, map, c);
}
{ // mapping + container
mda_t a(map, c);
check(a, ext, map, c);
}
{ // extents + move(container)
auto c2 = c;
double* ptr = c2.data();
mda_t a(ext, std::move(c2));
check(a, ext, map, c);
assert(a.data() == ptr);
}
{ // mapping + move(container)
auto c2 = c;
double* ptr = c2.data();
mda_t a(map, std::move(c2));
check(a, ext, map, c);
assert(a.data() == ptr);
}
{ // mdspan
mda_t a(10, 15);
a(0,0) = 99;
mda_t b(a.to_mdspan());
check(b, ext, map, c);
assert(b(0,0) == 99);
}
}
// baseline allocator support adds 6 ctors
template<class mda_t, class Container>
void test_alloc_set() {
using ext_t = typename mda_t::extents_type;
using map_t = typename mda_t::mapping_type;
ext_t ext(10, 15);
map_t map(ext);
Container c(map.required_span_size());
auto alloc = std::allocator<typename mda_t::element_type>();
{ // default construction
mda_t a;
assert(a.extents() == ext_t());
assert(a.container_size() == map_t(ext_t()).required_span_size());
}
{ // integrals -> never directly supported
#ifdef MDARRAY_SUPPORT_ALLOC
mda_t a(ext_t(10, 15), alloc);
check(a, ext, map, c);
#endif
}
{ // extents
#ifdef MDARRAY_SUPPORT_ALLOC
mda_t a(ext, alloc);
check(a, ext, map, c);
#endif
}
{ // mapping
#ifdef MDARRAY_SUPPORT_ALLOC
mda_t a(map, alloc);
check(a, ext, map, c);
#endif
}
{ // extents + container
#ifdef MDARRAY_SUPPORT_ALLOC
mda_t a(ext, c, alloc);
#else
mda_t a(ext, std::move(Container(c,alloc)));
#endif
check(a, ext, map, c);
}
{ // mapping + container
#ifdef MDARRAY_SUPPORT_ALLOC
mda_t a(map, c, alloc);
#else
mda_t a(map, std::move(Container(c,alloc)));
#endif
check(a, ext, map, c);
}
{ // extents + move(container)
auto c2 = c;
double* ptr = c2.data();
#ifdef MDARRAY_SUPPORT_ALLOC
mda_t a(ext, std::move(c2), alloc);
#else
mda_t a(ext, std::move(Container(std::move(c2),alloc)));
#endif
check(a, ext, map, c);
assert(a.data() == ptr);
}
{ // mapping + move(container)
auto c2 = c;
double* ptr = c2.data();
#ifdef MDARRAY_SUPPORT_ALLOC
mda_t a(map, std::move(c2), alloc);
#else
mda_t a(map, std::move(Container(std::move(c2),alloc)));
#endif
check(a, ext, map, c);
assert(a.data() == ptr);
}
}
// construct with fill value adds 2+2(alloc) ctors
template<class mda_t, class Container>
void test_value_set() {
using ext_t = typename mda_t::extents_type;
using map_t = typename mda_t::mapping_type;
ext_t ext(10, 15);
map_t map(ext);
Container c(map.required_span_size());
auto alloc = std::allocator<typename mda_t::element_type>();
{ // default construction
mda_t a;
assert(a.extents() == ext_t());
assert(a.container_size() == map_t(ext_t()).required_span_size());
}
{ // integrals -> never directly supported
mda_t a(ext_t(10, 15), 99);
check(a, ext, map, c);
assert(a(0,0)==99);
}
{ // extents
mda_t a(ext, 99);
check(a, ext, map, c);
assert(a(0,0)==99);
}
{ // mapping
mda_t a(map, 99);
check(a, ext, map, c);
assert(a(0,0)==99);
}
{ // extents
#ifdef MDARRAY_SUPPORT_ALLOC
mda_t a(ext, 99, alloc);
#else
mda_t a(ext, std::move(Container(99, alloc)));
#endif
check(a, ext, map, c);
assert(a(0,0)==99);
}
{ // mapping
#ifdef MDARRAY_SUPPORT_ALLOC
mda_t a(map, 99, alloc);
#else
mda_t a(map, std::move(Container(99, alloc)));
#endif
check(a, ext, map, c);
assert(a(0,0)==99);
}
}
#ifdef MDARRAY_SUPPORT_ITERATOR
// ctors from iterator 2+2
// requires container constructible from iterators
// precondition: Container(begin, end).size() >= map.required_span_size()
template<class mda_t, class Container>
void test_iterator_set() {
using ext_t = typename mda_t::extents_type;
using map_t = typename mda_t::mapping_type;
ext_t ext(10, 15);
map_t map(ext);
Container c(map.required_span_size());
c[0] = 99;
auto alloc = std::allocator<typename mda_t::element_type>();
{ // integrals -> never directly supported
mda_t a(ext_t(10, 15), c.begin(), c.end());
check(a, ext, map, c);
assert(a(0,0)==99);
}
{ // extents
mda_t a(ext, c.begin(), c.end());
check(a, ext, map, c);
assert(a(0,0)==99);
}
{ // mapping
mda_t a(map, c.begin(), c.end());
check(a, ext, map, c);
assert(a(0,0)==99);
}
{ // extents
#ifdef MDSPAN_SUPPORT_ALLOC
mda_t a(ext, c.begin(), c.end(), alloc);
#else
mda_t a(ext, std::move(Container(c.begin(), c.end(), alloc)));
#endif
check(a, ext, map, c);
assert(a(0,0)==99);
}
{ // mapping
#ifdef MDSPAN_SUPPORT_ALLOC
mda_t a(map, c.begin(), c.end(), alloc);
#else
mda_t a(map, std::move(Container(c.begin(), c.end(), alloc)));
#endif
check(a, ext, map, c);
assert(a(0,0)==99);
}
}
#endif
#ifdef MDARRAY_SUPPORT_INITLIST
// ctors from iterator 2+2
template<class mda_t, class Container>
void test_initlist_set() {
using ext_t = typename mda_t::extents_type;
using map_t = typename mda_t::mapping_type;
ext_t ext(2, 3);
map_t map(ext);
Container c({99,2,3,4,5,6});
auto alloc = std::allocator<typename mda_t::element_type>();
{ // integrals -> never directly supported
mda_t a(ext_t(2, 3), {99,2,3,4,5,6});
check(a, ext, map, c);
assert(a(0,0)==99);
}
{ // extents
mda_t a(ext, {99,2,3,4,5,6});
check(a, ext, map, c);
assert(a(0,0)==99);
}
{ // mapping
mda_t a(map, {99,2,3,4,5,6});
check(a, ext, map, c);
assert(a(0,0)==99);
}
{ // extents
#ifdef MDARRAY_SUPPORT_ALLOC
mda_t a(ext, {99,2,3,4,5,6}, alloc);
#else
mda_t a(ext, std::move(Container({99,2,3,4,5,6}, alloc)));
#endif
check(a, ext, map, c);
assert(a(0,0)==99);
}
{ // mapping
#ifdef MDARRAY_SUPPORT_ALLOC
mda_t a(map, {99,2,3,4,5,6}, alloc);
#else
mda_t a(map, std::move(Container({99,2,3,4,5,6}, alloc)));
#endif
check(a, ext, map, c);
assert(a(0,0)==99);
}
}
#endif
int main(int argc, char* argv[]) {
test_minimal_set<Kokkos::Experimental::mdarray<double, Kokkos::extents<int, Kokkos::dynamic_extent, Kokkos::dynamic_extent>>, std::vector<double>>();
test_alloc_set<Kokkos::Experimental::mdarray<double, Kokkos::extents<int, Kokkos::dynamic_extent, Kokkos::dynamic_extent>>, std::vector<double>>();
test_value_set<Kokkos::Experimental::mdarray<double, Kokkos::extents<int, Kokkos::dynamic_extent, Kokkos::dynamic_extent>>, std::vector<double>>();
#ifdef MDARRAY_SUPPORT_ITERATOR
test_iterator_set<Kokkos::Experimental::mdarray<double, Kokkos::extents<int, Kokkos::dynamic_extent, Kokkos::dynamic_extent>>, std::vector<double>>();
#endif
#ifdef MDARRAY_SUPPORT_INITLIST
test_initlist_set<Kokkos::Experimental::mdarray<double, Kokkos::extents<int, Kokkos::dynamic_extent, Kokkos::dynamic_extent>>, std::vector<double>>();
#endif
return 0;
}