|
| 1 | +// Copyright (C) 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 "ur_api.h" |
| 7 | +#include "uur/checks.h" |
| 8 | +#include "ze_api.h" |
| 9 | +#include <cstring> |
| 10 | +#include <thread> |
| 11 | +#include <uur/fixtures.h> |
| 12 | + |
| 13 | +using namespace std::chrono_literals; |
| 14 | +using urLevelZeroEventNativeHandleTest = uur::urQueueTest; |
| 15 | +UUR_INSTANTIATE_DEVICE_TEST_SUITE_P(urLevelZeroEventNativeHandleTest); |
| 16 | + |
| 17 | +#define TEST_MEMCPY_SIZE 4096 |
| 18 | + |
| 19 | +TEST_P(urLevelZeroEventNativeHandleTest, WaitForNative) { |
| 20 | + ze_event_pool_desc_t desc; |
| 21 | + desc.stype = ZE_STRUCTURE_TYPE_EVENT_POOL_DESC; |
| 22 | + desc.pNext = nullptr; |
| 23 | + desc.count = 1; |
| 24 | + desc.flags = ZE_EVENT_POOL_FLAG_HOST_VISIBLE; |
| 25 | + |
| 26 | + ur_native_handle_t nativeContext; |
| 27 | + ASSERT_SUCCESS(urContextGetNativeHandle(context, &nativeContext)); |
| 28 | + |
| 29 | + ur_native_handle_t nativeDevice; |
| 30 | + ASSERT_SUCCESS(urDeviceGetNativeHandle(device, &nativeDevice)); |
| 31 | + |
| 32 | + ze_event_pool_handle_t pool = nullptr; |
| 33 | + |
| 34 | + ASSERT_EQ(zeEventPoolCreate((ze_context_handle_t)nativeContext, &desc, 1, |
| 35 | + (ze_device_handle_t *)&nativeDevice, &pool), |
| 36 | + ZE_RESULT_SUCCESS); |
| 37 | + |
| 38 | + ze_event_desc_t eventDesc; |
| 39 | + eventDesc.pNext = nullptr; |
| 40 | + eventDesc.stype = ZE_STRUCTURE_TYPE_EVENT_DESC; |
| 41 | + eventDesc.index = 0; |
| 42 | + eventDesc.signal = ZE_EVENT_SCOPE_FLAG_HOST; |
| 43 | + eventDesc.wait = 0; |
| 44 | + |
| 45 | + ze_event_handle_t zeEvent; |
| 46 | + ASSERT_EQ(zeEventCreate(pool, &eventDesc, &zeEvent), ZE_RESULT_SUCCESS); |
| 47 | + |
| 48 | + ur_event_native_properties_t pprops; |
| 49 | + pprops.isNativeHandleOwned = false; |
| 50 | + pprops.pNext = nullptr; |
| 51 | + pprops.stype = UR_STRUCTURE_TYPE_EVENT_NATIVE_PROPERTIES; |
| 52 | + |
| 53 | + ur_event_handle_t urEvent; |
| 54 | + ASSERT_SUCCESS(urEventCreateWithNativeHandle((ur_native_handle_t)zeEvent, |
| 55 | + context, &pprops, &urEvent)); |
| 56 | + |
| 57 | + int *src = (int *)malloc(TEST_MEMCPY_SIZE); |
| 58 | + memset(src, 0xc, TEST_MEMCPY_SIZE); |
| 59 | + |
| 60 | + int *dst = (int *)malloc(TEST_MEMCPY_SIZE); |
| 61 | + memset(dst, 0, TEST_MEMCPY_SIZE); |
| 62 | + |
| 63 | + int *dst2 = (int *)malloc(TEST_MEMCPY_SIZE); |
| 64 | + memset(dst, 0, TEST_MEMCPY_SIZE); |
| 65 | + |
| 66 | + ur_event_handle_t memcpyEvent2; |
| 67 | + ASSERT_SUCCESS(urEnqueueUSMMemcpy(queue, false, dst2, src, TEST_MEMCPY_SIZE, |
| 68 | + 0, nullptr, &memcpyEvent2)); |
| 69 | + |
| 70 | + ur_event_handle_t memcpyEvent3; |
| 71 | + ASSERT_SUCCESS(urEnqueueUSMMemcpy(queue, false, dst2, src, TEST_MEMCPY_SIZE, |
| 72 | + 0, nullptr, &memcpyEvent3)); |
| 73 | + |
| 74 | + // just to make wait lists contain more than 1 event |
| 75 | + ur_event_handle_t events[] = {memcpyEvent2, urEvent, memcpyEvent3}; |
| 76 | + |
| 77 | + ur_event_handle_t waitEvent; |
| 78 | + ASSERT_SUCCESS( |
| 79 | + urEnqueueEventsWaitWithBarrier(queue, 3, events, &waitEvent)); |
| 80 | + |
| 81 | + ur_event_handle_t memcpyEvent; |
| 82 | + ASSERT_SUCCESS(urEnqueueUSMMemcpy(queue, false, dst, src, TEST_MEMCPY_SIZE, |
| 83 | + 1, &waitEvent, &memcpyEvent)); |
| 84 | + |
| 85 | + // urQueueFinish would hang, so we flush and then wait |
| 86 | + // some time to make sure the gpu had plenty of time |
| 87 | + // to do the memcpy. |
| 88 | + urQueueFlush(queue); |
| 89 | + std::this_thread::sleep_for(500ms); |
| 90 | + |
| 91 | + ASSERT_NE(memcmp(src, dst, TEST_MEMCPY_SIZE), 0); |
| 92 | + |
| 93 | + zeEventHostSignal(zeEvent); |
| 94 | + |
| 95 | + urQueueFinish(queue); |
| 96 | + |
| 97 | + ASSERT_EQ(memcmp(src, dst, 4096), 0); |
| 98 | + |
| 99 | + free(src); |
| 100 | + free(dst); |
| 101 | + free(dst2); |
| 102 | + urEventRelease(urEvent); |
| 103 | + urEventRelease(waitEvent); |
| 104 | + urEventRelease(memcpyEvent); |
| 105 | + urEventRelease(memcpyEvent2); |
| 106 | + urEventRelease(memcpyEvent3); |
| 107 | + zeEventDestroy(zeEvent); |
| 108 | + zeEventPoolDestroy(pool); |
| 109 | +} |
0 commit comments