DPC++ Runtime
Runtime libraries for oneAPI DPC++
context.hpp
Go to the documentation of this file.
1 //==---------------- context.hpp - SYCL context ----------------------------==//
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 <sycl/async_handler.hpp> // for async_handler
12 #include <sycl/backend_types.hpp> // for backend, backend_return_t
13 #include <sycl/detail/defines_elementary.hpp> // for __SYCL2020_DEPRECATED
14 #include <sycl/detail/export.hpp> // for __SYCL_EXPORT
15 #include <sycl/detail/helpers.hpp> // for context_impl
16 #include <sycl/detail/info_desc_helpers.hpp> // for is_context_info_desc
17 #include <sycl/detail/owner_less_base.hpp> // for OwnerLessBase
18 #include <sycl/detail/pi.h> // for pi_native_handle
19 #include <sycl/device.hpp> // for device
20 #include <sycl/platform.hpp> // for platform
21 #include <sycl/property_list.hpp> // for property_list
22 
23 #ifdef __SYCL_INTERNAL_API
24 #include <sycl/detail/cl.h>
25 #endif
26 
27 #include <cstddef> // for size_t
28 #include <memory> // for shared_ptr, hash, opera...
29 #include <type_traits> // for add_pointer_t
30 #include <variant> // for hash
31 #include <vector> // for vector
32 
33 // 4.6.2 Context class
34 
35 namespace sycl {
36 inline namespace _V1 {
37 // Forward declarations
38 class device;
39 class platform;
40 
41 namespace detail {
42 class context_impl;
43 }
44 template <backend Backend, class SyclT>
45 auto get_native(const SyclT &Obj) -> backend_return_t<Backend, SyclT>;
46 
51 class __SYCL_EXPORT context : public detail::OwnerLessBase<context> {
52 public:
61  explicit context(const property_list &PropList = {});
62 
74  explicit context(const async_handler &AsyncHandler,
75  const property_list &PropList = {});
76 
86  explicit context(const device &Device, const property_list &PropList = {});
87 
100  explicit context(const device &Device, async_handler AsyncHandler,
101  const property_list &PropList = {});
102 
112  explicit context(const platform &Platform,
113  const property_list &PropList = {});
114 
127  explicit context(const platform &Platform, async_handler AsyncHandler,
128  const property_list &PropList = {});
129 
140  explicit context(const std::vector<device> &DeviceList,
141  const property_list &PropList = {});
142 
156  explicit context(const std::vector<device> &DeviceList,
157  async_handler AsyncHandler,
158  const property_list &PropList = {});
159 
168 #ifdef __SYCL_INTERNAL_API
169  context(cl_context ClContext, async_handler AsyncHandler = {});
170 #endif
171 
175  template <typename Param>
176  typename detail::is_context_info_desc<Param>::return_type get_info() const;
177 
178  context(const context &rhs) = default;
179 
180  context(context &&rhs) = default;
181 
182  context &operator=(const context &rhs) = default;
183 
184  context &operator=(context &&rhs) = default;
185 
186  bool operator==(const context &rhs) const { return impl == rhs.impl; }
187 
188  bool operator!=(const context &rhs) const { return !(*this == rhs); }
189 
193  template <typename propertyT> bool has_property() const noexcept;
194 
201  template <typename propertyT> propertyT get_property() const;
202 
208 #ifdef __SYCL_INTERNAL_API
209  cl_context get() const;
210 #endif
211 
216  "is_host() is deprecated as the host device is no longer supported.")
217  bool is_host() const;
218 
222  backend get_backend() const noexcept;
223 
227  platform get_platform() const;
228 
232  std::vector<device> get_devices() const;
233 
234 private:
236  context(std::shared_ptr<detail::context_impl> Impl);
237 
238  pi_native_handle getNative() const;
239 
240  std::shared_ptr<detail::context_impl> impl;
241 
242  template <backend Backend, class SyclT>
243  friend auto get_native(const SyclT &Obj) -> backend_return_t<Backend, SyclT>;
244 
245  template <class Obj>
246  friend decltype(Obj::impl) detail::getSyclObjImpl(const Obj &SyclObject);
247 
248  template <class T>
249  friend typename std::add_pointer_t<typename decltype(T::impl)::element_type>
250  detail::getRawSyclObjImpl(const T &SyclObject);
251 
252  template <class T>
253  friend T detail::createSyclObjFromImpl(decltype(T::impl) ImplObj);
254 };
255 
256 } // namespace _V1
257 } // namespace sycl
258 
259 namespace std {
260 template <> struct hash<sycl::context> {
261  size_t operator()(const sycl::context &Context) const {
262  return hash<std::shared_ptr<sycl::detail::context_impl>>()(
264  }
265 };
266 } // namespace std
The context class represents a SYCL context on which kernel functions may be executed.
Definition: context.hpp:51
context & operator=(const context &rhs)=default
bool operator==(const context &rhs) const
Definition: context.hpp:186
context & operator=(context &&rhs)=default
__SYCL2020_DEPRECATED("is_host() is deprecated as the host device is no longer supported.") bool is_host() const
Gets OpenCL interoperability context.
bool operator!=(const context &rhs) const
Definition: context.hpp:188
context(context &&rhs)=default
bool has_property() const noexcept
Checks if this context has a property of type propertyT.
context(const context &rhs)=default
The SYCL device class encapsulates a single SYCL device on which kernels may be executed.
Definition: device.hpp:66
Encapsulates a SYCL platform on which kernels may be executed.
Definition: platform.hpp:109
Objects of the property_list class are containers for the SYCL properties.
std::add_pointer_t< typename decltype(T::impl)::element_type > getRawSyclObjImpl(const T &SyclObject)
Definition: impl_utils.hpp:40
decltype(Obj::impl) getSyclObjImpl(const Obj &SyclObject)
Definition: impl_utils.hpp:30
T createSyclObjFromImpl(decltype(T::impl) ImplObj)
Definition: impl_utils.hpp:48
static constexpr auto get_property()
pointer get() const
Definition: multi_ptr.hpp:544
auto get_native(const SyclObjectT &Obj) -> backend_return_t< BackendName, SyclObjectT >
Definition: backend.hpp:136
typename backend_traits< Backend >::template return_type< SyclType > backend_return_t
Definition: backend.hpp:87
std::function< void(sycl::exception_list)> async_handler
Definition: access.hpp:18
uintptr_t pi_native_handle
Definition: pi.h:209
_Abi const simd< _Tp, _Abi > & noexcept
Definition: simd.hpp:1324
size_t operator()(const sycl::context &Context) const
Definition: context.hpp:261