|
| 1 | +// Copyright (C) 2025 Intel Corporation |
| 2 | +// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM |
| 3 | +// Exceptions. See LICENSE.TXT |
| 4 | +// |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | + |
| 7 | +#include "fixtures.h" |
| 8 | +#include <array> |
| 9 | +#include <cstring> |
| 10 | + |
| 11 | +// Tests that adapter implementation of urEnqueueCommandBufferExp serializes |
| 12 | +// submissions of the same UR command-buffer object with respect to previous |
| 13 | +// submissions. |
| 14 | +// |
| 15 | +// Done using a kernel that increments a value, so the ordering of the two |
| 16 | +// submission isn't tested, only that they didn't run concurrently. See the |
| 17 | +// enqueue_update.cpp test for a test verifying the order of submissions, as |
| 18 | +// the input/output to the kernels can be modified between the submissions. |
| 19 | +struct urEnqueueCommandBufferExpTest |
| 20 | + : uur::command_buffer::urCommandBufferExpExecutionTest { |
| 21 | + virtual void SetUp() override { |
| 22 | + program_name = "increment"; |
| 23 | + UUR_RETURN_ON_FATAL_FAILURE(urCommandBufferExpExecutionTest::SetUp()); |
| 24 | + |
| 25 | + // Create an in-order queue |
| 26 | + ur_queue_properties_t queue_properties = { |
| 27 | + UR_STRUCTURE_TYPE_QUEUE_PROPERTIES, nullptr, 0}; |
| 28 | + ASSERT_SUCCESS( |
| 29 | + urQueueCreate(context, device, &queue_properties, &in_order_queue)); |
| 30 | + |
| 31 | + // Create an out-of-order queue |
| 32 | + queue_properties.flags = UR_QUEUE_FLAG_OUT_OF_ORDER_EXEC_MODE_ENABLE; |
| 33 | + ASSERT_SUCCESS( |
| 34 | + urQueueCreate(context, device, &queue_properties, &out_of_order_queue)); |
| 35 | + ASSERT_NE(out_of_order_queue, nullptr); |
| 36 | + |
| 37 | + ASSERT_SUCCESS(urUSMDeviceAlloc(context, device, nullptr, nullptr, |
| 38 | + allocation_size, &device_ptr)); |
| 39 | + ASSERT_NE(device_ptr, nullptr); |
| 40 | + |
| 41 | + uint32_t zero_pattern = 0; |
| 42 | + ASSERT_SUCCESS(urEnqueueUSMFill(queue, device_ptr, sizeof(zero_pattern), |
| 43 | + &zero_pattern, allocation_size, 0, nullptr, |
| 44 | + nullptr)); |
| 45 | + ASSERT_SUCCESS(urQueueFinish(queue)); |
| 46 | + |
| 47 | + // Create command-buffer with a single kernel that does "Ptr[i] += 1;" |
| 48 | + ASSERT_SUCCESS(urKernelSetArgPointer(kernel, 0, nullptr, device_ptr)); |
| 49 | + ASSERT_SUCCESS(urCommandBufferAppendKernelLaunchExp( |
| 50 | + cmd_buf_handle, kernel, n_dimensions, &global_offset, &global_size, |
| 51 | + nullptr, 0, nullptr, 0, nullptr, 0, nullptr, nullptr, nullptr, |
| 52 | + nullptr)); |
| 53 | + ASSERT_SUCCESS(urCommandBufferFinalizeExp(cmd_buf_handle)); |
| 54 | + } |
| 55 | + |
| 56 | + virtual void TearDown() override { |
| 57 | + if (in_order_queue) { |
| 58 | + EXPECT_SUCCESS(urQueueRelease(in_order_queue)); |
| 59 | + } |
| 60 | + |
| 61 | + if (out_of_order_queue) { |
| 62 | + EXPECT_SUCCESS(urQueueRelease(out_of_order_queue)); |
| 63 | + } |
| 64 | + |
| 65 | + if (device_ptr) { |
| 66 | + EXPECT_SUCCESS(urUSMFree(context, device_ptr)); |
| 67 | + } |
| 68 | + |
| 69 | + UUR_RETURN_ON_FATAL_FAILURE(urCommandBufferExpExecutionTest::TearDown()); |
| 70 | + } |
| 71 | + |
| 72 | + ur_queue_handle_t in_order_queue = nullptr; |
| 73 | + ur_queue_handle_t out_of_order_queue = nullptr; |
| 74 | + |
| 75 | + static constexpr size_t global_size = 16; |
| 76 | + static constexpr size_t global_offset = 0; |
| 77 | + static constexpr size_t n_dimensions = 1; |
| 78 | + static constexpr size_t allocation_size = sizeof(uint32_t) * global_size; |
| 79 | + void *device_ptr = nullptr; |
| 80 | +}; |
| 81 | + |
| 82 | +UUR_INSTANTIATE_DEVICE_TEST_SUITE(urEnqueueCommandBufferExpTest); |
| 83 | + |
| 84 | +// Tests that the same command-buffer submitted across different in-order |
| 85 | +// queues has an implicit dependency on first submission |
| 86 | +TEST_P(urEnqueueCommandBufferExpTest, SerializeAcrossQueues) { |
| 87 | + // Execute command-buffer to first in-order queue (created by parent |
| 88 | + // urQueueTest fixture) |
| 89 | + ASSERT_SUCCESS( |
| 90 | + urEnqueueCommandBufferExp(queue, cmd_buf_handle, 0, nullptr, nullptr)); |
| 91 | + |
| 92 | + // Execute command-buffer to second in-order queue, should have implicit |
| 93 | + // dependency on first submission. |
| 94 | + ASSERT_SUCCESS(urEnqueueCommandBufferExp(in_order_queue, cmd_buf_handle, 0, |
| 95 | + nullptr, nullptr)); |
| 96 | + |
| 97 | + // Wait for both submissions to complete |
| 98 | + ASSERT_SUCCESS(urQueueFlush(queue)); |
| 99 | + ASSERT_SUCCESS(urQueueFinish(in_order_queue)); |
| 100 | + |
| 101 | + std::vector<uint32_t> Output(global_size); |
| 102 | + ASSERT_SUCCESS(urEnqueueUSMMemcpy(queue, false, Output.data(), device_ptr, |
| 103 | + allocation_size, 0, nullptr, nullptr)); |
| 104 | + ASSERT_SUCCESS(urQueueFinish(queue)); |
| 105 | + |
| 106 | + // Verify |
| 107 | + const uint32_t reference = 2; |
| 108 | + for (size_t i = 0; i < global_size; i++) { |
| 109 | + ASSERT_EQ(reference, Output[i]); |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +// Tests that submitting a command-buffer twice to an out-of-order queue |
| 114 | +// relying on implicit serialization semantics for dependencies. |
| 115 | +TEST_P(urEnqueueCommandBufferExpTest, SerializeOutofOrderQueue) { |
| 116 | + ASSERT_SUCCESS(urEnqueueCommandBufferExp(out_of_order_queue, cmd_buf_handle, |
| 117 | + 0, nullptr, nullptr)); |
| 118 | + ASSERT_SUCCESS(urEnqueueCommandBufferExp(out_of_order_queue, cmd_buf_handle, |
| 119 | + 0, nullptr, nullptr)); |
| 120 | + |
| 121 | + // Wait for both submissions to complete |
| 122 | + ASSERT_SUCCESS(urQueueFinish(out_of_order_queue)); |
| 123 | + |
| 124 | + std::vector<uint32_t> Output(global_size); |
| 125 | + ASSERT_SUCCESS(urEnqueueUSMMemcpy(out_of_order_queue, true, Output.data(), |
| 126 | + device_ptr, allocation_size, 0, nullptr, |
| 127 | + nullptr)); |
| 128 | + |
| 129 | + // Verify |
| 130 | + const uint32_t reference = 2; |
| 131 | + for (size_t i = 0; i < global_size; i++) { |
| 132 | + ASSERT_EQ(reference, Output[i]); |
| 133 | + } |
| 134 | +} |
0 commit comments