-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathaccessor_helper.hpp
578 lines (488 loc) · 22.1 KB
/
accessor_helper.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
/*******************************<GINKGO LICENSE>******************************
Copyright (c) 2017-2021, the Ginkgo authors
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************<GINKGO LICENSE>*******************************/
#ifndef GKO_CORE_BASE_ACCESSOR_HELPER_HPP_
#define GKO_CORE_BASE_ACCESSOR_HELPER_HPP_
#include <array>
#include <tuple>
#include <type_traits>
#include <utility>
#include <ginkgo/core/base/dim.hpp>
#include <ginkgo/core/base/range.hpp>
#include <ginkgo/core/base/types.hpp>
namespace gko {
namespace accessor {
/**
* This namespace contains helper functionality for the accessors.
*
* @note This namespace is not part of the public interface and can change
* without notice.
*/
namespace helper {
namespace detail {
/**
* This helper runs from first to last dimension in order to compute the index.
* The index is computed like this:
* indices: x1, x2, x3, ...
* compute(stride, x1, x2, x3) -> x1 * stride[0] + x2 * stride[1] + x3
*/
template <typename ValueType, size_type total_dim, size_type current_iter = 1>
struct row_major_helper_s {
static_assert(total_dim >= 1, "Dimensionality must be >= 1");
static_assert(current_iter < total_dim, "Iteration must be < total_dim!");
static constexpr size_type dim_idx{current_iter - 1};
template <typename FirstType, typename... Indices>
static constexpr GKO_ATTRIBUTES ValueType
compute(const dim<total_dim> &size,
const std::array<ValueType, (total_dim > 1 ? total_dim - 1 : 0)>
&stride,
FirstType first, Indices &&... idxs)
{
// The ASSERT size check must NOT be indexed with `dim_idx` directy,
// otherwise, it leads to a linker error. The reason is likely that
// `dim<N>::operator[](const size_type &)` uses a reference. Since
// `dim_idx` is constexpr (and not defined in a namespace scope), it
// can't be odr-used.
return GKO_ASSERT(first < size[static_cast<size_type>(dim_idx)]),
first * stride[dim_idx] +
row_major_helper_s<ValueType, total_dim, current_iter + 1>::
compute(size, stride, std::forward<Indices>(idxs)...);
}
};
template <typename ValueType, size_type total_dim>
struct row_major_helper_s<ValueType, total_dim, total_dim> {
template <typename FirstType>
static constexpr GKO_ATTRIBUTES ValueType
compute(const dim<total_dim> &size,
const std::array<ValueType, (total_dim > 1 ? total_dim - 1 : 0)>,
FirstType first)
{
return GKO_ASSERT(first < size[total_dim - 1]), first;
}
};
} // namespace detail
/**
* Computes the storage index for the given indices with respect to the given
* stride array
*/
template <typename ValueType, size_type total_dim, typename... Indices>
constexpr GKO_ATTRIBUTES ValueType compute_storage_index(
const dim<total_dim> &size,
const std::array<ValueType, (total_dim > 1 ? total_dim - 1 : 0)> &stride,
Indices &&... idxs)
{
return detail::row_major_helper_s<ValueType, total_dim>::compute(
size, stride, std::forward<Indices>(idxs)...);
}
namespace detail {
/**
* This helper walks through the index arguments from left to right (lowest to
* highest dimension) in order to properly use the stride for the scalar
* indices. The mask indicates which indices are actually used. The least
* significant bit set means using the last index, second bit corresponds to the
* second last dimension, and so on.
*
* Basically, this computes indices in a similar fashion as `row_major_helper_s`
* while having a mask signaling which indices to skip.
*
* Example: Mask = 0b1101
* compute(stride, tuple(x1, x2, x3, x4))
* -> x1 * stride[1] + x2 * stride[2] + x4 (x3 skipped since bit not set)
*/
template <typename ValueType, size_type mask, size_type set_bits_processed,
size_type stride_size, size_type dim_idx, size_type total_dim,
// Determine if the bit in the mask is set for dim_idx
// If the end is reached (dim_idx == total_dim), set it to false in
// order to only need one specialization
bool mask_set = (dim_idx < total_dim)
? static_cast<bool>(mask &(
size_type{1} << (total_dim - 1 - dim_idx)))
: false>
struct row_major_masked_helper_s {};
// bit for current dimensionality is set
template <typename ValueType, size_type mask, size_type set_bits_processed,
size_type stride_size, size_type dim_idx, size_type total_dim>
struct row_major_masked_helper_s<ValueType, mask, set_bits_processed,
stride_size, dim_idx, total_dim, true> {
static_assert(mask &
(size_type{1}
<< (dim_idx < total_dim ? total_dim - 1 - dim_idx : 0)),
"Do not touch the `mask_set` template parameter!");
static_assert(dim_idx < total_dim,
"The iteration count must be smaller than total_dim here!!!");
static_assert(set_bits_processed <= stride_size,
"The processed bits must be < total number of set bits!");
template <typename... Args>
static constexpr GKO_ATTRIBUTES std::array<ValueType, stride_size>
build_stride(const dim<total_dim> &size, Args &&... args)
{
return row_major_masked_helper_s<
ValueType, mask, set_bits_processed + 1, stride_size, dim_idx + 1,
total_dim>::build_stride(size, std::forward<Args>(args)...,
mult_size_upwards(size));
}
static constexpr GKO_ATTRIBUTES ValueType
mult_size_upwards(const dim<total_dim> &size)
{
return size[dim_idx] *
row_major_masked_helper_s<
ValueType, mask, set_bits_processed + 1, stride_size,
dim_idx + 1, total_dim>::mult_size_upwards(size);
}
template <typename First, typename... Indices>
static constexpr GKO_ATTRIBUTES ValueType
compute_mask_idx(const dim<total_dim> &size,
const std::array<ValueType, stride_size> &stride,
First first, Indices &&... idxs)
{
static_assert(sizeof...(Indices) + 1 == total_dim - dim_idx,
"Mismatching number of Idxs!");
// If it is the last set dimension, there is no need for a stride
return GKO_ASSERT(first < size[dim_idx]),
first * (set_bits_processed == stride_size
? 1
: stride[set_bits_processed]) +
row_major_masked_helper_s<
ValueType, mask, set_bits_processed + 1, stride_size,
dim_idx + 1,
total_dim>::compute_mask_idx(size, stride,
std::forward<Indices>(
idxs)...);
}
template <typename First, typename... Indices>
static constexpr GKO_ATTRIBUTES ValueType
compute_direct_idx(const dim<total_dim> &size,
const std::array<ValueType, stride_size> &stride,
First first, Indices &&... idxs)
{
static_assert(sizeof...(Indices) == stride_size - set_bits_processed,
"Mismatching number of Idxs!");
// If it is the last set dimension, there is no need for a stride
return GKO_ASSERT(first < size[dim_idx]),
first * (set_bits_processed == stride_size
? 1
: stride[set_bits_processed]) +
row_major_masked_helper_s<
ValueType, mask, set_bits_processed + 1, stride_size,
dim_idx + 1,
total_dim>::compute_direct_idx(size, stride,
std::forward<Indices>(
idxs)...);
}
};
// first set bit (from the left) for current dimensionality encountered:
// Do not calculate stride value for it (since no lower dimension needs it)!
template <typename ValueType, size_type mask, size_type stride_size,
size_type dim_idx, size_type total_dim>
struct row_major_masked_helper_s<ValueType, mask, 0, stride_size, dim_idx,
total_dim, true> {
static constexpr size_type set_bits_processed{0};
static_assert(mask &
(size_type{1}
<< (dim_idx < total_dim ? total_dim - 1 - dim_idx : 0)),
"Do not touch the `mask_set` template parameter!");
static_assert(dim_idx < total_dim,
"The iteration count must be smaller than total_dim here!!!");
template <typename... Args>
static constexpr GKO_ATTRIBUTES std::array<ValueType, stride_size>
build_stride(const dim<total_dim> &size, Args &&... args)
{
return row_major_masked_helper_s<
ValueType, mask, set_bits_processed + 1, stride_size, dim_idx + 1,
total_dim>::build_stride(size, std::forward<Args>(args)...);
}
static constexpr GKO_ATTRIBUTES ValueType
mult_size_upwards(const dim<total_dim> &size)
{
return row_major_masked_helper_s<
ValueType, mask, set_bits_processed + 1, stride_size, dim_idx + 1,
total_dim>::mult_size_upwards(size);
}
template <typename First, typename... Indices>
static constexpr GKO_ATTRIBUTES ValueType
compute_mask_idx(const dim<total_dim> &size,
const std::array<ValueType, stride_size> &stride,
First first, Indices &&... idxs)
{
static_assert(sizeof...(Indices) + 1 == total_dim - dim_idx,
"Mismatching number of Idxs!");
// If it is the last set dimension, there is no need for a stride
return GKO_ASSERT(first < size[dim_idx]),
first * (set_bits_processed == stride_size
? 1
: stride[set_bits_processed]) +
row_major_masked_helper_s<
ValueType, mask, set_bits_processed + 1, stride_size,
dim_idx + 1,
total_dim>::compute_mask_idx(size, stride,
std::forward<Indices>(
idxs)...);
}
template <typename First, typename... Indices>
static constexpr GKO_ATTRIBUTES ValueType
compute_direct_idx(const dim<total_dim> &size,
const std::array<ValueType, stride_size> &stride,
First first, Indices &&... idxs)
{
static_assert(sizeof...(Indices) == stride_size - set_bits_processed,
"Mismatching number of Idxs!");
// If it is the last set dimension, there is no need for a stride
return GKO_ASSERT(first < size[dim_idx]),
first * (set_bits_processed == stride_size
? 1
: stride[set_bits_processed]) +
row_major_masked_helper_s<
ValueType, mask, set_bits_processed + 1, stride_size,
dim_idx + 1,
total_dim>::compute_direct_idx(size, stride,
std::forward<Indices>(
idxs)...);
}
};
// bit for current dimensionality is not set
template <typename ValueType, size_type mask, size_type set_bits_processed,
size_type stride_size, size_type dim_idx, size_type total_dim>
struct row_major_masked_helper_s<ValueType, mask, set_bits_processed,
stride_size, dim_idx, total_dim, false> {
static_assert((mask & (size_type{1}
<< (dim_idx < total_dim ? total_dim - 1 - dim_idx
: 0))) == 0,
"Do not touch the `mask_set` template parameter!");
static_assert(dim_idx < total_dim,
"The iteration count must be smaller than total_dim here!!!");
static_assert(set_bits_processed <= stride_size + 1,
"The processed bits must be < total number of set bits!");
template <typename... Args>
static constexpr GKO_ATTRIBUTES std::array<ValueType, stride_size>
build_stride(const dim<total_dim> &size, Args &&... args)
{
return row_major_masked_helper_s<
ValueType, mask, set_bits_processed, stride_size, dim_idx + 1,
total_dim>::build_stride(size, std::forward<Args>(args)...);
}
static constexpr GKO_ATTRIBUTES ValueType
mult_size_upwards(const dim<total_dim> &size)
{
return row_major_masked_helper_s<ValueType, mask, set_bits_processed,
stride_size, dim_idx + 1,
total_dim>::mult_size_upwards(size);
}
template <typename First, typename... Indices>
static constexpr GKO_ATTRIBUTES ValueType
compute_mask_idx(const dim<total_dim> &size,
const std::array<ValueType, stride_size> &stride, First,
Indices &&... idxs)
{
static_assert(sizeof...(Indices) + 1 == total_dim - dim_idx,
"Mismatching number of Idxs!");
return row_major_masked_helper_s<
ValueType, mask, set_bits_processed, stride_size, dim_idx + 1,
total_dim>::compute_mask_idx(size, stride,
std::forward<Indices>(idxs)...);
}
template <typename... Indices>
static constexpr GKO_ATTRIBUTES ValueType compute_direct_idx(
const dim<total_dim> &size,
const std::array<ValueType, stride_size> &stride, Indices &&... idxs)
{
return row_major_masked_helper_s<
ValueType, mask, set_bits_processed, stride_size, dim_idx + 1,
total_dim>::compute_direct_idx(size, stride,
std::forward<Indices>(idxs)...);
}
};
// Specialization for the end of recursion: build_stride array from created
// arguments
template <typename ValueType, size_type mask, size_type set_bits_processed,
size_type stride_size, size_type total_dim>
struct row_major_masked_helper_s<ValueType, mask, set_bits_processed,
stride_size, total_dim, total_dim, false> {
static_assert(set_bits_processed <= stride_size + 1,
"The processed bits must be smaller than the total number of "
"set bits!");
template <typename... Args>
static constexpr GKO_ATTRIBUTES std::array<ValueType, stride_size>
build_stride(const dim<total_dim> &, Args &&... args)
{
return {{std::forward<Args>(args)...}};
}
static constexpr GKO_ATTRIBUTES ValueType
mult_size_upwards(const dim<total_dim> &)
{
return 1;
}
static constexpr GKO_ATTRIBUTES ValueType compute_mask_idx(
const dim<total_dim> &, const std::array<ValueType, stride_size> &)
{
return 0;
}
static constexpr GKO_ATTRIBUTES ValueType compute_direct_idx(
const dim<total_dim> &, const std::array<ValueType, stride_size> &)
{
return 0;
}
};
} // namespace detail
/**
* Computes the memory index for the given indices considering the stride.
* Only indices are considered where the corresponding mask bit is set.
*/
template <typename ValueType, size_type mask, size_type stride_size,
size_type total_dim, typename... Indices>
constexpr GKO_ATTRIBUTES auto compute_masked_index(
const dim<total_dim> &size,
const std::array<ValueType, stride_size> &stride, Indices &&... idxs)
{
return detail::row_major_masked_helper_s<
ValueType, mask, 0, stride_size, 0,
total_dim>::compute_mask_idx(size, stride,
std::forward<Indices>(idxs)...);
}
/**
* Computes the memory index for the given indices considering the stride.
*/
template <typename ValueType, size_type mask, size_type stride_size,
size_type total_dim, typename... Indices>
constexpr GKO_ATTRIBUTES auto compute_masked_index_direct(
const dim<total_dim> &size,
const std::array<ValueType, stride_size> &stride, Indices &&... idxs)
{
return detail::row_major_masked_helper_s<
ValueType, mask, 0, stride_size, 0,
total_dim>::compute_direct_idx(size, stride,
std::forward<Indices>(idxs)...);
}
/**
* Computes the default stride array from a size and a given mask which
* indicates which array indices to consider. It is assumed that there is no
* padding
*/
template <typename ValueType, size_type mask, size_type stride_size,
size_type total_dim>
constexpr GKO_ATTRIBUTES auto compute_default_masked_stride_array(
const dim<total_dim> &size)
{
return detail::row_major_masked_helper_s<ValueType, mask, 0, stride_size, 0,
total_dim>::build_stride(size);
}
namespace detail {
template <bool has_span, typename... Args>
struct are_span_compatible_impl
: public std::integral_constant<bool, has_span> {};
template <bool has_span, typename First, typename... Args>
struct are_span_compatible_impl<has_span, First, Args...>
: public std::conditional<
std::is_integral<std::decay_t<First>>::value ||
std::is_same<std::decay_t<First>, span>::value,
are_span_compatible_impl<
has_span || std::is_same<std::decay_t<First>, span>::value,
Args...>,
std::false_type>::type {};
} // namespace detail
/**
* Evaluates if at least one type of Args is a gko::span and the others either
* also gko::span or fulfill std::is_integral
*/
template <typename... Args>
using are_span_compatible = detail::are_span_compatible_impl<false, Args...>;
namespace detail {
template <size_type iter, size_type N, typename Callable, typename... Indices>
GKO_ATTRIBUTES std::enable_if_t<iter == N> multidim_for_each_impl(
const dim<N> &, Callable callable, Indices &&... indices)
{
static_assert(iter == sizeof...(Indices),
"Number arguments must match current iteration!");
callable(std::forward<Indices>(indices)...);
}
template <size_type iter, size_type N, typename Callable, typename... Indices>
GKO_ATTRIBUTES std::enable_if_t<(iter < N)> multidim_for_each_impl(
const dim<N> &size, Callable &&callable, Indices &&... indices)
{
static_assert(iter == sizeof...(Indices),
"Number arguments must match current iteration!");
for (size_type i = 0; i < size[iter]; ++i) {
multidim_for_each_impl<iter + 1>(size, std::forward<Callable>(callable),
std::forward<Indices>(indices)..., i);
}
}
} // namespace detail
/**
* Creates a recursive for-loop for each dimension and calls dest(indices...) =
* source(indices...)
*/
template <size_type N, typename Callable>
GKO_ATTRIBUTES void multidim_for_each(const dim<N> &size, Callable &&callable)
{
detail::multidim_for_each_impl<0>(size, std::forward<Callable>(callable));
}
namespace detail {
template <size_type iter, size_type N>
constexpr GKO_ATTRIBUTES std::enable_if_t<iter == N, int> spans_in_size(
const dim<N> &)
{
return 0;
}
template <size_type iter, size_type N, typename First, typename... Remaining>
constexpr GKO_ATTRIBUTES std::enable_if_t<(iter < N), int> spans_in_size(
const dim<N> &size, First first, Remaining &&... remaining)
{
static_assert(sizeof...(Remaining) + 1 == N - iter,
"Number of remaining spans must be equal to N - iter");
return GKO_ASSERT(span{first}.is_valid()),
GKO_ASSERT(span{first} <= span{size[iter]}),
spans_in_size<iter + 1>(size, std::forward<Remaining>(remaining)...);
}
} // namespace detail
template <size_type N, typename... Spans>
constexpr GKO_ATTRIBUTES int validate_spans(const dim<N> &size,
Spans &&... spans)
{
return detail::spans_in_size<0>(size, std::forward<Spans>(spans)...);
}
namespace detail {
template <size_type, size_type N, size_type iter = 0>
constexpr std::enable_if_t<iter == N, size_type>
count_mask_dimensionality_impl()
{
return 0;
}
template <size_type mask, size_type N, size_type iter = 0>
constexpr std::enable_if_t<(iter < N), size_type>
count_mask_dimensionality_impl()
{
return (mask & size_type{1}) +
count_mask_dimensionality_impl<(mask >> 1), N, iter + 1>();
}
} // namespace detail
template <size_type mask, size_type N>
constexpr size_type count_mask_dimensionality()
{
return detail::count_mask_dimensionality_impl<mask, N>();
}
} // namespace helper
} // namespace accessor
} // namespace gko
#endif // GKO_CORE_BASE_ACCESSOR_HELPER_HPP_