Skip to content

Commit 36bbbb0

Browse files
authored
[UR][Offload] Improvements to offload event handling (#19515)
* Implemented device infos for event handles. * Fixed event being deleted before it should have been. * Make the tests consider profiling info optional.
1 parent a0752e2 commit 36bbbb0

File tree

6 files changed

+53
-32
lines changed

6 files changed

+53
-32
lines changed

unified-runtime/source/adapters/offload/enqueue.cpp

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueKernelLaunch(
7474
hKernel->Args.getStorageSize(), &LaunchArgs, &EventOut));
7575

7676
if (phEvent) {
77-
auto *Event = new ur_event_handle_t_();
77+
auto *Event = new ur_event_handle_t_(UR_COMMAND_KERNEL_LAUNCH, hQueue);
7878
Event->OffloadEvent = EventOut;
7979
*phEvent = Event;
8080
}
@@ -94,10 +94,10 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueUSMMemcpy2D(
9494
}
9595

9696
namespace {
97-
ur_result_t doMemcpy(ur_queue_handle_t hQueue, void *DestPtr,
98-
ol_device_handle_t DestDevice, const void *SrcPtr,
99-
ol_device_handle_t SrcDevice, size_t size, bool blocking,
100-
uint32_t numEventsInWaitList,
97+
ur_result_t doMemcpy(ur_command_t Command, ur_queue_handle_t hQueue,
98+
void *DestPtr, ol_device_handle_t DestDevice,
99+
const void *SrcPtr, ol_device_handle_t SrcDevice,
100+
size_t size, bool blocking, uint32_t numEventsInWaitList,
101101
const ur_event_handle_t *phEventWaitList,
102102
ur_event_handle_t *phEvent) {
103103
// Ignore wait list for now
@@ -115,7 +115,7 @@ ur_result_t doMemcpy(ur_queue_handle_t hQueue, void *DestPtr,
115115
}
116116

117117
if (phEvent) {
118-
auto *Event = new ur_event_handle_t_();
118+
auto *Event = new ur_event_handle_t_(Command, hQueue);
119119
Event->OffloadEvent = EventOut;
120120
*phEvent = Event;
121121
}
@@ -131,8 +131,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemBufferRead(
131131
char *DevPtr =
132132
reinterpret_cast<char *>(std::get<BufferMem>(hBuffer->Mem).Ptr);
133133

134-
return doMemcpy(hQueue, pDst, Adapter->HostDevice, DevPtr + offset,
135-
hQueue->OffloadDevice, size, blockingRead,
134+
return doMemcpy(UR_COMMAND_MEM_BUFFER_READ, hQueue, pDst, Adapter->HostDevice,
135+
DevPtr + offset, hQueue->OffloadDevice, size, blockingRead,
136136
numEventsInWaitList, phEventWaitList, phEvent);
137137
}
138138

@@ -143,9 +143,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemBufferWrite(
143143
char *DevPtr =
144144
reinterpret_cast<char *>(std::get<BufferMem>(hBuffer->Mem).Ptr);
145145

146-
return doMemcpy(hQueue, DevPtr + offset, hQueue->OffloadDevice, pSrc,
147-
Adapter->HostDevice, size, blockingWrite, numEventsInWaitList,
148-
phEventWaitList, phEvent);
146+
return doMemcpy(UR_COMMAND_MEM_BUFFER_WRITE, hQueue, DevPtr + offset,
147+
hQueue->OffloadDevice, pSrc, Adapter->HostDevice, size,
148+
blockingWrite, numEventsInWaitList, phEventWaitList, phEvent);
149149
}
150150

151151
UR_APIEXPORT ur_result_t UR_APICALL urEnqueueDeviceGlobalVariableRead(
@@ -159,10 +159,10 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueDeviceGlobalVariableRead(
159159
return Err;
160160
}
161161

162-
return doMemcpy(hQueue, pDst, Adapter->HostDevice,
163-
reinterpret_cast<const char *>(Ptr) + offset,
164-
hQueue->OffloadDevice, count, blockingRead,
165-
numEventsInWaitList, phEventWaitList, phEvent);
162+
return doMemcpy(
163+
UR_COMMAND_DEVICE_GLOBAL_VARIABLE_READ, hQueue, pDst, Adapter->HostDevice,
164+
reinterpret_cast<const char *>(Ptr) + offset, hQueue->OffloadDevice,
165+
count, blockingRead, numEventsInWaitList, phEventWaitList, phEvent);
166166
}
167167

168168
UR_APIEXPORT ur_result_t UR_APICALL urEnqueueDeviceGlobalVariableWrite(
@@ -176,18 +176,20 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueDeviceGlobalVariableWrite(
176176
return Err;
177177
}
178178

179-
return doMemcpy(hQueue, reinterpret_cast<char *>(Ptr) + offset,
180-
hQueue->OffloadDevice, pSrc, Adapter->HostDevice, count,
181-
blockingWrite, numEventsInWaitList, phEventWaitList, phEvent);
179+
return doMemcpy(UR_COMMAND_DEVICE_GLOBAL_VARIABLE_WRITE, hQueue,
180+
reinterpret_cast<char *>(Ptr) + offset, hQueue->OffloadDevice,
181+
pSrc, Adapter->HostDevice, count, blockingWrite,
182+
numEventsInWaitList, phEventWaitList, phEvent);
182183
}
183184

184-
ur_result_t enqueueNoOp(ur_queue_handle_t hQueue, ur_event_handle_t *phEvent) {
185+
ur_result_t enqueueNoOp(ur_command_t Type, ur_queue_handle_t hQueue,
186+
ur_event_handle_t *phEvent) {
185187
// This path is a no-op, but we can't output a real event because
186188
// Offload doesn't currently support creating arbitrary events, and we
187189
// don't know the last real event in the queue. Instead we just have to
188190
// wait on the whole queue and then return an empty (implicitly
189191
// finished) event.
190-
*phEvent = ur_event_handle_t_::createEmptyEvent();
192+
*phEvent = ur_event_handle_t_::createEmptyEvent(Type, hQueue);
191193
return urQueueFinish(hQueue);
192194
}
193195

@@ -221,7 +223,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemBufferMap(
221223
}
222224

223225
if (phEvent) {
224-
enqueueNoOp(hQueue, phEvent);
226+
enqueueNoOp(UR_COMMAND_MEM_BUFFER_MAP, hQueue, phEvent);
225227
}
226228
}
227229
*ppRetMap = MapPtr;
@@ -255,7 +257,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemUnmap(
255257
}
256258

257259
if (phEvent) {
258-
enqueueNoOp(hQueue, phEvent);
260+
enqueueNoOp(UR_COMMAND_MEM_UNMAP, hQueue, phEvent);
259261
}
260262
}
261263
BufferImpl.unmap(pMappedPtr);

unified-runtime/source/adapters/offload/event.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <ur_api.h>
1313

1414
#include "event.hpp"
15+
#include "queue.hpp"
1516
#include "ur2offload.hpp"
1617

1718
UR_APIEXPORT ur_result_t UR_APICALL urEventGetInfo(ur_event_handle_t hEvent,
@@ -22,6 +23,12 @@ UR_APIEXPORT ur_result_t UR_APICALL urEventGetInfo(ur_event_handle_t hEvent,
2223
UrReturnHelper ReturnValue(propSize, pPropValue, pPropSizeRet);
2324

2425
switch (propName) {
26+
case UR_EVENT_INFO_CONTEXT:
27+
return ReturnValue(hEvent->UrQueue->UrContext);
28+
case UR_EVENT_INFO_COMMAND_QUEUE:
29+
return ReturnValue(hEvent->UrQueue);
30+
case UR_EVENT_INFO_COMMAND_TYPE:
31+
return ReturnValue(hEvent->Type);
2532
case UR_EVENT_INFO_REFERENCE_COUNT:
2633
return ReturnValue(hEvent->RefCount.load());
2734
default:
@@ -61,9 +68,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEventRelease(ur_event_handle_t hEvent) {
6168
if (Res) {
6269
return offloadResultToUR(Res);
6370
}
71+
delete hEvent;
6472
}
6573

66-
delete hEvent;
6774
return UR_RESULT_SUCCESS;
6875
}
6976

unified-runtime/source/adapters/offload/event.hpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,14 @@
1818
struct ur_event_handle_t_ : RefCounted {
1919
ol_event_handle_t OffloadEvent;
2020
ur_command_t Type;
21+
ur_queue_handle_t UrQueue;
2122

22-
static ur_event_handle_t createEmptyEvent() {
23-
auto *Event = new ur_event_handle_t_();
23+
ur_event_handle_t_(ur_command_t Type, ur_queue_handle_t Queue)
24+
: Type(Type), UrQueue(Queue) {}
25+
26+
static ur_event_handle_t createEmptyEvent(ur_command_t Type,
27+
ur_queue_handle_t Queue) {
28+
auto *Event = new ur_event_handle_t_(Type, Queue);
2429
// Null event represents an empty event. Waiting on it is a no-op.
2530
Event->OffloadEvent = nullptr;
2631

unified-runtime/source/adapters/offload/queue.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
#include "queue.hpp"
1818
#include "ur2offload.hpp"
1919

20-
UR_APIEXPORT ur_result_t UR_APICALL urQueueCreate(
21-
[[maybe_unused]] ur_context_handle_t hContext, ur_device_handle_t hDevice,
22-
const ur_queue_properties_t *, ur_queue_handle_t *phQueue) {
20+
UR_APIEXPORT ur_result_t UR_APICALL urQueueCreate(ur_context_handle_t hContext,
21+
ur_device_handle_t hDevice,
22+
const ur_queue_properties_t *,
23+
ur_queue_handle_t *phQueue) {
2324

2425
assert(hContext->Device == hDevice);
2526

@@ -31,6 +32,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urQueueCreate(
3132
}
3233

3334
Queue->OffloadDevice = hDevice->OffloadDevice;
35+
Queue->UrContext = hContext;
3436

3537
*phQueue = Queue;
3638

unified-runtime/source/adapters/offload/queue.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@
1818
struct ur_queue_handle_t_ : RefCounted {
1919
ol_queue_handle_t OffloadQueue;
2020
ol_device_handle_t OffloadDevice;
21+
ur_context_handle_t UrContext;
2122
};

unified-runtime/test/conformance/event/urEventGetProfilingInfo.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66

77
#include "fixtures.h"
8+
#include "uur/checks.h"
89
#include "uur/known_failure.h"
910

1011
using urEventGetProfilingInfoTest = uur::event::urEventTest;
@@ -179,8 +180,9 @@ TEST_P(urEventGetProfilingInfoTest, InvalidValue) {
179180

180181
const ur_profiling_info_t property_name = UR_PROFILING_INFO_COMMAND_QUEUED;
181182
size_t property_size = 0;
182-
ASSERT_SUCCESS(urEventGetProfilingInfo(event, property_name, 0, nullptr,
183-
&property_size));
183+
ASSERT_SUCCESS_OR_OPTIONAL_QUERY(
184+
urEventGetProfilingInfo(event, property_name, 0, nullptr, &property_size),
185+
property_name);
184186
ASSERT_NE(property_size, 0);
185187

186188
uint64_t property_value = 0;
@@ -221,8 +223,10 @@ UUR_INSTANTIATE_DEVICE_TEST_SUITE(urEventGetProfilingInfoForWaitWithBarrier);
221223

222224
TEST_P(urEventGetProfilingInfoForWaitWithBarrier, Success) {
223225
uint64_t submit_value = 0;
224-
ASSERT_SUCCESS(urEventGetProfilingInfo(event, UR_PROFILING_INFO_COMMAND_START,
225-
size, &submit_value, nullptr));
226+
ASSERT_SUCCESS_OR_OPTIONAL_QUERY(
227+
urEventGetProfilingInfo(event, UR_PROFILING_INFO_COMMAND_START, size,
228+
&submit_value, nullptr),
229+
UR_PROFILING_INFO_COMMAND_START);
226230
ASSERT_NE(submit_value, 0);
227231

228232
uint64_t complete_value = 0;

0 commit comments

Comments
 (0)