DPC++ Runtime
Runtime libraries for oneAPI Data Parallel C++
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 
39 context::context(const device &Device, async_handler AsyncHandler,
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.", PI_INVALID_VALUE);
58  }
59  auto NonHostDeviceIter = std::find_if_not(
60  DeviceList.begin(), DeviceList.end(),
61  [&](const device &CurrentDevice) { return CurrentDevice.is_host(); });
62  if (NonHostDeviceIter == DeviceList.end())
63  impl = std::make_shared<detail::context_impl>(DeviceList[0], AsyncHandler,
64  PropList);
65  else {
66  const device &NonHostDevice = *NonHostDeviceIter;
67  const auto &NonHostPlatform =
68  detail::getSyclObjImpl(NonHostDevice.get_platform())->getHandleRef();
69  if (std::any_of(DeviceList.begin(), DeviceList.end(),
70  [&](const device &CurrentDevice) {
71  return (
72  CurrentDevice.is_host() ||
73  (detail::getSyclObjImpl(CurrentDevice.get_platform())
74  ->getHandleRef() != NonHostPlatform));
75  }))
76  throw invalid_parameter_error(
77  "Can't add devices across platforms to a single context.",
79  else
80  impl = std::make_shared<detail::context_impl>(DeviceList, AsyncHandler,
81  PropList);
82  }
83 }
84 context::context(cl_context ClContext, async_handler AsyncHandler) {
85  const auto &Plugin = RT::getPlugin<backend::opencl>();
86  impl = std::make_shared<detail::context_impl>(
87  detail::pi::cast<detail::RT::PiContext>(ClContext), AsyncHandler, Plugin);
88 }
89 
90 #define __SYCL_PARAM_TRAITS_SPEC(param_type, param, ret_type) \
91  template <> \
92  __SYCL_EXPORT ret_type context::get_info<info::param_type::param>() const { \
93  return impl->get_info<info::param_type::param>(); \
94  }
95 
96 #include <CL/sycl/info/context_traits.def>
97 
98 #undef __SYCL_PARAM_TRAITS_SPEC
99 
100 #define __SYCL_PARAM_TRAITS_SPEC(param_type) \
101  template <> __SYCL_EXPORT bool context::has_property<param_type>() const { \
102  return impl->has_property<param_type>(); \
103  }
104 #include <CL/sycl/detail/properties_traits.def>
105 
106 #undef __SYCL_PARAM_TRAITS_SPEC
107 
108 #define __SYCL_PARAM_TRAITS_SPEC(param_type) \
109  template <> \
110  __SYCL_EXPORT param_type context::get_property<param_type>() const { \
111  return impl->get_property<param_type>(); \
112  }
113 #include <CL/sycl/detail/properties_traits.def>
114 
115 #undef __SYCL_PARAM_TRAITS_SPEC
116 
117 cl_context context::get() const { return impl->get(); }
118 
119 bool context::is_host() const { return impl->is_host(); }
120 
121 backend context::get_backend() const noexcept { return getImplBackend(impl); }
122 
124  return impl->get_info<info::context::platform>();
125 }
126 
127 std::vector<device> context::get_devices() const {
128  return impl->get_info<info::context::devices>();
129 }
130 
131 context::context(std::shared_ptr<detail::context_impl> Impl) : impl(Impl) {}
132 
133 pi_native_handle context::getNative() const { return impl->getNative(); }
134 
135 } // namespace sycl
136 } // __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
cl::sycl::property_list
Objects of the property_list class are containers for the SYCL properties.
Definition: property_list.hpp:25
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:123
cl::sycl::detail::getImplBackend
backend getImplBackend(const T &Impl)
Definition: backend_impl.hpp:16
cl::sycl::device
The SYCL device class encapsulates a single SYCL device on which kernels may be executed.
Definition: device.hpp:34
cl::sycl::context::is_host
bool is_host() const
Gets OpenCL interoperability context.
Definition: context.cpp:119
cl::sycl::context::get_backend
backend get_backend() const noexcept
Returns the backend associated with this context.
Definition: context.cpp:121
all_properties.hpp
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:72
cl::sycl::context::get_devices
std::vector< device > get_devices() const
Gets devices associated with this SYCL context.
Definition: context.cpp:127
std::get
constexpr tuple_element< I, tuple< Types... > >::type & get(cl::sycl::detail::tuple< Types... > &Arg) noexcept
Definition: tuple.hpp:199
cl::sycl::info::context::platform
@ platform
PI_INVALID_VALUE
@ PI_INVALID_VALUE
Definition: pi.h:87
cl::sycl::detail::getSyclObjImpl
decltype(Obj::impl) getSyclObjImpl(const Obj &SyclObject)
Definition: common.hpp:182
platform.hpp
exception.hpp
cl::sycl::platform
Encapsulates a SYCL platform on which kernels may be executed.
Definition: platform.hpp:33
std
Definition: accessor.hpp:2358
cl::sycl::context
The context class represents a SYCL context on which kernel functions may be executed.
Definition: context.hpp:35
backend_impl.hpp
exception_list.hpp
common.hpp
cl::sycl::info::context
context
Definition: info_desc.hpp:40
cl::sycl::async_handler
std::function< void(cl::sycl::exception_list)> async_handler
Definition: exception_list.hpp:53
PI_INVALID_DEVICE
@ PI_INVALID_DEVICE
Definition: pi.h:90
cl::sycl::info::context::devices
@ devices
__SYCL_INLINE_NAMESPACE
#define __SYCL_INLINE_NAMESPACE(X)
Definition: defines_elementary.hpp:12