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
sycl::_V1::property_list
Objects of the property_list class are containers for the SYCL properties.
Definition: property_list.hpp:31
sycl::_V1::__SYCL2020_DEPRECATED
signed char __SYCL2020_DEPRECATED
Definition: aliases.hpp:94
sycl::_V1::operator=
multi_ptr & operator=(const multi_ptr &)=default
property_list.hpp
sycl::_V1::detail::getRawSyclObjImpl
std::add_pointer_t< typename decltype(T::impl)::element_type > getRawSyclObjImpl(const T &SyclObject)
Definition: impl_utils.hpp:40
pi.h
sycl::_V1::errc::platform
@ platform
sycl::_V1::backend
backend
Definition: backend_types.hpp:18
device.hpp
sycl::_V1::get
pointer_t get() const
Definition: multi_ptr.hpp:974
std::hash< sycl::context >::operator()
size_t operator()(const sycl::context &Context) const
Definition: context.hpp:261
helpers.hpp
sycl
Definition: access.hpp:18
noexcept
_Abi const simd< _Tp, _Abi > & noexcept
Definition: simd.hpp:1324
owner_less_base.hpp
bool
export.hpp
sycl::_V1::ext::oneapi::experimental::has_property
static constexpr bool has_property()
Definition: annotated_arg.hpp:162
defines_elementary.hpp
cl.h
sycl::_V1::backend_return_t
typename backend_traits< Backend >::template return_type< SyclType > backend_return_t
Definition: backend.hpp:87
sycl::_V1::device
The SYCL device class encapsulates a single SYCL device on which kernels may be executed.
Definition: device.hpp:59
sycl::_V1::detail::createSyclObjFromImpl
T createSyclObjFromImpl(decltype(T::impl) ImplObj)
Definition: impl_utils.hpp:48
sycl::_V1::access::target::device
@ device
sycl::_V1::ext::oneapi::experimental::get_property
static constexpr auto get_property()
Definition: annotated_arg.hpp:166
pi_native_handle
uintptr_t pi_native_handle
Definition: pi.h:198
sycl::_V1::element_type
std::conditional_t< std::is_same_v< ElementType, half >, sycl::detail::half_impl::BIsRepresentationT, ElementType > element_type
Definition: multi_ptr.hpp:752
backend_types.hpp
platform.hpp
std
Definition: accessor.hpp:4171
info_desc_helpers.hpp
sycl::_V1::async_handler
std::function< void(sycl::exception_list)> async_handler
Definition: async_handler.hpp:19
sycl::_V1::context::operator!=
bool operator!=(const context &rhs) const
Definition: context.hpp:188
sycl::_V1::context::operator==
bool operator==(const context &rhs) const
Definition: context.hpp:186
sycl::_V1::platform
Encapsulates a SYCL platform on which kernels may be executed.
Definition: platform.hpp:63
async_handler.hpp
sycl::_V1::get_native
auto get_native(const SyclObjectT &Obj) -> backend_return_t< BackendName, SyclObjectT >
Definition: backend.hpp:136
sycl::_V1::detail::getSyclObjImpl
decltype(Obj::impl) getSyclObjImpl(const Obj &SyclObject)
Definition: impl_utils.hpp:30
sycl::_V1::context
The context class represents a SYCL context on which kernel functions may be executed.
Definition: context.hpp:51