mdspan.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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-exception
  7. //
  8. // Kokkos v. 4.0
  9. // Copyright (2022) National Technology & Engineering
  10. // 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. #ifndef _LIBCPP___MDSPAN_MDSPAN_H
  17. #define _LIBCPP___MDSPAN_MDSPAN_H
  18. #include <__assert>
  19. #include <__config>
  20. #include <__fwd/mdspan.h>
  21. #include <__mdspan/default_accessor.h>
  22. #include <__mdspan/extents.h>
  23. #include <__type_traits/extent.h>
  24. #include <__type_traits/is_abstract.h>
  25. #include <__type_traits/is_array.h>
  26. #include <__type_traits/is_constructible.h>
  27. #include <__type_traits/is_convertible.h>
  28. #include <__type_traits/is_default_constructible.h>
  29. #include <__type_traits/is_nothrow_constructible.h>
  30. #include <__type_traits/is_pointer.h>
  31. #include <__type_traits/is_same.h>
  32. #include <__type_traits/rank.h>
  33. #include <__type_traits/remove_all_extents.h>
  34. #include <__type_traits/remove_cv.h>
  35. #include <__type_traits/remove_pointer.h>
  36. #include <__type_traits/remove_reference.h>
  37. #include <__utility/integer_sequence.h>
  38. #include <array>
  39. #include <cinttypes>
  40. #include <cstddef>
  41. #include <limits>
  42. #include <span>
  43. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  44. # pragma GCC system_header
  45. #endif
  46. _LIBCPP_PUSH_MACROS
  47. #include <__undef_macros>
  48. _LIBCPP_BEGIN_NAMESPACE_STD
  49. #if _LIBCPP_STD_VER >= 23
  50. // Helper for lightweight test checking that one did pass a layout policy as LayoutPolicy template argument
  51. namespace __mdspan_detail {
  52. template <class _Layout, class _Extents>
  53. concept __has_invalid_mapping = !requires { typename _Layout::template mapping<_Extents>; };
  54. } // namespace __mdspan_detail
  55. template <class _ElementType,
  56. class _Extents,
  57. class _LayoutPolicy = layout_right,
  58. class _AccessorPolicy = default_accessor<_ElementType> >
  59. class mdspan {
  60. private:
  61. static_assert(__mdspan_detail::__is_extents_v<_Extents>,
  62. "mdspan: Extents template parameter must be a specialization of extents.");
  63. static_assert(!is_array_v<_ElementType>, "mdspan: ElementType template parameter may not be an array type");
  64. static_assert(!is_abstract_v<_ElementType>, "mdspan: ElementType template parameter may not be an abstract class");
  65. static_assert(is_same_v<_ElementType, typename _AccessorPolicy::element_type>,
  66. "mdspan: ElementType template parameter must match AccessorPolicy::element_type");
  67. static_assert(!__mdspan_detail::__has_invalid_mapping<_LayoutPolicy, _Extents>,
  68. "mdspan: LayoutPolicy template parameter is invalid. A common mistake is to pass a layout mapping "
  69. "instead of a layout policy");
  70. public:
  71. using extents_type = _Extents;
  72. using layout_type = _LayoutPolicy;
  73. using accessor_type = _AccessorPolicy;
  74. using mapping_type = typename layout_type::template mapping<extents_type>;
  75. using element_type = _ElementType;
  76. using value_type = remove_cv_t<element_type>;
  77. using index_type = typename extents_type::index_type;
  78. using size_type = typename extents_type::size_type;
  79. using rank_type = typename extents_type::rank_type;
  80. using data_handle_type = typename accessor_type::data_handle_type;
  81. using reference = typename accessor_type::reference;
  82. _LIBCPP_HIDE_FROM_ABI static constexpr rank_type rank() noexcept { return extents_type::rank(); }
  83. _LIBCPP_HIDE_FROM_ABI static constexpr rank_type rank_dynamic() noexcept { return extents_type::rank_dynamic(); }
  84. _LIBCPP_HIDE_FROM_ABI static constexpr size_t static_extent(rank_type __r) noexcept {
  85. return extents_type::static_extent(__r);
  86. }
  87. _LIBCPP_HIDE_FROM_ABI constexpr index_type extent(rank_type __r) const noexcept {
  88. return __map_.extents().extent(__r);
  89. };
  90. public:
  91. //--------------------------------------------------------------------------------
  92. // [mdspan.mdspan.cons], mdspan constructors, assignment, and destructor
  93. _LIBCPP_HIDE_FROM_ABI constexpr mdspan()
  94. requires((extents_type::rank_dynamic() > 0) && is_default_constructible_v<data_handle_type> &&
  95. is_default_constructible_v<mapping_type> && is_default_constructible_v<accessor_type>)
  96. = default;
  97. _LIBCPP_HIDE_FROM_ABI constexpr mdspan(const mdspan&) = default;
  98. _LIBCPP_HIDE_FROM_ABI constexpr mdspan(mdspan&&) = default;
  99. template <class... _OtherIndexTypes>
  100. requires((is_convertible_v<_OtherIndexTypes, index_type> && ...) &&
  101. (is_nothrow_constructible_v<index_type, _OtherIndexTypes> && ...) &&
  102. ((sizeof...(_OtherIndexTypes) == rank()) || (sizeof...(_OtherIndexTypes) == rank_dynamic())) &&
  103. is_constructible_v<mapping_type, extents_type> && is_default_constructible_v<accessor_type>)
  104. _LIBCPP_HIDE_FROM_ABI explicit constexpr mdspan(data_handle_type __p, _OtherIndexTypes... __exts)
  105. : __ptr_(std::move(__p)), __map_(extents_type(static_cast<index_type>(std::move(__exts))...)), __acc_{} {}
  106. template <class _OtherIndexType, size_t _Size>
  107. requires(is_convertible_v<const _OtherIndexType&, index_type> &&
  108. is_nothrow_constructible_v<index_type, const _OtherIndexType&> &&
  109. ((_Size == rank()) || (_Size == rank_dynamic())) && is_constructible_v<mapping_type, extents_type> &&
  110. is_default_constructible_v<accessor_type>)
  111. explicit(_Size != rank_dynamic())
  112. _LIBCPP_HIDE_FROM_ABI constexpr mdspan(data_handle_type __p, const array<_OtherIndexType, _Size>& __exts)
  113. : __ptr_(std::move(__p)), __map_(extents_type(__exts)), __acc_{} {}
  114. template <class _OtherIndexType, size_t _Size>
  115. requires(is_convertible_v<const _OtherIndexType&, index_type> &&
  116. is_nothrow_constructible_v<index_type, const _OtherIndexType&> &&
  117. ((_Size == rank()) || (_Size == rank_dynamic())) && is_constructible_v<mapping_type, extents_type> &&
  118. is_default_constructible_v<accessor_type>)
  119. explicit(_Size != rank_dynamic())
  120. _LIBCPP_HIDE_FROM_ABI constexpr mdspan(data_handle_type __p, span<_OtherIndexType, _Size> __exts)
  121. : __ptr_(std::move(__p)), __map_(extents_type(__exts)), __acc_{} {}
  122. _LIBCPP_HIDE_FROM_ABI constexpr mdspan(data_handle_type __p, const extents_type& __exts)
  123. requires(is_default_constructible_v<accessor_type> && is_constructible_v<mapping_type, const extents_type&>)
  124. : __ptr_(std::move(__p)), __map_(__exts), __acc_{} {}
  125. _LIBCPP_HIDE_FROM_ABI constexpr mdspan(data_handle_type __p, const mapping_type& __m)
  126. requires(is_default_constructible_v<accessor_type>)
  127. : __ptr_(std::move(__p)), __map_(__m), __acc_{} {}
  128. _LIBCPP_HIDE_FROM_ABI constexpr mdspan(data_handle_type __p, const mapping_type& __m, const accessor_type& __a)
  129. : __ptr_(std::move(__p)), __map_(__m), __acc_(__a) {}
  130. template <class _OtherElementType, class _OtherExtents, class _OtherLayoutPolicy, class _OtherAccessor>
  131. requires(is_constructible_v<mapping_type, const typename _OtherLayoutPolicy::template mapping<_OtherExtents>&> &&
  132. is_constructible_v<accessor_type, const _OtherAccessor&>)
  133. explicit(!is_convertible_v<const typename _OtherLayoutPolicy::template mapping<_OtherExtents>&, mapping_type> ||
  134. !is_convertible_v<const _OtherAccessor&, accessor_type>)
  135. _LIBCPP_HIDE_FROM_ABI constexpr mdspan(
  136. const mdspan<_OtherElementType, _OtherExtents, _OtherLayoutPolicy, _OtherAccessor>& __other)
  137. : __ptr_(__other.__ptr_), __map_(__other.__map_), __acc_(__other.__acc_) {
  138. static_assert(is_constructible_v<data_handle_type, const typename _OtherAccessor::data_handle_type&>,
  139. "mdspan: incompatible data_handle_type for mdspan construction");
  140. static_assert(
  141. is_constructible_v<extents_type, _OtherExtents>, "mdspan: incompatible extents for mdspan construction");
  142. // The following precondition is part of the standard, but is unlikely to be triggered.
  143. // The extents constructor checks this and the mapping must be storing the extents, since
  144. // its extents() function returns a const reference to extents_type.
  145. // The only way this can be triggered is if the mapping conversion constructor would for example
  146. // always construct its extents() only from the dynamic extents, instead of from the other extents.
  147. if constexpr (rank() > 0) {
  148. for (size_t __r = 0; __r < rank(); __r++) {
  149. // Not catching this could lead to out of bounds errors later
  150. // e.g. mdspan<int, dextents<char,1>, non_checking_layout> m =
  151. // mdspan<int, dextents<unsigned, 1>, non_checking_layout>(ptr, 200); leads to an extent of -56 on m
  152. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
  153. (static_extent(__r) == dynamic_extent) ||
  154. (static_cast<index_type>(__other.extent(__r)) == static_cast<index_type>(static_extent(__r))),
  155. "mdspan: conversion mismatch of source dynamic extents with static extents");
  156. }
  157. }
  158. }
  159. _LIBCPP_HIDE_FROM_ABI constexpr mdspan& operator=(const mdspan&) = default;
  160. _LIBCPP_HIDE_FROM_ABI constexpr mdspan& operator=(mdspan&&) = default;
  161. //--------------------------------------------------------------------------------
  162. // [mdspan.mdspan.members], members
  163. template <class... _OtherIndexTypes>
  164. requires((is_convertible_v<_OtherIndexTypes, index_type> && ...) &&
  165. (is_nothrow_constructible_v<index_type, _OtherIndexTypes> && ...) &&
  166. (sizeof...(_OtherIndexTypes) == rank()))
  167. _LIBCPP_HIDE_FROM_ABI constexpr reference operator[](_OtherIndexTypes... __indices) const {
  168. // Note the standard layouts would also check this, but user provided ones may not, so we
  169. // check the precondition here
  170. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__mdspan_detail::__is_multidimensional_index_in(extents(), __indices...),
  171. "mdspan: operator[] out of bounds access");
  172. return __acc_.access(__ptr_, __map_(static_cast<index_type>(std::move(__indices))...));
  173. }
  174. template <class _OtherIndexType>
  175. requires(is_convertible_v<const _OtherIndexType&, index_type> &&
  176. is_nothrow_constructible_v<index_type, const _OtherIndexType&>)
  177. _LIBCPP_HIDE_FROM_ABI constexpr reference operator[](const array< _OtherIndexType, rank()>& __indices) const {
  178. return __acc_.access(__ptr_, [&]<size_t... _Idxs>(index_sequence<_Idxs...>) {
  179. return __map_(__indices[_Idxs]...);
  180. }(make_index_sequence<rank()>()));
  181. }
  182. template <class _OtherIndexType>
  183. requires(is_convertible_v<const _OtherIndexType&, index_type> &&
  184. is_nothrow_constructible_v<index_type, const _OtherIndexType&>)
  185. _LIBCPP_HIDE_FROM_ABI constexpr reference operator[](span<_OtherIndexType, rank()> __indices) const {
  186. return __acc_.access(__ptr_, [&]<size_t... _Idxs>(index_sequence<_Idxs...>) {
  187. return __map_(__indices[_Idxs]...);
  188. }(make_index_sequence<rank()>()));
  189. }
  190. _LIBCPP_HIDE_FROM_ABI constexpr size_type size() const noexcept {
  191. // Could leave this as only checked in debug mode: semantically size() is never
  192. // guaranteed to be related to any accessible range
  193. _LIBCPP_ASSERT_UNCATEGORIZED(
  194. false == ([&]<size_t... _Idxs>(index_sequence<_Idxs...>) {
  195. size_type __prod = 1;
  196. return (__builtin_mul_overflow(__prod, extent(_Idxs), &__prod) || ... || false);
  197. }(make_index_sequence<rank()>())),
  198. "mdspan: size() is not representable as size_type");
  199. return [&]<size_t... _Idxs>(index_sequence<_Idxs...>) {
  200. return ((static_cast<size_type>(__map_.extents().extent(_Idxs))) * ... * size_type(1));
  201. }(make_index_sequence<rank()>());
  202. }
  203. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool empty() const noexcept {
  204. return [&]<size_t... _Idxs>(index_sequence<_Idxs...>) {
  205. return (rank() > 0) && ((__map_.extents().extent(_Idxs) == index_type(0)) || ... || false);
  206. }(make_index_sequence<rank()>());
  207. }
  208. _LIBCPP_HIDE_FROM_ABI friend constexpr void swap(mdspan& __x, mdspan& __y) noexcept {
  209. swap(__x.__ptr_, __y.__ptr_);
  210. swap(__x.__map_, __y.__map_);
  211. swap(__x.__acc_, __y.__acc_);
  212. }
  213. _LIBCPP_HIDE_FROM_ABI constexpr const extents_type& extents() const noexcept { return __map_.extents(); };
  214. _LIBCPP_HIDE_FROM_ABI constexpr const data_handle_type& data_handle() const noexcept { return __ptr_; };
  215. _LIBCPP_HIDE_FROM_ABI constexpr const mapping_type& mapping() const noexcept { return __map_; };
  216. _LIBCPP_HIDE_FROM_ABI constexpr const accessor_type& accessor() const noexcept { return __acc_; };
  217. _LIBCPP_HIDE_FROM_ABI static constexpr bool is_always_unique() { return mapping_type::is_always_unique(); };
  218. _LIBCPP_HIDE_FROM_ABI static constexpr bool is_always_exhaustive() { return mapping_type::is_always_exhaustive(); };
  219. _LIBCPP_HIDE_FROM_ABI static constexpr bool is_always_strided() { return mapping_type::is_always_strided(); };
  220. _LIBCPP_HIDE_FROM_ABI constexpr bool is_unique() const { return __map_.is_unique(); };
  221. _LIBCPP_HIDE_FROM_ABI constexpr bool is_exhaustive() const { return __map_.is_exhaustive(); };
  222. _LIBCPP_HIDE_FROM_ABI constexpr bool is_strided() const { return __map_.is_strided(); };
  223. _LIBCPP_HIDE_FROM_ABI constexpr index_type stride(rank_type __r) const { return __map_.stride(__r); };
  224. private:
  225. _LIBCPP_NO_UNIQUE_ADDRESS data_handle_type __ptr_{};
  226. _LIBCPP_NO_UNIQUE_ADDRESS mapping_type __map_{};
  227. _LIBCPP_NO_UNIQUE_ADDRESS accessor_type __acc_{};
  228. template <class, class, class, class>
  229. friend class mdspan;
  230. };
  231. template <class _ElementType, class... _OtherIndexTypes>
  232. requires((is_convertible_v<_OtherIndexTypes, size_t> && ...) && (sizeof...(_OtherIndexTypes) > 0))
  233. explicit mdspan(_ElementType*, _OtherIndexTypes...)
  234. -> mdspan<_ElementType, dextents<size_t, sizeof...(_OtherIndexTypes)>>;
  235. template <class _Pointer>
  236. requires(is_pointer_v<remove_reference_t<_Pointer>>)
  237. mdspan(_Pointer&&) -> mdspan<remove_pointer_t<remove_reference_t<_Pointer>>, extents<size_t>>;
  238. template <class _CArray>
  239. requires(is_array_v<_CArray> && (rank_v<_CArray> == 1))
  240. mdspan(_CArray&) -> mdspan<remove_all_extents_t<_CArray>, extents<size_t, extent_v<_CArray, 0>>>;
  241. template <class _ElementType, class _OtherIndexType, size_t _Size>
  242. mdspan(_ElementType*, const array<_OtherIndexType, _Size>&) -> mdspan<_ElementType, dextents<size_t, _Size>>;
  243. template <class _ElementType, class _OtherIndexType, size_t _Size>
  244. mdspan(_ElementType*, span<_OtherIndexType, _Size>) -> mdspan<_ElementType, dextents<size_t, _Size>>;
  245. // This one is necessary because all the constructors take `data_handle_type`s, not
  246. // `_ElementType*`s, and `data_handle_type` is taken from `accessor_type::data_handle_type`, which
  247. // seems to throw off automatic deduction guides.
  248. template <class _ElementType, class _OtherIndexType, size_t... _ExtentsPack>
  249. mdspan(_ElementType*, const extents<_OtherIndexType, _ExtentsPack...>&)
  250. -> mdspan<_ElementType, extents<_OtherIndexType, _ExtentsPack...>>;
  251. template <class _ElementType, class _MappingType>
  252. mdspan(_ElementType*, const _MappingType&)
  253. -> mdspan<_ElementType, typename _MappingType::extents_type, typename _MappingType::layout_type>;
  254. template <class _MappingType, class _AccessorType>
  255. mdspan(const typename _AccessorType::data_handle_type, const _MappingType&, const _AccessorType&)
  256. -> mdspan<typename _AccessorType::element_type,
  257. typename _MappingType::extents_type,
  258. typename _MappingType::layout_type,
  259. _AccessorType>;
  260. #endif // _LIBCPP_STD_VER >= 23
  261. _LIBCPP_END_NAMESPACE_STD
  262. _LIBCPP_POP_MACROS
  263. #endif // _LIBCPP___MDSPAN_MDSPAN_H