|
| 1 | +//==--------- graph_dynamic.hpp --- SYCL graph 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 | + |
| 9 | +#pragma once |
| 10 | + |
| 11 | +#include "command_graph.hpp" |
| 12 | +#include <sycl/accessor.hpp> // for local_accessor |
| 13 | +#include <sycl/detail/export.hpp> // for __SYCL_EXPORT |
| 14 | +#include <sycl/detail/kernel_desc.hpp> // for kernel_param_kind_t |
| 15 | +#include <sycl/ext/oneapi/experimental/detail/properties/graph_properties.hpp> // for graph_state |
| 16 | +#include <sycl/ext/oneapi/experimental/work_group_memory.hpp> // for work_group_memory |
| 17 | +#include <sycl/ext/oneapi/properties/properties.hpp> // for empty_properties_t |
| 18 | + |
| 19 | +#include <functional> // for function |
| 20 | +#include <memory> // for shared_ptr |
| 21 | +#include <vector> // for vector |
| 22 | + |
| 23 | +namespace sycl { |
| 24 | +inline namespace _V1 { |
| 25 | +// Forward declarations |
| 26 | +class handler; |
| 27 | +class property_list; |
| 28 | + |
| 29 | +namespace detail { |
| 30 | +// Forward declarations |
| 31 | +class AccessorBaseHost; |
| 32 | +} // namespace detail |
| 33 | + |
| 34 | +namespace ext { |
| 35 | +namespace oneapi { |
| 36 | +namespace experimental { |
| 37 | +// Forward declarations |
| 38 | +class raw_kernel_arg; |
| 39 | +template <typename, typename> class work_group_memory; |
| 40 | + |
| 41 | +namespace detail { |
| 42 | +// Forward declarations |
| 43 | +class dynamic_parameter_impl; |
| 44 | +class dynamic_command_group_impl; |
| 45 | +} // namespace detail |
| 46 | + |
| 47 | +class __SYCL_EXPORT dynamic_command_group { |
| 48 | +public: |
| 49 | + dynamic_command_group( |
| 50 | + const command_graph<graph_state::modifiable> &Graph, |
| 51 | + const std::vector<std::function<void(handler &)>> &CGFList); |
| 52 | + |
| 53 | + size_t get_active_index() const; |
| 54 | + void set_active_index(size_t Index); |
| 55 | + |
| 56 | + /// Common Reference Semantics |
| 57 | + friend bool operator==(const dynamic_command_group &LHS, |
| 58 | + const dynamic_command_group &RHS) { |
| 59 | + return LHS.impl == RHS.impl; |
| 60 | + } |
| 61 | + friend bool operator!=(const dynamic_command_group &LHS, |
| 62 | + const dynamic_command_group &RHS) { |
| 63 | + return !operator==(LHS, RHS); |
| 64 | + } |
| 65 | + |
| 66 | +private: |
| 67 | + template <class Obj> |
| 68 | + friend const decltype(Obj::impl) & |
| 69 | + sycl::detail::getSyclObjImpl(const Obj &SyclObject); |
| 70 | + |
| 71 | + std::shared_ptr<detail::dynamic_command_group_impl> impl; |
| 72 | +}; |
| 73 | + |
| 74 | +namespace detail { |
| 75 | +class __SYCL_EXPORT dynamic_parameter_base { |
| 76 | +public: |
| 77 | +#ifdef __INTEL_PREVIEW_BREAKING_CHANGES |
| 78 | + dynamic_parameter_base(size_t ParamSize, const void *Data); |
| 79 | + dynamic_parameter_base(); |
| 80 | +#else |
| 81 | + dynamic_parameter_base() = default; |
| 82 | +#endif |
| 83 | + |
| 84 | + dynamic_parameter_base( |
| 85 | + const std::shared_ptr<detail::dynamic_parameter_impl> &impl); |
| 86 | + |
| 87 | + dynamic_parameter_base(const sycl::ext::oneapi::experimental::command_graph< |
| 88 | + graph_state::modifiable> |
| 89 | + Graph); |
| 90 | + |
| 91 | + dynamic_parameter_base(const sycl::ext::oneapi::experimental::command_graph< |
| 92 | + graph_state::modifiable> |
| 93 | + Graph, |
| 94 | + size_t ParamSize, const void *Data); |
| 95 | + |
| 96 | + /// Common Reference Semantics |
| 97 | + friend bool operator==(const dynamic_parameter_base &LHS, |
| 98 | + const dynamic_parameter_base &RHS) { |
| 99 | + return LHS.impl == RHS.impl; |
| 100 | + } |
| 101 | + friend bool operator!=(const dynamic_parameter_base &LHS, |
| 102 | + const dynamic_parameter_base &RHS) { |
| 103 | + return !operator==(LHS, RHS); |
| 104 | + } |
| 105 | + |
| 106 | +protected: |
| 107 | + void updateValue(const void *NewValue, size_t Size); |
| 108 | + |
| 109 | + // Update a sycl_ext_oneapi_raw_kernel_arg parameter. Size parameter is |
| 110 | + // ignored as it represents sizeof(raw_kernel_arg), which doesn't represent |
| 111 | + // the number of underlying bytes. |
| 112 | + void updateValue(const raw_kernel_arg *NewRawValue, size_t Size); |
| 113 | + |
| 114 | + void updateAccessor(const sycl::detail::AccessorBaseHost *Acc); |
| 115 | + |
| 116 | + std::shared_ptr<dynamic_parameter_impl> impl; |
| 117 | + |
| 118 | + template <class Obj> |
| 119 | + friend const decltype(Obj::impl) & |
| 120 | + sycl::detail::getSyclObjImpl(const Obj &SyclObject); |
| 121 | +}; |
| 122 | + |
| 123 | +class __SYCL_EXPORT dynamic_work_group_memory_base |
| 124 | + : public dynamic_parameter_base { |
| 125 | + |
| 126 | +public: |
| 127 | + dynamic_work_group_memory_base() = default; |
| 128 | + |
| 129 | +#ifdef __INTEL_PREVIEW_BREAKING_CHANGES |
| 130 | + dynamic_work_group_memory_base(size_t BufferSizeInBytes); |
| 131 | +#endif |
| 132 | + // TODO: Remove in next ABI breaking window |
| 133 | + dynamic_work_group_memory_base( |
| 134 | + const experimental::command_graph<graph_state::modifiable> Graph, |
| 135 | + size_t BufferSizeInBytes); |
| 136 | + |
| 137 | +protected: |
| 138 | + void updateWorkGroupMem(size_t NewBufferSizeInBytes); |
| 139 | +}; |
| 140 | + |
| 141 | +class __SYCL_EXPORT dynamic_local_accessor_base |
| 142 | + : public dynamic_parameter_base { |
| 143 | +public: |
| 144 | + dynamic_local_accessor_base() = default; |
| 145 | + |
| 146 | + dynamic_local_accessor_base(sycl::range<3> AllocationSize, int Dims, |
| 147 | + int ElemSize, const property_list &PropList); |
| 148 | + |
| 149 | +protected: |
| 150 | + void updateLocalAccessor(sycl::range<3> NewAllocationSize); |
| 151 | +}; |
| 152 | +} // namespace detail |
| 153 | + |
| 154 | +template <typename DataT, typename PropertyListT = empty_properties_t> |
| 155 | +class __SYCL_SPECIAL_CLASS |
| 156 | +__SYCL_TYPE(dynamic_work_group_memory) dynamic_work_group_memory |
| 157 | +#ifndef __SYCL_DEVICE_ONLY__ |
| 158 | + : public detail::dynamic_work_group_memory_base |
| 159 | +#endif |
| 160 | +{ |
| 161 | +public: |
| 162 | + // Check that DataT is an unbounded array type. |
| 163 | + static_assert(std::is_array_v<DataT> && std::extent_v<DataT, 0> == 0); |
| 164 | + static_assert(std::is_same_v<PropertyListT, empty_properties_t>); |
| 165 | + |
| 166 | + // Frontend requires special types to have a default constructor in order to |
| 167 | + // have a uniform way of initializing an object of special type to then call |
| 168 | + // the __init method on it. This is purely an implementation detail and not |
| 169 | + // part of the spec. |
| 170 | + // TODO: Revisit this once https://github.com/intel/llvm/issues/16061 is |
| 171 | + // closed. |
| 172 | + dynamic_work_group_memory() = default; |
| 173 | + |
| 174 | +#ifdef __INTEL_PREVIEW_BREAKING_CHANGES |
| 175 | +#ifndef __SYCL_DEVICE_ONLY__ |
| 176 | + /// Constructs a new dynamic_work_group_memory object. |
| 177 | + /// @param Num Number of elements in the unbounded array DataT. |
| 178 | + dynamic_work_group_memory(size_t Num) |
| 179 | + : detail::dynamic_work_group_memory_base( |
| 180 | + Num * sizeof(std::remove_extent_t<DataT>)) {} |
| 181 | +#else |
| 182 | + dynamic_work_group_memory(size_t /*Num*/) {} |
| 183 | +#endif |
| 184 | +#endif |
| 185 | + |
| 186 | +#ifndef __INTEL_PREVIEW_BREAKING_CHANGES |
| 187 | + __SYCL_DEPRECATED("Dynamic_work_group_memory constructors taking a graph " |
| 188 | + "object have been deprecated " |
| 189 | + "and will be removed in the next ABI breaking window.") |
| 190 | +#endif |
| 191 | + |
| 192 | +#ifndef __SYCL_DEVICE_ONLY__ |
| 193 | + /// Constructs a new dynamic_work_group_memory object. |
| 194 | + /// @param Graph The graph associated with this object. |
| 195 | + /// @param Num Number of elements in the unbounded array DataT. |
| 196 | + dynamic_work_group_memory( |
| 197 | + const experimental::command_graph<graph_state::modifiable> &Graph, |
| 198 | + size_t Num) |
| 199 | + : detail::dynamic_work_group_memory_base( |
| 200 | + Graph, Num * sizeof(std::remove_extent_t<DataT>)) {} |
| 201 | + |
| 202 | +#else |
| 203 | + dynamic_work_group_memory( |
| 204 | + const experimental::command_graph<graph_state::modifiable> & |
| 205 | + /* Graph */, |
| 206 | + size_t /* Num */) {} |
| 207 | +#endif |
| 208 | + |
| 209 | + work_group_memory<DataT, PropertyListT> get() const { |
| 210 | +#ifndef __SYCL_DEVICE_ONLY__ |
| 211 | + throw sycl::exception(sycl::make_error_code(errc::invalid), |
| 212 | + "Error: dynamic_work_group_memory::get() can be only " |
| 213 | + "called on the device!"); |
| 214 | +#endif |
| 215 | + return WorkGroupMem; |
| 216 | + } |
| 217 | + |
| 218 | + /// Updates on the host this dynamic_work_group_memory and all registered |
| 219 | + /// nodes with a new buffer size. |
| 220 | + /// @param Num The new number of elements in the unbounded array. |
| 221 | + void update([[maybe_unused]] size_t Num) { |
| 222 | +#ifndef __SYCL_DEVICE_ONLY__ |
| 223 | + updateWorkGroupMem(Num * sizeof(std::remove_extent_t<DataT>)); |
| 224 | +#endif |
| 225 | + } |
| 226 | + |
| 227 | +private: |
| 228 | + work_group_memory<DataT, PropertyListT> WorkGroupMem; |
| 229 | + |
| 230 | +#ifdef __SYCL_DEVICE_ONLY__ |
| 231 | + using value_type = std::remove_all_extents_t<DataT>; |
| 232 | + using decoratedPtr = typename sycl::detail::DecoratedType< |
| 233 | + value_type, access::address_space::local_space>::type *; |
| 234 | + |
| 235 | + void __init(decoratedPtr Ptr) { this->WorkGroupMem.__init(Ptr); } |
| 236 | + |
| 237 | + [[maybe_unused]] unsigned char |
| 238 | + Padding[sizeof(detail::dynamic_work_group_memory_base)]; |
| 239 | +#endif |
| 240 | +}; |
| 241 | + |
| 242 | +template <typename DataT, int Dimensions = 1> |
| 243 | +class __SYCL_SPECIAL_CLASS |
| 244 | +__SYCL_TYPE(dynamic_local_accessor) dynamic_local_accessor |
| 245 | +#ifndef __SYCL_DEVICE_ONLY__ |
| 246 | + : public detail::dynamic_local_accessor_base |
| 247 | +#endif |
| 248 | +{ |
| 249 | +public: |
| 250 | + static_assert(Dimensions > 0 && Dimensions <= 3); |
| 251 | + |
| 252 | + // Frontend requires special types to have a default constructor in order to |
| 253 | + // have a uniform way of initializing an object of special type to then call |
| 254 | + // the __init method on it. This is purely an implementation detail and not |
| 255 | + // part of the spec. |
| 256 | + // TODO: Revisit this once https://github.com/intel/llvm/issues/16061 is |
| 257 | + // closed. |
| 258 | + dynamic_local_accessor() = default; |
| 259 | + |
| 260 | +#ifndef __SYCL_DEVICE_ONLY__ |
| 261 | + /// Constructs a new dynamic_local_accessor object. |
| 262 | + /// @param Graph The graph associated with this object. |
| 263 | + /// @param AllocationSize The size of the local accessor. |
| 264 | + /// @param PropList List of properties for the underlying accessor. |
| 265 | + dynamic_local_accessor( |
| 266 | + const experimental::command_graph<graph_state::modifiable> & /* Graph */, |
| 267 | + range<Dimensions> AllocationSize, const property_list &PropList = {}) |
| 268 | + : detail::dynamic_local_accessor_base( |
| 269 | + detail::convertToArrayOfN<3, 1>(AllocationSize), Dimensions, |
| 270 | + sizeof(DataT), PropList) {} |
| 271 | +#else |
| 272 | + dynamic_local_accessor( |
| 273 | + const experimental::command_graph<graph_state::modifiable> & |
| 274 | + /* Graph */, |
| 275 | + range<Dimensions> /* AllocationSize */, |
| 276 | + const property_list & /*PropList */ = {}) {} |
| 277 | +#endif |
| 278 | + |
| 279 | + local_accessor<DataT, Dimensions> get() const { |
| 280 | +#ifndef __SYCL_DEVICE_ONLY__ |
| 281 | + throw sycl::exception(sycl::make_error_code(errc::invalid), |
| 282 | + "Error: dynamic_local_accessor::get() can be only " |
| 283 | + "called on the device!"); |
| 284 | +#endif |
| 285 | + return LocalAccessor; |
| 286 | + } |
| 287 | + |
| 288 | + /// Updates on the host this dynamic_local_accessor and all registered |
| 289 | + /// nodes with a new size. |
| 290 | + /// @param Num The new number of elements in the unbounded array. |
| 291 | + void update([[maybe_unused]] range<Dimensions> NewAllocationSize) { |
| 292 | +#ifndef __SYCL_DEVICE_ONLY__ |
| 293 | + updateLocalAccessor(detail::convertToArrayOfN<3, 1>(NewAllocationSize)); |
| 294 | +#endif |
| 295 | + } |
| 296 | + |
| 297 | +private: |
| 298 | + local_accessor<DataT, Dimensions> LocalAccessor; |
| 299 | + |
| 300 | +#ifdef __SYCL_DEVICE_ONLY__ |
| 301 | + void __init(typename local_accessor<DataT, Dimensions>::ConcreteASPtrType Ptr, |
| 302 | + range<Dimensions> AccessRange, range<Dimensions> range, |
| 303 | + id<Dimensions> id) { |
| 304 | + this->LocalAccessor.__init(Ptr, AccessRange, range, id); |
| 305 | + } |
| 306 | + |
| 307 | + [[maybe_unused]] unsigned char |
| 308 | + Padding[sizeof(detail::dynamic_local_accessor_base)]; |
| 309 | +#endif |
| 310 | +}; |
| 311 | + |
| 312 | +template <typename ValueT> |
| 313 | +class dynamic_parameter : public detail::dynamic_parameter_base { |
| 314 | + static constexpr bool IsAccessor = |
| 315 | + std::is_base_of_v<sycl::detail::AccessorBaseHost, ValueT>; |
| 316 | + static constexpr sycl::detail::kernel_param_kind_t ParamType = |
| 317 | + IsAccessor ? sycl::detail::kernel_param_kind_t::kind_accessor |
| 318 | + : std::is_pointer_v<ValueT> |
| 319 | + ? sycl::detail::kernel_param_kind_t::kind_pointer |
| 320 | + : sycl::detail::kernel_param_kind_t::kind_std_layout; |
| 321 | + |
| 322 | +public: |
| 323 | +#ifdef __INTEL_PREVIEW_BREAKING_CHANGES |
| 324 | + /// Constructs a new dynamic parameter. |
| 325 | + /// @param Graph The graph associated with this parameter. |
| 326 | + /// @param Param A reference value for this parameter used for CTAD. |
| 327 | + dynamic_parameter(const ValueT &Param) |
| 328 | + : detail::dynamic_parameter_base(sizeof(ValueT), &Param) {} |
| 329 | +#endif |
| 330 | + |
| 331 | +#ifndef __INTEL_PREVIEW_BREAKING_CHANGES |
| 332 | + __SYCL_DEPRECATED("Dynamic_parameter constructors taking a graph object have " |
| 333 | + "been deprecated " |
| 334 | + "and will be removed in the next ABI breaking window.") |
| 335 | +#endif |
| 336 | + /// Constructs a new dynamic parameter. |
| 337 | + /// @param Graph The graph associated with this parameter. |
| 338 | + /// @param Param A reference value for this parameter used for CTAD. |
| 339 | + dynamic_parameter( |
| 340 | + const experimental::command_graph<graph_state::modifiable> &Graph, |
| 341 | + const ValueT &Param) |
| 342 | + : detail::dynamic_parameter_base(Graph, sizeof(ValueT), &Param) {} |
| 343 | + |
| 344 | + /// Updates this dynamic parameter and all registered nodes with a new value. |
| 345 | + /// @param NewValue The new value for the parameter. |
| 346 | + void update(const ValueT &NewValue) { |
| 347 | + if constexpr (IsAccessor) { |
| 348 | + detail::dynamic_parameter_base::updateAccessor(&NewValue); |
| 349 | + } else { |
| 350 | + detail::dynamic_parameter_base::updateValue(&NewValue, sizeof(ValueT)); |
| 351 | + } |
| 352 | + } |
| 353 | +}; |
| 354 | + |
| 355 | +/// Additional CTAD deduction guides. |
| 356 | +template <typename ValueT> |
| 357 | +dynamic_parameter( |
| 358 | + const experimental::command_graph<graph_state::modifiable> &Graph, |
| 359 | + const ValueT &Param) -> dynamic_parameter<ValueT>; |
| 360 | + |
| 361 | +} // namespace experimental |
| 362 | +} // namespace oneapi |
| 363 | +} // namespace ext |
| 364 | + |
| 365 | +} // namespace _V1 |
| 366 | +} // namespace sycl |
0 commit comments