mdspan.h 16 KB

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