Skip to content

Commit 00940d2

Browse files
authored
[UR][L0 v2] Port USM alloc to adapter v2 (#18179)
This PR ports USM alloc enqueue API introduced to L0 adapter in intel/llvm#17112 to L0 adapter v2.
1 parent 23c4c4b commit 00940d2

17 files changed

Lines changed: 283 additions & 53 deletions

sycl/test-e2e/AsyncAlloc/device/async_alloc_from_default_pool.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// RUN: %{build} -o %t.out
22
// RUN: %{run} %t.out
33

4+
// UNSUPPORTED: level_zero_v2_adapter
5+
// UNSUPPORTED-TRACKER: https://github.com/intel/llvm/issues/18488
6+
47
#include <iostream>
58
#include <sycl/detail/core.hpp>
69
#include <sycl/properties/queue_properties.hpp>

sycl/test-e2e/AsyncAlloc/device/async_alloc_from_pool.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// RUN: %{build} -o %t.out
22
// RUN: %{run} %t.out
33

4+
// UNSUPPORTED: level_zero_v2_adapter
5+
// UNSUPPORTED-TRACKER: https://github.com/intel/llvm/issues/18488
6+
47
#include <iostream>
58
#include <sycl/detail/core.hpp>
69
#include <sycl/properties/queue_properties.hpp>

sycl/test-e2e/AsyncAlloc/device/async_alloc_zero_init.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// RUN: %{build} -o %t.out
22
// RUN: %{run} %t.out
33

4+
// UNSUPPORTED: level_zero_v2_adapter
5+
// UNSUPPORTED-TRACKER: https://github.com/intel/llvm/issues/18488
6+
47
#include <iostream>
58
#include <sycl/detail/core.hpp>
69
#include <sycl/properties/queue_properties.hpp>

sycl/test-e2e/AsyncAlloc/device/ooo_queue_async_alloc_from_pool.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// RUN: %{build} -o %t.out
22
// RUN: %{run} %t.out
33

4+
// UNSUPPORTED: level_zero_v2_adapter
5+
// UNSUPPORTED-TRACKER: https://github.com/intel/llvm/issues/18488
6+
47
#include <iostream>
58
#include <sycl/detail/core.hpp>
69
#include <sycl/properties/queue_properties.hpp>
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
11
config.required_features += ['aspect-ext_oneapi_async_memory_alloc']
2-
# V2 adapter does not support async alloc api yet
3-
config.unsupported_features += ['level_zero_v2_adapter']

unified-runtime/source/adapters/level_zero/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ if(UR_BUILD_ADAPTER_L0_V2)
141141
${CMAKE_CURRENT_SOURCE_DIR}/common.cpp
142142
${CMAKE_CURRENT_SOURCE_DIR}/command_buffer_command.cpp
143143
${CMAKE_CURRENT_SOURCE_DIR}/device.cpp
144+
${CMAKE_CURRENT_SOURCE_DIR}/enqueued_pool.cpp
144145
${CMAKE_CURRENT_SOURCE_DIR}/image_common.cpp
145146
${CMAKE_CURRENT_SOURCE_DIR}/ur_interface_loader.cpp
146147
${CMAKE_CURRENT_SOURCE_DIR}/platform.cpp

unified-runtime/source/adapters/level_zero/async_alloc.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ static ur_result_t enqueueUSMAllocHelper(
9393
break;
9494
default:
9595
UR_LOG(ERR, "enqueueUSMAllocHelper: unsupported USM type");
96-
throw UR_RESULT_ERROR_UNKNOWN;
96+
throw UR_RESULT_ERROR_INVALID_ARGUMENT;
9797
}
9898
UR_CALL(createEventAndAssociateQueue(Queue, Event, CommandType, CommandList,
9999
IsInternal, false));
@@ -247,6 +247,7 @@ ur_result_t urEnqueueUSMFreeExp(
247247
}
248248

249249
size_t size = umfPoolMallocUsableSize(hPool, Mem);
250+
(*Event)->RefCount.increment();
250251
usmPool->AsyncPool.insert(Mem, size, *Event, Queue);
251252

252253
// Signal that USM free event was finished

unified-runtime/source/adapters/level_zero/enqueued_pool.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,13 @@
99
//===----------------------------------------------------------------------===//
1010

1111
#include "enqueued_pool.hpp"
12-
#include "event.hpp"
1312

1413
#include <ur_api.h>
1514

1615
EnqueuedPool::~EnqueuedPool() { cleanup(); }
1716

1817
std::optional<EnqueuedPool::Allocation>
19-
EnqueuedPool::getBestFit(size_t Size, size_t Alignment,
20-
ur_queue_handle_t Queue) {
18+
EnqueuedPool::getBestFit(size_t Size, size_t Alignment, void *Queue) {
2119
auto Lock = std::lock_guard(Mutex);
2220

2321
Allocation Alloc = {nullptr, Size, nullptr, Queue, Alignment};
@@ -47,12 +45,11 @@ EnqueuedPool::getBestFit(size_t Size, size_t Alignment,
4745
}
4846

4947
void EnqueuedPool::insert(void *Ptr, size_t Size, ur_event_handle_t Event,
50-
ur_queue_handle_t Queue) {
48+
void *Queue) {
5149
auto Lock = std::lock_guard(Mutex);
5250

5351
uintptr_t Address = (uintptr_t)Ptr;
5452
size_t Alignment = Address & (~Address + 1);
55-
Event->RefCount.increment();
5653

5754
Freelist.emplace(Allocation{Ptr, Size, Event, Queue, Alignment});
5855
}
@@ -67,14 +64,15 @@ bool EnqueuedPool::cleanup() {
6764
auto umfRet [[maybe_unused]] = umfPoolFree(hPool, It.Ptr);
6865
assert(umfRet == UMF_RESULT_SUCCESS);
6966

70-
urEventReleaseInternal(It.Event);
67+
if (It.Event)
68+
eventRelease(It.Event);
7169
}
7270
Freelist.clear();
7371

7472
return FreedAllocations;
7573
}
7674

77-
bool EnqueuedPool::cleanupForQueue(ur_queue_handle_t Queue) {
75+
bool EnqueuedPool::cleanupForQueue(void *Queue) {
7876
auto Lock = std::lock_guard(Mutex);
7977

8078
Allocation Alloc = {nullptr, 0, nullptr, Queue, 0};
@@ -90,7 +88,8 @@ bool EnqueuedPool::cleanupForQueue(ur_queue_handle_t Queue) {
9088
auto umfRet [[maybe_unused]] = umfPoolFree(hPool, It->Ptr);
9189
assert(umfRet == UMF_RESULT_SUCCESS);
9290

93-
urEventReleaseInternal(It->Event);
91+
if (It->Event)
92+
eventRelease(It->Event);
9493

9594
// Erase the current allocation and move to the next one
9695
It = Freelist.erase(It);

unified-runtime/source/adapters/level_zero/enqueued_pool.hpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,24 @@ class EnqueuedPool {
2222
void *Ptr;
2323
size_t Size;
2424
ur_event_handle_t Event;
25-
ur_queue_handle_t Queue;
25+
// Queue handle, used as an identifier for the associated queue.
26+
// This can either be a `ur_queue_handle_t` or a pointer to a v2 queue
27+
// object.
28+
void *Queue;
2629
size_t Alignment;
2730
};
2831

32+
using event_release_callback_t = ur_result_t (*)(ur_event_handle_t);
33+
34+
EnqueuedPool(event_release_callback_t eventRelease)
35+
: eventRelease(eventRelease) {}
36+
2937
~EnqueuedPool();
3038
std::optional<Allocation> getBestFit(size_t Size, size_t Alignment,
31-
ur_queue_handle_t Queue);
32-
void insert(void *Ptr, size_t Size, ur_event_handle_t Event,
33-
ur_queue_handle_t Queue);
39+
void *Queue);
40+
void insert(void *Ptr, size_t Size, ur_event_handle_t Event, void *Queue);
3441
bool cleanup();
35-
bool cleanupForQueue(ur_queue_handle_t Queue);
42+
bool cleanupForQueue(void *Queue);
3643

3744
private:
3845
struct Comparator {
@@ -53,4 +60,5 @@ class EnqueuedPool {
5360
using AllocationSet = std::set<Allocation, Comparator>;
5461
ur_mutex Mutex;
5562
AllocationSet Freelist;
63+
event_release_callback_t eventRelease;
5664
};

unified-runtime/source/adapters/level_zero/usm.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "common.hpp"
1313

1414
#include "enqueued_pool.hpp"
15+
#include "event.hpp"
1516
#include "ur_api.h"
1617
#include "ur_pool_manager.hpp"
1718
#include <set>
@@ -20,7 +21,10 @@
2021
usm::DisjointPoolAllConfigs InitializeDisjointPoolConfig();
2122

2223
struct UsmPool {
23-
UsmPool(umf::pool_unique_handle_t Pool) : UmfPool(std::move(Pool)) {}
24+
UsmPool(umf::pool_unique_handle_t Pool)
25+
: UmfPool(std::move(Pool)), AsyncPool([](ur_event_handle_t Event) {
26+
return urEventReleaseInternal(Event);
27+
}) {}
2428
umf::pool_unique_handle_t UmfPool;
2529
// 'AsyncPool' needs to be declared after 'UmfPool' so its destructor is
2630
// invoked first.

0 commit comments

Comments
 (0)