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

[SYCL] E2E test for interop that keeps ownership in the app #153

Merged
merged 5 commits into from
Feb 25, 2021
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
39 changes: 39 additions & 0 deletions SYCL/Plugin/interop-level-zero-keep-ownership.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// REQUIRES: level_zero,level_zero_headers
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple -I %sycl_source_dir %s -o %t.out -lze_loader
// RUN: env SYCL_BE=PI_LEVEL_ZERO %GPU_RUN_PLACEHOLDER %t.out

// Test for Level Zero interop API where SYCL RT doesn't take ownership

#include <CL/sycl.hpp>
// clang-format off
#include <level_zero/ze_api.h>
#include <CL/sycl/backend/level_zero.hpp>
// clang-format on

using namespace cl::sycl;

int main() {

// Creat SYCL platform/device
device Device(gpu_selector{});
platform Platform = Device.get_info<info::device::platform>();

// Create native Level-Zero context
ze_context_handle_t ZeContext;
ze_context_desc_t ZeContextDesc = {ZE_STRUCTURE_TYPE_CONTEXT_DESC, nullptr,
0};
auto ZeDriver = Platform.get_native<backend::level_zero>();
auto ZeDevice = Device.get_native<backend::level_zero>();
zeContextCreate(ZeDriver, &ZeContextDesc, &ZeContext);

{ // Scope in which SYCL interop context object is live
vector_class<device> Devices{};
Devices.push_back(Device);
auto ContextInterop = level_zero::make<context>(
Devices, ZeContext, level_zero::ownership::keep);
}

// Verifies that Level-Zero context is not destroyed by SYCL RT yet.
zeContextDestroy(ZeContext);
return 0;
}
15 changes: 11 additions & 4 deletions SYCL/Plugin/interop-level-zero.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// REQUIRES: level_zero,level_zero_headers
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple -I %sycl_source_dir %s -o %t.out
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple -I %sycl_source_dir %s -o %t.out -lze_loader
// RUN: env SYCL_BE=PI_LEVEL_ZERO %GPU_RUN_PLACEHOLDER %t.out

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

// Create native Level-Zero context.
// It then will be owned/destroyed by SYCL RT.
ze_context_handle_t ZeContextInterop{};
ze_context_desc_t ZeContextDesc = {ZE_STRUCTURE_TYPE_CONTEXT_DESC, nullptr,
0};
zeContextCreate(ZePlatform, &ZeContextDesc, &ZeContextInterop);

// Re-create SYCL objects from native Level Zero handles
auto PlatformInterop = level_zero::make<platform>(ZePlatform);
auto DeviceInterop = level_zero::make<device>(PlatformInterop, ZeDevice);
auto ContextInterop =
level_zero::make<context>(PlatformInterop.get_devices(), ZeContext);
auto ContextInterop = level_zero::make<context>(PlatformInterop.get_devices(),
ZeContextInterop);
auto QueueInterop = level_zero::make<queue>(ContextInterop, ZeQueue);

// Check native handles
assert(ZePlatform == PlatformInterop.get_native<backend::level_zero>());
assert(ZeDevice == DeviceInterop.get_native<backend::level_zero>());
assert(ZeContext == ContextInterop.get_native<backend::level_zero>());
assert(ZeContextInterop == ContextInterop.get_native<backend::level_zero>());
assert(ZeQueue == QueueInterop.get_native<backend::level_zero>());

// Verify re-created objects
Expand Down