layout_stride.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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_LAYOUT_STRIDE_H
  17. #define _LIBCPP___MDSPAN_LAYOUT_STRIDE_H
  18. #include <__assert>
  19. #include <__config>
  20. #include <__fwd/mdspan.h>
  21. #include <__mdspan/extents.h>
  22. #include <__type_traits/is_constructible.h>
  23. #include <__type_traits/is_convertible.h>
  24. #include <__type_traits/is_nothrow_constructible.h>
  25. #include <__utility/as_const.h>
  26. #include <__utility/integer_sequence.h>
  27. #include <__utility/swap.h>
  28. #include <array>
  29. #include <cinttypes>
  30. #include <cstddef>
  31. #include <limits>
  32. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  33. # pragma GCC system_header
  34. #endif
  35. _LIBCPP_PUSH_MACROS
  36. #include <__undef_macros>
  37. _LIBCPP_BEGIN_NAMESPACE_STD
  38. #if _LIBCPP_STD_VER >= 23
  39. namespace __mdspan_detail {
  40. template <class _Layout, class _Mapping>
  41. constexpr bool __is_mapping_of =
  42. is_same_v<typename _Layout::template mapping<typename _Mapping::extents_type>, _Mapping>;
  43. template <class _Mapping>
  44. concept __layout_mapping_alike = requires {
  45. requires __is_mapping_of<typename _Mapping::layout_type, _Mapping>;
  46. requires __is_extents_v<typename _Mapping::extents_type>;
  47. { _Mapping::is_always_strided() } -> same_as<bool>;
  48. { _Mapping::is_always_exhaustive() } -> same_as<bool>;
  49. { _Mapping::is_always_unique() } -> same_as<bool>;
  50. bool_constant<_Mapping::is_always_strided()>::value;
  51. bool_constant<_Mapping::is_always_exhaustive()>::value;
  52. bool_constant<_Mapping::is_always_unique()>::value;
  53. };
  54. } // namespace __mdspan_detail
  55. template <class _Extents>
  56. class layout_stride::mapping {
  57. public:
  58. static_assert(__mdspan_detail::__is_extents<_Extents>::value,
  59. "layout_stride::mapping template argument must be a specialization of extents.");
  60. using extents_type = _Extents;
  61. using index_type = typename extents_type::index_type;
  62. using size_type = typename extents_type::size_type;
  63. using rank_type = typename extents_type::rank_type;
  64. using layout_type = layout_stride;
  65. private:
  66. static constexpr rank_type __rank_ = extents_type::rank();
  67. // Used for default construction check and mandates
  68. _LIBCPP_HIDE_FROM_ABI static constexpr bool __required_span_size_is_representable(const extents_type& __ext) {
  69. if constexpr (__rank_ == 0)
  70. return true;
  71. index_type __prod = __ext.extent(0);
  72. for (rank_type __r = 1; __r < __rank_; __r++) {
  73. bool __overflowed = __builtin_mul_overflow(__prod, __ext.extent(__r), &__prod);
  74. if (__overflowed)
  75. return false;
  76. }
  77. return true;
  78. }
  79. template <class _OtherIndexType>
  80. _LIBCPP_HIDE_FROM_ABI static constexpr bool
  81. __required_span_size_is_representable(const extents_type& __ext, span<_OtherIndexType, __rank_> __strides) {
  82. if constexpr (__rank_ == 0)
  83. return true;
  84. index_type __size = 1;
  85. for (rank_type __r = 0; __r < __rank_; __r++) {
  86. // We can only check correct conversion of _OtherIndexType if it is an integral
  87. if constexpr (is_integral_v<_OtherIndexType>) {
  88. using _CommonType = common_type_t<index_type, _OtherIndexType>;
  89. if (static_cast<_CommonType>(__strides[__r]) > static_cast<_CommonType>(numeric_limits<index_type>::max()))
  90. return false;
  91. }
  92. if (__ext.extent(__r) == static_cast<index_type>(0))
  93. return true;
  94. index_type __prod = (__ext.extent(__r) - 1);
  95. bool __overflowed_mul = __builtin_mul_overflow(__prod, static_cast<index_type>(__strides[__r]), &__prod);
  96. if (__overflowed_mul)
  97. return false;
  98. bool __overflowed_add = __builtin_add_overflow(__size, __prod, &__size);
  99. if (__overflowed_add)
  100. return false;
  101. }
  102. return true;
  103. }
  104. // compute offset of a strided layout mapping
  105. template <class _StridedMapping>
  106. _LIBCPP_HIDE_FROM_ABI static constexpr index_type __offset(const _StridedMapping& __mapping) {
  107. if constexpr (_StridedMapping::extents_type::rank() == 0) {
  108. return static_cast<index_type>(__mapping());
  109. } else if (__mapping.required_span_size() == static_cast<typename _StridedMapping::index_type>(0)) {
  110. return static_cast<index_type>(0);
  111. } else {
  112. return [&]<size_t... _Pos>(index_sequence<_Pos...>) {
  113. return static_cast<index_type>(__mapping((_Pos ? 0 : 0)...));
  114. }(make_index_sequence<__rank_>());
  115. }
  116. }
  117. // compute the permutation for sorting the stride array
  118. // we never actually sort the stride array
  119. _LIBCPP_HIDE_FROM_ABI constexpr void __bubble_sort_by_strides(array<rank_type, __rank_>& __permute) const {
  120. for (rank_type __i = __rank_ - 1; __i > 0; __i--) {
  121. for (rank_type __r = 0; __r < __i; __r++) {
  122. if (__strides_[__permute[__r]] > __strides_[__permute[__r + 1]]) {
  123. swap(__permute[__r], __permute[__r + 1]);
  124. } else {
  125. // if two strides are the same then one of the associated extents must be 1 or 0
  126. // both could be, but you can't have one larger than 1 come first
  127. if ((__strides_[__permute[__r]] == __strides_[__permute[__r + 1]]) &&
  128. (__extents_.extent(__permute[__r]) > static_cast<index_type>(1)))
  129. swap(__permute[__r], __permute[__r + 1]);
  130. }
  131. }
  132. }
  133. }
  134. static_assert((extents_type::rank_dynamic() > 0) || __required_span_size_is_representable(extents_type()),
  135. "layout_stride::mapping product of static extents must be representable as index_type.");
  136. public:
  137. // [mdspan.layout.stride.cons], constructors
  138. _LIBCPP_HIDE_FROM_ABI constexpr mapping() noexcept : __extents_(extents_type()) {
  139. // Note the nominal precondition is covered by above static assert since
  140. // if rank_dynamic is != 0 required_span_size is zero for default construction
  141. if constexpr (__rank_ > 0) {
  142. index_type __stride = 1;
  143. for (rank_type __r = __rank_ - 1; __r > static_cast<rank_type>(0); __r--) {
  144. __strides_[__r] = __stride;
  145. __stride *= __extents_.extent(__r);
  146. }
  147. __strides_[0] = __stride;
  148. }
  149. }
  150. _LIBCPP_HIDE_FROM_ABI constexpr mapping(const mapping&) noexcept = default;
  151. template <class _OtherIndexType>
  152. requires(is_convertible_v<const _OtherIndexType&, index_type> &&
  153. is_nothrow_constructible_v<index_type, const _OtherIndexType&>)
  154. _LIBCPP_HIDE_FROM_ABI constexpr mapping(const extents_type& __ext, span<_OtherIndexType, __rank_> __strides) noexcept
  155. : __extents_(__ext), __strides_([&]<size_t... _Pos>(index_sequence<_Pos...>) {
  156. return __mdspan_detail::__possibly_empty_array<index_type, __rank_>{
  157. static_cast<index_type>(std::as_const(__strides[_Pos]))...};
  158. }(make_index_sequence<__rank_>())) {
  159. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
  160. ([&]<size_t... _Pos>(index_sequence<_Pos...>) {
  161. // For integrals we can do a pre-conversion check, for other types not
  162. if constexpr (is_integral_v<_OtherIndexType>) {
  163. return ((__strides[_Pos] > static_cast<_OtherIndexType>(0)) && ... && true);
  164. } else {
  165. return ((static_cast<index_type>(__strides[_Pos]) > static_cast<index_type>(0)) && ... && true);
  166. }
  167. }(make_index_sequence<__rank_>())),
  168. "layout_stride::mapping ctor: all strides must be greater than 0");
  169. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
  170. __required_span_size_is_representable(__ext, __strides),
  171. "layout_stride::mapping ctor: required span size is not representable as index_type.");
  172. if constexpr (__rank_ > 1) {
  173. _LIBCPP_ASSERT_UNCATEGORIZED(
  174. ([&]<size_t... _Pos>(index_sequence<_Pos...>) {
  175. // basically sort the dimensions based on strides and extents, sorting is represented in permute array
  176. array<rank_type, __rank_> __permute{_Pos...};
  177. __bubble_sort_by_strides(__permute);
  178. // check that this permutations represents a growing set
  179. for (rank_type __i = 1; __i < __rank_; __i++)
  180. if (static_cast<index_type>(__strides[__permute[__i]]) <
  181. static_cast<index_type>(__strides[__permute[__i - 1]]) * __extents_.extent(__permute[__i - 1]))
  182. return false;
  183. return true;
  184. }(make_index_sequence<__rank_>())),
  185. "layout_stride::mapping ctor: the provided extents and strides lead to a non-unique mapping");
  186. }
  187. }
  188. template <class _OtherIndexType>
  189. requires(is_convertible_v<const _OtherIndexType&, index_type> &&
  190. is_nothrow_constructible_v<index_type, const _OtherIndexType&>)
  191. _LIBCPP_HIDE_FROM_ABI constexpr mapping(const extents_type& __ext,
  192. const array<_OtherIndexType, __rank_>& __strides) noexcept
  193. : mapping(__ext, span(__strides)) {}
  194. template <class _StridedLayoutMapping>
  195. requires(__mdspan_detail::__layout_mapping_alike<_StridedLayoutMapping> &&
  196. is_constructible_v<extents_type, typename _StridedLayoutMapping::extents_type> &&
  197. _StridedLayoutMapping::is_always_unique() && _StridedLayoutMapping::is_always_strided())
  198. _LIBCPP_HIDE_FROM_ABI constexpr explicit(
  199. !(is_convertible_v<typename _StridedLayoutMapping::extents_type, extents_type> &&
  200. (__mdspan_detail::__is_mapping_of<layout_left, _StridedLayoutMapping> ||
  201. __mdspan_detail::__is_mapping_of<layout_right, _StridedLayoutMapping> ||
  202. __mdspan_detail::__is_mapping_of<layout_stride, _StridedLayoutMapping>)))
  203. mapping(const _StridedLayoutMapping& __other) noexcept
  204. : __extents_(__other.extents()), __strides_([&]<size_t... _Pos>(index_sequence<_Pos...>) {
  205. // stride() only compiles for rank > 0
  206. if constexpr (__rank_ > 0) {
  207. return __mdspan_detail::__possibly_empty_array<index_type, __rank_>{
  208. static_cast<index_type>(__other.stride(_Pos))...};
  209. } else {
  210. return __mdspan_detail::__possibly_empty_array<index_type, 0>{};
  211. }
  212. }(make_index_sequence<__rank_>())) {
  213. // stride() only compiles for rank > 0
  214. if constexpr (__rank_ > 0) {
  215. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
  216. ([&]<size_t... _Pos>(index_sequence<_Pos...>) {
  217. return ((static_cast<index_type>(__other.stride(_Pos)) > static_cast<index_type>(0)) && ... && true);
  218. }(make_index_sequence<__rank_>())),
  219. "layout_stride::mapping converting ctor: all strides must be greater than 0");
  220. }
  221. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
  222. __mdspan_detail::__is_representable_as<index_type>(__other.required_span_size()),
  223. "layout_stride::mapping converting ctor: other.required_span_size() must be representable as index_type.");
  224. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(static_cast<index_type>(0) == __offset(__other),
  225. "layout_stride::mapping converting ctor: base offset of mapping must be zero.");
  226. }
  227. _LIBCPP_HIDE_FROM_ABI constexpr mapping& operator=(const mapping&) noexcept = default;
  228. // [mdspan.layout.stride.obs], observers
  229. _LIBCPP_HIDE_FROM_ABI constexpr const extents_type& extents() const noexcept { return __extents_; }
  230. _LIBCPP_HIDE_FROM_ABI constexpr array<index_type, __rank_> strides() const noexcept {
  231. return [&]<size_t... _Pos>(index_sequence<_Pos...>) {
  232. return array<index_type, __rank_>{__strides_[_Pos]...};
  233. }(make_index_sequence<__rank_>());
  234. }
  235. _LIBCPP_HIDE_FROM_ABI constexpr index_type required_span_size() const noexcept {
  236. if constexpr (__rank_ == 0) {
  237. return static_cast<index_type>(1);
  238. } else {
  239. return [&]<size_t... _Pos>(index_sequence<_Pos...>) {
  240. if ((__extents_.extent(_Pos) * ... * 1) == 0)
  241. return static_cast<index_type>(0);
  242. else
  243. return static_cast<index_type>(
  244. static_cast<index_type>(1) +
  245. (((__extents_.extent(_Pos) - static_cast<index_type>(1)) * __strides_[_Pos]) + ... +
  246. static_cast<index_type>(0)));
  247. }(make_index_sequence<__rank_>());
  248. }
  249. }
  250. template <class... _Indices>
  251. requires((sizeof...(_Indices) == __rank_) && (is_convertible_v<_Indices, index_type> && ...) &&
  252. (is_nothrow_constructible_v<index_type, _Indices> && ...))
  253. _LIBCPP_HIDE_FROM_ABI constexpr index_type operator()(_Indices... __idx) const noexcept {
  254. // Mappings are generally meant to be used for accessing allocations and are meant to guarantee to never
  255. // return a value exceeding required_span_size(), which is used to know how large an allocation one needs
  256. // Thus, this is a canonical point in multi-dimensional data structures to make invalid element access checks
  257. // However, mdspan does check this on its own, so for now we avoid double checking in hardened mode
  258. _LIBCPP_ASSERT_UNCATEGORIZED(__mdspan_detail::__is_multidimensional_index_in(__extents_, __idx...),
  259. "layout_stride::mapping: out of bounds indexing");
  260. return [&]<size_t... _Pos>(index_sequence<_Pos...>) {
  261. return ((static_cast<index_type>(__idx) * __strides_[_Pos]) + ... + index_type(0));
  262. }(make_index_sequence<sizeof...(_Indices)>());
  263. }
  264. _LIBCPP_HIDE_FROM_ABI static constexpr bool is_always_unique() noexcept { return true; }
  265. _LIBCPP_HIDE_FROM_ABI static constexpr bool is_always_exhaustive() noexcept { return false; }
  266. _LIBCPP_HIDE_FROM_ABI static constexpr bool is_always_strided() noexcept { return true; }
  267. _LIBCPP_HIDE_FROM_ABI static constexpr bool is_unique() noexcept { return true; }
  268. // The answer of this function is fairly complex in the case where one or more
  269. // extents are zero.
  270. // Technically it is meaningless to query is_exhaustive() in that case, but unfortunately
  271. // the way the standard defines this function, we can't give a simple true or false then.
  272. _LIBCPP_HIDE_FROM_ABI constexpr bool is_exhaustive() const noexcept {
  273. if constexpr (__rank_ == 0)
  274. return true;
  275. else {
  276. index_type __span_size = required_span_size();
  277. if (__span_size == static_cast<index_type>(0)) {
  278. if constexpr (__rank_ == 1)
  279. return __strides_[0] == 1;
  280. else {
  281. rank_type __r_largest = 0;
  282. for (rank_type __r = 1; __r < __rank_; __r++)
  283. if (__strides_[__r] > __strides_[__r_largest])
  284. __r_largest = __r;
  285. for (rank_type __r = 0; __r < __rank_; __r++)
  286. if (__extents_.extent(__r) == 0 && __r != __r_largest)
  287. return false;
  288. return true;
  289. }
  290. } else {
  291. return required_span_size() == [&]<size_t... _Pos>(index_sequence<_Pos...>) {
  292. return (__extents_.extent(_Pos) * ... * static_cast<index_type>(1));
  293. }(make_index_sequence<__rank_>());
  294. }
  295. }
  296. }
  297. _LIBCPP_HIDE_FROM_ABI static constexpr bool is_strided() noexcept { return true; }
  298. // according to the standard layout_stride does not have a constraint on stride(r) for rank>0
  299. // it still has the precondition though
  300. _LIBCPP_HIDE_FROM_ABI constexpr index_type stride(rank_type __r) const noexcept {
  301. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__r < __rank_, "layout_stride::mapping::stride(): invalid rank index");
  302. return __strides_[__r];
  303. }
  304. template <class _OtherMapping>
  305. requires(__mdspan_detail::__layout_mapping_alike<_OtherMapping> &&
  306. (_OtherMapping::extents_type::rank() == __rank_) && _OtherMapping::is_always_strided())
  307. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const mapping& __lhs, const _OtherMapping& __rhs) noexcept {
  308. if (__offset(__rhs))
  309. return false;
  310. if constexpr (__rank_ == 0)
  311. return true;
  312. else {
  313. return __lhs.extents() == __rhs.extents() && [&]<size_t... _Pos>(index_sequence<_Pos...>) {
  314. // avoid warning when comparing signed and unsigner integers and pick the wider of two types
  315. using _CommonType = common_type_t<index_type, typename _OtherMapping::index_type>;
  316. return ((static_cast<_CommonType>(__lhs.stride(_Pos)) == static_cast<_CommonType>(__rhs.stride(_Pos))) && ... &&
  317. true);
  318. }(make_index_sequence<__rank_>());
  319. }
  320. }
  321. private:
  322. _LIBCPP_NO_UNIQUE_ADDRESS extents_type __extents_{};
  323. _LIBCPP_NO_UNIQUE_ADDRESS __mdspan_detail::__possibly_empty_array<index_type, __rank_> __strides_{};
  324. };
  325. #endif // _LIBCPP_STD_VER >= 23
  326. _LIBCPP_END_NAMESPACE_STD
  327. _LIBCPP_POP_MACROS
  328. #endif // _LIBCPP___MDSPAN_LAYOUT_STRIDE_H