Skip to content
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
97 changes: 97 additions & 0 deletions source/adapters/level_zero/v2/common.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
//===--------- common.hpp - Level Zero Adapter ---------------------------===//
//
// Copyright (C) 2024 Intel Corporation
//
// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM
// Exceptions. See LICENSE.TXT
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#pragma once

#include <exception>
#include <ze_api.h>

#include "../common.hpp"

namespace v2 {

namespace raii {

template <typename ZeHandleT, ze_result_t (*destroy)(ZeHandleT)>
struct ze_handle_wrapper {
ze_handle_wrapper(bool ownZeHandle = true)
: handle(nullptr), ownZeHandle(ownZeHandle) {}

ze_handle_wrapper(ZeHandleT handle, bool ownZeHandle = true)
: handle(handle), ownZeHandle(ownZeHandle) {}

ze_handle_wrapper(const ze_handle_wrapper &) = delete;
ze_handle_wrapper &operator=(const ze_handle_wrapper &) = delete;

ze_handle_wrapper(ze_handle_wrapper &&other)
: handle(other.handle), ownZeHandle(other.ownZeHandle) {
other.handle = nullptr;
}

ze_handle_wrapper &operator=(ze_handle_wrapper &&other) {
if (this == &other) {
return *this;
}

if (handle) {
reset();
}
handle = other.handle;
ownZeHandle = other.ownZeHandle;
other.handle = nullptr;
return *this;
}

~ze_handle_wrapper() {
try {
reset();
} catch (...) {
}
}

void reset() {
if (!handle) {
return;
}

auto zeResult = ZE_CALL_NOCHECK(destroy, (handle));
// Gracefully handle the case that L0 was already unloaded.
if (zeResult && zeResult != ZE_RESULT_ERROR_UNINITIALIZED)
throw ze2urResult(zeResult);

handle = nullptr;
}

ZeHandleT release() {
auto handle = this->handle;
this->handle = nullptr;
return handle;
}

ZeHandleT get() const { return handle; }

ZeHandleT *ptr() { return &handle; }

private:
ZeHandleT handle;
bool ownZeHandle;
};

using ze_kernel_handle_t =
ze_handle_wrapper<::ze_kernel_handle_t, zeKernelDestroy>;

using ze_event_handle_t =
ze_handle_wrapper<::ze_event_handle_t, zeEventDestroy>;

using ze_event_pool_handle_t =
ze_handle_wrapper<::ze_event_pool_handle_t, zeEventPoolDestroy>;

} // namespace raii
} // namespace v2
10 changes: 2 additions & 8 deletions source/adapters/level_zero/v2/event_provider_counter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ provider_counter::provider_counter(ur_platform_handle_t platform,
(ZEL_HANDLE_DEVICE, device->ZeDevice, (void **)&translatedDevice));
}

provider_counter::~provider_counter() {
for (auto &e : freelist) {
ZE_CALL_NOCHECK(zeEventDestroy, (e));
}
}

event_allocation provider_counter::allocate() {
if (freelist.empty()) {
ZeStruct<ze_event_desc_t> desc;
Expand All @@ -54,11 +48,11 @@ event_allocation provider_counter::allocate() {
freelist.emplace_back(handle);
}

auto event = freelist.back();
auto event = std::move(freelist.back());
freelist.pop_back();

return {event_type::EVENT_COUNTER,
event_borrowed(event, [this](ze_event_handle_t handle) {
event_borrowed(event.release(), [this](ze_event_handle_t handle) {
freelist.push_back(handle);
})};
}
Expand Down
3 changes: 1 addition & 2 deletions source/adapters/level_zero/v2/event_provider_counter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ class provider_counter : public event_provider {
public:
provider_counter(ur_platform_handle_t platform, ur_context_handle_t,
ur_device_handle_t);
~provider_counter() override;

event_allocation allocate() override;
ur_device_handle_t device() override;
Expand All @@ -48,7 +47,7 @@ class provider_counter : public event_provider {

zexCounterBasedEventCreate eventCreateFunc;

std::vector<ze_event_handle_t> freelist;
std::vector<raii::ze_event_handle_t> freelist;
};

} // namespace v2
18 changes: 6 additions & 12 deletions source/adapters/level_zero/v2/event_provider_normal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,33 +38,27 @@ provider_pool::provider_pool(ur_context_handle_t context,
ZE2UR_CALL_THROWS(zeEventPoolCreate,
(context->ZeContext, &desc, 1,
const_cast<ze_device_handle_t *>(&device->ZeDevice),
&pool));
pool.ptr()));

freelist.resize(EVENTS_BURST);
for (int i = 0; i < EVENTS_BURST; ++i) {
ZeStruct<ze_event_desc_t> desc;
desc.index = i;
desc.signal = 0;
desc.wait = 0;
ZE2UR_CALL_THROWS(zeEventCreate, (pool, &desc, &freelist[i]));
ZE2UR_CALL_THROWS(zeEventCreate, (pool.get(), &desc, freelist[i].ptr()));
}
}

provider_pool::~provider_pool() {
for (auto e : freelist) {
ZE_CALL_NOCHECK(zeEventDestroy, (e));
}
ZE_CALL_NOCHECK(zeEventPoolDestroy, (pool));
}

event_borrowed provider_pool::allocate() {
if (freelist.empty()) {
return nullptr;
}
ze_event_handle_t e = freelist.back();
auto e = std::move(freelist.back());
freelist.pop_back();
return event_borrowed(
e, [this](ze_event_handle_t handle) { freelist.push_back(handle); });
return event_borrowed(e.release(), [this](ze_event_handle_t handle) {
freelist.push_back(handle);
});
}

size_t provider_pool::nfree() const { return freelist.size(); }
Expand Down
7 changes: 2 additions & 5 deletions source/adapters/level_zero/v2/event_provider_normal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,13 @@ class provider_pool {
public:
provider_pool(ur_context_handle_t, ur_device_handle_t, event_type,
queue_type);
~provider_pool();

event_borrowed allocate();
size_t nfree() const;

private:
// TODO: use a RAII wrapper for the pool handle
ze_event_pool_handle_t pool;

std::vector<ze_event_handle_t> freelist;
raii::ze_event_pool_handle_t pool;
std::vector<raii::ze_event_handle_t> freelist;
};

class provider_normal : public event_provider {
Expand Down