DPC++ Runtime
Runtime libraries for oneAPI DPC++
context.cpp
Go to the documentation of this file.
1 //==---------------- context.cpp - 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 
10 #include <detail/context_impl.hpp>
11 #include <sycl/context.hpp>
12 #include <sycl/detail/common.hpp>
13 #include <sycl/device.hpp>
14 #include <sycl/device_selector.hpp>
15 #include <sycl/exception.hpp>
16 #include <sycl/exception_list.hpp>
17 #include <sycl/platform.hpp>
19 
20 #include <algorithm>
21 #include <memory>
22 #include <utility>
23 
24 // 4.6.2 Context class
25 
26 namespace sycl {
27 inline namespace _V1 {
28 
29 context::context(const property_list &PropList) : context(device{}, PropList) {}
30 
31 context::context(const async_handler &AsyncHandler,
32  const property_list &PropList)
33  : context(device{}, AsyncHandler, PropList) {}
34 
35 context::context(const device &Device, const property_list &PropList)
36  : context(std::vector<device>(1, Device), PropList) {}
37 
38 context::context(const device &Device, async_handler AsyncHandler,
39  const property_list &PropList)
40  : context(std::vector<device>(1, Device), AsyncHandler, PropList) {}
41 
42 context::context(const platform &Platform, const property_list &PropList)
43  : context(Platform.get_devices(), PropList) {}
44 
45 context::context(const platform &Platform, async_handler AsyncHandler,
46  const property_list &PropList)
47  : context(Platform.get_devices(), AsyncHandler, PropList) {}
48 
49 context::context(const std::vector<device> &DeviceList,
50  const property_list &PropList)
51  : context(DeviceList, detail::defaultAsyncHandler, PropList) {}
52 
53 context::context(const std::vector<device> &DeviceList,
54  async_handler AsyncHandler, const property_list &PropList) {
55  if (DeviceList.empty()) {
56  throw exception(make_error_code(errc::invalid), "DeviceList is empty.");
57  }
58 
59  const auto &RefPlatform =
60  detail::getSyclObjImpl(DeviceList[0].get_platform())->getHandleRef();
61  if (std::any_of(DeviceList.begin(), DeviceList.end(),
62  [&](const device &CurrentDevice) {
63  return (detail::getSyclObjImpl(CurrentDevice.get_platform())
64  ->getHandleRef() != RefPlatform);
65  }))
67  "Can't add devices across platforms to a single context.");
68  else
69  impl = std::make_shared<detail::context_impl>(DeviceList, AsyncHandler,
70  PropList);
71 }
72 context::context(cl_context ClContext, async_handler AsyncHandler) {
73  const auto &Plugin = sycl::detail::pi::getPlugin<backend::opencl>();
74  impl = std::make_shared<detail::context_impl>(
75  detail::pi::cast<sycl::detail::pi::PiContext>(ClContext), AsyncHandler,
76  Plugin);
77 }
78 
79 template <typename Param>
80 typename detail::is_context_info_desc<Param>::return_type
82  return impl->template get_info<Param>();
83 }
84 
85 #define __SYCL_PARAM_TRAITS_SPEC(DescType, Desc, ReturnT, PiCode) \
86  template __SYCL_EXPORT ReturnT context::get_info<info::DescType::Desc>() \
87  const;
88 
89 #include <sycl/info/context_traits.def>
90 
91 #undef __SYCL_PARAM_TRAITS_SPEC
92 
93 template <typename Param>
96  return impl->get_backend_info<Param>();
97 }
98 
99 #define __SYCL_PARAM_TRAITS_SPEC(DescType, Desc, ReturnT, Picode) \
100  template __SYCL_EXPORT ReturnT \
101  context::get_backend_info<info::DescType::Desc>() const;
102 
103 #include <sycl/info/sycl_backend_traits.def>
104 
105 #undef __SYCL_PARAM_TRAITS_SPEC
106 
107 cl_context context::get() const { return impl->get(); }
108 
109 backend context::get_backend() const noexcept { return impl->getBackend(); }
110 
112  return impl->get_info<info::context::platform>();
113 }
114 
115 std::vector<device> context::get_devices() const {
116  return impl->get_info<info::context::devices>();
117 }
118 
119 context::context(std::shared_ptr<detail::context_impl> Impl) : impl(Impl) {}
120 
121 pi_native_handle context::getNative() const { return impl->getNative(); }
122 
123 const property_list &context::getPropList() const {
124  return impl->getPropList();
125 }
126 
127 } // namespace _V1
128 } // namespace sycl
The context class represents a SYCL context on which kernel functions may be executed.
Definition: context.hpp:50
std::vector< device > get_devices() const
Gets devices associated with this SYCL context.
Definition: context.cpp:115
detail::is_context_info_desc< Param >::return_type get_info() const
Constructs a SYCL context instance from OpenCL cl_context.
Definition: context.cpp:81
context(const property_list &PropList={})
Constructs a SYCL context instance using an instance of default_selector.
Definition: context.cpp:29
platform get_platform() const
Gets platform associated with this SYCL context.
Definition: context.cpp:111
backend get_backend() const noexcept
Gets OpenCL interoperability context.
Definition: context.cpp:109
detail::is_backend_info_desc< Param >::return_type get_backend_info() const
Queries this SYCL context for SYCL backend-specific information.
Definition: context.cpp:95
The SYCL device class encapsulates a single SYCL device on which kernels may be executed.
Definition: device.hpp:64
Encapsulates a SYCL platform on which kernels may be executed.
Definition: platform.hpp:99
Objects of the property_list class are containers for the SYCL properties.
constexpr tuple_element< I, tuple< Types... > >::type & get(sycl::detail::tuple< Types... > &Arg) noexcept
Definition: tuple.hpp:198
decltype(Obj::impl) const & getSyclObjImpl(const Obj &SyclObject)
Definition: impl_utils.hpp:31
void defaultAsyncHandler(exception_list Exceptions)
std::function< void(sycl::exception_list)> async_handler
std::error_code make_error_code(sycl::errc E) noexcept
Constructs an error code using e and sycl_category()
Definition: exception.cpp:64
Definition: access.hpp:18
uintptr_t pi_native_handle
Definition: pi.h:256
bool any_of(const simd_mask< _Tp, _Abi > &) noexcept
_Abi const simd< _Tp, _Abi > & noexcept
Definition: simd.hpp:1324