|
| 1 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM |
| 2 | +// Exceptions. See https://llvm.org/LICENSE.txt for license information. |
| 3 | +// |
| 4 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 5 | +// |
| 6 | +// Multi-device buffer tests that stress host-staged migration when a discrete |
| 7 | +// buffer is accessed from different devices/queues (for example when device |
| 8 | +// peer access is not available). Corresponds to L0 v2 discrete-buffer |
| 9 | +// getDevicePtr migration ordering. |
| 10 | +// |
| 11 | +// The tests cover two migration paths inside getDevicePtr: |
| 12 | +// - Async path (cmdList != nullptr): triggered by urEnqueueMem* operations. |
| 13 | +// - Sync fallback (cmdList == nullptr): triggered by urMemGetNativeHandle. |
| 14 | + |
| 15 | +#include <uur/fixtures.h> |
| 16 | +#include <vector> |
| 17 | + |
| 18 | +struct urEnqueueMemBufferMultiDeviceMigrationTest |
| 19 | + : uur::urMultiDeviceMemBufferQueueTest { |
| 20 | + void SetUp() override { |
| 21 | + UUR_RETURN_ON_FATAL_FAILURE(uur::urMultiDeviceMemBufferQueueTest::SetUp()); |
| 22 | + |
| 23 | + if (devices.size() < 2) { |
| 24 | + GTEST_SKIP() << "Test requires at least 2 devices"; |
| 25 | + } |
| 26 | + |
| 27 | + // Check that the USM P2P extension is supported on both devices. |
| 28 | + for (size_t i = 0; i < 2; i++) { |
| 29 | + ur_bool_t usm_p2p_support = false; |
| 30 | + ASSERT_SUCCESS( |
| 31 | + urDeviceGetInfo(devices[i], UR_DEVICE_INFO_USM_P2P_SUPPORT_EXP, |
| 32 | + sizeof(usm_p2p_support), &usm_p2p_support, nullptr)); |
| 33 | + if (!usm_p2p_support) { |
| 34 | + GTEST_SKIP() << "EXP usm p2p feature is not supported on device " << i; |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + // This test exercises the host-mediated migration fallback, which is only |
| 39 | + // triggered when P2P access is NOT available between the two devices. |
| 40 | + // Skip if hardware P2P is present — the fallback path would never run. |
| 41 | + int p2pSupported = 0; |
| 42 | + ur_result_t res = urUsmP2PPeerAccessGetInfoExp( |
| 43 | + devices[0], devices[1], UR_EXP_PEER_INFO_UR_PEER_ACCESS_SUPPORT, |
| 44 | + sizeof(p2pSupported), &p2pSupported, nullptr); |
| 45 | + if (res == UR_RESULT_SUCCESS && p2pSupported) { |
| 46 | + GTEST_SKIP() << "Devices have P2P access; host-migration path is not " |
| 47 | + "exercised"; |
| 48 | + } |
| 49 | + } |
| 50 | +}; |
| 51 | +UUR_INSTANTIATE_PLATFORM_TEST_SUITE(urEnqueueMemBufferMultiDeviceMigrationTest); |
| 52 | + |
| 53 | +TEST_P(urEnqueueMemBufferMultiDeviceMigrationTest, |
| 54 | + AsyncFillThenReadOnSecondQueueWithWait) { |
| 55 | + const uint32_t pattern = 0xA5A5A501; |
| 56 | + ur_event_handle_t fillEv = nullptr; |
| 57 | + ASSERT_SUCCESS(urEnqueueMemBufferFill(queues[0], buffer, &pattern, |
| 58 | + sizeof(pattern), 0, size, 0, nullptr, |
| 59 | + &fillEv)); |
| 60 | + ASSERT_NE(fillEv, nullptr); |
| 61 | + |
| 62 | + std::vector<uint32_t> output(count, 0); |
| 63 | + ASSERT_SUCCESS(urEnqueueMemBufferRead(queues[1], buffer, true, 0, size, |
| 64 | + output.data(), 1, &fillEv, nullptr)); |
| 65 | + |
| 66 | + ASSERT_SUCCESS(urEventRelease(fillEv)); |
| 67 | + |
| 68 | + for (size_t i = 0; i < count; ++i) { |
| 69 | + ASSERT_EQ(pattern, output[i]) << "Mismatch at index " << i; |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +TEST_P(urEnqueueMemBufferMultiDeviceMigrationTest, |
| 74 | + PingPongFillBetweenTwoDeviceQueues) { |
| 75 | + const uint32_t pattern1 = 0xC001D00u; |
| 76 | + ur_event_handle_t evFill1 = nullptr; |
| 77 | + ASSERT_SUCCESS(urEnqueueMemBufferFill(queues[0], buffer, &pattern1, |
| 78 | + sizeof(pattern1), 0, size, 0, nullptr, |
| 79 | + &evFill1)); |
| 80 | + ASSERT_NE(evFill1, nullptr); |
| 81 | + |
| 82 | + std::vector<uint32_t> stage1(count, 0); |
| 83 | + ASSERT_SUCCESS(urEnqueueMemBufferRead(queues[1], buffer, true, 0, size, |
| 84 | + stage1.data(), 1, &evFill1, nullptr)); |
| 85 | + ASSERT_SUCCESS(urEventRelease(evFill1)); |
| 86 | + for (size_t i = 0; i < count; ++i) { |
| 87 | + ASSERT_EQ(pattern1, stage1[i]); |
| 88 | + } |
| 89 | + |
| 90 | + const uint32_t pattern2 = 0xD00DAD00u; |
| 91 | + ur_event_handle_t evFill2 = nullptr; |
| 92 | + ASSERT_SUCCESS(urEnqueueMemBufferFill(queues[1], buffer, &pattern2, |
| 93 | + sizeof(pattern2), 0, size, 0, nullptr, |
| 94 | + &evFill2)); |
| 95 | + ASSERT_NE(evFill2, nullptr); |
| 96 | + |
| 97 | + std::vector<uint32_t> stage2(count, 0); |
| 98 | + ASSERT_SUCCESS(urEnqueueMemBufferRead(queues[0], buffer, true, 0, size, |
| 99 | + stage2.data(), 1, &evFill2, nullptr)); |
| 100 | + ASSERT_SUCCESS(urEventRelease(evFill2)); |
| 101 | + for (size_t i = 0; i < count; ++i) { |
| 102 | + ASSERT_EQ(pattern2, stage2[i]); |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +TEST_P(urEnqueueMemBufferMultiDeviceMigrationTest, |
| 107 | + ChainedAsyncOpsAcrossQueuesWithEvents) { |
| 108 | + const uint32_t patternA = 0x11111111u; |
| 109 | + ur_event_handle_t evFill = nullptr; |
| 110 | + ASSERT_SUCCESS(urEnqueueMemBufferFill(queues[0], buffer, &patternA, |
| 111 | + sizeof(patternA), 0, size, 0, nullptr, |
| 112 | + &evFill)); |
| 113 | + ASSERT_NE(evFill, nullptr); |
| 114 | + |
| 115 | + std::vector<uint32_t> verifyA(count, 0); |
| 116 | + ASSERT_SUCCESS(urEnqueueMemBufferRead(queues[1], buffer, true, 0, size, |
| 117 | + verifyA.data(), 1, &evFill, nullptr)); |
| 118 | + ASSERT_SUCCESS(urEventRelease(evFill)); |
| 119 | + for (size_t i = 0; i < count; ++i) { |
| 120 | + ASSERT_EQ(patternA, verifyA[i]); |
| 121 | + } |
| 122 | + |
| 123 | + const uint32_t patternB = 0x22222222u; |
| 124 | + std::vector<uint32_t> hostB(count, patternB); |
| 125 | + ur_event_handle_t evWrite = nullptr; |
| 126 | + ASSERT_SUCCESS(urEnqueueMemBufferWrite(queues[1], buffer, true, 0, size, |
| 127 | + hostB.data(), 0, nullptr, &evWrite)); |
| 128 | + ASSERT_NE(evWrite, nullptr); |
| 129 | + |
| 130 | + std::vector<uint32_t> verifyB(count, 0); |
| 131 | + ASSERT_SUCCESS(urEnqueueMemBufferRead(queues[0], buffer, true, 0, size, |
| 132 | + verifyB.data(), 1, &evWrite, nullptr)); |
| 133 | + ASSERT_SUCCESS(urEventRelease(evWrite)); |
| 134 | + for (size_t i = 0; i < count; ++i) { |
| 135 | + ASSERT_EQ(patternB, verifyB[i]); |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +// Exercise the synchronous fallback migration path in getDevicePtr |
| 140 | +// (cmdList == nullptr), which is triggered by urMemGetNativeHandle. |
| 141 | +// Fill the buffer on device 0, then request its native pointer on device 1 to |
| 142 | +// force a synchronous host-staged migration, then verify the data on device 1. |
| 143 | +TEST_P(urEnqueueMemBufferMultiDeviceMigrationTest, |
| 144 | + SyncFallbackMigrationViaNativeHandle) { |
| 145 | + const uint32_t pattern = 0xDEADBEEFu; |
| 146 | + ASSERT_SUCCESS(urEnqueueMemBufferFill(queues[0], buffer, &pattern, |
| 147 | + sizeof(pattern), 0, size, 0, nullptr, |
| 148 | + nullptr)); |
| 149 | + ASSERT_SUCCESS(urQueueFinish(queues[0])); |
| 150 | + |
| 151 | + // urMemGetNativeHandle calls getDevicePtr with cmdList == nullptr, |
| 152 | + // triggering the synchronous device->host->device migration path. |
| 153 | + ur_native_handle_t nativePtr = 0; |
| 154 | + ASSERT_SUCCESS(urMemGetNativeHandle(buffer, devices[1], &nativePtr)); |
| 155 | + ASSERT_NE(nativePtr, (ur_native_handle_t)0); |
| 156 | + |
| 157 | + std::vector<uint32_t> output(count, 0); |
| 158 | + ASSERT_SUCCESS(urEnqueueMemBufferRead(queues[1], buffer, true, 0, size, |
| 159 | + output.data(), 0, nullptr, nullptr)); |
| 160 | + for (size_t i = 0; i < count; ++i) { |
| 161 | + ASSERT_EQ(pattern, output[i]) << "Mismatch at index " << i; |
| 162 | + } |
| 163 | +} |
0 commit comments