Skip to content
This repository was archived by the owner on Mar 28, 2023. It is now read-only.

[SYCL] Add tests for get_native(buffer) for OpenCL and CUDA backends #945

Merged
merged 3 commits into from
Apr 26, 2022
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
17 changes: 16 additions & 1 deletion SYCL/HostInteropTask/interop-task.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// RUN: %clangxx -fsycl -DSYCL2020_CONFORMANT_APIS -fsycl-targets=%sycl_triple %s -o %t.out %threads_lib %opencl_lib
// RUN: %CPU_RUN_PLACEHOLDER %t.out
// RUN: %GPU_RUN_PLACEHOLDER %t.out
// RUN: %ACC_RUN_PLACEHOLDER %t.out
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out %threads_lib %opencl_lib
// RUN: %CPU_RUN_PLACEHOLDER %t.out
// RUN: %GPU_RUN_PLACEHOLDER %t.out
Expand Down Expand Up @@ -42,9 +46,15 @@ void copy(buffer<DataT, 1> &Src, buffer<DataT, 1> &Dst, queue &Q) {
auto DstMem = IH.get_native_mem<backend::opencl>(DstA);
cl_event Event;

#ifdef SYCL2020_CONFORMANT_APIS
int RC = clEnqueueCopyBuffer(NativeQ, SrcMem[0], DstMem[0], 0, 0,
sizeof(DataT) * SrcA.get_count(), 0, nullptr,
&Event);
#else
int RC = clEnqueueCopyBuffer(NativeQ, SrcMem, DstMem, 0, 0,
sizeof(DataT) * SrcA.get_count(), 0, nullptr,
&Event);
#endif

if (RC != CL_SUCCESS)
throw runtime_error("Can't enqueue buffer copy", RC);
Expand Down Expand Up @@ -169,10 +179,15 @@ void test3(queue &Q) {

Q.submit([&](handler &CGH) {
auto Func = [=](interop_handle IH) {
#ifdef SYCL2020_CONFORMANT_APIS
std::vector<cl_event> Ev = get_native<backend::opencl>(Event);

int RC = clWaitForEvents(1, Ev.data());
#else
cl_event Ev = get_native<backend::opencl>(Event);

int RC = clWaitForEvents(1, &Ev);

#endif
if (RC != CL_SUCCESS)
throw runtime_error("Can't wait for events", RC);
};
Expand Down
29 changes: 29 additions & 0 deletions SYCL/Plugin/interop-buffer-cuda.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// REQUIRES: cuda

// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out
// RUN: %GPU_RUN_PLACEHOLDER %t.out

//==--- interop-cuda.cpp - SYCL test for CUDA buffer interop API ----------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include <CL/sycl.hpp>

int main() {
int Data[1] = {0};
sycl::buffer<int, 1> Buffer(&Data[0], sycl::range<1>(1));
{
sycl::queue Queue{sycl::gpu_selector()};
Queue.submit([&](sycl::handler &cgh) {
auto Acc = Buffer.get_access<sycl::access::mode::read_write>(cgh);
cgh.single_task<class kernel>([=]() { (void)Acc; });
});
}

auto NativeObj = sycl::get_native<sycl::backend::ext_oneapi_cuda>(Buffer);
assert(NativeObj != 0);
}
31 changes: 31 additions & 0 deletions SYCL/Plugin/interop-opencl.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// REQUIRES: opencl

// RUN: %clangxx -fsycl -DSYCL2020_CONFORMANT_APIS -fsycl-targets=%sycl_triple %s -o %t.out
// RUN: env SYCL_DEVICE_FILTER=opencl %CPU_RUN_PLACEHOLDER %t.out
// RUN: env SYCL_DEVICE_FILTER=opencl %GPU_RUN_PLACEHOLDER %t.out
// RUN: env SYCL_DEVICE_FILTER=opencl %ACC_RUN_PLACEHOLDER %t.out
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out
// RUN: env SYCL_DEVICE_FILTER=opencl %CPU_RUN_PLACEHOLDER %t.out
// RUN: env SYCL_DEVICE_FILTER=opencl %GPU_RUN_PLACEHOLDER %t.out
Expand All @@ -25,23 +29,50 @@ int main() {
auto Device = Queue.get_info<info::queue::device>();
auto Platform = Device.get_info<info::device::platform>();

int Data[1] = {0};
sycl::buffer<int, 1> Buffer(&Data[0], sycl::range<1>(1));
{
Queue.submit([&](sycl::handler &cgh) {
auto Acc = Buffer.get_access<sycl::access::mode::read_write>(cgh);
cgh.host_task([=](const sycl::interop_handle &ih) {
(void)Acc;
auto BufNative = ih.get_native_mem<sycl::backend::opencl>(Acc);
#ifdef SYCL2020_CONFORMANT_APIS
assert(BufNative.size() == 1);
#endif
});
});
}

// Get native OpenCL handles
auto ocl_platform = Platform.get_native<backend::opencl>();
auto ocl_device = Device.get_native<backend::opencl>();
auto ocl_context = Context.get_native<backend::opencl>();
auto ocl_queue = Queue.get_native<backend::opencl>();
auto ocl_buffers = get_native<backend::opencl>(Buffer);
#ifdef SYCL2020_CONFORMANT_APIS
assert(ocl_buffers.size() == 1);
#endif

// Re-create SYCL objects from native OpenCL handles
auto PlatformInterop = opencl::make<platform>(ocl_platform);
auto DeviceInterop = opencl::make<device>(ocl_device);
auto ContextInterop = opencl::make<context>(ocl_context);
auto QueueInterop = opencl::make<queue>(ContextInterop, ocl_queue);
#ifdef SYCL2020_CONFORMANT_APIS
auto BufferInterop =
sycl::make_buffer<backend::opencl, int>(ocl_buffers[0], ContextInterop);
#else
auto BufferInterop =
sycl::make_buffer<backend::opencl, int>(ocl_buffers, ContextInterop);
#endif

// Check native handles
assert(ocl_platform == PlatformInterop.get_native<backend::opencl>());
assert(ocl_device == DeviceInterop.get_native<backend::opencl>());
assert(ocl_context == ContextInterop.get_native<backend::opencl>());
assert(ocl_queue == QueueInterop.get_native<backend::opencl>());
assert(ocl_buffers == get_native<backend::opencl>(BufferInterop));

return 0;
}