From ed85be932a9badfd44e2ed71f58fde1eff09a1e0 Mon Sep 17 00:00:00 2001 From: Georgi Mirazchiyski Date: Tue, 13 Feb 2024 16:02:12 +0000 Subject: [PATCH] [CUDA] Return error on silently failing urEventGetInfo queries for interop created events --- source/adapters/cuda/event.cpp | 31 +++++++++- test/adapters/cuda/CMakeLists.txt | 1 + test/adapters/cuda/event_tests.cpp | 59 +++++++++++++++++++ test/adapters/cuda/raii.h | 25 ++++++++ .../cuda/urEventCreateWithNativeHandle.cpp | 15 +---- 5 files changed, 114 insertions(+), 17 deletions(-) create mode 100644 test/adapters/cuda/event_tests.cpp create mode 100644 test/adapters/cuda/raii.h diff --git a/source/adapters/cuda/event.cpp b/source/adapters/cuda/event.cpp index 804b35a9b7..20e1c45e8a 100644 --- a/source/adapters/cuda/event.cpp +++ b/source/adapters/cuda/event.cpp @@ -17,6 +17,7 @@ #include #include +#include ur_event_handle_t_::ur_event_handle_t_(ur_command_t Type, ur_context_handle_t Context, @@ -162,11 +163,26 @@ UR_APIEXPORT ur_result_t UR_APICALL urEventGetInfo(ur_event_handle_t hEvent, size_t propValueSize, void *pPropValue, size_t *pPropValueSizeRet) { +#ifndef NDEBUG + UR_ASSERT(hEvent, UR_RESULT_ERROR_INVALID_NULL_HANDLE); +#endif + UrReturnHelper ReturnValue(propValueSize, pPropValue, pPropValueSizeRet); switch (propName) { - case UR_EVENT_INFO_COMMAND_QUEUE: + case UR_EVENT_INFO_COMMAND_QUEUE: { + // 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->backendHasOwnership()) { + 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; + } + assert(hEvent->getQueue()); return ReturnValue(hEvent->getQueue()); + } case UR_EVENT_INFO_COMMAND_TYPE: return ReturnValue(hEvent->getCommandType()); case UR_EVENT_INFO_REFERENCE_COUNT: @@ -283,8 +299,17 @@ UR_APIEXPORT ur_result_t UR_APICALL urEventCreateWithNativeHandle( std::unique_ptr EventPtr{nullptr}; - *phEvent = ur_event_handle_t_::makeWithNative( - hContext, reinterpret_cast(hNativeEvent)); + try { + EventPtr = + std::unique_ptr(ur_event_handle_t_::makeWithNative( + hContext, reinterpret_cast(hNativeEvent))); + } catch (const std::bad_alloc &) { + return UR_RESULT_ERROR_OUT_OF_HOST_MEMORY; + } catch (...) { + return UR_RESULT_ERROR_UNKNOWN; + } + + *phEvent = EventPtr.release(); return UR_RESULT_SUCCESS; } diff --git a/test/adapters/cuda/CMakeLists.txt b/test/adapters/cuda/CMakeLists.txt index fbc15b47e8..5dac386c5b 100644 --- a/test/adapters/cuda/CMakeLists.txt +++ b/test/adapters/cuda/CMakeLists.txt @@ -15,6 +15,7 @@ add_adapter_test(cuda urEventCreateWithNativeHandle.cpp kernel_tests.cpp memory_tests.cpp + event_tests.cpp ENVIRONMENT "UR_ADAPTERS_FORCE_LOAD=\"$\"" ) diff --git a/test/adapters/cuda/event_tests.cpp b/test/adapters/cuda/event_tests.cpp new file mode 100644 index 0000000000..b86cd31305 --- /dev/null +++ b/test/adapters/cuda/event_tests.cpp @@ -0,0 +1,59 @@ +// Copyright (C) 2022-2024 Intel Corporation +// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions. +// See LICENSE.TXT +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#include "event.hpp" +#include "fixtures.h" +#include "raii.h" + +using cudaEventTest = uur::urContextTest; +UUR_INSTANTIATE_DEVICE_TEST_SUITE_P(cudaEventTest); + +// Testing the urEventGetInfo behaviour for natively constructed (Cuda) events. +// Backend interop APIs can lead to creating event objects that are not fully +// initialized. In the Cuda adapter, an event can have nullptr command queue +// because the interop API does not associate a UR-owned queue with the event. +TEST_P(cudaEventTest, GetQueueFromEventCreatedWithNativeHandle) { + RAIICUevent cuda_event; + ASSERT_SUCCESS_CUDA(cuEventCreate(cuda_event.ptr(), CU_EVENT_DEFAULT)); + + auto native_event = reinterpret_cast(cuda_event.get()); + uur::raii::Event event{nullptr}; + ASSERT_SUCCESS(urEventCreateWithNativeHandle(native_event, context, nullptr, + event.ptr())); + EXPECT_NE(event, nullptr); + + size_t ret_size{}; + ur_queue_handle_t q{}; + ASSERT_EQ_RESULT(urEventGetInfo(event, UR_EVENT_INFO_COMMAND_QUEUE, + sizeof(ur_queue_handle_t), &q, &ret_size), + UR_RESULT_ERROR_ADAPTER_SPECIFIC); +} + +TEST_P(cudaEventTest, GetQueueFromNativeEvent) { + // Create a Cuda event object + CUevent cuda_event{nullptr}; + ASSERT_SUCCESS_CUDA(cuEventCreate(&cuda_event, CU_EVENT_DEFAULT)); + + auto native_event = reinterpret_cast(cuda_event); + + { + uur::raii::Event event{nullptr}; + ASSERT_SUCCESS(urEventCreateWithNativeHandle(native_event, context, + nullptr, event.ptr())); + EXPECT_NE(event, nullptr); + + size_t ret_size{}; + ur_queue_handle_t q{}; + ASSERT_EQ_RESULT(urEventGetInfo(event, UR_EVENT_INFO_COMMAND_QUEUE, + sizeof(ur_queue_handle_t), &q, + &ret_size), + UR_RESULT_ERROR_ADAPTER_SPECIFIC); + } + + // Release the Cuda event object + if (cuda_event) { + ASSERT_SUCCESS_CUDA(cuEventDestroy(cuda_event)); + } +} diff --git a/test/adapters/cuda/raii.h b/test/adapters/cuda/raii.h new file mode 100644 index 0000000000..e401082e48 --- /dev/null +++ b/test/adapters/cuda/raii.h @@ -0,0 +1,25 @@ +// Copyright (C) 2022-2024 Intel Corporation +// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions. +// See LICENSE.TXT +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#ifndef UR_TEST_CONFORMANCE_ADAPTERS_CUDA_RAII_H_INCLUDED +#define UR_TEST_CONFORMANCE_ADAPTERS_CUDA_RAII_H_INCLUDED + +#include "uur/raii.h" +#include + +struct RAIICUevent { + CUevent handle = nullptr; + + ~RAIICUevent() { + if (handle) { + cuEventDestroy(handle); + } + } + + CUevent *ptr() { return &handle; } + CUevent get() { return handle; } +}; + +#endif // UR_TEST_CONFORMANCE_ADAPTERS_CUDA_RAII_H_INCLUDED diff --git a/test/adapters/cuda/urEventCreateWithNativeHandle.cpp b/test/adapters/cuda/urEventCreateWithNativeHandle.cpp index 68a99bba4b..6079eb194c 100644 --- a/test/adapters/cuda/urEventCreateWithNativeHandle.cpp +++ b/test/adapters/cuda/urEventCreateWithNativeHandle.cpp @@ -4,24 +4,11 @@ // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception #include "fixtures.h" -#include "uur/raii.h" +#include "raii.h" using urCudaEventCreateWithNativeHandleTest = uur::urQueueTest; UUR_INSTANTIATE_DEVICE_TEST_SUITE_P(urCudaEventCreateWithNativeHandleTest); -struct RAIICUevent { - CUevent handle = nullptr; - - ~RAIICUevent() { - if (handle) { - cuEventDestroy(handle); - } - } - - CUevent *ptr() { return &handle; } - CUevent get() { return handle; } -}; - TEST_P(urCudaEventCreateWithNativeHandleTest, Success) { RAIICUevent cuda_event; ASSERT_SUCCESS_CUDA(cuEventCreate(cuda_event.ptr(), CU_EVENT_DEFAULT));