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 {
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) {
40  backend::opencl);
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, 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(
58  errc::backend_mismatch,
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  detail::RT::PiPlatform PluginPlatform = PlatformImpl->getHandleRef();
65  const plugin &Plugin = PlatformImpl->getPlugin();
66 
67  // Manual invocation of plugin API to avoid using deprecated
68  // info::platform::extensions call.
69  size_t ResultSize = 0;
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(
89  errc::backend_mismatch,
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  detail::RT::PiDevice PluginDevice = DeviceImpl->getHandleRef();
96  const plugin &Plugin = DeviceImpl->getPlugin();
97 
98  // Manual invocation of plugin API to avoid using deprecated
99  // info::device::extensions call.
100  size_t ResultSize = 0;
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,
109  PI_DEVICE_INFO_EXTENSIONS, ResultSize,
110  Result.get(), nullptr);
111 
112  std::string_view ExtensionsString(Result.get());
113  return ExtensionsString.find(Extension) != std::string::npos;
114 }
115 } // namespace opencl
116 } // __SYCL_INLINE_VER_NAMESPACE(_V1)
117 } // namespace sycl
The context class represents a SYCL context on which kernel functions may be executed.
Definition: context.hpp:39
The plugin class provides a unified interface to the underlying low-level runtimes for the device-agn...
Definition: plugin.hpp:90
void call(ArgsT... Args) const
Calls the API, traces the call, checks the result.
Definition: plugin.hpp:217
The SYCL device class encapsulates a single SYCL device on which kernels may be executed.
Definition: device.hpp:47
Encapsulates a SYCL platform on which kernels may be executed.
Definition: platform.hpp:45
Encapsulates a single SYCL queue which schedules kernels on a SYCL device.
Definition: queue.hpp:86
#define __SYCL_INLINE_VER_NAMESPACE(X)
::pi_device PiDevice
Definition: pi.hpp:110
::pi_platform PiPlatform
Definition: pi.hpp:109
void defaultAsyncHandler(exception_list Exceptions)
decltype(Obj::impl) getSyclObjImpl(const Obj &SyclObject)
Definition: common.hpp:240
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
---— Error handling, matching OpenCL plugin semantics.
Definition: access.hpp:14
uintptr_t pi_native_handle
Definition: pi.h:109
@ PI_DEVICE_INFO_EXTENSIONS
Definition: pi.h:254
@ PI_PLATFORM_INFO_EXTENSIONS
Definition: pi.h:132
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...
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)