1010
1111#include < ze_api.h>
1212
13+ #include " context.hpp"
1314#include " event.hpp"
1415#include " event_pool.hpp"
1516#include " event_provider.hpp"
1617#include " queue_api.hpp"
1718
1819#include " ../ur_interface_loader.hpp"
1920
20- ur_event_handle_t_::ur_event_handle_t_ (
21- v2::raii::cache_borrowed_event eventAllocation, v2::event_pool *pool)
22- : zeEvent(std::move(eventAllocation)), pool(pool),
21+ ur_event_handle_t_::ur_event_handle_t_ (ze_event_handle_t hZeEvent,
22+ ur_device_handle_t hDevice,
23+ ur_command_t commandType,
24+ v2::event_flags_t flags)
25+ : hZeEvent(hZeEvent), hDevice(hDevice), flags(flags),
2326 adjustedEventStartTimestamp(0 ), recordEventEndTimestamp(0 ),
2427 adjustedEventEndTimestamp(0 ),
2528 zeTimerResolution(getDevice()->ZeDeviceProperties->timerResolution),
26- timestampMaxValue(getDevice()->getTimestampMask()) {}
27-
28- void ur_event_handle_t_::resetQueueAndCommand (ur_queue_handle_t hQueue,
29- ur_command_t commandType) {
30- this ->hQueue = hQueue;
31- this ->commandType = commandType;
32- }
29+ timestampMaxValue(getDevice()->getTimestampMask()),
30+ commandType(commandType) {}
3331
3432void ur_event_handle_t_::reset () {
3533 // consider make an abstraction for regular/counter based
3634 // events if there's more of this type of conditions
37- if (pool-> getFlags () & v2::EVENT_FLAGS_COUNTER) {
38- zeEventHostReset (zeEvent. get () );
35+ if (flags & v2::EVENT_FLAGS_COUNTER) {
36+ zeEventHostReset (hZeEvent );
3937 }
4038}
4139
4240ze_event_handle_t ur_event_handle_t_::getZeEvent () const {
43- assert (hQueue);
4441 assert (commandType != UR_COMMAND_FORCE_UINT32);
45- return zeEvent. get () ;
42+ return hZeEvent ;
4643}
4744
4845ur_result_t ur_event_handle_t_::retain () {
@@ -51,11 +48,10 @@ ur_result_t ur_event_handle_t_::retain() {
5148}
5249
5350ur_result_t ur_event_handle_t_::releaseDeferred () {
54- assert (zeEventQueryStatus (zeEvent. get () ) == ZE_RESULT_SUCCESS);
51+ assert (zeEventQueryStatus (hZeEvent ) == ZE_RESULT_SUCCESS);
5552 assert (RefCount.load () == 0 );
5653
57- pool->free (this );
58- return UR_RESULT_SUCCESS;
54+ return this ->forceRelease ();
5955}
6056
6157ur_result_t ur_event_handle_t_::release () {
@@ -69,14 +65,12 @@ ur_result_t ur_event_handle_t_::release() {
6965 // L0 will write end timestamp to this event some time in the future,
7066 // so we can't release it yet.
7167
72- assert (hQueue);
68+ assert (hQueue); // hQueue must have been set in recordStartTimestamp
7369 hQueue->deferEventFree (this );
7470 return UR_RESULT_SUCCESS;
7571 }
7672
77- pool->free (this );
78-
79- return UR_RESULT_SUCCESS;
73+ return this ->forceRelease ();
8074}
8175
8276bool ur_event_handle_t_::isTimestamped () const {
@@ -85,12 +79,10 @@ bool ur_event_handle_t_::isTimestamped() const {
8579}
8680
8781bool ur_event_handle_t_::isProfilingEnabled () const {
88- return pool-> getFlags () & v2::EVENT_FLAGS_PROFILING_ENABLED;
82+ return flags & v2::EVENT_FLAGS_PROFILING_ENABLED;
8983}
9084
91- ur_device_handle_t ur_event_handle_t_::getDevice () const {
92- return pool->getProvider ()->device ();
93- }
85+ ur_device_handle_t ur_event_handle_t_::getDevice () const { return hDevice; }
9486
9587uint64_t ur_event_handle_t_::getEventStartTimestmap () const {
9688 return adjustedEventStartTimestamp;
@@ -121,7 +113,7 @@ uint64_t ur_event_handle_t_::getEventEndTimestamp() {
121113 if (adjustedEventEndTimestamp)
122114 return adjustedEventEndTimestamp;
123115
124- auto status = zeEventQueryStatus (zeEvent. get () );
116+ auto status = zeEventQueryStatus (hZeEvent );
125117 if (status != ZE_RESULT_SUCCESS) {
126118 // profiling info not ready
127119 return 0 ;
@@ -134,7 +126,12 @@ uint64_t ur_event_handle_t_::getEventEndTimestamp() {
134126 return adjustedEventEndTimestamp;
135127}
136128
137- void ur_event_handle_t_::recordStartTimestamp () {
129+ void ur_event_handle_t_::recordStartTimestamp (ur_queue_handle_t hQueue) {
130+ // this->hQueue can be for events created from native handle
131+ assert (hQueue == this ->hQueue || !this ->hQueue );
132+
133+ this ->hQueue = hQueue;
134+
138135 uint64_t deviceStartTimestamp = 0 ;
139136 UR_CALL_THROWS (ur::level_zero::urDeviceGetGlobalTimestamps (
140137 getDevice (), &deviceStartTimestamp, nullptr ));
@@ -145,13 +142,45 @@ void ur_event_handle_t_::recordStartTimestamp() {
145142
146143std::pair<uint64_t *, ze_event_handle_t >
147144ur_event_handle_t_::getEventEndTimestampAndHandle () {
148- return {&recordEventEndTimestamp, zeEvent. get () };
145+ return {&recordEventEndTimestamp, hZeEvent };
149146}
150147
151148ur_queue_handle_t ur_event_handle_t_::getQueue () const { return hQueue; }
152149
153150ur_command_t ur_event_handle_t_::getCommandType () const { return commandType; }
154151
152+ ur_pooled_event_t ::ur_pooled_event_t (
153+ v2::raii::cache_borrowed_event eventAllocation, v2::event_pool *pool)
154+ : ur_event_handle_t_(eventAllocation.get(), pool->getProvider ()->device(),
155+ UR_COMMAND_FORCE_UINT32, pool->getFlags()),
156+ zeEvent(std::move(eventAllocation)), pool(pool) {}
157+
158+ void ur_pooled_event_t::resetQueueAndCommand (ur_queue_handle_t hQueue,
159+ ur_command_t commandType) {
160+ this ->hQueue = hQueue;
161+ this ->commandType = commandType;
162+ }
163+
164+ ur_result_t ur_pooled_event_t::forceRelease () {
165+ pool->free (this );
166+ return UR_RESULT_SUCCESS;
167+ }
168+
169+ ur_native_event_t ::ur_native_event_t (
170+ ur_native_handle_t hNativeEvent, ur_context_handle_t hContext,
171+ const ur_event_native_properties_t *pProperties)
172+ : ur_event_handle_t_(reinterpret_cast <ze_event_handle_t >(hNativeEvent),
173+ hContext->getDevices ()[0],
174+ UR_EXT_COMMAND_TYPE_USER, v2::EVENT_FLAGS_PROFILING_ENABLED /* TODO: this follows legacy adapter logic, we could check this with zeEventGetPool */ ),
175+ zeEvent(reinterpret_cast <ze_event_handle_t >(hNativeEvent),
176+ pProperties ? pProperties->isNativeHandleOwned : false) {}
177+
178+ ur_result_t ur_native_event_t::forceRelease () {
179+ zeEvent.release ();
180+ delete this ;
181+ return UR_RESULT_SUCCESS;
182+ }
183+
155184namespace ur ::level_zero {
156185ur_result_t urEventRetain (ur_event_handle_t hEvent) { return hEvent->retain (); }
157186
@@ -288,4 +317,24 @@ ur_result_t urEventGetProfilingInfo(
288317
289318 return UR_RESULT_SUCCESS;
290319}
320+
321+ ur_result_t urEventGetNativeHandle (ur_event_handle_t hEvent,
322+ ur_native_handle_t *phNativeEvent) try {
323+ *phNativeEvent = reinterpret_cast <ur_native_handle_t >(hEvent->getZeEvent ());
324+ return UR_RESULT_SUCCESS;
325+ } catch (...) {
326+ return exceptionToResult (std::current_exception ());
327+ }
328+
329+ ur_result_t
330+ urEventCreateWithNativeHandle (ur_native_handle_t hNativeEvent,
331+ ur_context_handle_t hContext,
332+ const ur_event_native_properties_t *pProperties,
333+ ur_event_handle_t *phEvent) try {
334+ *phEvent = new ur_native_event_t (hNativeEvent, hContext, pProperties);
335+ return UR_RESULT_SUCCESS;
336+ } catch (...) {
337+ return exceptionToResult (std::current_exception ());
338+ }
339+
291340} // namespace ur::level_zero
0 commit comments