| 1 |
//@HEADER |
| 2 |
// ************************************************************************ |
| 3 |
// |
| 4 |
// Kokkos v. 4.0 |
| 5 |
// Copyright (2022) National Technology & Engineering |
| 6 |
// Solutions of Sandia, LLC (NTESS). |
| 7 |
// |
| 8 |
// Under the terms of Contract DE-NA0003525 with NTESS, |
| 9 |
// the U.S. Government retains certain rights in this software. |
| 10 |
// |
| 11 |
// Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions. |
| 12 |
// See https://kokkos.org/LICENSE for license information. |
| 13 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 14 |
// |
| 15 |
//@HEADER |
| 16 |
#pragma once |
| 17 |
|
| 18 |
#include "macros.hpp" |
| 19 |
#include "trait_backports.hpp" |
| 20 |
#include "extents.hpp" |
| 21 |
#include "layout_stride.hpp" |
| 22 |
#include "utility.hpp" |
| 23 |
#if MDSPAN_HAS_CXX_17 |
| 24 |
#include "../__p2642_bits/layout_padded_fwd.hpp" |
| 25 |
#endif |
| 26 |
|
| 27 |
namespace MDSPAN_IMPL_STANDARD_NAMESPACE { |
| 28 |
|
| 29 |
//============================================================================== |
| 30 |
template <class Extents> |
| 31 |
class layout_right::mapping { |
| 32 |
public: |
| 33 |
using extents_type = Extents; |
| 34 |
using index_type = typename extents_type::index_type; |
| 35 |
using size_type = typename extents_type::size_type; |
| 36 |
using rank_type = typename extents_type::rank_type; |
| 37 |
using layout_type = layout_right; |
| 38 |
private: |
| 39 |
|
| 40 |
static_assert(detail::__is_extents_v<extents_type>, |
| 41 |
MDSPAN_IMPL_STANDARD_NAMESPACE_STRING "::layout_right::mapping must be instantiated with a specialization of " MDSPAN_IMPL_STANDARD_NAMESPACE_STRING "::extents."); |
| 42 |
|
| 43 |
template <class> |
| 44 |
friend class mapping; |
| 45 |
|
| 46 |
// i0+(i1 + E(1)*(i2 + E(2)*i3)) |
| 47 |
template <size_t r, size_t Rank> |
| 48 |
struct __rank_count {}; |
| 49 |
|
| 50 |
template <size_t r, size_t Rank, class I, class... Indices> |
| 51 |
_MDSPAN_HOST_DEVICE |
| 52 |
constexpr index_type __compute_offset( |
| 53 |
index_type offset, __rank_count<r,Rank>, const I& i, Indices... idx) const { |
| 54 |
return __compute_offset(offset * __extents.extent(r) + i,__rank_count<r+1,Rank>(), idx...); |
| 55 |
} |
| 56 |
|
| 57 |
template<class I, class ... Indices> |
| 58 |
_MDSPAN_HOST_DEVICE |
| 59 |
constexpr index_type __compute_offset( |
| 60 |
__rank_count<0,extents_type::rank()>, const I& i, Indices... idx) const { |
| 61 |
return __compute_offset(i,__rank_count<1,extents_type::rank()>(),idx...); |
| 62 |
} |
| 63 |
|
| 64 |
_MDSPAN_HOST_DEVICE |
| 65 |
constexpr index_type __compute_offset(size_t offset, __rank_count<extents_type::rank(), extents_type::rank()>) const { |
| 66 |
return static_cast<index_type>(offset); |
| 67 |
} |
| 68 |
|
| 69 |
_MDSPAN_HOST_DEVICE |
| 70 |
constexpr index_type __compute_offset(__rank_count<0,0>) const { return 0; } |
| 71 |
|
| 72 |
public: |
| 73 |
|
| 74 |
//-------------------------------------------------------------------------------- |
| 75 |
|
| 76 |
MDSPAN_INLINE_FUNCTION_DEFAULTED constexpr mapping() noexcept = default; |
| 77 |
MDSPAN_INLINE_FUNCTION_DEFAULTED constexpr mapping(mapping const&) noexcept = default; |
| 78 |
|
| 79 |
_MDSPAN_HOST_DEVICE |
| 80 |
constexpr mapping(extents_type const& __exts) noexcept |
| 81 |
:__extents(__exts) |
| 82 |
{ } |
| 83 |
|
| 84 |
MDSPAN_TEMPLATE_REQUIRES( |
| 85 |
class OtherExtents, |
| 86 |
/* requires */ ( |
| 87 |
_MDSPAN_TRAIT(std::is_constructible, extents_type, OtherExtents) |
| 88 |
) |
| 89 |
) |
| 90 |
MDSPAN_CONDITIONAL_EXPLICIT((!std::is_convertible<OtherExtents, extents_type>::value)) // needs two () due to comma |
| 91 |
MDSPAN_INLINE_FUNCTION _MDSPAN_CONSTEXPR_14 |
| 92 |
mapping(mapping<OtherExtents> const& other) noexcept // NOLINT(google-explicit-constructor) |
| 93 |
:__extents(other.extents()) |
| 94 |
{ |
| 95 |
/* |
| 96 |
* TODO: check precondition |
| 97 |
* other.required_span_size() is a representable value of type index_type |
| 98 |
*/ |
| 99 |
} |
| 100 |
|
| 101 |
MDSPAN_TEMPLATE_REQUIRES( |
| 102 |
class OtherExtents, |
| 103 |
/* requires */ ( |
| 104 |
_MDSPAN_TRAIT(std::is_constructible, extents_type, OtherExtents) && |
| 105 |
(extents_type::rank() <= 1) |
| 106 |
) |
| 107 |
) |
| 108 |
MDSPAN_CONDITIONAL_EXPLICIT((!std::is_convertible<OtherExtents, extents_type>::value)) // needs two () due to comma |
| 109 |
MDSPAN_INLINE_FUNCTION _MDSPAN_CONSTEXPR_14 |
| 110 |
mapping(layout_left::mapping<OtherExtents> const& other) noexcept // NOLINT(google-explicit-constructor) |
| 111 |
:__extents(other.extents()) |
| 112 |
{ |
| 113 |
/* |
| 114 |
* TODO: check precondition |
| 115 |
* other.required_span_size() is a representable value of type index_type |
| 116 |
*/ |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Converting constructor from `layout_right_padded::mapping`. |
| 121 |
* |
| 122 |
* This overload participates in overload resolution only if _Mapping is a layout_right_padded mapping and |
| 123 |
* extents_type is constructible from _Mapping::extents_type. |
| 124 |
* |
| 125 |
* \note There is currently a difference from p2642r2, where this function is specified as taking |
| 126 |
* `layout_right_padded< padding_value >::mapping< Extents>`. However, this makes `padding_value` non-deducible. |
| 127 |
*/ |
| 128 |
#if MDSPAN_HAS_CXX_17 |
| 129 |
MDSPAN_TEMPLATE_REQUIRES( |
| 130 |
class _Mapping, |
| 131 |
/* requires */ ( |
| 132 |
MDSPAN_IMPL_PROPOSED_NAMESPACE::detail::is_layout_right_padded_mapping<_Mapping>::value |
| 133 |
&& std::is_constructible_v<extents_type, typename _Mapping::extents_type>)) |
| 134 |
MDSPAN_CONDITIONAL_EXPLICIT((!std::is_convertible_v<typename _Mapping::extents_type, extents_type>)) |
| 135 |
mapping(const _Mapping &__other) noexcept |
| 136 |
: __extents(__other.extents()) |
| 137 |
{ |
| 138 |
MDSPAN_IMPL_PROPOSED_NAMESPACE::detail:: |
| 139 |
check_padded_layout_converting_constructor_mandates< |
| 140 |
extents_type, _Mapping>(detail::with_rank<extents_type::rank()>{}); |
| 141 |
MDSPAN_IMPL_PROPOSED_NAMESPACE::detail:: |
| 142 |
check_padded_layout_converting_constructor_preconditions< |
| 143 |
extents_type>(detail::with_rank<extents_type::rank()>{}, __other); |
| 144 |
} |
| 145 |
#endif |
| 146 |
|
| 147 |
MDSPAN_TEMPLATE_REQUIRES( |
| 148 |
class OtherExtents, |
| 149 |
/* requires */ ( |
| 150 |
_MDSPAN_TRAIT(std::is_constructible, extents_type, OtherExtents) |
| 151 |
) |
| 152 |
) |
| 153 |
MDSPAN_CONDITIONAL_EXPLICIT((extents_type::rank() > 0)) |
| 154 |
MDSPAN_INLINE_FUNCTION _MDSPAN_CONSTEXPR_14 |
| 155 |
mapping(layout_stride::mapping<OtherExtents> const& other) noexcept // NOLINT(google-explicit-constructor) |
| 156 |
:__extents(other.extents()) |
| 157 |
{ |
| 158 |
/* |
| 159 |
* TODO: check precondition |
| 160 |
* other.required_span_size() is a representable value of type index_type |
| 161 |
*/ |
| 162 |
detail::validate_strides(detail::with_rank<extents_type::rank()>{}, layout_right{}, __extents, other); |
| 163 |
} |
| 164 |
|
| 165 |
MDSPAN_INLINE_FUNCTION_DEFAULTED _MDSPAN_CONSTEXPR_14_DEFAULTED mapping& operator=(mapping const&) noexcept = default; |
| 166 |
|
| 167 |
MDSPAN_INLINE_FUNCTION |
| 168 |
constexpr const extents_type& extents() const noexcept { |
| 169 |
return __extents; |
| 170 |
} |
| 171 |
|
| 172 |
MDSPAN_INLINE_FUNCTION |
| 173 |
constexpr index_type required_span_size() const noexcept { |
| 174 |
index_type value = 1; |
| 175 |
for(rank_type r=0; r != extents_type::rank(); ++r) value*=__extents.extent(r); |
| 176 |
return value; |
| 177 |
} |
| 178 |
|
| 179 |
//-------------------------------------------------------------------------------- |
| 180 |
|
| 181 |
MDSPAN_TEMPLATE_REQUIRES( |
| 182 |
class ... Indices, |
| 183 |
/* requires */ ( |
| 184 |
(sizeof...(Indices) == extents_type::rank()) && |
| 185 |
(detail::are_valid_indices<index_type, Indices...>()) |
| 186 |
) |
| 187 |
) |
| 188 |
_MDSPAN_HOST_DEVICE |
| 189 |
constexpr index_type operator()(Indices... idxs) const noexcept { |
| 190 |
#if ! defined(NDEBUG) |
| 191 |
detail::check_all_indices(this->extents(), idxs...); |
| 192 |
#endif // ! NDEBUG |
| 193 |
return __compute_offset(__rank_count<0, extents_type::rank()>(), static_cast<index_type>(idxs)...); |
| 194 |
} |
| 195 |
|
| 196 |
MDSPAN_INLINE_FUNCTION static constexpr bool is_always_unique() noexcept { return true; } |
| 197 |
MDSPAN_INLINE_FUNCTION static constexpr bool is_always_exhaustive() noexcept { return true; } |
| 198 |
MDSPAN_INLINE_FUNCTION static constexpr bool is_always_strided() noexcept { return true; } |
| 199 |
MDSPAN_INLINE_FUNCTION static constexpr bool is_unique() noexcept { return true; } |
| 200 |
MDSPAN_INLINE_FUNCTION static constexpr bool is_exhaustive() noexcept { return true; } |
| 201 |
MDSPAN_INLINE_FUNCTION static constexpr bool is_strided() noexcept { return true; } |
| 202 |
|
| 203 |
MDSPAN_INLINE_FUNCTION |
| 204 |
constexpr index_type stride(rank_type i) const noexcept |
| 205 |
#if MDSPAN_HAS_CXX_20 |
| 206 |
requires ( Extents::rank() > 0 ) |
| 207 |
#endif |
| 208 |
{ |
| 209 |
index_type value = 1; |
| 210 |
for(rank_type r=extents_type::rank()-1; r>i; r--) value*=__extents.extent(r); |
| 211 |
return value; |
| 212 |
} |
| 213 |
|
| 214 |
MDSPAN_TEMPLATE_REQUIRES( |
| 215 |
class OtherExtents, |
| 216 |
/* requires */ ( Extents::rank() == OtherExtents::rank()) |
| 217 |
) |
| 218 |
MDSPAN_INLINE_FUNCTION |
| 219 |
friend constexpr bool operator==(mapping const& lhs, mapping<OtherExtents> const& rhs) noexcept { |
| 220 |
return lhs.extents() == rhs.extents(); |
| 221 |
} |
| 222 |
|
| 223 |
// In C++ 20 the not equal exists if equal is found |
| 224 |
#if !(MDSPAN_HAS_CXX_20) |
| 225 |
MDSPAN_TEMPLATE_REQUIRES( |
| 226 |
class OtherExtents, |
| 227 |
/* requires */ (Extents::rank() == OtherExtents::rank()) |
| 228 |
) |
| 229 |
MDSPAN_INLINE_FUNCTION |
| 230 |
friend constexpr bool operator!=(mapping const& lhs, mapping<OtherExtents> const& rhs) noexcept { |
| 231 |
return lhs.extents() != rhs.extents(); |
| 232 |
} |
| 233 |
#endif |
| 234 |
|
| 235 |
// Not really public, but currently needed to implement fully constexpr useable submdspan: |
| 236 |
template<size_t N, class SizeType, size_t ... E, size_t ... Idx> |
| 237 |
MDSPAN_INLINE_FUNCTION |
| 238 |
constexpr index_type __get_stride(MDSPAN_IMPL_STANDARD_NAMESPACE::extents<SizeType, E...>,std::integer_sequence<size_t, Idx...>) const { |
| 239 |
return _MDSPAN_FOLD_TIMES_RIGHT((Idx>N? __extents.template __extent<Idx>():1),1); |
| 240 |
} |
| 241 |
template<size_t N> |
| 242 |
MDSPAN_INLINE_FUNCTION |
| 243 |
constexpr index_type __stride() const noexcept { |
| 244 |
return __get_stride<N>(__extents, std::make_index_sequence<extents_type::rank()>()); |
| 245 |
} |
| 246 |
|
| 247 |
private: |
| 248 |
_MDSPAN_NO_UNIQUE_ADDRESS extents_type __extents{}; |
| 249 |
|
| 250 |
// [mdspan.submdspan.mapping], submdspan mapping specialization |
| 251 |
template<class... SliceSpecifiers> |
| 252 |
MDSPAN_INLINE_FUNCTION |
| 253 |
constexpr auto submdspan_mapping_impl( |
| 254 |
SliceSpecifiers... slices) const; |
| 255 |
|
| 256 |
template<class... SliceSpecifiers> |
| 257 |
MDSPAN_INLINE_FUNCTION |
| 258 |
friend constexpr auto submdspan_mapping( |
| 259 |
const mapping& src, SliceSpecifiers... slices) { |
| 260 |
return src.submdspan_mapping_impl(slices...); |
| 261 |
} |
| 262 |
}; |
| 263 |
|
| 264 |
} // end namespace MDSPAN_IMPL_STANDARD_NAMESPACE |
| 265 |
|