|
| 1 | += sycl_ext_oneapi_weak_object |
| 2 | + |
| 3 | +:source-highlighter: coderay |
| 4 | +:coderay-linenums-mode: table |
| 5 | + |
| 6 | +// This section needs to be after the document title. |
| 7 | +:doctype: book |
| 8 | +:toc2: |
| 9 | +:toc: left |
| 10 | +:encoding: utf-8 |
| 11 | +:lang: en |
| 12 | +:dpcpp: pass:[DPC++] |
| 13 | + |
| 14 | +// Set the default source code type in this document to C++, |
| 15 | +// for syntax highlighting purposes. This is needed because |
| 16 | +// docbook uses c++ and html5 uses cpp. |
| 17 | +:language: {basebackend@docbook:c++:cpp} |
| 18 | + |
| 19 | + |
| 20 | +== Notice |
| 21 | + |
| 22 | +[%hardbreaks] |
| 23 | +Copyright (C) 2022-2022 Intel Corporation. All rights reserved. |
| 24 | + |
| 25 | +Khronos(R) is a registered trademark and SYCL(TM) and SPIR(TM) are trademarks |
| 26 | +of The Khronos Group Inc. OpenCL(TM) is a trademark of Apple Inc. used by |
| 27 | +permission by Khronos. |
| 28 | + |
| 29 | + |
| 30 | +== Contact |
| 31 | + |
| 32 | +To report problems with this extension, please open a new issue at: |
| 33 | + |
| 34 | +https://github.com/intel/llvm/issues |
| 35 | + |
| 36 | + |
| 37 | +== Dependencies |
| 38 | + |
| 39 | +This extension is written against the SYCL 2020 revision 6 specification. All |
| 40 | +references below to the "core SYCL specification" or to section numbers in the |
| 41 | +SYCL specification refer to that revision. |
| 42 | + |
| 43 | + |
| 44 | +== Status |
| 45 | + |
| 46 | +This extension is implemented and fully supported by {dpcpp}. |
| 47 | + |
| 48 | + |
| 49 | +== Overview |
| 50 | + |
| 51 | +SYCL 2020 has a number of classes with common reference semantics, which mean |
| 52 | +that copies of objects of these classes will reference the same objects and |
| 53 | +will be considered the same. As a result, these objects will only be fully |
| 54 | +destroyed when all copies have been destroyed. However, there is currently no |
| 55 | +way to have a reference to an object with these semantics without keeping the |
| 56 | +object alive. |
| 57 | + |
| 58 | +This extension adds the notion of a "weak" SYCL object, represented by the |
| 59 | +`weak_object` class. A weak object holds a reference to a SYCL object with |
| 60 | +common reference semantics of the specified type but will not keep the |
| 61 | +underlying object alive. A weak object is considered expired if the underlying |
| 62 | +object is no longer alive. If a weak object has not expired, a copy of the |
| 63 | +underlying object can be created by calling either `lock` or `try_lock`. |
| 64 | + |
| 65 | +Additionally this extension adds the notion of owner-based ordering to allow for |
| 66 | +use of SYCL object and their weak variants in order-oriented data structures and |
| 67 | +operations. |
| 68 | + |
| 69 | + |
| 70 | +== Specification |
| 71 | + |
| 72 | +=== Feature test macro |
| 73 | + |
| 74 | +This extension provides a feature-test macro as described in the core SYCL |
| 75 | +specification. An implementation supporting this extension must predefine the |
| 76 | +macro `SYCL_EXT_ONEAPI_WEAK_OBJECT` to one of the values defined in the table |
| 77 | +below. Applications can test for the existence of this macro to determine if |
| 78 | +the implementation supports this feature, or applications can test the macro's |
| 79 | +value to determine which of the extension's features the implementation |
| 80 | +supports. |
| 81 | + |
| 82 | +[%header,cols="1,5"] |
| 83 | +|=== |
| 84 | +|Value |
| 85 | +|Description |
| 86 | + |
| 87 | +|1 |
| 88 | +|Initial version of this extension. |
| 89 | +|=== |
| 90 | + |
| 91 | + |
| 92 | +=== The new `weak_object` class |
| 93 | + |
| 94 | +This extension adds the following class: |
| 95 | + |
| 96 | +[source] |
| 97 | +---- |
| 98 | +namespace sycl { |
| 99 | +namespace ext { |
| 100 | +namespace oneapi { |
| 101 | +
|
| 102 | +template <typename SyclObject> |
| 103 | +class weak_object { |
| 104 | +public: |
| 105 | + using object_type = SyclObject; |
| 106 | +
|
| 107 | + constexpr weak_object() noexcept; |
| 108 | + weak_object(const SyclObject &SYCLObj) noexcept; |
| 109 | + weak_object(const weak_object &Other) noexcept; |
| 110 | + weak_object(weak_object &&Other) noexcept; |
| 111 | +
|
| 112 | + weak_object &operator=(const SyclObject &SYCLObj) noexcept; |
| 113 | + weak_object &operator=(const weak_object &Other) noexcept; |
| 114 | + weak_object &operator=(weak_object &&Other) noexcept; |
| 115 | +
|
| 116 | + void reset() noexcept; |
| 117 | + void swap(weak_object &Other) noexcept; |
| 118 | +
|
| 119 | + bool expired() const noexcept; |
| 120 | + std::optional<SyclObject> try_lock() const noexcept; |
| 121 | + SyclObject lock() const; |
| 122 | +
|
| 123 | + bool owner_before(const weak_object &Other) const noexcept; |
| 124 | + bool owner_before(const SyclObject &Other) const noexcept; |
| 125 | +}; |
| 126 | +
|
| 127 | +} // namespace oneapi |
| 128 | +} // namespace ext |
| 129 | +} // namespace sycl |
| 130 | +---- |
| 131 | + |
| 132 | +The methods of the new `weak_object` class have the following semantics: |
| 133 | + |
| 134 | +[cols="60a,40"] |
| 135 | +|=== |
| 136 | +| Member Function | Description |
| 137 | + |
| 138 | +a| |
| 139 | +[source,c++] |
| 140 | +---- |
| 141 | +constexpr weak_object() noexcept; |
| 142 | +---- |
| 143 | + |
| 144 | +| Constructor for creating an empty `weak_object`. |
| 145 | + |
| 146 | +a| |
| 147 | +[source,c++] |
| 148 | +---- |
| 149 | +weak_object(const SyclObject &SYCLObj) noexcept; |
| 150 | +
|
| 151 | +weak_object(const weak_object &Other) noexcept; |
| 152 | +---- |
| 153 | + |
| 154 | +| Copy constructor. |
| 155 | + |
| 156 | +a| |
| 157 | +[source,c++] |
| 158 | +---- |
| 159 | +weak_object(weak_object &&Other) noexcept; |
| 160 | +---- |
| 161 | + |
| 162 | +| Move constructor. |
| 163 | + |
| 164 | +a| |
| 165 | +[source,c++] |
| 166 | +---- |
| 167 | +weak_object &operator=(const SyclObject &SYCLObj) noexcept; |
| 168 | +
|
| 169 | +weak_object &operator=(const weak_object &Other) noexcept; |
| 170 | +---- |
| 171 | + |
| 172 | +| Copy assignment operator. |
| 173 | + |
| 174 | +a| |
| 175 | +[source,c++] |
| 176 | +---- |
| 177 | +weak_object &operator=(weak_object &&Other) noexcept; |
| 178 | +---- |
| 179 | + |
| 180 | +| Move assignment operator. |
| 181 | + |
| 182 | +a| |
| 183 | +[source,c++] |
| 184 | +---- |
| 185 | +void reset() noexcept; |
| 186 | +---- |
| 187 | + |
| 188 | +| Releases the reference to the underlying SYCL object. |
| 189 | + |
| 190 | +a| |
| 191 | +[source,c++] |
| 192 | +---- |
| 193 | +void swap(weak_object &Other) noexcept; |
| 194 | +---- |
| 195 | + |
| 196 | +| Exchanges the reference to the underlying SYCL object with the one held by |
| 197 | +`Other`. |
| 198 | + |
| 199 | +a| |
| 200 | +[source,c++] |
| 201 | +---- |
| 202 | +bool expired() const noexcept; |
| 203 | +---- |
| 204 | + |
| 205 | +| Returns `true` if the underlying SYCL object has been destroyed or its |
| 206 | +destruction is imminent. Otherwise returns `false`. |
| 207 | + |
| 208 | +a| |
| 209 | +[source,c++] |
| 210 | +---- |
| 211 | +std::optional<SyclObject> try_lock() const noexcept; |
| 212 | +---- |
| 213 | + |
| 214 | +| Attempts to return the underlying SYCL object. Returns `std::nullopt` if the |
| 215 | +`weak_object` is empty or if `expired()` returns `true`. |
| 216 | + |
| 217 | +a| |
| 218 | +[source,c++] |
| 219 | +---- |
| 220 | +SyclObject lock() const; |
| 221 | +---- |
| 222 | + |
| 223 | +| Returns the underlying SYCL object. |
| 224 | + |
| 225 | +Throws an exception with the `errc::invalid` error code if the `weak_object` is |
| 226 | +empty or if `expired()` returns `true`. |
| 227 | + |
| 228 | +a| |
| 229 | +[source,c++] |
| 230 | +---- |
| 231 | +bool owner_before(const weak_object &Other) const noexcept; |
| 232 | +
|
| 233 | +bool owner_before(const SyclObject &Other) const noexcept; |
| 234 | +---- |
| 235 | + |
| 236 | +| Checks whether this `weak_object` precedes `Other` in the |
| 237 | +implementation-defined owner-based order. The order is defined such that two |
| 238 | +objects defining this ordering compare equivalent if both are `weak_object` |
| 239 | +without an underlying SYCL object or if both reference the same SYCL object. |
| 240 | + |
| 241 | +|=== |
| 242 | + |
| 243 | +Additionally the following members are added to the members in SYCL classes with |
| 244 | +common reference semantics: |
| 245 | + |
| 246 | +[source] |
| 247 | +---- |
| 248 | +namespace sycl { |
| 249 | +
|
| 250 | +// Where T is a SYCL type with common reference semantics. |
| 251 | +class T { |
| 252 | + ... |
| 253 | +
|
| 254 | +public: |
| 255 | + ... |
| 256 | +
|
| 257 | + bool ext_oneapi_owner_before(const ext::oneapi::weak_object<T> &Other) const noexcept; |
| 258 | + bool ext_oneapi_owner_before(const T &Other) const noexcept; |
| 259 | +}; |
| 260 | +
|
| 261 | +} // namespace sycl |
| 262 | +---- |
| 263 | + |
| 264 | +These new methods have the following semantics: |
| 265 | + |
| 266 | +[cols="60a,40"] |
| 267 | +|=== |
| 268 | +| Member Function | Description |
| 269 | + |
| 270 | +a| |
| 271 | +[source,c++] |
| 272 | +---- |
| 273 | +bool ext_oneapi_owner_before(const ext::oneapi::weak_object<T> &Other) const noexcept; |
| 274 | +
|
| 275 | +bool ext_oneapi_owner_before(const T &Other) const noexcept; |
| 276 | +---- |
| 277 | + |
| 278 | +| Checks whether this SYCL object precedes `Other` in the |
| 279 | +implementation-defined owner-based order. The order is defined such that two |
| 280 | +objects defining this ordering compare equivalent if both are `weak_object` |
| 281 | +without an underlying SYCL object or if both reference the same SYCL object. |
| 282 | + |
| 283 | +|=== |
| 284 | + |
| 285 | +The `owner_less` function object is added with the following specializations: |
| 286 | + |
| 287 | +[source] |
| 288 | +---- |
| 289 | +namespace sycl { |
| 290 | +namespace ext { |
| 291 | +namespace oneapi { |
| 292 | +
|
| 293 | +template <typename SyclObject> struct owner_less; |
| 294 | +
|
| 295 | +// Where T is a SYCL type with common reference semantics. |
| 296 | +template <> struct owner_less<T> { |
| 297 | + bool operator()(const T &lhs, const T &rhs) const noexcept; |
| 298 | + bool operator()(const weak_object<T> &lhs, |
| 299 | + const weak_object<T> &rhs) const noexcept; |
| 300 | + bool operator()(const T &lhs, const weak_object<T> &rhs) const noexcept; |
| 301 | + bool operator()(const weak_object<T> &lhs, const T &rhs) const noexcept; |
| 302 | +}; |
| 303 | +
|
| 304 | +} // namespace oneapi |
| 305 | +} // namespace ext |
| 306 | +} // namespace sycl |
| 307 | +---- |
| 308 | + |
| 309 | +The operator overloads of the new `owner_less` function object have the |
| 310 | +following semantics: |
| 311 | + |
| 312 | +[cols="60a,40"] |
| 313 | +|=== |
| 314 | +| Member Function | Description |
| 315 | + |
| 316 | +a| |
| 317 | +[source,c++] |
| 318 | +---- |
| 319 | +bool operator()(const T &lhs, const T &rhs) const noexcept; |
| 320 | +bool operator()(const weak_object<T> &lhs, |
| 321 | + const weak_object<T> &rhs) const noexcept; |
| 322 | +bool operator()(const T &lhs, const weak_object<T> &rhs) const noexcept; |
| 323 | +bool operator()(const weak_object<T> &lhs, const T &rhs) const noexcept; |
| 324 | +---- |
| 325 | + |
| 326 | +| Compares `lhs` and `rhs` using owner-based semantics, similar to calling |
| 327 | +`owner_before` on `weak_object` or `ext_oneapi_owner_before` on SYCL objects. |
| 328 | +`lhs` and `rhs` are equivalent if they both reference the same SYCL object or if |
| 329 | +they are both empty `weak_object` instances. |
| 330 | + |
| 331 | +|=== |
| 332 | + |
0 commit comments