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

Commit 0d30e9f

Browse files
[SYCL] E2E test for interop_task with Level-Zero and OpenCL (#252)
1 parent 4c2c1c5 commit 0d30e9f

File tree

2 files changed

+94
-33
lines changed

2 files changed

+94
-33
lines changed

SYCL/Plugin/interop-level-zero-interop-task-mem.cpp

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,54 @@
22
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %level_zero_options %s -o %t.out
33
// RUN: env SYCL_BE=PI_LEVEL_ZERO %GPU_RUN_PLACEHOLDER %t.out
44

5-
// Test fails on Level Zero on Linux
6-
// UNSUPPORTED: level_zero && linux
5+
// Test for Level Zero interop_task.
76

8-
// Test for Level Zero interop_task
7+
// Level-Zero
8+
#include <level_zero/ze_api.h>
99

10+
// SYCL
1011
#include <CL/sycl.hpp>
11-
// clang-format off
12-
#include <level_zero/ze_api.h>
1312
#include <CL/sycl/backend/level_zero.hpp>
14-
// clang-format on
15-
16-
class my_selector : public cl::sycl::device_selector {
17-
public:
18-
int operator()(const cl::sycl::device &dev) const override {
19-
return (dev.get_platform().get_backend() == cl::sycl::backend::level_zero)
20-
? 1
21-
: 0;
22-
}
23-
};
13+
14+
using namespace sycl;
15+
16+
constexpr size_t SIZE = 16;
2417

2518
int main() {
26-
sycl::queue sycl_queue = sycl::queue(my_selector());
27-
28-
ze_context_handle_t ze_context =
29-
sycl_queue.get_context().get_native<sycl::backend::level_zero>();
30-
std::cout << "zeContextGetStatus = " << zeContextGetStatus(ze_context)
31-
<< std::endl;
32-
33-
auto buf = cl::sycl::buffer<uint8_t, 1>(1024);
34-
sycl_queue.submit([&](cl::sycl::handler &cgh) {
35-
auto acc = buf.get_access<cl::sycl::access::mode::write>(cgh);
36-
cgh.interop_task([&](const cl::sycl::interop_handler &ih) {
37-
void *device_ptr = ih.get_mem<sycl::backend::level_zero>(acc);
38-
ze_memory_allocation_properties_t memAllocProperties{};
39-
zeMemGetAllocProperties(ze_context, device_ptr, &memAllocProperties,
40-
nullptr);
41-
std::cout << "Memory type = " << memAllocProperties.type << std::endl;
42-
});
43-
});
19+
queue queue{};
20+
21+
try {
22+
buffer<uint8_t, 1> buffer(SIZE);
23+
image<2> image(image_channel_order::rgba, image_channel_type::fp32,
24+
{SIZE, SIZE});
25+
26+
ze_context_handle_t ze_context =
27+
queue.get_context().get_native<backend::level_zero>();
28+
29+
queue
30+
.submit([&](handler &cgh) {
31+
auto buffer_acc = buffer.get_access<access::mode::write>(cgh);
32+
auto image_acc = image.get_access<float4, access::mode::write>(cgh);
33+
cgh.interop_task([=](const interop_handler &ih) {
34+
void *device_ptr = ih.get_mem<backend::level_zero>(buffer_acc);
35+
ze_memory_allocation_properties_t memAllocProperties{};
36+
ze_result_t res = zeMemGetAllocProperties(
37+
ze_context, device_ptr, &memAllocProperties, nullptr);
38+
assert(res == ZE_RESULT_SUCCESS);
39+
40+
ze_image_handle_t ze_image =
41+
ih.get_mem<backend::level_zero>(image_acc);
42+
assert(ze_image != nullptr);
43+
});
44+
})
45+
.wait();
46+
} catch (exception const &e) {
47+
std::cout << "SYCL exception caught: " << e.what() << std::endl;
48+
return e.get_cl_code();
49+
} catch (const char *msg) {
50+
std::cout << "Exception caught: " << msg << std::endl;
51+
return 1;
52+
}
4453

4554
return 0;
4655
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// REQUIRES: opencl, opencl_icd
2+
3+
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out %opencl_lib
4+
// RUN: %GPU_RUN_PLACEHOLDER %t.out
5+
6+
// Test for OpenCL interop_task.
7+
8+
#include <CL/opencl.h>
9+
#include <CL/sycl.hpp>
10+
#include <CL/sycl/backend/opencl.hpp>
11+
12+
using namespace sycl;
13+
14+
constexpr size_t SIZE = 16;
15+
16+
int main() {
17+
queue queue{};
18+
19+
try {
20+
buffer<uint8_t, 1> buffer(SIZE);
21+
image<2> image(image_channel_order::rgba, image_channel_type::fp32,
22+
{SIZE, SIZE});
23+
24+
queue
25+
.submit([&](handler &cgh) {
26+
auto buffer_acc = buffer.get_access<access::mode::write>(cgh);
27+
auto image_acc = image.get_access<float4, access::mode::write>(cgh);
28+
cgh.interop_task([=](const interop_handler &ih) {
29+
cl_mem buffer_mem = ih.get_mem<backend::opencl>(buffer_acc);
30+
size_t size = 0;
31+
clGetMemObjectInfo(buffer_mem, CL_MEM_SIZE, sizeof(size),
32+
(void *)&size, nullptr);
33+
assert(size == SIZE);
34+
35+
cl_mem mem = ih.get_mem<backend::opencl>(image_acc);
36+
size_t width = 0;
37+
clGetImageInfo(mem, CL_IMAGE_WIDTH, sizeof(width), (void *)&width,
38+
nullptr);
39+
assert(width == SIZE);
40+
});
41+
})
42+
.wait();
43+
} catch (exception const &e) {
44+
std::cout << "SYCL exception caught: " << e.what() << std::endl;
45+
return e.get_cl_code();
46+
} catch (const char *msg) {
47+
std::cout << "Exception caught: " << msg << std::endl;
48+
return 1;
49+
}
50+
51+
return 0;
52+
}

0 commit comments

Comments
 (0)