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

Commit bb1a99c

Browse files
authored
[SYCL] Add tests for get_native(buffer) for OpenCL and CUDA backends (#945)
1 parent d1abced commit bb1a99c

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

SYCL/HostInteropTask/interop-task.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// RUN: %clangxx -fsycl -DSYCL2020_CONFORMANT_APIS -fsycl-targets=%sycl_triple %s -o %t.out %threads_lib %opencl_lib
2+
// RUN: %CPU_RUN_PLACEHOLDER %t.out
3+
// RUN: %GPU_RUN_PLACEHOLDER %t.out
4+
// RUN: %ACC_RUN_PLACEHOLDER %t.out
15
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out %threads_lib %opencl_lib
26
// RUN: %CPU_RUN_PLACEHOLDER %t.out
37
// RUN: %GPU_RUN_PLACEHOLDER %t.out
@@ -42,9 +46,15 @@ void copy(buffer<DataT, 1> &Src, buffer<DataT, 1> &Dst, queue &Q) {
4246
auto DstMem = IH.get_native_mem<backend::opencl>(DstA);
4347
cl_event Event;
4448

49+
#ifdef SYCL2020_CONFORMANT_APIS
50+
int RC = clEnqueueCopyBuffer(NativeQ, SrcMem[0], DstMem[0], 0, 0,
51+
sizeof(DataT) * SrcA.get_count(), 0, nullptr,
52+
&Event);
53+
#else
4554
int RC = clEnqueueCopyBuffer(NativeQ, SrcMem, DstMem, 0, 0,
4655
sizeof(DataT) * SrcA.get_count(), 0, nullptr,
4756
&Event);
57+
#endif
4858

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

170180
Q.submit([&](handler &CGH) {
171181
auto Func = [=](interop_handle IH) {
182+
#ifdef SYCL2020_CONFORMANT_APIS
183+
std::vector<cl_event> Ev = get_native<backend::opencl>(Event);
184+
185+
int RC = clWaitForEvents(1, Ev.data());
186+
#else
172187
cl_event Ev = get_native<backend::opencl>(Event);
173188

174189
int RC = clWaitForEvents(1, &Ev);
175-
190+
#endif
176191
if (RC != CL_SUCCESS)
177192
throw runtime_error("Can't wait for events", RC);
178193
};

SYCL/Plugin/interop-buffer-cuda.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// REQUIRES: cuda
2+
3+
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out
4+
// RUN: %GPU_RUN_PLACEHOLDER %t.out
5+
6+
//==--- interop-cuda.cpp - SYCL test for CUDA buffer interop API ----------===//
7+
//
8+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
9+
// See https://llvm.org/LICENSE.txt for license information.
10+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#include <CL/sycl.hpp>
15+
16+
int main() {
17+
int Data[1] = {0};
18+
sycl::buffer<int, 1> Buffer(&Data[0], sycl::range<1>(1));
19+
{
20+
sycl::queue Queue{sycl::gpu_selector()};
21+
Queue.submit([&](sycl::handler &cgh) {
22+
auto Acc = Buffer.get_access<sycl::access::mode::read_write>(cgh);
23+
cgh.single_task<class kernel>([=]() { (void)Acc; });
24+
});
25+
}
26+
27+
auto NativeObj = sycl::get_native<sycl::backend::ext_oneapi_cuda>(Buffer);
28+
assert(NativeObj != 0);
29+
}

SYCL/Plugin/interop-opencl.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
// REQUIRES: opencl
22

3+
// RUN: %clangxx -fsycl -DSYCL2020_CONFORMANT_APIS -fsycl-targets=%sycl_triple %s -o %t.out
4+
// RUN: env SYCL_DEVICE_FILTER=opencl %CPU_RUN_PLACEHOLDER %t.out
5+
// RUN: env SYCL_DEVICE_FILTER=opencl %GPU_RUN_PLACEHOLDER %t.out
6+
// RUN: env SYCL_DEVICE_FILTER=opencl %ACC_RUN_PLACEHOLDER %t.out
37
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out
48
// RUN: env SYCL_DEVICE_FILTER=opencl %CPU_RUN_PLACEHOLDER %t.out
59
// RUN: env SYCL_DEVICE_FILTER=opencl %GPU_RUN_PLACEHOLDER %t.out
@@ -25,23 +29,50 @@ int main() {
2529
auto Device = Queue.get_info<info::queue::device>();
2630
auto Platform = Device.get_info<info::device::platform>();
2731

32+
int Data[1] = {0};
33+
sycl::buffer<int, 1> Buffer(&Data[0], sycl::range<1>(1));
34+
{
35+
Queue.submit([&](sycl::handler &cgh) {
36+
auto Acc = Buffer.get_access<sycl::access::mode::read_write>(cgh);
37+
cgh.host_task([=](const sycl::interop_handle &ih) {
38+
(void)Acc;
39+
auto BufNative = ih.get_native_mem<sycl::backend::opencl>(Acc);
40+
#ifdef SYCL2020_CONFORMANT_APIS
41+
assert(BufNative.size() == 1);
42+
#endif
43+
});
44+
});
45+
}
46+
2847
// Get native OpenCL handles
2948
auto ocl_platform = Platform.get_native<backend::opencl>();
3049
auto ocl_device = Device.get_native<backend::opencl>();
3150
auto ocl_context = Context.get_native<backend::opencl>();
3251
auto ocl_queue = Queue.get_native<backend::opencl>();
52+
auto ocl_buffers = get_native<backend::opencl>(Buffer);
53+
#ifdef SYCL2020_CONFORMANT_APIS
54+
assert(ocl_buffers.size() == 1);
55+
#endif
3356

3457
// Re-create SYCL objects from native OpenCL handles
3558
auto PlatformInterop = opencl::make<platform>(ocl_platform);
3659
auto DeviceInterop = opencl::make<device>(ocl_device);
3760
auto ContextInterop = opencl::make<context>(ocl_context);
3861
auto QueueInterop = opencl::make<queue>(ContextInterop, ocl_queue);
62+
#ifdef SYCL2020_CONFORMANT_APIS
63+
auto BufferInterop =
64+
sycl::make_buffer<backend::opencl, int>(ocl_buffers[0], ContextInterop);
65+
#else
66+
auto BufferInterop =
67+
sycl::make_buffer<backend::opencl, int>(ocl_buffers, ContextInterop);
68+
#endif
3969

4070
// Check native handles
4171
assert(ocl_platform == PlatformInterop.get_native<backend::opencl>());
4272
assert(ocl_device == DeviceInterop.get_native<backend::opencl>());
4373
assert(ocl_context == ContextInterop.get_native<backend::opencl>());
4474
assert(ocl_queue == QueueInterop.get_native<backend::opencl>());
75+
assert(ocl_buffers == get_native<backend::opencl>(BufferInterop));
4576

4677
return 0;
4778
}

0 commit comments

Comments
 (0)