Skip to content

Commit d671824

Browse files
Georgi Mirazchiyskiomarahmed1111
authored andcommitted
[CUDA] Return error on silently failing urEventGetInfo queries for interop created events
1 parent de53693 commit d671824

File tree

5 files changed

+72
-15
lines changed

5 files changed

+72
-15
lines changed

source/adapters/cuda/event.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include <cassert>
1919
#include <cuda.h>
20+
#include <memory>
2021

2122
ur_event_handle_t_::ur_event_handle_t_(ur_command_t Type,
2223
ur_context_handle_t Context,
@@ -164,8 +165,19 @@ UR_APIEXPORT ur_result_t UR_APICALL urEventGetInfo(ur_event_handle_t hEvent,
164165
UrReturnHelper ReturnValue(propValueSize, pPropValue, pPropValueSizeRet);
165166

166167
switch (propName) {
167-
case UR_EVENT_INFO_COMMAND_QUEUE:
168+
case UR_EVENT_INFO_COMMAND_QUEUE: {
169+
// If the runtime owns the native handle, we have reference to the queue.
170+
// Otherwise, the event handle comes from an interop API with no RT refs.
171+
if (!hEvent->backendHasOwnership()) {
172+
setErrorMessage("Command queue info cannot be queried for the event. The "
173+
"event object was created from a native event and has no "
174+
"valid reference to a command queue.",
175+
UR_RESULT_ERROR_INVALID_VALUE);
176+
return UR_RESULT_ERROR_ADAPTER_SPECIFIC;
177+
}
178+
assert(hEvent->getQueue());
168179
return ReturnValue(hEvent->getQueue());
180+
}
169181
case UR_EVENT_INFO_COMMAND_TYPE:
170182
return ReturnValue(hEvent->getCommandType());
171183
case UR_EVENT_INFO_REFERENCE_COUNT:

test/adapters/cuda/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ add_adapter_test(cuda
1616
urQueueGetNativeHandle.cpp
1717
kernel_tests.cpp
1818
memory_tests.cpp
19+
event_tests.cpp
1920
#FIXME: make this cleaner
2021
${CMAKE_CURRENT_SOURCE_DIR}/../../../source/adapters/cuda/queue.cpp
2122
${CMAKE_CURRENT_SOURCE_DIR}/../../../source/adapters/cuda/common.cpp

test/adapters/cuda/event_tests.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (C) 2022-2024 Intel Corporation
2+
// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions.
3+
// See LICENSE.TXT
4+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5+
6+
#include "event.hpp"
7+
#include "fixtures.h"
8+
#include "raii.h"
9+
10+
using cudaEventTest = uur::urContextTest;
11+
UUR_INSTANTIATE_DEVICE_TEST_SUITE_P(cudaEventTest);
12+
13+
// Testing the urEventGetInfo behaviour for natively constructed (Cuda) events.
14+
// Backend interop APIs can lead to creating event objects that are not fully
15+
// initialized. In the Cuda adapter, an event can have nullptr command queue
16+
// because the interop API does not associate a UR-owned queue with the event.
17+
TEST_P(cudaEventTest, GetQueueFromEventCreatedWithNativeHandle) {
18+
RAIICUevent cuda_event;
19+
ASSERT_SUCCESS_CUDA(cuEventCreate(cuda_event.ptr(), CU_EVENT_DEFAULT));
20+
21+
auto native_event = reinterpret_cast<ur_native_handle_t>(cuda_event.get());
22+
uur::raii::Event event{nullptr};
23+
ASSERT_SUCCESS(urEventCreateWithNativeHandle(native_event, context, nullptr,
24+
event.ptr()));
25+
EXPECT_NE(event, nullptr);
26+
27+
size_t ret_size{};
28+
ur_queue_handle_t q{};
29+
ASSERT_EQ_RESULT(urEventGetInfo(event, UR_EVENT_INFO_COMMAND_QUEUE,
30+
sizeof(ur_queue_handle_t), &q, &ret_size),
31+
UR_RESULT_ERROR_ADAPTER_SPECIFIC);
32+
}

test/adapters/cuda/raii.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (C) 2022-2024 Intel Corporation
2+
// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions.
3+
// See LICENSE.TXT
4+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5+
6+
#ifndef UR_TEST_CONFORMANCE_ADAPTERS_CUDA_RAII_H_INCLUDED
7+
#define UR_TEST_CONFORMANCE_ADAPTERS_CUDA_RAII_H_INCLUDED
8+
9+
#include "uur/raii.h"
10+
#include <cuda.h>
11+
12+
struct RAIICUevent {
13+
CUevent handle = nullptr;
14+
15+
~RAIICUevent() {
16+
if (handle) {
17+
cuEventDestroy(handle);
18+
}
19+
}
20+
21+
CUevent *ptr() { return &handle; }
22+
CUevent get() { return handle; }
23+
};
24+
25+
#endif // UR_TEST_CONFORMANCE_ADAPTERS_CUDA_RAII_H_INCLUDED

test/adapters/cuda/urEventCreateWithNativeHandle.cpp

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,11 @@
44
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
55

66
#include "fixtures.h"
7-
#include "uur/raii.h"
7+
#include "raii.h"
88

99
using urCudaEventCreateWithNativeHandleTest = uur::urQueueTest;
1010
UUR_INSTANTIATE_DEVICE_TEST_SUITE_P(urCudaEventCreateWithNativeHandleTest);
1111

12-
struct RAIICUevent {
13-
CUevent handle = nullptr;
14-
15-
~RAIICUevent() {
16-
if (handle) {
17-
cuEventDestroy(handle);
18-
}
19-
}
20-
21-
CUevent *ptr() { return &handle; }
22-
CUevent get() { return handle; }
23-
};
24-
2512
TEST_P(urCudaEventCreateWithNativeHandleTest, Success) {
2613
RAIICUevent cuda_event;
2714
ASSERT_SUCCESS_CUDA(cuEventCreate(cuda_event.ptr(), CU_EVENT_DEFAULT));

0 commit comments

Comments
 (0)