Skip to content

[SYCL] Fix opencl/sycl interoperability #2234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions sycl/source/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ context::context(const vector_class<device> &DeviceList,
}
}
context::context(cl_context ClContext, async_handler AsyncHandler) {
const auto &Plugin = RT::getPlugin<backend::opencl>();
impl = std::make_shared<detail::context_impl>(
detail::pi::cast<detail::RT::PiContext>(ClContext), AsyncHandler,
*RT::GlobalPlugin);
detail::pi::cast<detail::RT::PiContext>(ClContext), AsyncHandler, Plugin);
}

#define PARAM_TRAITS_SPEC(param_type, param, ret_type) \
Expand Down
3 changes: 2 additions & 1 deletion sycl/source/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ device::device() : impl(std::make_shared<detail::device_impl>()) {}

device::device(cl_device_id deviceId)
: impl(std::make_shared<detail::device_impl>(
detail::pi::cast<pi_native_handle>(deviceId), *RT::GlobalPlugin)) {
detail::pi::cast<pi_native_handle>(deviceId),
RT::getPlugin<backend::opencl>())) {
// The implementation constructor takes ownership of the native handle so we
// must retain it in order to adhere to SYCL 1.2.1 spec (Rev6, section 4.3.1.)
clRetainDevice(deviceId);
Expand Down
2 changes: 1 addition & 1 deletion sycl/source/platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ platform::platform() : impl(std::make_shared<detail::platform_impl>()) {}
platform::platform(cl_platform_id PlatformId)
: impl(std::make_shared<detail::platform_impl>(
detail::pi::cast<detail::RT::PiPlatform>(PlatformId),
RT::GlobalPlugin)) {}
RT::getPlugin<backend::opencl>())) {}

platform::platform(const device_selector &dev_selector) {
*this = dev_selector.select_device().get_platform();
Expand Down
81 changes: 81 additions & 0 deletions sycl/test/basic_tests/opencl_interop.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// REQUIRES: opencl

// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out -L %opencl_libs_dir -lOpenCL
// RUN: %CPU_RUN_PLACEHOLDER %t.out
// RUN: %GPU_RUN_PLACEHOLDER %t.out
// RUN: %ACC_RUN_PLACEHOLDER %t.out

#include <CL/sycl.hpp>
#include <cassert>
#include <exception>
#include <vector>

#define CL_CHECK_ERRORS(ERR) \
if (ERR != CL_SUCCESS) { \
throw std::runtime_error("Error in OpenCL object creation."); \
}

cl_platform_id selectOpenCLPlatform() {
cl_int err = 0;
cl_uint num_of_platforms = 0;

err = clGetPlatformIDs(0, NULL, &num_of_platforms);
CL_CHECK_ERRORS(err);

std::vector<cl_platform_id> platforms(num_of_platforms);

err = clGetPlatformIDs(num_of_platforms, &platforms[0], 0);
CL_CHECK_ERRORS(err);

return platforms[0];
}

cl_device_id selectOpenCLDevice(cl_platform_id platform) {
cl_int err = 0;
cl_uint num_of_devices = 0;

err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, 0, 0, &num_of_devices);
CL_CHECK_ERRORS(err);

std::vector<cl_device_id> devices(num_of_devices);

err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, num_of_devices,
&devices[0], 0);
CL_CHECK_ERRORS(err);

return devices[0];
}

cl_context createOpenCLContext(cl_platform_id platform, cl_device_id device) {
cl_int err = 0;
cl_context context;
std::vector<cl_context_properties> context_props(3);

context_props[0] = CL_CONTEXT_PLATFORM;
context_props[1] = cl_context_properties(platform);
context_props.back() = 0;

context = clCreateContext(&context_props[0], 1, &device, 0, 0, &err);
CL_CHECK_ERRORS(err);

return context;
}

int main() {
cl_platform_id ocl_platform = selectOpenCLPlatform();
cl::sycl::platform syclPlt(ocl_platform);
assert(ocl_platform == syclPlt.get() &&
"SYCL returns invalid OpenCL platform id");

cl_device_id ocl_device = selectOpenCLDevice(ocl_platform);
cl::sycl::device syclDev(ocl_device);
assert(ocl_device == syclDev.get() &&
"SYCL returns invalid OpenCL device id");

cl_context ocl_context = createOpenCLContext(ocl_platform, ocl_device);
cl::sycl::context syclContext(ocl_context);
assert(ocl_context == syclContext.get() &&
"SYCL returns invalid OpenCL context id");

return 0;
}