-
Notifications
You must be signed in to change notification settings - Fork 69
/
layout_right.hpp
223 lines (192 loc) · 8.17 KB
/
layout_right.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
//@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
#pragma once
#include "macros.hpp"
#include "trait_backports.hpp"
#include "extents.hpp"
#include <stdexcept>
#include "layout_stride.hpp"
namespace MDSPAN_IMPL_STANDARD_NAMESPACE {
//==============================================================================
template <class Extents>
class layout_right::mapping {
public:
using extents_type = Extents;
using index_type = typename extents_type::index_type;
using size_type = typename extents_type::size_type;
using rank_type = typename extents_type::rank_type;
using layout_type = layout_right;
private:
static_assert(detail::__is_extents_v<extents_type>,
MDSPAN_IMPL_STANDARD_NAMESPACE_STRING "::layout_right::mapping must be instantiated with a specialization of " MDSPAN_IMPL_STANDARD_NAMESPACE_STRING "::extents.");
template <class>
friend class mapping;
// i0+(i1 + E(1)*(i2 + E(2)*i3))
template <size_t r, size_t Rank>
struct __rank_count {};
template <size_t r, size_t Rank, class I, class... Indices>
_MDSPAN_HOST_DEVICE
constexpr index_type __compute_offset(
index_type offset, __rank_count<r,Rank>, const I& i, Indices... idx) const {
return __compute_offset(offset * __extents.extent(r) + i,__rank_count<r+1,Rank>(), idx...);
}
template<class I, class ... Indices>
_MDSPAN_HOST_DEVICE
constexpr index_type __compute_offset(
__rank_count<0,extents_type::rank()>, const I& i, Indices... idx) const {
return __compute_offset(i,__rank_count<1,extents_type::rank()>(),idx...);
}
_MDSPAN_HOST_DEVICE
constexpr index_type __compute_offset(size_t offset, __rank_count<extents_type::rank(), extents_type::rank()>) const {
return static_cast<index_type>(offset);
}
_MDSPAN_HOST_DEVICE
constexpr index_type __compute_offset(__rank_count<0,0>) const { return 0; }
public:
//--------------------------------------------------------------------------------
MDSPAN_INLINE_FUNCTION_DEFAULTED constexpr mapping() noexcept = default;
MDSPAN_INLINE_FUNCTION_DEFAULTED constexpr mapping(mapping const&) noexcept = default;
_MDSPAN_HOST_DEVICE
constexpr mapping(extents_type const& __exts) noexcept
:__extents(__exts)
{ }
MDSPAN_TEMPLATE_REQUIRES(
class OtherExtents,
/* requires */ (
_MDSPAN_TRAIT(std::is_constructible, extents_type, OtherExtents)
)
)
MDSPAN_CONDITIONAL_EXPLICIT((!std::is_convertible<OtherExtents, extents_type>::value)) // needs two () due to comma
MDSPAN_INLINE_FUNCTION _MDSPAN_CONSTEXPR_14
mapping(mapping<OtherExtents> const& other) noexcept // NOLINT(google-explicit-constructor)
:__extents(other.extents())
{
/*
* TODO: check precondition
* other.required_span_size() is a representable value of type index_type
*/
}
MDSPAN_TEMPLATE_REQUIRES(
class OtherExtents,
/* requires */ (
_MDSPAN_TRAIT(std::is_constructible, extents_type, OtherExtents) &&
(extents_type::rank() <= 1)
)
)
MDSPAN_CONDITIONAL_EXPLICIT((!std::is_convertible<OtherExtents, extents_type>::value)) // needs two () due to comma
MDSPAN_INLINE_FUNCTION _MDSPAN_CONSTEXPR_14
mapping(layout_left::mapping<OtherExtents> const& other) noexcept // NOLINT(google-explicit-constructor)
:__extents(other.extents())
{
/*
* TODO: check precondition
* other.required_span_size() is a representable value of type index_type
*/
}
MDSPAN_TEMPLATE_REQUIRES(
class OtherExtents,
/* requires */ (
_MDSPAN_TRAIT(std::is_constructible, extents_type, OtherExtents)
)
)
MDSPAN_CONDITIONAL_EXPLICIT((extents_type::rank() > 0))
MDSPAN_INLINE_FUNCTION _MDSPAN_CONSTEXPR_14
mapping(layout_stride::mapping<OtherExtents> const& other) noexcept // NOLINT(google-explicit-constructor)
:__extents(other.extents())
{
/*
* TODO: check precondition
* other.required_span_size() is a representable value of type index_type
*/
#if !defined(_MDSPAN_HAS_CUDA) && !defined(_MDSPAN_HAS_HIP) && !defined(NDEBUG)
index_type stride = 1;
for(rank_type r=__extents.rank(); r>0; r--) {
if(stride != static_cast<index_type>(other.stride(r-1))) {
// Note this throw will lead to a terminate if triggered since this function is marked noexcept
throw std::runtime_error("Assigning layout_stride to layout_right with invalid strides.");
}
stride *= __extents.extent(r-1);
}
#endif
}
MDSPAN_INLINE_FUNCTION_DEFAULTED _MDSPAN_CONSTEXPR_14_DEFAULTED mapping& operator=(mapping const&) noexcept = default;
MDSPAN_INLINE_FUNCTION
constexpr const extents_type& extents() const noexcept {
return __extents;
}
MDSPAN_INLINE_FUNCTION
constexpr index_type required_span_size() const noexcept {
index_type value = 1;
for(rank_type r=0; r != extents_type::rank(); ++r) value*=__extents.extent(r);
return value;
}
//--------------------------------------------------------------------------------
MDSPAN_TEMPLATE_REQUIRES(
class... Indices,
/* requires */ (
(sizeof...(Indices) == extents_type::rank()) &&
_MDSPAN_FOLD_AND(
(_MDSPAN_TRAIT(std::is_convertible, Indices, index_type) &&
_MDSPAN_TRAIT(std::is_nothrow_constructible, index_type, Indices))
)
)
)
_MDSPAN_HOST_DEVICE
constexpr index_type operator()(Indices... idxs) const noexcept {
return __compute_offset(__rank_count<0, extents_type::rank()>(), static_cast<index_type>(idxs)...);
}
MDSPAN_INLINE_FUNCTION static constexpr bool is_always_unique() noexcept { return true; }
MDSPAN_INLINE_FUNCTION static constexpr bool is_always_exhaustive() noexcept { return true; }
MDSPAN_INLINE_FUNCTION static constexpr bool is_always_strided() noexcept { return true; }
MDSPAN_INLINE_FUNCTION constexpr bool is_unique() const noexcept { return true; }
MDSPAN_INLINE_FUNCTION constexpr bool is_exhaustive() const noexcept { return true; }
MDSPAN_INLINE_FUNCTION constexpr bool is_strided() const noexcept { return true; }
MDSPAN_INLINE_FUNCTION
constexpr index_type stride(rank_type i) const noexcept
#if MDSPAN_HAS_CXX_20
requires ( Extents::rank() > 0 )
#endif
{
index_type value = 1;
for(rank_type r=extents_type::rank()-1; r>i; r--) value*=__extents.extent(r);
return value;
}
template<class OtherExtents>
MDSPAN_INLINE_FUNCTION
friend constexpr bool operator==(mapping const& lhs, mapping<OtherExtents> const& rhs) noexcept {
return lhs.extents() == rhs.extents();
}
// In C++ 20 the not equal exists if equal is found
#if !(MDSPAN_HAS_CXX_20)
template<class OtherExtents>
MDSPAN_INLINE_FUNCTION
friend constexpr bool operator!=(mapping const& lhs, mapping<OtherExtents> const& rhs) noexcept {
return lhs.extents() != rhs.extents();
}
#endif
// Not really public, but currently needed to implement fully constexpr useable submdspan:
template<size_t N, class SizeType, size_t ... E, size_t ... Idx>
constexpr index_type __get_stride(MDSPAN_IMPL_STANDARD_NAMESPACE::extents<SizeType, E...>,std::integer_sequence<size_t, Idx...>) const {
return _MDSPAN_FOLD_TIMES_RIGHT((Idx>N? __extents.template __extent<Idx>():1),1);
}
template<size_t N>
constexpr index_type __stride() const noexcept {
return __get_stride<N>(__extents, std::make_index_sequence<extents_type::rank()>());
}
private:
_MDSPAN_NO_UNIQUE_ADDRESS extents_type __extents{};
};
} // end namespace MDSPAN_IMPL_STANDARD_NAMESPACE