| 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 |
// |
| 13 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 14 |
// |
| 15 |
//@HEADER |
| 16 |
#pragma once |
| 17 |
|
| 18 |
#include <cassert> |
| 19 |
#include "../__p0009_bits/dynamic_extent.hpp" |
| 20 |
#include "../__p0009_bits/utility.hpp" |
| 21 |
|
| 22 |
namespace MDSPAN_IMPL_STANDARD_NAMESPACE { |
| 23 |
namespace MDSPAN_IMPL_PROPOSED_NAMESPACE { |
| 24 |
|
| 25 |
template <size_t padding_value = dynamic_extent> |
| 26 |
struct layout_left_padded { |
| 27 |
template <class _Extents> |
| 28 |
class mapping; |
| 29 |
}; |
| 30 |
|
| 31 |
template <size_t padding_value = dynamic_extent> |
| 32 |
struct layout_right_padded { |
| 33 |
template <class _Extents> |
| 34 |
class mapping; |
| 35 |
}; |
| 36 |
|
| 37 |
namespace detail { |
| 38 |
// The layout_padded_constants structs are only useful if rank > 1, otherwise they may wrap |
| 39 |
template <class _Layout, class _ExtentsType> |
| 40 |
struct layout_padded_constants; |
| 41 |
|
| 42 |
template <class _ExtentsType, size_t _PaddingStride> |
| 43 |
struct layout_padded_constants<layout_left_padded<_PaddingStride>, _ExtentsType> |
| 44 |
{ |
| 45 |
using rank_type = typename _ExtentsType::rank_type; |
| 46 |
static constexpr rank_type padded_stride_idx = 1; |
| 47 |
static constexpr rank_type extent_to_pad_idx = 0; |
| 48 |
}; |
| 49 |
|
| 50 |
template <class _ExtentsType, size_t _PaddingStride> |
| 51 |
struct layout_padded_constants<layout_right_padded<_PaddingStride>, _ExtentsType> |
| 52 |
{ |
| 53 |
using rank_type = typename _ExtentsType::rank_type; |
| 54 |
static constexpr rank_type padded_stride_idx = _ExtentsType::rank() - 2; |
| 55 |
static constexpr rank_type extent_to_pad_idx = _ExtentsType::rank() - 1; |
| 56 |
}; |
| 57 |
|
| 58 |
template <class _Layout> |
| 59 |
struct is_layout_left_padded : std::false_type {}; |
| 60 |
|
| 61 |
template <size_t _PaddingStride> |
| 62 |
struct is_layout_left_padded<layout_left_padded<_PaddingStride>> : std::true_type {}; |
| 63 |
|
| 64 |
template <class _Mapping, class _Enabled = void> |
| 65 |
struct is_layout_left_padded_mapping : std::false_type {}; |
| 66 |
|
| 67 |
template <class _Mapping> |
| 68 |
struct is_layout_left_padded_mapping<_Mapping, |
| 69 |
std::enable_if_t<std::is_same<_Mapping, typename layout_left_padded<_Mapping::padding_value>::template mapping<typename _Mapping::extents_type>>::value>> |
| 70 |
: std::true_type {}; |
| 71 |
|
| 72 |
template <class _Layout> |
| 73 |
struct is_layout_right_padded : std::false_type {}; |
| 74 |
|
| 75 |
template <size_t _PaddingStride> |
| 76 |
struct is_layout_right_padded<layout_right_padded<_PaddingStride>> : std::true_type {}; |
| 77 |
|
| 78 |
template <class _Mapping, class _Enabled = void> |
| 79 |
struct is_layout_right_padded_mapping : std::false_type {}; |
| 80 |
|
| 81 |
template <class _Mapping> |
| 82 |
struct is_layout_right_padded_mapping<_Mapping, |
| 83 |
std::enable_if_t<std::is_same<_Mapping, typename layout_right_padded<_Mapping::padding_value>::template mapping<typename _Mapping::extents_type>>::value>> |
| 84 |
: std::true_type {}; |
| 85 |
|
| 86 |
|
| 87 |
template <class _LayoutExtentsType, class _PaddedLayoutMappingType> |
| 88 |
MDSPAN_INLINE_FUNCTION |
| 89 |
constexpr void check_padded_layout_converting_constructor_mandates(MDSPAN_IMPL_STANDARD_NAMESPACE::detail::with_rank<0>) {} |
| 90 |
|
| 91 |
template <class _LayoutExtentsType, class _PaddedLayoutMappingType> |
| 92 |
MDSPAN_INLINE_FUNCTION |
| 93 |
constexpr void check_padded_layout_converting_constructor_mandates(MDSPAN_IMPL_STANDARD_NAMESPACE::detail::with_rank<1>) {} |
| 94 |
|
| 95 |
template <class _LayoutExtentsType, class _PaddedLayoutMappingType, std::size_t N> |
| 96 |
MDSPAN_INLINE_FUNCTION |
| 97 |
constexpr void check_padded_layout_converting_constructor_mandates(MDSPAN_IMPL_STANDARD_NAMESPACE::detail::with_rank<N>) |
| 98 |
{ |
| 99 |
using extents_type = typename _PaddedLayoutMappingType::extents_type; |
| 100 |
constexpr auto padding_value = _PaddedLayoutMappingType::padding_value; |
| 101 |
constexpr auto idx = layout_padded_constants<typename _PaddedLayoutMappingType::layout_type, _LayoutExtentsType >::extent_to_pad_idx; |
| 102 |
|
| 103 |
constexpr auto statically_determinable = |
| 104 |
(_LayoutExtentsType::static_extent(idx) != dynamic_extent) && |
| 105 |
(extents_type::static_extent(idx) != dynamic_extent) && |
| 106 |
(padding_value != dynamic_extent); |
| 107 |
|
| 108 |
static_assert(!statically_determinable || |
| 109 |
(padding_value == 0 |
| 110 |
? _LayoutExtentsType::static_extent(idx) == 0 |
| 111 |
: _LayoutExtentsType::static_extent(idx) % padding_value == 0), |
| 112 |
""); |
| 113 |
} |
| 114 |
|
| 115 |
template <typename _ExtentsType, typename _OtherMapping> |
| 116 |
MDSPAN_INLINE_FUNCTION |
| 117 |
constexpr void check_padded_layout_converting_constructor_preconditions(MDSPAN_IMPL_STANDARD_NAMESPACE::detail::with_rank<0>, |
| 118 |
const _OtherMapping&) {} |
| 119 |
template <typename _ExtentsType, typename _OtherMapping> |
| 120 |
MDSPAN_INLINE_FUNCTION |
| 121 |
constexpr void check_padded_layout_converting_constructor_preconditions(MDSPAN_IMPL_STANDARD_NAMESPACE::detail::with_rank<1>, |
| 122 |
const _OtherMapping&) {} |
| 123 |
template <typename _ExtentsType, typename _OtherMapping, std::size_t N> |
| 124 |
MDSPAN_INLINE_FUNCTION |
| 125 |
constexpr void check_padded_layout_converting_constructor_preconditions(MDSPAN_IMPL_STANDARD_NAMESPACE::detail::with_rank<N>, |
| 126 |
const _OtherMapping &other_mapping) { |
| 127 |
constexpr auto padded_stride_idx = |
| 128 |
layout_padded_constants<typename _OtherMapping::layout_type, |
| 129 |
_ExtentsType>::padded_stride_idx; |
| 130 |
constexpr auto extent_to_pad_idx = layout_padded_constants<typename _OtherMapping::layout_type, _ExtentsType>::extent_to_pad_idx; |
| 131 |
MDSPAN_IMPL_PRECONDITION(other_mapping.stride(padded_stride_idx) == other_mapping.extents().extent(extent_to_pad_idx)); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
} |
| 136 |
} |
| 137 |
} |