|
| 1 | +//==----- group_interface.hpp --- sycl_khr_group_interface extension -------==// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +#pragma once |
| 9 | + |
| 10 | +#include <sycl/ext/oneapi/free_function_queries.hpp> |
| 11 | +#include <sycl/id.hpp> |
| 12 | +#include <sycl/range.hpp> |
| 13 | + |
| 14 | +#if __cplusplus >= 202302L && defined(__has_include) |
| 15 | +#if __has_include(<mdspan>) |
| 16 | +#include <mdspan> |
| 17 | +#endif |
| 18 | +#endif |
| 19 | + |
| 20 | +namespace sycl { |
| 21 | +inline namespace _V1 { |
| 22 | + |
| 23 | +namespace khr { |
| 24 | + |
| 25 | +// Forward declarations for friend functions and traits. |
| 26 | +template <int Dimensions> class work_group; |
| 27 | +template class sub_group; |
| 28 | +template <typename ParentGroup> class work_item; |
| 29 | +template <typename ParentGroup> work_item<ParentGroup> get_item(ParentGroup); |
| 30 | + |
| 31 | +} // namespace khr |
| 32 | + |
| 33 | +namespace detail { |
| 34 | +#if defined(__cpp_lib_mdspan) |
| 35 | +template <typename IndexType, int Dimensions> struct single_extents; |
| 36 | + |
| 37 | +template <typename IndexType> single_extents<1> { |
| 38 | + using type = std::extents<IndexType, 1>; |
| 39 | +} |
| 40 | + |
| 41 | +template <typename IndexType> single_extents<2> { |
| 42 | + using type = std::extents<IndexType, 1, 1>; |
| 43 | +} |
| 44 | + |
| 45 | +template <typename IndexType> single_extents<3> { |
| 46 | + using type = std::extents<IndexType, 1, 1, 1>; |
| 47 | +} |
| 48 | +#endif |
| 49 | + |
| 50 | +template <typename T> struct is_khr_group : public std::false_type {}; |
| 51 | + |
| 52 | +template <int Dimensions> |
| 53 | +struct is_khr_group<khr::work_group<Dimensions>> : public std::true_type {}; |
| 54 | + |
| 55 | +struct is_khr_group<khr::sub_group> : public std::true_type {}; |
| 56 | + |
| 57 | +} // namespace detail |
| 58 | + |
| 59 | +namespace khr { |
| 60 | + |
| 61 | +template <int Dimensions = 1> class work_group { |
| 62 | +public: |
| 63 | + using id_type = id<Dimensions>; |
| 64 | + using linear_id_type = size_t; |
| 65 | + using range_type = range<Dimensions>; |
| 66 | +#if defined(__cpp_lib_mdspan) |
| 67 | + using extents_type = std::dextents<size_t, Dimensions>; |
| 68 | +#endif |
| 69 | + using size_type = size_t; |
| 70 | + static constexpr int dimensions = Dimensions; |
| 71 | + static constexpr memory_scope fence_scope = memory_scope::work_group; |
| 72 | + |
| 73 | + work_group(group<Dimensions> g) noexcept {} |
| 74 | + |
| 75 | + operator group<Dimensions>() const noexcept { return legacy(); } |
| 76 | + |
| 77 | + id_type id() const noexcept { return legacy().get_group_id(); } |
| 78 | + |
| 79 | + linear_id_type linear_id() const noexcept { |
| 80 | + return legacy().get_group_linear_id(); |
| 81 | + } |
| 82 | + |
| 83 | + range_type range() const noexcept { return legacy().get_group_range(); } |
| 84 | + |
| 85 | +#if defined(__cpp_lib_mdspan) |
| 86 | + constexpr extents_type extents() const noexcept { |
| 87 | + auto LocalRange = legacy().get_local_range(); |
| 88 | + if constexpr (dimensions == 1) { |
| 89 | + return extents_type(LocalRange[0]); |
| 90 | + } else if constexpr (dimensions == 2) { |
| 91 | + return extents_type(LocalRange[0], LocalRange[1]); |
| 92 | + } else if constexpr (dimensions == 3) { |
| 93 | + return extents_type(LocalRange[0], LocalRange[1], LocalRange[2]); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + constexpr index_type extent(rank_type r) const noexcept { |
| 98 | + return extents().extent(r); |
| 99 | + } |
| 100 | +#endif |
| 101 | + |
| 102 | + constexpr size_type size() const noexcept { |
| 103 | + return legacy().get_local_range().size(); |
| 104 | + } |
| 105 | + |
| 106 | +private: |
| 107 | + group<Dimensions> legacy() const noexcept { |
| 108 | + return ext::oneapi::this_work_item::get_work_group<Dimensions>(); |
| 109 | + } |
| 110 | +}; |
| 111 | + |
| 112 | +class sub_group { |
| 113 | +public: |
| 114 | + using id_type = id<1>; |
| 115 | + using linear_id_type = uint32_t; |
| 116 | + using range_type = range<1>; |
| 117 | +#if defined(__cpp_lib_mdspan) |
| 118 | + using extents_type = std::dextents<uint32_t, 1>; |
| 119 | +#endif |
| 120 | + using size_type = uint32_t; |
| 121 | + static constexpr int dimensions = 1; |
| 122 | + static constexpr memory_scope fence_scope = memory_scope::sub_group; |
| 123 | + |
| 124 | + sub_group(sycl::sub_group g) noexcept {} |
| 125 | + |
| 126 | + operator sycl::sub_group() const noexcept { return legacy(); } |
| 127 | + |
| 128 | + id_type id() const noexcept { return legacy().get_group_id(); } |
| 129 | + |
| 130 | + linear_id_type linear_id() const noexcept { |
| 131 | + return legacy().get_group_linear_id(); |
| 132 | + } |
| 133 | + |
| 134 | + range_type range() const noexcept { return legacy().get_group_range(); } |
| 135 | + |
| 136 | +#if defined(__cpp_lib_mdspan) |
| 137 | + constexpr extents_type extents() const noexcept { |
| 138 | + return extents_type(legacy().get_local_range()[0]); |
| 139 | + } |
| 140 | + |
| 141 | + constexpr index_type extent(rank_type r) const noexcept { |
| 142 | + return extents().extent(r); |
| 143 | + } |
| 144 | +#endif |
| 145 | + |
| 146 | + constexpr size_type size() const noexcept { |
| 147 | + return legacy().get_local_range()[0]; |
| 148 | + } |
| 149 | + |
| 150 | + constexpr size_type max_size() const noexcept { |
| 151 | + return legacy().get_max_local_range()[0]; |
| 152 | + } |
| 153 | + |
| 154 | +private: |
| 155 | + sycl::sub_group legacy() const noexcept { |
| 156 | + return ext::oneapi::this_work_item::get_sub_group(); |
| 157 | + } |
| 158 | +}; |
| 159 | + |
| 160 | +template <typename ParentGroup> class work_item { |
| 161 | +public: |
| 162 | + using id_type = typename ParentGroup::id_type; |
| 163 | + using linear_id_type = typename ParentGroup::linear_id_type; |
| 164 | + using range_type = typename ParentGroup::range_type; |
| 165 | +#if defined(__cpp_lib_mdspan) |
| 166 | + using extents_type = |
| 167 | + detail::single_extents<typename ParentGroup::extents_type::index_type, |
| 168 | + ParentGroup::dimensions>; |
| 169 | +#endif |
| 170 | + using size_type = typename ParentGroup::size_type; |
| 171 | + static constexpr int dimensions = ParentGroup::dimensions; |
| 172 | + static constexpr memory_scope fence_scope = memory_scope::work_item; |
| 173 | + |
| 174 | + id_type id() const noexcept { return legacy().get_local_id(); } |
| 175 | + |
| 176 | + linear_id_type linear_id() const noexcept { |
| 177 | + return legacy().get_local_linear_id(); |
| 178 | + } |
| 179 | + |
| 180 | + range_type range() const noexcept { return legacy().get_local_range(); } |
| 181 | + |
| 182 | +#if defined(__cpp_lib_mdspan) |
| 183 | + constexpr extents_type extents() const noexcept { return extents_type(); } |
| 184 | + |
| 185 | + constexpr index_type extent(rank_type r) const noexcept { |
| 186 | + return extents().extent(r); |
| 187 | + } |
| 188 | +#endif |
| 189 | + |
| 190 | + constexpr size_type size() const noexcept { return 1; } |
| 191 | + |
| 192 | +private: |
| 193 | + auto legacy() const noexcept { |
| 194 | + if constexpr (std::is_same_v<ParentGroup, sub_group>) { |
| 195 | + return ext::oneapi::this_work_item::get_sub_group(); |
| 196 | + } else { |
| 197 | + return ext::oneapi::this_work_item::get_work_group< |
| 198 | + ParentGroup::dimensions>(); |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | +protected: |
| 203 | + work_item() {} |
| 204 | + |
| 205 | + friend work_item<ParentGroup> get_item<ParentGroup>(ParentGroup); |
| 206 | +}; |
| 207 | + |
| 208 | +template <typename ParentGroup> |
| 209 | +std::enable_if_t<detail::is_khr_group<ParentGroup>::value, |
| 210 | + work_item<ParentGroup>> |
| 211 | +get_item(ParentGroup g) { |
| 212 | + return work_item<ParentGroup>{}; |
| 213 | +} |
| 214 | + |
| 215 | +template <typename Group> bool leader_of(Group g) { |
| 216 | + return get_item(g).linear_id() == 0; |
| 217 | +} |
| 218 | + |
| 219 | +} // namespace khr |
| 220 | +} // namespace _V1 |
| 221 | +} // namespace sycl |
0 commit comments