Skip to content
Draft
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
7 changes: 6 additions & 1 deletion unified-runtime/include/ur_api.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions unified-runtime/include/ur_print.hpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion unified-runtime/scripts/core/event.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ etors:
desc: Event created by $xEnqueueReadHostPipe
- name: WRITE_HOST_PIPE
desc: Event created by $xEnqueueWriteHostPipe
- name: FROM_NATIVE
desc: Command type used by events created by $xEventCreateWithNativeHandle
--- #--------------------------------------------------------------------------
type: enum
desc: "Event Status"
Expand All @@ -101,7 +103,7 @@ name: $x_event_info_t
typed_etors: True
etors:
- name: COMMAND_QUEUE
desc: "[$x_queue_handle_t] Command queue information of an event object"
desc: "[$x_queue_handle_t] Command queue information of an event object. This may be NULL if the event was created via $xEventCreateWithNativeHandle and the adapter has no associated UR queue"
- name: CONTEXT
desc: "[$x_context_handle_t] Context information of an event object"
- name: COMMAND_TYPE
Expand Down
8 changes: 2 additions & 6 deletions unified-runtime/source/adapters/cuda/event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ ur_event_handle_t_::ur_event_handle_t_(ur_command_t Type,

ur_event_handle_t_::ur_event_handle_t_(ur_context_handle_t Context,
CUevent EventNative)
: handle_base(), CommandType{UR_COMMAND_EVENTS_WAIT}, HasOwnership{false},
: handle_base(), CommandType{UR_COMMAND_FROM_NATIVE}, HasOwnership{false},
IsInterop{true}, StreamToken{std::numeric_limits<uint32_t>::max()},
EvEnd{EventNative}, EvStart{nullptr}, EvQueued{nullptr}, Queue{nullptr},
Stream{nullptr}, Context{Context} {
Expand Down Expand Up @@ -168,11 +168,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urEventGetInfo(ur_event_handle_t hEvent,
// If the runtime owns the native handle, we have reference to the queue.
// Otherwise, the event handle comes from an interop API with no RT refs.
if (!hEvent->getQueue()) {
setErrorMessage("Command queue info cannot be queried for the event. The "
"event object was created from a native event and has no "
"valid reference to a command queue.",
UR_RESULT_ERROR_INVALID_VALUE);
return UR_RESULT_ERROR_ADAPTER_SPECIFIC;
return ReturnValue(nullptr);
}
return ReturnValue(hEvent->getQueue());
}
Expand Down
8 changes: 2 additions & 6 deletions unified-runtime/source/adapters/hip/event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ ur_event_handle_t_::ur_event_handle_t_(ur_command_t Type,

ur_event_handle_t_::ur_event_handle_t_(ur_context_handle_t Context,
hipEvent_t EventNative)
: handle_base(), CommandType{UR_COMMAND_EVENTS_WAIT}, HasOwnership{false},
: handle_base(), CommandType{UR_COMMAND_FROM_NATIVE}, HasOwnership{false},
IsInterop{true}, StreamToken{std::numeric_limits<uint32_t>::max()},
EvEnd{EventNative}, EvStart{nullptr}, EvQueued{nullptr}, Queue{nullptr},
Stream{nullptr}, Context{Context} {
Expand Down Expand Up @@ -178,11 +178,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urEventGetInfo(ur_event_handle_t hEvent,
// If the runtime owns the native handle, we have reference to the queue.
// Otherwise, the event handle comes from an interop API with no RT refs.
if (!hEvent->getQueue()) {
setErrorMessage("Command queue info cannot be queried for the event. The "
"event object was created from a native event and has no "
"valid reference to a command queue.",
UR_RESULT_ERROR_INVALID_VALUE);
return UR_RESULT_ERROR_ADAPTER_SPECIFIC;
return ReturnValue(nullptr);
}
return ReturnValue(hEvent->getQueue());
}
Expand Down
6 changes: 3 additions & 3 deletions unified-runtime/source/adapters/level_zero/event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -972,9 +972,9 @@ ur_result_t urEventCreateWithNativeHandle(
auto ZeEvent = ur_cast<ze_event_handle_t>(NativeEvent);
ur_event_handle_t_ *UREvent{};
try {
UREvent = new ur_event_handle_t_(ZeEvent, nullptr /* ZeEventPool */,
Context, UR_EXT_COMMAND_TYPE_USER,
Properties->isNativeHandleOwned);
UREvent = new ur_event_handle_t_(
ZeEvent, nullptr /* ZeEventPool */, Context, UR_COMMAND_FROM_NATIVE,
Properties ? Properties->isNativeHandleOwned : false);
UREvent->RefCountExternal++;

} catch (const std::bad_alloc &) {
Expand Down
35 changes: 27 additions & 8 deletions unified-runtime/source/adapters/offload/event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urEventGetInfo(ur_event_handle_t hEvent,

switch (propName) {
case UR_EVENT_INFO_CONTEXT:
return ReturnValue(hEvent->UrQueue->UrContext);
return ReturnValue(hEvent->UrContext);
case UR_EVENT_INFO_COMMAND_QUEUE:
return ReturnValue(hEvent->UrQueue);
case UR_EVENT_INFO_COMMAND_TYPE:
Expand Down Expand Up @@ -103,7 +103,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urEventRetain(ur_event_handle_t hEvent) {

UR_APIEXPORT ur_result_t UR_APICALL urEventRelease(ur_event_handle_t hEvent) {
if (--hEvent->RefCount == 0) {
if (hEvent->OffloadEvent) {
if (hEvent->OffloadEvent && hEvent->isNativeHandleOwned) {
auto Res = olDestroyEvent(hEvent->OffloadEvent);
if (Res) {
return offloadResultToUR(Res);
Expand All @@ -115,13 +115,32 @@ UR_APIEXPORT ur_result_t UR_APICALL urEventRelease(ur_event_handle_t hEvent) {
return UR_RESULT_SUCCESS;
}

UR_APIEXPORT ur_result_t UR_APICALL
urEventGetNativeHandle(ur_event_handle_t, ur_native_handle_t *) {
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
UR_APIEXPORT ur_result_t UR_APICALL urEventGetNativeHandle(
ur_event_handle_t hEvent, ur_native_handle_t *phNativeEvent) {
if (!hEvent->OffloadEvent) {
// There is no associated event - rather than returning nullptr, create an
// empty one
ol_queue_handle_t TmpQueue;
OL_RETURN_ON_ERR(olCreateQueue(hEvent->UrQueue->OffloadDevice, &TmpQueue));
OL_RETURN_ON_ERR(olCreateEvent(
TmpQueue, reinterpret_cast<ol_event_handle_t *>(phNativeEvent)));
OL_RETURN_ON_ERR(olDestroyQueue(TmpQueue));
} else {
*phNativeEvent = reinterpret_cast<ur_native_handle_t>(hEvent->OffloadEvent);
}

return UR_RESULT_SUCCESS;
}

UR_APIEXPORT ur_result_t UR_APICALL urEventCreateWithNativeHandle(
ur_native_handle_t, ur_context_handle_t,
const ur_event_native_properties_t *, ur_event_handle_t *) {
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
ur_native_handle_t hNativeEvent, ur_context_handle_t hContext,
const ur_event_native_properties_t *pProperties,
ur_event_handle_t *hEvent) {
bool IsNativeHandleOwned =
pProperties ? pProperties->isNativeHandleOwned : false;

*hEvent =
new ur_event_handle_t_(reinterpret_cast<ol_event_handle_t>(hNativeEvent),
hContext, IsNativeHandleOwned);
return UR_RESULT_SUCCESS;
}
11 changes: 10 additions & 1 deletion unified-runtime/source/adapters/offload/event.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,23 @@
#include <ur_api.h>

#include "common.hpp"
#include "queue.hpp"

struct ur_event_handle_t_ : RefCounted {
ol_event_handle_t OffloadEvent;
ur_command_t Type;
ur_queue_handle_t UrQueue;
ur_context_handle_t UrContext;
bool isNativeHandleOwned;

ur_event_handle_t_(ur_command_t Type, ur_queue_handle_t Queue)
: Type(Type), UrQueue(Queue) {}
: Type(Type), UrQueue(Queue), UrContext(Queue->UrContext),
isNativeHandleOwned(true) {}
ur_event_handle_t_(ol_event_handle_t NativeHandle,
ur_context_handle_t Context, bool isNativeHandleOwned)
: OffloadEvent(NativeHandle), Type(UR_COMMAND_FROM_NATIVE),
UrQueue(nullptr), UrContext(Context),
isNativeHandleOwned(isNativeHandleOwned) {}

static ur_event_handle_t createEmptyEvent(ur_command_t Type,
ur_queue_handle_t Queue) {
Expand Down
20 changes: 20 additions & 0 deletions unified-runtime/source/adapters/offload/queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,29 @@

#include "context.hpp"
#include "device.hpp"
#include "event.hpp"
#include "queue.hpp"
#include "ur2offload.hpp"

ol_result_t ur_queue_handle_t_::nextQueueNoLock(ol_queue_handle_t &Handle) {
auto &Slot = OffloadQueues[(QueueOffset++) % OffloadQueues.size()];

if (!Slot) {
if (auto Res = olCreateQueue(OffloadDevice, &Slot)) {
return Res;
}

if (auto Event = Barrier) {
if (auto Res = olWaitEvents(Slot, &Event->OffloadEvent, 1)) {
return Res;
}
}
}

Handle = Slot;
return nullptr;
}

UR_APIEXPORT ur_result_t UR_APICALL urQueueCreate(
[[maybe_unused]] ur_context_handle_t hContext, ur_device_handle_t hDevice,
const ur_queue_properties_t *pProps, ur_queue_handle_t *phQueue) {
Expand Down
20 changes: 1 addition & 19 deletions unified-runtime/source/adapters/offload/queue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include <ur_api.h>

#include "common.hpp"
#include "event.hpp"

constexpr size_t OOO_QUEUE_POOL_SIZE = 32;

Expand Down Expand Up @@ -66,24 +65,7 @@ struct ur_queue_handle_t_ : RefCounted {
return OL_SUCCESS;
}

ol_result_t nextQueueNoLock(ol_queue_handle_t &Handle) {
auto &Slot = OffloadQueues[(QueueOffset++) % OffloadQueues.size()];

if (!Slot) {
if (auto Res = olCreateQueue(OffloadDevice, &Slot)) {
return Res;
}

if (auto Event = Barrier) {
if (auto Res = olWaitEvents(Slot, &Event->OffloadEvent, 1)) {
return Res;
}
}
}

Handle = Slot;
return nullptr;
}
ol_result_t nextQueueNoLock(ol_queue_handle_t &Handle);

ol_result_t nextQueue(ol_queue_handle_t &Handle) {
std::lock_guard<std::mutex> Lock(OooMutex);
Expand Down
8 changes: 4 additions & 4 deletions unified-runtime/test/conformance/event/urEventGetInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ TEST_P(urEventGetInfoTest, SuccessRoundtripContext) {
}

TEST_P(urEventGetInfoTest, SuccessRoundtripCommandQueue) {
UUR_KNOWN_FAILURE_ON(uur::HIP{}, uur::CUDA{});
// Segfaults
UUR_KNOWN_FAILURE_ON(uur::LevelZero{}, uur::LevelZeroV2{});

const ur_event_info_t property_name = UR_EVENT_INFO_COMMAND_QUEUE;
size_t property_size = sizeof(ur_queue_handle_t);

Expand All @@ -66,6 +62,10 @@ TEST_P(urEventGetInfoTest, SuccessRoundtripCommandQueue) {
ur_queue_handle_t property_value = nullptr;
ASSERT_SUCCESS(urEventGetInfo(from_native_event, property_name, property_size,
&property_value, nullptr));
if (property_value == nullptr) {
GTEST_SKIP()
<< "Backend cannot regenerate command queues from native handles";
}

// We can't assume that the two queue handles are equal (since creating the
// link to the UR structures has been severed by going through native handle,
Expand Down
Loading