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 
9 #include <CL/sycl/context.hpp>
11 #include <CL/sycl/device.hpp>
13 #include <CL/sycl/exception.hpp>
15 #include <CL/sycl/platform.hpp>
17 #include <CL/sycl/stl.hpp>
18 #include <detail/backend_impl.hpp>
19 #include <detail/context_impl.hpp>
20 
21 #include <algorithm>
22 #include <memory>
23 #include <utility>
24 
25 // 4.6.2 Context class
26 
28 namespace sycl {
30  : context(default_selector().select_device(), PropList) {}
31 
32 context::context(const async_handler &AsyncHandler,
33  const property_list &PropList)
34  : context(default_selector().select_device(), AsyncHandler, PropList) {}
35 
36 context::context(const device &Device, const property_list &PropList)
37  : context(std::vector<device>(1, Device), PropList) {}
38 
40  const property_list &PropList)
41  : context(std::vector<device>(1, Device), AsyncHandler, PropList) {}
42 
43 context::context(const platform &Platform, const property_list &PropList)
44  : context(Platform.get_devices(), PropList) {}
45 
46 context::context(const platform &Platform, async_handler AsyncHandler,
47  const property_list &PropList)
48  : context(Platform.get_devices(), AsyncHandler, PropList) {}
49 
50 context::context(const std::vector<device> &DeviceList,
51  const property_list &PropList)
52  : context(DeviceList, async_handler{}, PropList) {}
53 
54 context::context(const std::vector<device> &DeviceList,
55  async_handler AsyncHandler, const property_list &PropList) {
56  if (DeviceList.empty()) {
57  throw invalid_parameter_error("DeviceList is empty.",
58  PI_ERROR_INVALID_VALUE);
59  }
60  auto NonHostDeviceIter = std::find_if_not(
61  DeviceList.begin(), DeviceList.end(),
62  [&](const device &CurrentDevice) { return CurrentDevice.is_host(); });
63  if (NonHostDeviceIter == DeviceList.end())
64  impl = std::make_shared<detail::context_impl>(DeviceList[0], AsyncHandler,
65  PropList);
66  else {
67  const device &NonHostDevice = *NonHostDeviceIter;
68  const auto &NonHostPlatform =
69  detail::getSyclObjImpl(NonHostDevice.get_platform())->getHandleRef();
70  if (std::any_of(DeviceList.begin(), DeviceList.end(),
71  [&](const device &CurrentDevice) {
72  return (
73  CurrentDevice.is_host() ||
74  (detail::getSyclObjImpl(CurrentDevice.get_platform())
75  ->getHandleRef() != NonHostPlatform));
76  }))
77  throw invalid_parameter_error(
78  "Can't add devices across platforms to a single context.",
79  PI_ERROR_INVALID_DEVICE);
80  else
81  impl = std::make_shared<detail::context_impl>(DeviceList, AsyncHandler,
82  PropList);
83  }
84 }
85 context::context(cl_context ClContext, async_handler AsyncHandler) {
86  const auto &Plugin = RT::getPlugin<backend::opencl>();
87  impl = std::make_shared<detail::context_impl>(
88  detail::pi::cast<detail::RT::PiContext>(ClContext), AsyncHandler, Plugin);
89 }
90 
91 #define __SYCL_PARAM_TRAITS_SPEC(param_type, param, ret_type) \
92  template <> \
93  __SYCL_EXPORT ret_type context::get_info<info::param_type::param>() const { \
94  return impl->get_info<info::param_type::param>(); \
95  }
96 
97 #include <CL/sycl/info/context_traits.def>
98 
99 #undef __SYCL_PARAM_TRAITS_SPEC
100 
101 #define __SYCL_PARAM_TRAITS_SPEC(param_type) \
102  template <> __SYCL_EXPORT bool context::has_property<param_type>() const { \
103  return impl->has_property<param_type>(); \
104  }
105 #include <CL/sycl/detail/properties_traits.def>
106 
107 #undef __SYCL_PARAM_TRAITS_SPEC
108 
109 #define __SYCL_PARAM_TRAITS_SPEC(param_type) \
110  template <> \
111  __SYCL_EXPORT param_type context::get_property<param_type>() const { \
112  return impl->get_property<param_type>(); \
113  }
114 #include <CL/sycl/detail/properties_traits.def>
115 
116 #undef __SYCL_PARAM_TRAITS_SPEC
117 
118 cl_context context::get() const { return impl->get(); }
119 
120 bool context::is_host() const { return impl->is_host(); }
121 
122 backend context::get_backend() const noexcept { return getImplBackend(impl); }
123 
125  return impl->get_info<info::context::platform>();
126 }
127 
128 std::vector<device> context::get_devices() const {
129  return impl->get_info<info::context::devices>();
130 }
131 
132 context::context(std::shared_ptr<detail::context_impl> Impl) : impl(Impl) {}
133 
134 pi_native_handle context::getNative() const { return impl->getNative(); }
135 
136 } // namespace sycl
137 } // __SYCL_INLINE_NAMESPACE(cl)
cl::sycl::backend
backend
Definition: backend_types.hpp:21
context_impl.hpp
stl.hpp
device_selector.hpp
device.hpp
cl::sycl::context::context
context(const property_list &PropList={})
Constructs a SYCL context instance using an instance of default_selector.
Definition: context.cpp:29
cl::sycl::device::get_platform
platform get_platform() const
Get associated SYCL platform.
Definition: device.cpp:110
context.hpp
sycl
Definition: invoke_simd.hpp:68
cl::sycl::property_list
Objects of the property_list class are containers for the SYCL properties.
Definition: property_list.hpp:26
cl::sycl::info::context::devices
@ devices
cl::sycl::default_selector
The default selector chooses the first available SYCL device.
Definition: device_selector.hpp:46
cl::sycl::context::get_platform
platform get_platform() const
Gets platform associated with this SYCL context.
Definition: context.cpp:124
cl::sycl::detail::getImplBackend
backend getImplBackend(const T &Impl)
Definition: backend_impl.hpp:16
cl::sycl::info::context::platform
@ platform
cl::sycl::device
The SYCL device class encapsulates a single SYCL device on which kernels may be executed.
Definition: device.hpp:36
cl::sycl::context::is_host
bool is_host() const
Gets OpenCL interoperability context.
Definition: context.cpp:120
cl::sycl::context::get_backend
backend get_backend() const noexcept
Returns the backend associated with this context.
Definition: context.cpp:122
all_properties.hpp
Device
@ Device
Definition: usm_allocator.hpp:14
cl
We provide new interfaces for matrix muliply in this patch:
Definition: access.hpp:13
pi_native_handle
uintptr_t pi_native_handle
Definition: pi.h:98
cl::sycl::context::get_devices
std::vector< device > get_devices() const
Gets devices associated with this SYCL context.
Definition: context.cpp:128
cl::sycl::info::context
context
Definition: info_desc.hpp:42
std::get
constexpr tuple_element< I, tuple< Types... > >::type & get(cl::sycl::detail::tuple< Types... > &Arg) noexcept
Definition: tuple.hpp:199
cl::sycl::detail::getSyclObjImpl
decltype(Obj::impl) getSyclObjImpl(const Obj &SyclObject)
Definition: common.hpp:204
platform.hpp
exception.hpp
cl::sycl::platform
Encapsulates a SYCL platform on which kernels may be executed.
Definition: platform.hpp:34
std
Definition: accessor.hpp:2617
cl::sycl::context
The context class represents a SYCL context on which kernel functions may be executed.
Definition: context.hpp:38
backend_impl.hpp
exception_list.hpp
any_of
bool any_of(const simd_mask< _Tp, _Abi > &) noexcept
common.hpp
cl::sycl::async_handler
std::function< void(cl::sycl::exception_list)> async_handler
Definition: exception_list.hpp:53
__SYCL_INLINE_NAMESPACE
#define __SYCL_INLINE_NAMESPACE(X)
Definition: defines_elementary.hpp:12