326 lines · c
1// -*- C++ -*-2//===----------------------------------------------------------------------===//3//4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.5// See https://llvm.org/LICENSE.txt for license information.6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception7//8// Kokkos v. 4.09// Copyright (2022) National Technology & Engineering10// Solutions of Sandia, LLC (NTESS).11//12// Under the terms of Contract DE-NA0003525 with NTESS,13// the U.S. Government retains certain rights in this software.14//15//===---------------------------------------------------------------------===//16 17#ifndef _LIBCPP___MDSPAN_MDSPAN_H18#define _LIBCPP___MDSPAN_MDSPAN_H19 20#include <__assert>21#include <__config>22#include <__fwd/mdspan.h>23#include <__mdspan/aligned_accessor.h>24#include <__mdspan/default_accessor.h>25#include <__mdspan/extents.h>26#include <__memory/addressof.h>27#include <__type_traits/extent.h>28#include <__type_traits/is_abstract.h>29#include <__type_traits/is_array.h>30#include <__type_traits/is_constructible.h>31#include <__type_traits/is_convertible.h>32#include <__type_traits/is_nothrow_constructible.h>33#include <__type_traits/is_pointer.h>34#include <__type_traits/is_same.h>35#include <__type_traits/rank.h>36#include <__type_traits/remove_all_extents.h>37#include <__type_traits/remove_cv.h>38#include <__type_traits/remove_pointer.h>39#include <__type_traits/remove_reference.h>40#include <__utility/integer_sequence.h>41#include <array>42#include <span>43 44#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)45# pragma GCC system_header46#endif47 48_LIBCPP_PUSH_MACROS49#include <__undef_macros>50 51_LIBCPP_BEGIN_NAMESPACE_STD52 53#if _LIBCPP_STD_VER >= 2354 55// Helper for lightweight test checking that one did pass a layout policy as LayoutPolicy template argument56namespace __mdspan_detail {57template <class _Layout, class _Extents>58concept __has_invalid_mapping = !requires { typename _Layout::template mapping<_Extents>; };59} // namespace __mdspan_detail60 61template <class _ElementType,62 class _Extents,63 class _LayoutPolicy = layout_right,64 class _AccessorPolicy = default_accessor<_ElementType> >65class mdspan {66private:67 static_assert(__mdspan_detail::__is_extents_v<_Extents>,68 "mdspan: Extents template parameter must be a specialization of extents.");69 static_assert(!is_array_v<_ElementType>, "mdspan: ElementType template parameter may not be an array type");70 static_assert(!is_abstract_v<_ElementType>, "mdspan: ElementType template parameter may not be an abstract class");71 static_assert(is_same_v<_ElementType, typename _AccessorPolicy::element_type>,72 "mdspan: ElementType template parameter must match AccessorPolicy::element_type");73 static_assert(!__mdspan_detail::__has_invalid_mapping<_LayoutPolicy, _Extents>,74 "mdspan: LayoutPolicy template parameter is invalid. A common mistake is to pass a layout mapping "75 "instead of a layout policy");76 77public:78 using extents_type = _Extents;79 using layout_type = _LayoutPolicy;80 using accessor_type = _AccessorPolicy;81 using mapping_type = typename layout_type::template mapping<extents_type>;82 using element_type = _ElementType;83 using value_type = remove_cv_t<element_type>;84 using index_type = typename extents_type::index_type;85 using size_type = typename extents_type::size_type;86 using rank_type = typename extents_type::rank_type;87 using data_handle_type = typename accessor_type::data_handle_type;88 using reference = typename accessor_type::reference;89 90 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static constexpr rank_type rank() noexcept { return extents_type::rank(); }91 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static constexpr rank_type rank_dynamic() noexcept {92 return extents_type::rank_dynamic();93 }94 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static constexpr size_t static_extent(rank_type __r) noexcept {95 return extents_type::static_extent(__r);96 }97 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr index_type extent(rank_type __r) const noexcept {98 return __map_.extents().extent(__r);99 };100 101public:102 //--------------------------------------------------------------------------------103 // [mdspan.mdspan.cons], mdspan constructors, assignment, and destructor104 105 _LIBCPP_HIDE_FROM_ABI constexpr mdspan()106 requires((extents_type::rank_dynamic() > 0) && is_default_constructible_v<data_handle_type> &&107 is_default_constructible_v<mapping_type> && is_default_constructible_v<accessor_type>)108 = default;109 _LIBCPP_HIDE_FROM_ABI constexpr mdspan(const mdspan&) = default;110 _LIBCPP_HIDE_FROM_ABI constexpr mdspan(mdspan&&) = default;111 112 template <class... _OtherIndexTypes>113 requires((is_convertible_v<_OtherIndexTypes, index_type> && ...) &&114 (is_nothrow_constructible_v<index_type, _OtherIndexTypes> && ...) &&115 ((sizeof...(_OtherIndexTypes) == rank()) || (sizeof...(_OtherIndexTypes) == rank_dynamic())) &&116 is_constructible_v<mapping_type, extents_type> && is_default_constructible_v<accessor_type>)117 _LIBCPP_HIDE_FROM_ABI explicit constexpr mdspan(data_handle_type __p, _OtherIndexTypes... __exts)118 : __ptr_(std::move(__p)), __map_(extents_type(static_cast<index_type>(std::move(__exts))...)), __acc_{} {}119 120 template <class _OtherIndexType, size_t _Size>121 requires(is_convertible_v<const _OtherIndexType&, index_type> &&122 is_nothrow_constructible_v<index_type, const _OtherIndexType&> &&123 ((_Size == rank()) || (_Size == rank_dynamic())) && is_constructible_v<mapping_type, extents_type> &&124 is_default_constructible_v<accessor_type>)125 explicit(_Size != rank_dynamic())126 _LIBCPP_HIDE_FROM_ABI constexpr mdspan(data_handle_type __p, const array<_OtherIndexType, _Size>& __exts)127 : __ptr_(std::move(__p)), __map_(extents_type(__exts)), __acc_{} {}128 129 template <class _OtherIndexType, size_t _Size>130 requires(is_convertible_v<const _OtherIndexType&, index_type> &&131 is_nothrow_constructible_v<index_type, const _OtherIndexType&> &&132 ((_Size == rank()) || (_Size == rank_dynamic())) && is_constructible_v<mapping_type, extents_type> &&133 is_default_constructible_v<accessor_type>)134 explicit(_Size != rank_dynamic())135 _LIBCPP_HIDE_FROM_ABI constexpr mdspan(data_handle_type __p, span<_OtherIndexType, _Size> __exts)136 : __ptr_(std::move(__p)), __map_(extents_type(__exts)), __acc_{} {}137 138 _LIBCPP_HIDE_FROM_ABI constexpr mdspan(data_handle_type __p, const extents_type& __exts)139 requires(is_default_constructible_v<accessor_type> && is_constructible_v<mapping_type, const extents_type&>)140 : __ptr_(std::move(__p)), __map_(__exts), __acc_{} {}141 142 _LIBCPP_HIDE_FROM_ABI constexpr mdspan(data_handle_type __p, const mapping_type& __m)143 requires(is_default_constructible_v<accessor_type>)144 : __ptr_(std::move(__p)), __map_(__m), __acc_{} {}145 146 _LIBCPP_HIDE_FROM_ABI constexpr mdspan(data_handle_type __p, const mapping_type& __m, const accessor_type& __a)147 : __ptr_(std::move(__p)), __map_(__m), __acc_(__a) {}148 149 template <class _OtherElementType, class _OtherExtents, class _OtherLayoutPolicy, class _OtherAccessor>150 requires(is_constructible_v<mapping_type, const typename _OtherLayoutPolicy::template mapping<_OtherExtents>&> &&151 is_constructible_v<accessor_type, const _OtherAccessor&>)152 explicit(!is_convertible_v<const typename _OtherLayoutPolicy::template mapping<_OtherExtents>&, mapping_type> ||153 !is_convertible_v<const _OtherAccessor&, accessor_type>)154 _LIBCPP_HIDE_FROM_ABI constexpr mdspan(155 const mdspan<_OtherElementType, _OtherExtents, _OtherLayoutPolicy, _OtherAccessor>& __other)156 : __ptr_(__other.__ptr_), __map_(__other.__map_), __acc_(__other.__acc_) {157 static_assert(is_constructible_v<data_handle_type, const typename _OtherAccessor::data_handle_type&>,158 "mdspan: incompatible data_handle_type for mdspan construction");159 static_assert(160 is_constructible_v<extents_type, _OtherExtents>, "mdspan: incompatible extents for mdspan construction");161 162 // The following precondition is part of the standard, but is unlikely to be triggered.163 // The extents constructor checks this and the mapping must be storing the extents, since164 // its extents() function returns a const reference to extents_type.165 // The only way this can be triggered is if the mapping conversion constructor would for example166 // always construct its extents() only from the dynamic extents, instead of from the other extents.167 if constexpr (rank() > 0) {168 for (size_t __r = 0; __r < rank(); __r++) {169 // Not catching this could lead to out of bounds errors later170 // e.g. mdspan<int, dextents<char,1>, non_checking_layout> m =171 // mdspan<int, dextents<unsigned, 1>, non_checking_layout>(ptr, 200); leads to an extent of -56 on m172 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(173 (static_extent(__r) == dynamic_extent) ||174 (static_cast<index_type>(__other.extent(__r)) == static_cast<index_type>(static_extent(__r))),175 "mdspan: conversion mismatch of source dynamic extents with static extents");176 }177 }178 }179 180 _LIBCPP_HIDE_FROM_ABI constexpr mdspan& operator=(const mdspan&) = default;181 _LIBCPP_HIDE_FROM_ABI constexpr mdspan& operator=(mdspan&&) = default;182 183 //--------------------------------------------------------------------------------184 // [mdspan.mdspan.members], members185 186 template <class... _OtherIndexTypes>187 requires((is_convertible_v<_OtherIndexTypes, index_type> && ...) &&188 (is_nothrow_constructible_v<index_type, _OtherIndexTypes> && ...) &&189 (sizeof...(_OtherIndexTypes) == rank()))190 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr reference operator[](_OtherIndexTypes... __indices) const {191 // Note the standard layouts would also check this, but user provided ones may not, so we192 // check the precondition here193 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__mdspan_detail::__is_multidimensional_index_in(extents(), __indices...),194 "mdspan: operator[] out of bounds access");195 return __acc_.access(__ptr_, __map_(static_cast<index_type>(std::move(__indices))...));196 }197 198 template <class _OtherIndexType>199 requires(is_convertible_v<const _OtherIndexType&, index_type> &&200 is_nothrow_constructible_v<index_type, const _OtherIndexType&>)201 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr reference202 operator[](const array< _OtherIndexType, rank()>& __indices) const {203 return __acc_.access(__ptr_, [&]<size_t... _Idxs>(index_sequence<_Idxs...>) {204 return __map_(__indices[_Idxs]...);205 }(make_index_sequence<rank()>()));206 }207 208 template <class _OtherIndexType>209 requires(is_convertible_v<const _OtherIndexType&, index_type> &&210 is_nothrow_constructible_v<index_type, const _OtherIndexType&>)211 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr reference operator[](span<_OtherIndexType, rank()> __indices) const {212 return __acc_.access(__ptr_, [&]<size_t... _Idxs>(index_sequence<_Idxs...>) {213 return __map_(__indices[_Idxs]...);214 }(make_index_sequence<rank()>()));215 }216 217 _LIBCPP_HIDE_FROM_ABI constexpr size_type size() const noexcept {218 // Could leave this as only checked in debug mode: semantically size() is never219 // guaranteed to be related to any accessible range220 _LIBCPP_ASSERT_UNCATEGORIZED(221 false == ([&]<size_t... _Idxs>(index_sequence<_Idxs...>) {222 size_type __prod = 1;223 return (__builtin_mul_overflow(__prod, extent(_Idxs), std::addressof(__prod)) || ... || false);224 }(make_index_sequence<rank()>())),225 "mdspan: size() is not representable as size_type");226 return [&]<size_t... _Idxs>(index_sequence<_Idxs...>) {227 return ((static_cast<size_type>(__map_.extents().extent(_Idxs))) * ... * size_type(1));228 }(make_index_sequence<rank()>());229 }230 231 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool empty() const noexcept {232 return [&]<size_t... _Idxs>(index_sequence<_Idxs...>) {233 return (rank() > 0) && ((__map_.extents().extent(_Idxs) == index_type(0)) || ... || false);234 }(make_index_sequence<rank()>());235 }236 237 _LIBCPP_HIDE_FROM_ABI friend constexpr void swap(mdspan& __x, mdspan& __y) noexcept {238 swap(__x.__ptr_, __y.__ptr_);239 swap(__x.__map_, __y.__map_);240 swap(__x.__acc_, __y.__acc_);241 }242 243 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const extents_type& extents() const noexcept {244 return __map_.extents();245 };246 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const data_handle_type& data_handle() const noexcept { return __ptr_; };247 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const mapping_type& mapping() const noexcept { return __map_; };248 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const accessor_type& accessor() const noexcept { return __acc_; };249 250 // per LWG-4021 "mdspan::is_always_meow() should be noexcept"251 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static constexpr bool is_always_unique() noexcept {252 return mapping_type::is_always_unique();253 };254 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static constexpr bool is_always_exhaustive() noexcept {255 return mapping_type::is_always_exhaustive();256 };257 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static constexpr bool is_always_strided() noexcept {258 return mapping_type::is_always_strided();259 };260 261 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool is_unique() const { return __map_.is_unique(); };262 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool is_exhaustive() const { return __map_.is_exhaustive(); };263 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool is_strided() const { return __map_.is_strided(); };264 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr index_type stride(rank_type __r) const { return __map_.stride(__r); };265 266private:267 _LIBCPP_NO_UNIQUE_ADDRESS data_handle_type __ptr_{};268 _LIBCPP_NO_UNIQUE_ADDRESS mapping_type __map_{};269 _LIBCPP_NO_UNIQUE_ADDRESS accessor_type __acc_{};270 271 template <class, class, class, class>272 friend class mdspan;273};274 275# if _LIBCPP_STD_VER >= 26276template <class _ElementType, class... _OtherIndexTypes>277 requires((is_convertible_v<_OtherIndexTypes, size_t> && ...) && (sizeof...(_OtherIndexTypes) > 0))278explicit mdspan(_ElementType*, _OtherIndexTypes...)279 -> mdspan<_ElementType, extents<size_t, __maybe_static_ext<_OtherIndexTypes>...>>;280# else281template <class _ElementType, class... _OtherIndexTypes>282 requires((is_convertible_v<_OtherIndexTypes, size_t> && ...) && (sizeof...(_OtherIndexTypes) > 0))283explicit mdspan(_ElementType*, _OtherIndexTypes...)284 -> mdspan<_ElementType, dextents<size_t, sizeof...(_OtherIndexTypes)>>;285# endif286 287template <class _Pointer>288 requires(is_pointer_v<remove_reference_t<_Pointer>>)289mdspan(_Pointer&&) -> mdspan<remove_pointer_t<remove_reference_t<_Pointer>>, extents<size_t>>;290 291template <class _CArray>292 requires(is_array_v<_CArray> && (rank_v<_CArray> == 1))293mdspan(_CArray&) -> mdspan<remove_all_extents_t<_CArray>, extents<size_t, extent_v<_CArray, 0>>>;294 295template <class _ElementType, class _OtherIndexType, size_t _Size>296mdspan(_ElementType*, const array<_OtherIndexType, _Size>&) -> mdspan<_ElementType, dextents<size_t, _Size>>;297 298template <class _ElementType, class _OtherIndexType, size_t _Size>299mdspan(_ElementType*, span<_OtherIndexType, _Size>) -> mdspan<_ElementType, dextents<size_t, _Size>>;300 301// This one is necessary because all the constructors take `data_handle_type`s, not302// `_ElementType*`s, and `data_handle_type` is taken from `accessor_type::data_handle_type`, which303// seems to throw off automatic deduction guides.304template <class _ElementType, class _OtherIndexType, size_t... _ExtentsPack>305mdspan(_ElementType*, const extents<_OtherIndexType, _ExtentsPack...>&)306 -> mdspan<_ElementType, extents<_OtherIndexType, _ExtentsPack...>>;307 308template <class _ElementType, class _MappingType>309mdspan(_ElementType*, const _MappingType&)310 -> mdspan<_ElementType, typename _MappingType::extents_type, typename _MappingType::layout_type>;311 312template <class _MappingType, class _AccessorType>313mdspan(const typename _AccessorType::data_handle_type, const _MappingType&, const _AccessorType&)314 -> mdspan<typename _AccessorType::element_type,315 typename _MappingType::extents_type,316 typename _MappingType::layout_type,317 _AccessorType>;318 319#endif // _LIBCPP_STD_VER >= 23320 321_LIBCPP_END_NAMESPACE_STD322 323_LIBCPP_POP_MACROS324 325#endif // _LIBCPP___MDSPAN_MDSPAN_H326