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

Commit dd5b9df

Browse files
[SYCL] E2E test for interop that keeps ownership in the app (#153)
1 parent 23c261c commit dd5b9df

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// REQUIRES: level_zero,level_zero_headers
2+
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple -I %sycl_source_dir %s -o %t.out -lze_loader
3+
// RUN: env SYCL_BE=PI_LEVEL_ZERO %GPU_RUN_PLACEHOLDER %t.out
4+
5+
// Test for Level Zero interop API where SYCL RT doesn't take ownership
6+
7+
#include <CL/sycl.hpp>
8+
// clang-format off
9+
#include <level_zero/ze_api.h>
10+
#include <CL/sycl/backend/level_zero.hpp>
11+
// clang-format on
12+
13+
using namespace cl::sycl;
14+
15+
int main() {
16+
17+
// Creat SYCL platform/device
18+
device Device(gpu_selector{});
19+
platform Platform = Device.get_info<info::device::platform>();
20+
21+
// Create native Level-Zero context
22+
ze_context_handle_t ZeContext;
23+
ze_context_desc_t ZeContextDesc = {ZE_STRUCTURE_TYPE_CONTEXT_DESC, nullptr,
24+
0};
25+
auto ZeDriver = Platform.get_native<backend::level_zero>();
26+
auto ZeDevice = Device.get_native<backend::level_zero>();
27+
zeContextCreate(ZeDriver, &ZeContextDesc, &ZeContext);
28+
29+
{ // Scope in which SYCL interop context object is live
30+
vector_class<device> Devices{};
31+
Devices.push_back(Device);
32+
auto ContextInterop = level_zero::make<context>(
33+
Devices, ZeContext, level_zero::ownership::keep);
34+
}
35+
36+
// Verifies that Level-Zero context is not destroyed by SYCL RT yet.
37+
zeContextDestroy(ZeContext);
38+
return 0;
39+
}

SYCL/Plugin/interop-level-zero.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// REQUIRES: level_zero,level_zero_headers
2-
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple -I %sycl_source_dir %s -o %t.out
2+
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple -I %sycl_source_dir %s -o %t.out -lze_loader
33
// RUN: env SYCL_BE=PI_LEVEL_ZERO %GPU_RUN_PLACEHOLDER %t.out
44

55
// Test for Level Zero interop API
@@ -24,17 +24,24 @@ int main() {
2424
auto ZeContext = Context.get_native<backend::level_zero>();
2525
auto ZeQueue = Queue.get_native<backend::level_zero>();
2626

27+
// Create native Level-Zero context.
28+
// It then will be owned/destroyed by SYCL RT.
29+
ze_context_handle_t ZeContextInterop{};
30+
ze_context_desc_t ZeContextDesc = {ZE_STRUCTURE_TYPE_CONTEXT_DESC, nullptr,
31+
0};
32+
zeContextCreate(ZePlatform, &ZeContextDesc, &ZeContextInterop);
33+
2734
// Re-create SYCL objects from native Level Zero handles
2835
auto PlatformInterop = level_zero::make<platform>(ZePlatform);
2936
auto DeviceInterop = level_zero::make<device>(PlatformInterop, ZeDevice);
30-
auto ContextInterop =
31-
level_zero::make<context>(PlatformInterop.get_devices(), ZeContext);
37+
auto ContextInterop = level_zero::make<context>(PlatformInterop.get_devices(),
38+
ZeContextInterop);
3239
auto QueueInterop = level_zero::make<queue>(ContextInterop, ZeQueue);
3340

3441
// Check native handles
3542
assert(ZePlatform == PlatformInterop.get_native<backend::level_zero>());
3643
assert(ZeDevice == DeviceInterop.get_native<backend::level_zero>());
37-
assert(ZeContext == ContextInterop.get_native<backend::level_zero>());
44+
assert(ZeContextInterop == ContextInterop.get_native<backend::level_zero>());
3845
assert(ZeQueue == QueueInterop.get_native<backend::level_zero>());
3946

4047
// Verify re-created objects

0 commit comments

Comments
 (0)