Skip to content

Commit c4c3494

Browse files
authored
[SYCL] Fix opencl/sycl interoperability (#2234)
Currently SYCL object creation from OpenCL object fails with segmentation fault because of invalid GlobalPlugin. This patch fixes opencl/sycl interop constructor by initializing GlobalPlugin before using it. Signed-off-by: Nawrin Sultana <nawrin.sultana@intel.com>
1 parent cf10351 commit c4c3494

File tree

4 files changed

+86
-4
lines changed

4 files changed

+86
-4
lines changed

sycl/source/context.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ context::context(const vector_class<device> &DeviceList,
6767
}
6868
}
6969
context::context(cl_context ClContext, async_handler AsyncHandler) {
70+
const auto &Plugin = RT::getPlugin<backend::opencl>();
7071
impl = std::make_shared<detail::context_impl>(
71-
detail::pi::cast<detail::RT::PiContext>(ClContext), AsyncHandler,
72-
*RT::GlobalPlugin);
72+
detail::pi::cast<detail::RT::PiContext>(ClContext), AsyncHandler, Plugin);
7373
}
7474

7575
#define PARAM_TRAITS_SPEC(param_type, param, ret_type) \

sycl/source/device.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ device::device() : impl(std::make_shared<detail::device_impl>()) {}
3131

3232
device::device(cl_device_id deviceId)
3333
: impl(std::make_shared<detail::device_impl>(
34-
detail::pi::cast<pi_native_handle>(deviceId), *RT::GlobalPlugin)) {
34+
detail::pi::cast<pi_native_handle>(deviceId),
35+
RT::getPlugin<backend::opencl>())) {
3536
// The implementation constructor takes ownership of the native handle so we
3637
// must retain it in order to adhere to SYCL 1.2.1 spec (Rev6, section 4.3.1.)
3738
clRetainDevice(deviceId);

sycl/source/platform.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ platform::platform() : impl(std::make_shared<detail::platform_impl>()) {}
2121
platform::platform(cl_platform_id PlatformId)
2222
: impl(std::make_shared<detail::platform_impl>(
2323
detail::pi::cast<detail::RT::PiPlatform>(PlatformId),
24-
RT::GlobalPlugin)) {}
24+
RT::getPlugin<backend::opencl>())) {}
2525

2626
platform::platform(const device_selector &dev_selector) {
2727
*this = dev_selector.select_device().get_platform();
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// REQUIRES: opencl
2+
3+
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out -L %opencl_libs_dir -lOpenCL
4+
// RUN: %CPU_RUN_PLACEHOLDER %t.out
5+
// RUN: %GPU_RUN_PLACEHOLDER %t.out
6+
// RUN: %ACC_RUN_PLACEHOLDER %t.out
7+
8+
#include <CL/sycl.hpp>
9+
#include <cassert>
10+
#include <exception>
11+
#include <vector>
12+
13+
#define CL_CHECK_ERRORS(ERR) \
14+
if (ERR != CL_SUCCESS) { \
15+
throw std::runtime_error("Error in OpenCL object creation."); \
16+
}
17+
18+
cl_platform_id selectOpenCLPlatform() {
19+
cl_int err = 0;
20+
cl_uint num_of_platforms = 0;
21+
22+
err = clGetPlatformIDs(0, NULL, &num_of_platforms);
23+
CL_CHECK_ERRORS(err);
24+
25+
std::vector<cl_platform_id> platforms(num_of_platforms);
26+
27+
err = clGetPlatformIDs(num_of_platforms, &platforms[0], 0);
28+
CL_CHECK_ERRORS(err);
29+
30+
return platforms[0];
31+
}
32+
33+
cl_device_id selectOpenCLDevice(cl_platform_id platform) {
34+
cl_int err = 0;
35+
cl_uint num_of_devices = 0;
36+
37+
err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, 0, 0, &num_of_devices);
38+
CL_CHECK_ERRORS(err);
39+
40+
std::vector<cl_device_id> devices(num_of_devices);
41+
42+
err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, num_of_devices,
43+
&devices[0], 0);
44+
CL_CHECK_ERRORS(err);
45+
46+
return devices[0];
47+
}
48+
49+
cl_context createOpenCLContext(cl_platform_id platform, cl_device_id device) {
50+
cl_int err = 0;
51+
cl_context context;
52+
std::vector<cl_context_properties> context_props(3);
53+
54+
context_props[0] = CL_CONTEXT_PLATFORM;
55+
context_props[1] = cl_context_properties(platform);
56+
context_props.back() = 0;
57+
58+
context = clCreateContext(&context_props[0], 1, &device, 0, 0, &err);
59+
CL_CHECK_ERRORS(err);
60+
61+
return context;
62+
}
63+
64+
int main() {
65+
cl_platform_id ocl_platform = selectOpenCLPlatform();
66+
cl::sycl::platform syclPlt(ocl_platform);
67+
assert(ocl_platform == syclPlt.get() &&
68+
"SYCL returns invalid OpenCL platform id");
69+
70+
cl_device_id ocl_device = selectOpenCLDevice(ocl_platform);
71+
cl::sycl::device syclDev(ocl_device);
72+
assert(ocl_device == syclDev.get() &&
73+
"SYCL returns invalid OpenCL device id");
74+
75+
cl_context ocl_context = createOpenCLContext(ocl_platform, ocl_device);
76+
cl::sycl::context syclContext(ocl_context);
77+
assert(ocl_context == syclContext.get() &&
78+
"SYCL returns invalid OpenCL context id");
79+
80+
return 0;
81+
}

0 commit comments

Comments
 (0)