|
7 | 7 | // |
8 | 8 | //===----------------------------------------------------------------------===// |
9 | 9 |
|
| 10 | +#include <memory> |
| 11 | + |
10 | 12 | #include <unified-runtime/ur_api.h> |
11 | 13 |
|
| 14 | +#include "../platform.hpp" |
12 | 15 | #include "../ur_interface_loader.hpp" |
| 16 | +#include "context.hpp" |
| 17 | +#include "event.hpp" |
13 | 18 |
|
14 | 19 | namespace ur::level_zero { |
15 | 20 |
|
16 | | -ur_result_t urIPCGetEventHandleExp(ur_event_handle_t /*hEvent*/, |
17 | | - void ** /*ppIPCEventHandleData*/, |
18 | | - size_t * /*pIPCEventHandleDataSizeRet*/) { |
19 | | - return UR_RESULT_ERROR_UNSUPPORTED_FEATURE; |
| 21 | +namespace { |
| 22 | + |
| 23 | +// Fixed L0 IPC handle size; reported by Get and validated by Open. |
| 24 | +constexpr size_t kIpcEventHandleDataSize = |
| 25 | + sizeof(ze_ipc_event_counter_based_handle_t); |
| 26 | + |
| 27 | +} // namespace |
| 28 | + |
| 29 | +ur_result_t urIPCGetEventHandleExp(ur_event_handle_t hEvent, |
| 30 | + void **ppIPCEventHandleData, |
| 31 | + size_t *pIPCEventHandleDataSizeRet) try { |
| 32 | + UR_ASSERT(hEvent, UR_RESULT_ERROR_INVALID_NULL_HANDLE); |
| 33 | + UR_ASSERT(ppIPCEventHandleData && pIPCEventHandleDataSizeRet, |
| 34 | + UR_RESULT_ERROR_INVALID_NULL_POINTER); |
| 35 | + |
| 36 | + std::shared_lock<ur_shared_mutex> lock(hEvent->Mutex); |
| 37 | + |
| 38 | + UR_ASSERT(hEvent->isIpcCapable() && !hEvent->isIpcImported(), |
| 39 | + UR_RESULT_ERROR_INVALID_EVENT); |
| 40 | + UR_ASSERT(!hEvent->isProfilingEnabled() && !hEvent->isTimestamped(), |
| 41 | + UR_RESULT_ERROR_UNSUPPORTED_FEATURE); |
| 42 | + |
| 43 | + auto handle = std::make_unique<ze_ipc_event_counter_based_handle_t>(); |
| 44 | + ZE2UR_CALL(zeEventCounterBasedGetIpcHandle, |
| 45 | + (hEvent->getZeEvent(), handle.get())); |
| 46 | + |
| 47 | + // Caller releases the buffer via urIPCPutEventHandleExp. |
| 48 | + *ppIPCEventHandleData = handle.release(); |
| 49 | + *pIPCEventHandleDataSizeRet = kIpcEventHandleDataSize; |
| 50 | + return UR_RESULT_SUCCESS; |
| 51 | +} catch (...) { |
| 52 | + return exceptionToResult(std::current_exception()); |
20 | 53 | } |
21 | 54 |
|
22 | 55 | ur_result_t urIPCPutEventHandleExp(ur_context_handle_t /*hContext*/, |
23 | | - void * /*pIPCEventHandleData*/) { |
24 | | - return UR_RESULT_ERROR_UNSUPPORTED_FEATURE; |
| 56 | + void *pIPCEventHandleData) try { |
| 57 | + UR_ASSERT(pIPCEventHandleData, UR_RESULT_ERROR_INVALID_NULL_POINTER); |
| 58 | + // Free the buffer allocated by urIPCGetEventHandleExp via RAII. |
| 59 | + std::unique_ptr<ze_ipc_event_counter_based_handle_t> owner( |
| 60 | + static_cast<ze_ipc_event_counter_based_handle_t *>(pIPCEventHandleData)); |
| 61 | + return UR_RESULT_SUCCESS; |
| 62 | +} catch (...) { |
| 63 | + return exceptionToResult(std::current_exception()); |
25 | 64 | } |
26 | 65 |
|
27 | | -ur_result_t urIPCOpenEventHandleExp(ur_context_handle_t /*hContext*/, |
28 | | - const void * /*pIPCEventHandleData*/, |
29 | | - size_t /*ipcEventHandleDataSize*/, |
30 | | - ur_event_handle_t * /*phEvent*/) { |
31 | | - return UR_RESULT_ERROR_UNSUPPORTED_FEATURE; |
| 66 | +ur_result_t urIPCOpenEventHandleExp(ur_context_handle_t hContext, |
| 67 | + const void *pIPCEventHandleData, |
| 68 | + size_t ipcEventHandleDataSize, |
| 69 | + ur_event_handle_t *phEvent) try { |
| 70 | + UR_ASSERT(hContext, UR_RESULT_ERROR_INVALID_NULL_HANDLE); |
| 71 | + UR_ASSERT(pIPCEventHandleData && phEvent, |
| 72 | + UR_RESULT_ERROR_INVALID_NULL_POINTER); |
| 73 | + UR_ASSERT(ipcEventHandleDataSize == kIpcEventHandleDataSize, |
| 74 | + UR_RESULT_ERROR_INVALID_VALUE); |
| 75 | + |
| 76 | + // The driver consumes the handle by value. |
| 77 | + ze_ipc_event_counter_based_handle_t handle = |
| 78 | + *static_cast<const ze_ipc_event_counter_based_handle_t *>( |
| 79 | + pIPCEventHandleData); |
| 80 | + ze_event_handle_t hZeEvent = nullptr; |
| 81 | + ZE2UR_CALL(zeEventCounterBasedOpenIpcHandle, |
| 82 | + (hContext->getZeHandle(), handle, &hZeEvent)); |
| 83 | + |
| 84 | + // The ipc_event_handle_t variant runs zeEventCounterBasedCloseIpcHandle on |
| 85 | + // urEventRelease. |
| 86 | + *phEvent = |
| 87 | + new ur_event_handle_t_(hContext, v2::raii::ipc_event_handle_t{hZeEvent}, |
| 88 | + v2::EVENT_FLAGS_COUNTER | v2::EVENT_FLAGS_IPC | |
| 89 | + v2::EVENT_FLAGS_IPC_IMPORTED); |
| 90 | + return UR_RESULT_SUCCESS; |
| 91 | +} catch (...) { |
| 92 | + return exceptionToResult(std::current_exception()); |
32 | 93 | } |
33 | 94 |
|
34 | 95 | } // namespace ur::level_zero |
0 commit comments