|
| 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 <cuda.h> |
| 7 | +#include <uur/fixtures.h> |
| 8 | +#include <vector> |
| 9 | + |
| 10 | +using T = uint32_t; |
| 11 | + |
| 12 | +struct urCudaEnqueueNativeCommandTest : uur::urQueueTest { |
| 13 | + void SetUp() { |
| 14 | + UUR_RETURN_ON_FATAL_FAILURE(uur::urQueueTest::SetUp()); |
| 15 | + |
| 16 | + host_vec = std::vector<T>(global_size, 0); |
| 17 | + ASSERT_EQ(host_vec.size(), global_size); |
| 18 | + ASSERT_SUCCESS(urUSMDeviceAlloc(context, device, nullptr, nullptr, |
| 19 | + allocation_size, &device_ptr)); |
| 20 | + ASSERT_NE(device_ptr, nullptr); |
| 21 | + } |
| 22 | + static constexpr T val = 42; |
| 23 | + static constexpr uint32_t global_size = 1e7; |
| 24 | + std::vector<T> host_vec; |
| 25 | + void *device_ptr = nullptr; |
| 26 | + static constexpr size_t allocation_size = sizeof(val) * global_size; |
| 27 | +}; |
| 28 | + |
| 29 | +UUR_INSTANTIATE_DEVICE_TEST_SUITE_P(urCudaEnqueueNativeCommandTest); |
| 30 | + |
| 31 | +struct InteropData1 { |
| 32 | + void *fill_ptr; |
| 33 | +}; |
| 34 | + |
| 35 | +// Fill a device ptr with the pattern val |
| 36 | +void interop_func_1(ur_queue_handle_t hQueue, void *data) { |
| 37 | + CUstream stream; |
| 38 | + ASSERT_SUCCESS( |
| 39 | + urQueueGetNativeHandle(hQueue, nullptr, (ur_native_handle_t *)&stream)); |
| 40 | + InteropData1 *func_data = reinterpret_cast<InteropData1 *>(data); |
| 41 | + |
| 42 | + ASSERT_EQ(cuMemsetD32Async((CUdeviceptr)func_data->fill_ptr, |
| 43 | + urCudaEnqueueNativeCommandTest::val, |
| 44 | + urCudaEnqueueNativeCommandTest::global_size, |
| 45 | + stream), |
| 46 | + CUDA_SUCCESS); |
| 47 | +} |
| 48 | + |
| 49 | +struct InteropData2 { |
| 50 | + void *from, *to; |
| 51 | +}; |
| 52 | + |
| 53 | +// Read from device ptr to host ptr |
| 54 | +void interop_func_2(ur_queue_handle_t hQueue, void *data) { |
| 55 | + CUstream stream; |
| 56 | + ASSERT_SUCCESS( |
| 57 | + urQueueGetNativeHandle(hQueue, nullptr, (ur_native_handle_t *)&stream)); |
| 58 | + InteropData2 *func_data = reinterpret_cast<InteropData2 *>(data); |
| 59 | + |
| 60 | + ASSERT_EQ(cuMemcpyDtoHAsync(func_data->to, (CUdeviceptr)func_data->from, |
| 61 | + urCudaEnqueueNativeCommandTest::allocation_size, |
| 62 | + stream), |
| 63 | + CUDA_SUCCESS); |
| 64 | +} |
| 65 | + |
| 66 | +TEST_P(urCudaEnqueueNativeCommandTest, Success) { |
| 67 | + InteropData1 data_1{device_ptr}; |
| 68 | + ur_event_handle_t event_1; |
| 69 | + ASSERT_SUCCESS(urEnqueueNativeCommandExp(queue, &interop_func_1, &data_1, |
| 70 | + nullptr, 0, nullptr, &event_1)); |
| 71 | +} |
| 72 | + |
| 73 | +TEST_P(urCudaEnqueueNativeCommandTest, Dependencies) { |
| 74 | + ur_event_handle_t event_1, event_2; |
| 75 | + |
| 76 | + InteropData1 data_1{device_ptr}; |
| 77 | + ASSERT_SUCCESS(urEnqueueNativeCommandExp(queue, &interop_func_1, &data_1, |
| 78 | + nullptr, 0, nullptr, &event_1)); |
| 79 | + |
| 80 | + InteropData2 data_2{device_ptr, host_vec.data()}; |
| 81 | + ASSERT_SUCCESS(urEnqueueNativeCommandExp(queue, &interop_func_2, &data_2, |
| 82 | + nullptr, 1, &event_1, &event_2)); |
| 83 | + urQueueFinish(queue); |
| 84 | + for (auto &i : host_vec) { |
| 85 | + ASSERT_EQ(i, val); |
| 86 | + } |
| 87 | +} |
0 commit comments