Skip to content

Commit fc623a5

Browse files
committed
[L0 v2] implement queue native APIs
1 parent 44af8f0 commit fc623a5

File tree

6 files changed

+70
-23
lines changed

6 files changed

+70
-23
lines changed

source/adapters/level_zero/v2/command_list_cache.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ command_list_cache_t::createCommandList(const command_list_descriptor_t &desc) {
7676
}
7777
}
7878

79-
raii::cache_borrowed_command_list_t
80-
command_list_cache_t::getImmediateCommandList(
79+
raii::command_list_unique_handle command_list_cache_t::getImmediateCommandList(
8180
ze_device_handle_t ZeDevice, bool IsInOrder, uint32_t Ordinal,
8281
ze_command_queue_mode_t Mode, ze_command_queue_priority_t Priority,
8382
std::optional<uint32_t> Index) {
@@ -92,13 +91,13 @@ command_list_cache_t::getImmediateCommandList(
9291
Desc.Index = Index;
9392

9493
auto [CommandList, _] = getCommandList(Desc).release();
95-
return raii::cache_borrowed_command_list_t(
94+
return raii::command_list_unique_handle(
9695
CommandList, [Cache = this, Desc](ze_command_list_handle_t CmdList) {
9796
Cache->addCommandList(Desc, raii::ze_command_list_handle_t(CmdList));
9897
});
9998
}
10099

101-
raii::cache_borrowed_command_list_t
100+
raii::command_list_unique_handle
102101
command_list_cache_t::getRegularCommandList(ze_device_handle_t ZeDevice,
103102
bool IsInOrder, uint32_t Ordinal) {
104103
TRACK_SCOPE_LATENCY("command_list_cache_t::getRegularCommandList");
@@ -110,7 +109,7 @@ command_list_cache_t::getRegularCommandList(ze_device_handle_t ZeDevice,
110109

111110
auto [CommandList, _] = getCommandList(Desc).release();
112111

113-
return raii::cache_borrowed_command_list_t(
112+
return raii::command_list_unique_handle(
114113
CommandList, [Cache = this, Desc](ze_command_list_handle_t CmdList) {
115114
Cache->addCommandList(Desc, raii::ze_command_list_handle_t(CmdList));
116115
});

source/adapters/level_zero/v2/command_list_cache.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
namespace v2 {
2323
namespace raii {
24-
using cache_borrowed_command_list_t =
24+
using command_list_unique_handle =
2525
std::unique_ptr<::_ze_command_list_handle_t,
2626
std::function<void(::ze_command_list_handle_t)>>;
2727
} // namespace raii
@@ -54,12 +54,12 @@ struct command_list_descriptor_hash_t {
5454
struct command_list_cache_t {
5555
command_list_cache_t(ze_context_handle_t ZeContext);
5656

57-
raii::cache_borrowed_command_list_t
57+
raii::command_list_unique_handle
5858
getImmediateCommandList(ze_device_handle_t ZeDevice, bool IsInOrder,
5959
uint32_t Ordinal, ze_command_queue_mode_t Mode,
6060
ze_command_queue_priority_t Priority,
6161
std::optional<uint32_t> Index = std::nullopt);
62-
raii::cache_borrowed_command_list_t
62+
raii::command_list_unique_handle
6363
getRegularCommandList(ze_device_handle_t ZeDevice, bool IsInOrder,
6464
uint32_t Ordinal);
6565

source/adapters/level_zero/v2/queue_create.cpp

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,28 @@ ur_result_t urQueueCreateWithNativeHandle(
3636
ur_native_handle_t hNativeQueue, ur_context_handle_t hContext,
3737
ur_device_handle_t hDevice, const ur_queue_native_properties_t *pProperties,
3838
ur_queue_handle_t *phQueue) {
39-
std::ignore = hNativeQueue;
40-
std::ignore = hContext;
41-
std::ignore = hDevice;
42-
std::ignore = pProperties;
43-
std::ignore = phQueue;
44-
logger::error("{} function not implemented!", __FUNCTION__);
45-
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
39+
// TODO: For now, always assume it's immediate, in-order
40+
41+
bool ownNativeHandle = pProperties ? pProperties->isNativeHandleOwned : false;
42+
ur_queue_flags_t flags = 0;
43+
44+
if (pProperties) {
45+
void *pNext = pProperties->pNext;
46+
while (pNext) {
47+
const ur_base_properties_t *extendedProperties =
48+
reinterpret_cast<const ur_base_properties_t *>(pNext);
49+
if (extendedProperties->stype == UR_STRUCTURE_TYPE_QUEUE_PROPERTIES) {
50+
const ur_queue_properties_t *pUrProperties =
51+
reinterpret_cast<const ur_queue_properties_t *>(extendedProperties);
52+
flags = pUrProperties->flags;
53+
}
54+
pNext = extendedProperties->pNext;
55+
}
56+
}
57+
58+
*phQueue = new v2::ur_queue_immediate_in_order_t(
59+
hContext, hDevice, hNativeQueue, flags, ownNativeHandle);
60+
61+
return UR_RESULT_SUCCESS;
4662
}
4763
} // namespace ur::level_zero

source/adapters/level_zero/v2/queue_immediate_in_order.cpp

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,19 @@ ur_command_list_handler_t::ur_command_list_handler_t(
8484
ur::level_zero::urEventRelease(event);
8585
}) {}
8686

87+
ur_command_list_handler_t::ur_command_list_handler_t(
88+
ze_command_list_handle_t hZeCommandList, event_pool *eventPool,
89+
bool ownZeHandle)
90+
: commandList(hZeCommandList,
91+
[ownZeHandle](ze_command_list_handle_t hZeCommandList) {
92+
if (ownZeHandle) {
93+
zeCommandListDestroy(hZeCommandList);
94+
}
95+
}),
96+
internalEvent(eventPool->allocate(), [=](ur_event_handle_t event) {
97+
ur::level_zero::urEventRelease(event);
98+
}) {}
99+
87100
static event_flags_t eventFlagsFromQueueFlags(ur_queue_flags_t flags) {
88101
event_flags_t eventFlags = EVENT_FLAGS_COUNTER;
89102
if (flags & UR_QUEUE_FLAG_PROFILING_ENABLE)
@@ -102,6 +115,17 @@ ur_queue_immediate_in_order_t::ur_queue_immediate_in_order_t(
102115
computeHandler(hContext, hDevice, pProps, queue_group_type::Compute,
103116
eventPool.get()) {}
104117

118+
ur_queue_immediate_in_order_t::ur_queue_immediate_in_order_t(
119+
ur_context_handle_t hContext, ur_device_handle_t hDevice,
120+
ur_native_handle_t hNativeHandle, ur_queue_flags_t flags, bool ownZeQueue)
121+
: hContext(hContext), hDevice(hDevice), flags(flags),
122+
eventPool(hContext->eventPoolCache.borrow(
123+
hDevice->Id.value(), eventFlagsFromQueueFlags(flags))),
124+
copyHandler(
125+
reinterpret_cast<ze_command_list_handle_t>(hNativeHandle), eventPool.get(), false /* we're using a single command list for both handlers, only own it by one of them */),
126+
computeHandler(reinterpret_cast<ze_command_list_handle_t>(hNativeHandle),
127+
eventPool.get(), ownZeQueue) {}
128+
105129
ur_command_list_handler_t *
106130
ur_queue_immediate_in_order_t::getCommandListHandlerForCompute() {
107131
return &computeHandler;
@@ -161,9 +185,9 @@ ur_queue_immediate_in_order_t::queueGetInfo(ur_queue_info_t propName,
161185
return ReturnValue(false);
162186
}
163187
default:
164-
logger::error(
165-
"Unsupported ParamName in urQueueGetInfo: ParamName=ParamName={}(0x{})",
166-
propName, logger::toHex(propName));
188+
logger::error("Unsupported ParamName in urQueueGetInfo: "
189+
"ParamName=ParamName={}(0x{})",
190+
propName, logger::toHex(propName));
167191
return UR_RESULT_ERROR_INVALID_VALUE;
168192
}
169193

@@ -186,8 +210,9 @@ ur_result_t ur_queue_immediate_in_order_t::queueRelease() {
186210
ur_result_t ur_queue_immediate_in_order_t::queueGetNativeHandle(
187211
ur_queue_native_desc_t *pDesc, ur_native_handle_t *phNativeQueue) {
188212
std::ignore = pDesc;
189-
std::ignore = phNativeQueue;
190-
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
213+
*phNativeQueue = reinterpret_cast<ur_native_handle_t>(
214+
this->computeHandler.commandList.get());
215+
return UR_RESULT_SUCCESS;
191216
}
192217

193218
ur_result_t ur_queue_immediate_in_order_t::finalizeHandler(

source/adapters/level_zero/v2/queue_immediate_in_order.hpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ struct ur_command_list_handler_t {
2929
const ur_queue_properties_t *pProps,
3030
queue_group_type type, event_pool *eventPool);
3131

32-
raii::cache_borrowed_command_list_t commandList;
32+
ur_command_list_handler_t(ze_command_list_handle_t hZeCommandList,
33+
event_pool *eventPool, bool ownZeHandle);
34+
35+
raii::command_list_unique_handle commandList;
3336
std::unique_ptr<ur_event_handle_t_, std::function<void(ur_event_handle_t)>>
3437
internalEvent;
3538

@@ -88,6 +91,10 @@ struct ur_queue_immediate_in_order_t : _ur_object, public ur_queue_handle_t_ {
8891
public:
8992
ur_queue_immediate_in_order_t(ur_context_handle_t, ur_device_handle_t,
9093
const ur_queue_properties_t *);
94+
ur_queue_immediate_in_order_t(ur_context_handle_t, ur_device_handle_t,
95+
ur_native_handle_t, ur_queue_flags_t,
96+
bool ownZeQueue);
97+
9198
~ur_queue_immediate_in_order_t() {}
9299

93100
ur_result_t queueGetInfo(ur_queue_info_t propName, size_t propSize,

test/adapters/level_zero/v2/command_list_cache_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ TEST_P(CommandListCacheTest, CanStoreAndRetriveImmediateAndRegularCmdLists) {
3232
ze_command_queue_priority_t Priority = ZE_COMMAND_QUEUE_PRIORITY_NORMAL;
3333

3434
static constexpr int numListsPerType = 3;
35-
std::vector<v2::raii::cache_borrowed_command_list_t> regCmdListOwners;
36-
std::vector<v2::raii::cache_borrowed_command_list_t> immCmdListOwners;
35+
std::vector<v2::raii::command_list_unique_handle> regCmdListOwners;
36+
std::vector<v2::raii::command_list_unique_handle> immCmdListOwners;
3737

3838
std::unordered_set<ze_command_list_handle_t> regCmdLists;
3939
std::unordered_set<ze_command_list_handle_t> immCmdLists;

0 commit comments

Comments
 (0)