DPC++ Runtime
Runtime libraries for oneAPI DPC++
opencl.cpp
Go to the documentation of this file.
1 //==------- opencl.cpp - SYCL OpenCL backend -------------------------------==//
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 <detail/kernel_impl.hpp>
10 #include <detail/platform_impl.hpp>
11 #include <detail/plugin.hpp>
12 #include <detail/program_impl.hpp>
13 #include <detail/queue_impl.hpp>
14 #include <sycl/sycl.hpp>
15 
16 #include <memory>
17 #include <string_view>
18 
19 namespace sycl {
20 inline namespace _V1 {
21 namespace opencl {
22 using namespace detail;
23 
24 //----------------------------------------------------------------------------
25 // Implementation of opencl::make<platform>
26 __SYCL_EXPORT platform make_platform(pi_native_handle NativeHandle) {
27  return detail::make_platform(NativeHandle, backend::opencl);
28 }
29 
30 //----------------------------------------------------------------------------
31 // Implementation of opencl::make<device>
32 __SYCL_EXPORT device make_device(pi_native_handle NativeHandle) {
33  return detail::make_device(NativeHandle, backend::opencl);
34 }
35 
36 //----------------------------------------------------------------------------
37 // Implementation of opencl::make<context>
38 __SYCL_EXPORT context make_context(pi_native_handle NativeHandle) {
41 }
42 
43 //----------------------------------------------------------------------------
44 // Implementation of opencl::make<queue>
45 __SYCL_EXPORT queue make_queue(const context &Context,
46  pi_native_handle NativeHandle) {
47  const auto &ContextImpl = getSyclObjImpl(Context);
48  return detail::make_queue(NativeHandle, 0, Context, nullptr, false, {},
49  ContextImpl->get_async_handler(), backend::opencl);
50 }
51 
52 //----------------------------------------------------------------------------
53 // Free functions to query OpenCL backend extensions
54 __SYCL_EXPORT bool has_extension(const sycl::platform &SyclPlatform,
55  const std::string &Extension) {
56  if (SyclPlatform.get_backend() != sycl::backend::opencl) {
57  throw sycl::exception(
59  "has_extension can only be used with an OpenCL backend");
60  }
61 
62  std::shared_ptr<sycl::detail::platform_impl> PlatformImpl =
63  getSyclObjImpl(SyclPlatform);
64  sycl::detail::pi::PiPlatform PluginPlatform = PlatformImpl->getHandleRef();
65  const PluginPtr &Plugin = PlatformImpl->getPlugin();
66 
67  // Manual invocation of plugin API to avoid using deprecated
68  // info::platform::extensions call.
69  size_t ResultSize = 0;
70  Plugin->call<PiApiKind::piPlatformGetInfo>(
71  PluginPlatform, PI_PLATFORM_INFO_EXTENSIONS, /*param_value_size=*/0,
72  /*param_value_size=*/nullptr, &ResultSize);
73  if (ResultSize == 0)
74  return false;
75 
76  std::unique_ptr<char[]> Result(new char[ResultSize]);
77  Plugin->call<PiApiKind::piPlatformGetInfo>(PluginPlatform,
79  ResultSize, Result.get(), nullptr);
80 
81  std::string_view ExtensionsString(Result.get());
82  return ExtensionsString.find(Extension) != std::string::npos;
83 }
84 
85 __SYCL_EXPORT bool has_extension(const sycl::device &SyclDevice,
86  const std::string &Extension) {
87  if (SyclDevice.get_backend() != sycl::backend::opencl) {
88  throw sycl::exception(
90  "has_extension can only be used with an OpenCL backend");
91  }
92 
93  std::shared_ptr<sycl::detail::device_impl> DeviceImpl =
94  getSyclObjImpl(SyclDevice);
95  sycl::detail::pi::PiDevice PluginDevice = DeviceImpl->getHandleRef();
96  const PluginPtr &Plugin = DeviceImpl->getPlugin();
97 
98  // Manual invocation of plugin API to avoid using deprecated
99  // info::device::extensions call.
100  size_t ResultSize = 0;
101  Plugin->call<PiApiKind::piDeviceGetInfo>(
102  PluginDevice, PI_DEVICE_INFO_EXTENSIONS, /*param_value_size=*/0,
103  /*param_value_size=*/nullptr, &ResultSize);
104  if (ResultSize == 0)
105  return false;
106 
107  std::unique_ptr<char[]> Result(new char[ResultSize]);
108  Plugin->call<PiApiKind::piDeviceGetInfo>(PluginDevice,
110  ResultSize, Result.get(), nullptr);
111 
112  std::string_view ExtensionsString(Result.get());
113  return ExtensionsString.find(Extension) != std::string::npos;
114 }
115 } // namespace opencl
116 } // namespace _V1
117 } // namespace sycl
The context class represents a SYCL context on which kernel functions may be executed.
Definition: context.hpp:51
The SYCL device class encapsulates a single SYCL device on which kernels may be executed.
Definition: device.hpp:64
backend get_backend() const noexcept
Returns the backend associated with this device.
Definition: device.cpp:215
Encapsulates a SYCL platform on which kernels may be executed.
Definition: platform.hpp:99
backend get_backend() const noexcept
Returns the backend associated with this platform.
Definition: platform.cpp:57
Encapsulates a single SYCL queue which schedules kernels on a SYCL device.
Definition: queue.hpp:111
context make_context(pi_native_handle NativeHandle, const async_handler &Handler, backend Backend)
Definition: backend.cpp:94
void defaultAsyncHandler(exception_list Exceptions)
queue make_queue(pi_native_handle NativeHandle, int32_t nativeHandleDesc, const context &TargetContext, const device *TargetDevice, bool KeepOwnership, const property_list &PropList, const async_handler &Handler, backend Backend)
Definition: backend.cpp:107
platform make_platform(pi_native_handle NativeHandle, backend Backend)
Definition: backend.cpp:70
decltype(Obj::impl) getSyclObjImpl(const Obj &SyclObject)
Definition: impl_utils.hpp:30
device make_device(pi_native_handle NativeHandle, backend Backend)
Definition: backend.cpp:82
std::shared_ptr< plugin > PluginPtr
Definition: pi.hpp:48
device make_device(pi_native_handle NativeHandle)
Definition: opencl.cpp:32
platform make_platform(pi_native_handle NativeHandle)
Definition: opencl.cpp:26
bool has_extension(const sycl::platform &SyclPlatform, const std::string &Extension)
Definition: opencl.cpp:54
context make_context(pi_native_handle NativeHandle)
Definition: opencl.cpp:38
queue make_queue(const context &Context, pi_native_handle InteropHandle)
Definition: opencl.cpp:45
Definition: access.hpp:18
uintptr_t pi_native_handle
Definition: pi.h:217
@ PI_DEVICE_INFO_EXTENSIONS
Definition: pi.h:373
@ PI_PLATFORM_INFO_EXTENSIONS
Definition: pi.h:240
pi_result piDeviceGetInfo(pi_device device, pi_device_info param_name, size_t param_value_size, void *param_value, size_t *param_value_size_ret)
Returns requested info for provided native device Return PI_DEVICE_INFO_EXTENSION_DEVICELIB_ASSERT fo...
Definition: pi_cuda.cpp:78
pi_result piPlatformGetInfo(pi_platform platform, pi_platform_info param_name, size_t param_value_size, void *param_value, size_t *param_value_size_ret)
Definition: pi_cuda.cpp:35