From ed9d88d4547f12d684754bcbea6a1055e70004e0 Mon Sep 17 00:00:00 2001 From: Sergey V Maslov Date: Thu, 4 Mar 2021 15:39:37 -0800 Subject: [PATCH] [SYCL] Support interop_task with Level-Zero using get_mem() Signed-off-by: Sergey V Maslov --- sycl/plugins/level_zero/pi_level_zero.cpp | 61 ++++++++++++++++++----- 1 file changed, 49 insertions(+), 12 deletions(-) diff --git a/sycl/plugins/level_zero/pi_level_zero.cpp b/sycl/plugins/level_zero/pi_level_zero.cpp index 7e1ecfdd9df77..90e397e1d2677 100644 --- a/sycl/plugins/level_zero/pi_level_zero.cpp +++ b/sycl/plugins/level_zero/pi_level_zero.cpp @@ -911,7 +911,6 @@ zeModuleGetPropertiesMock(ze_module_handle_t hModule, static bool isOnlineLinkEnabled(); // End forward declarations for mock Level Zero APIs -std::once_flag OnceFlag; // This function will ensure compatibility with both Linux and Windowns for // setting environment variables. @@ -2548,9 +2547,8 @@ pi_result piMemImageCreate(pi_context Context, pi_mem_flags Flags, } pi_result piextMemGetNativeHandle(pi_mem Mem, pi_native_handle *NativeHandle) { - (void)Mem; - (void)NativeHandle; - die("piextMemGetNativeHandle: not supported"); + PI_ASSERT(Mem, PI_INVALID_MEM_OBJECT); + *NativeHandle = pi_cast(Mem->getZeHandle()); return PI_SUCCESS; } @@ -4107,13 +4105,52 @@ pi_result piSamplerRelease(pi_sampler Sampler) { // pi_result piEnqueueEventsWait(pi_queue Queue, pi_uint32 NumEventsInWaitList, const pi_event *EventWaitList, pi_event *Event) { - (void)Queue; - (void)NumEventsInWaitList; - (void)EventWaitList; - (void)Event; - die("piEnqueueEventsWait: not implemented"); - return {}; + PI_ASSERT(Queue, PI_INVALID_QUEUE); + PI_ASSERT(Event, PI_INVALID_EVENT); + + _pi_ze_event_list_t TmpWaitList; + if (auto Res = TmpWaitList.createAndRetainPiZeEventList(NumEventsInWaitList, + EventWaitList, Queue)) + return Res; + + // Lock automatically releases when this goes out of scope. + std::lock_guard lock(Queue->PiQueueMutex); + + // Get a new command list to be used on this call + ze_command_list_handle_t ZeCommandList = nullptr; + ze_fence_handle_t ZeFence = nullptr; + if (auto Res = Queue->Context->getAvailableCommandList(Queue, &ZeCommandList, + &ZeFence)) + return Res; + + ze_event_handle_t ZeEvent = nullptr; + auto Res = createEventAndAssociateQueue(Queue, Event, PI_COMMAND_TYPE_USER, + ZeCommandList); + if (Res != PI_SUCCESS) + return Res; + ZeEvent = (*Event)->ZeEvent; + (*Event)->WaitList = TmpWaitList; + + const auto &WaitList = (*Event)->WaitList; + if (WaitList.Length) { + ZE_CALL(zeCommandListAppendWaitOnEvents(ZeCommandList, WaitList.Length, + WaitList.ZeEventList)); + + ZE_CALL(zeCommandListAppendSignalEvent(ZeCommandList, ZeEvent)); + + // Execute command list asynchronously as the event will be used + // to track down its completion. + return Queue->executeCommandList(ZeCommandList, ZeFence); + } else { + // If wait-list is empty, then this particular command should wait until + // all previous enqueued commands to the command-queue have completed. + // + // TODO: find a way to do that without blocking the host. + ZE_CALL(zeCommandQueueSynchronize(Queue->ZeCommandQueue, UINT64_MAX)); + ZE_CALL(zeEventHostSignal(ZeEvent)); + return PI_SUCCESS; + } } pi_result piEnqueueEventsWaitWithBarrier(pi_queue Queue, @@ -4798,8 +4835,8 @@ enqueueMemImageCommandHelper(pi_command_type CommandType, pi_queue Queue, if (Result != PI_SUCCESS) return Result; - // TODO: Level Zero does not support row_pitch/slice_pitch for images yet. - // Check that SYCL RT did not want pitch larger than default. + // TODO: Level Zero does not support row_pitch/slice_pitch for images yet. + // Check that SYCL RT did not want pitch larger than default. (void)RowPitch; (void)SlicePitch; #ifndef NDEBUG