|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (C) 2025 Intel Corporation |
| 4 | + * |
| 5 | + * SPDX-License-Identifier: MIT |
| 6 | + * |
| 7 | + */ |
| 8 | + |
| 9 | +#include "gtest/gtest.h" |
| 10 | + |
| 11 | +#include "utils/utils.hpp" |
| 12 | +#include "test_harness/test_harness.hpp" |
| 13 | +#include "logging/logging.hpp" |
| 14 | + |
| 15 | +namespace lzt = level_zero_tests; |
| 16 | + |
| 17 | +#include <level_zero/ze_api.h> |
| 18 | + |
| 19 | +namespace { |
| 20 | + |
| 21 | +struct SharedSystemRemoteDeviceTests : public ::testing::TestWithParam<bool> { |
| 22 | + void SetUp() override { |
| 23 | + auto driver = lzt::get_default_driver(); |
| 24 | + auto context = lzt::get_default_context(); |
| 25 | + auto devices = lzt::get_ze_devices(driver); |
| 26 | + |
| 27 | + devices.erase( |
| 28 | + std::remove_if( |
| 29 | + begin(devices), end(devices), |
| 30 | + [](auto dev) { return !lzt::supports_shared_system_alloc(dev); }), |
| 31 | + end(devices)); |
| 32 | + |
| 33 | + LOG_INFO << "num_devices: " << devices.size(); |
| 34 | + if (devices.size() < 2) { |
| 35 | + GTEST_SKIP() << "Test requires at least two devices that support shared " |
| 36 | + "system allocations."; |
| 37 | + } |
| 38 | + |
| 39 | + device = devices[0]; |
| 40 | + remote_device = devices[1]; |
| 41 | + |
| 42 | + ASSERT_TRUE(lzt::can_access_peer(device, remote_device)); |
| 43 | + |
| 44 | + bool is_immediate = GetParam(); |
| 45 | + cmd_bundle = lzt::create_command_bundle(device, is_immediate); |
| 46 | + } |
| 47 | + |
| 48 | + void TearDown() override { |
| 49 | + if (cmd_bundle.list != nullptr) { |
| 50 | + lzt::destroy_command_bundle(cmd_bundle); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + void run_append_memory_fill_test(bool use_madvise) { |
| 55 | + void *memory = lzt::aligned_malloc(size, 1); |
| 56 | + memset(memory, 0, size); |
| 57 | + |
| 58 | + if (use_madvise) { |
| 59 | + lzt::append_memory_advise(cmd_bundle.list, remote_device, memory, size, |
| 60 | + ZE_MEMORY_ADVICE_SET_PREFERRED_LOCATION); |
| 61 | + } |
| 62 | + uint8_t pattern = 0xAB; |
| 63 | + lzt::append_memory_fill(cmd_bundle.list, memory, &pattern, sizeof(pattern), |
| 64 | + size, nullptr); |
| 65 | + |
| 66 | + lzt::close_command_list(cmd_bundle.list); |
| 67 | + lzt::execute_and_sync_command_bundle(cmd_bundle, UINT64_MAX); |
| 68 | + |
| 69 | + uint8_t *memory_as_byte = static_cast<uint8_t *>(memory); |
| 70 | + for (size_t i = 0; i < size; i++) { |
| 71 | + ASSERT_EQ(memory_as_byte[i], pattern) << "Memory fill did not match."; |
| 72 | + } |
| 73 | + |
| 74 | + lzt::aligned_free(memory); |
| 75 | + } |
| 76 | + |
| 77 | + void run_append_memory_copy_test(bool use_madvise_for_src, |
| 78 | + bool use_madvise_for_dst) { |
| 79 | + void *src_memory = lzt::aligned_malloc(size, 1); |
| 80 | + void *dst_memory = lzt::aligned_malloc(size, 1); |
| 81 | + memset(src_memory, 0xAB, size); |
| 82 | + memset(dst_memory, 0, size); |
| 83 | + |
| 84 | + if (use_madvise_for_src) { |
| 85 | + lzt::append_memory_advise(cmd_bundle.list, remote_device, src_memory, |
| 86 | + size, ZE_MEMORY_ADVICE_SET_PREFERRED_LOCATION); |
| 87 | + } |
| 88 | + if (use_madvise_for_dst) { |
| 89 | + lzt::append_memory_advise(cmd_bundle.list, remote_device, dst_memory, |
| 90 | + size, ZE_MEMORY_ADVICE_SET_PREFERRED_LOCATION); |
| 91 | + } |
| 92 | + lzt::append_memory_copy(cmd_bundle.list, dst_memory, src_memory, size); |
| 93 | + |
| 94 | + lzt::close_command_list(cmd_bundle.list); |
| 95 | + lzt::execute_and_sync_command_bundle(cmd_bundle, UINT64_MAX); |
| 96 | + |
| 97 | + uint8_t *src_memory_as_byte = static_cast<uint8_t *>(src_memory); |
| 98 | + uint8_t *dst_memory_as_byte = static_cast<uint8_t *>(dst_memory); |
| 99 | + for (size_t i = 0; i < size; i++) { |
| 100 | + ASSERT_EQ(src_memory_as_byte[i], dst_memory_as_byte[i]) |
| 101 | + << "Memory copy did not match."; |
| 102 | + } |
| 103 | + |
| 104 | + lzt::aligned_free(src_memory); |
| 105 | + lzt::aligned_free(dst_memory); |
| 106 | + } |
| 107 | + |
| 108 | + void run_append_memory_copy_region_test(bool use_madvise_for_src, |
| 109 | + bool use_madvise_for_dst) { |
| 110 | + void *src_memory = lzt::aligned_malloc(size, 1); |
| 111 | + void *dst_memory = lzt::aligned_malloc(size, 1); |
| 112 | + memset(src_memory, 0xAB, size); |
| 113 | + memset(dst_memory, 0, size); |
| 114 | + |
| 115 | + if (use_madvise_for_src) { |
| 116 | + lzt::append_memory_advise(cmd_bundle.list, remote_device, src_memory, |
| 117 | + size, ZE_MEMORY_ADVICE_SET_PREFERRED_LOCATION); |
| 118 | + } |
| 119 | + if (use_madvise_for_dst) { |
| 120 | + lzt::append_memory_advise(cmd_bundle.list, remote_device, dst_memory, |
| 121 | + size, ZE_MEMORY_ADVICE_SET_PREFERRED_LOCATION); |
| 122 | + } |
| 123 | + |
| 124 | + constexpr size_t region_size = size / 2; |
| 125 | + ze_copy_region_t region; |
| 126 | + region.originX = 0; |
| 127 | + region.originY = 0; |
| 128 | + region.originZ = 0; |
| 129 | + region.width = region_size; |
| 130 | + region.height = 1; |
| 131 | + region.depth = 1; |
| 132 | + |
| 133 | + lzt::append_memory_copy_region(cmd_bundle.list, dst_memory, ®ion, 1, 1, |
| 134 | + src_memory, ®ion, 1, 1, nullptr); |
| 135 | + lzt::close_command_list(cmd_bundle.list); |
| 136 | + lzt::execute_and_sync_command_bundle(cmd_bundle, UINT64_MAX); |
| 137 | + |
| 138 | + uint8_t *src_memory_as_byte = static_cast<uint8_t *>(src_memory); |
| 139 | + uint8_t *dst_memory_as_byte = static_cast<uint8_t *>(dst_memory); |
| 140 | + for (size_t i = 0; i < region_size; i++) { |
| 141 | + ASSERT_EQ(src_memory_as_byte[i], dst_memory_as_byte[i]) |
| 142 | + << "Memory copy did not match."; |
| 143 | + } |
| 144 | + |
| 145 | + lzt::aligned_free(src_memory); |
| 146 | + lzt::aligned_free(dst_memory); |
| 147 | + } |
| 148 | + |
| 149 | + void run_append_memory_image_copy_test(bool use_madvise_for_src, |
| 150 | + bool use_madvise_for_dst) { |
| 151 | + auto png_img_src = lzt::ImagePNG32Bit("test_input.png"); |
| 152 | + auto image_width = png_img_src.width(); |
| 153 | + auto image_height = png_img_src.height(); |
| 154 | + auto image_size = image_width * image_height * sizeof(uint32_t); |
| 155 | + auto png_img_dest = lzt::ImagePNG32Bit(image_width, image_height); |
| 156 | + |
| 157 | + ze_image_desc_t img_desc = {}; |
| 158 | + img_desc.stype = ZE_STRUCTURE_TYPE_IMAGE_DESC; |
| 159 | + img_desc.flags = ZE_IMAGE_FLAG_KERNEL_WRITE; |
| 160 | + img_desc.type = ZE_IMAGE_TYPE_2D; |
| 161 | + img_desc.format = { |
| 162 | + ZE_IMAGE_FORMAT_LAYOUT_8_8_8_8, ZE_IMAGE_FORMAT_TYPE_UNORM, |
| 163 | + ZE_IMAGE_FORMAT_SWIZZLE_R, ZE_IMAGE_FORMAT_SWIZZLE_G, |
| 164 | + ZE_IMAGE_FORMAT_SWIZZLE_B, ZE_IMAGE_FORMAT_SWIZZLE_A}; |
| 165 | + img_desc.width = image_width; |
| 166 | + img_desc.height = image_height; |
| 167 | + img_desc.depth = 1; |
| 168 | + img_desc.arraylevels = 0; |
| 169 | + img_desc.miplevels = 0; |
| 170 | + |
| 171 | + auto ze_img = lzt::create_ze_image(device, img_desc); |
| 172 | + |
| 173 | + if (use_madvise_for_src) { |
| 174 | + lzt::append_memory_advise(cmd_bundle.list, remote_device, |
| 175 | + png_img_src.raw_data(), size, |
| 176 | + ZE_MEMORY_ADVICE_SET_PREFERRED_LOCATION); |
| 177 | + } |
| 178 | + if (use_madvise_for_dst) { |
| 179 | + lzt::append_memory_advise(cmd_bundle.list, remote_device, |
| 180 | + png_img_dest.raw_data(), size, |
| 181 | + ZE_MEMORY_ADVICE_SET_PREFERRED_LOCATION); |
| 182 | + } |
| 183 | + |
| 184 | + lzt::append_image_copy_from_mem(cmd_bundle.list, ze_img, |
| 185 | + png_img_src.raw_data(), nullptr); |
| 186 | + lzt::append_barrier(cmd_bundle.list, nullptr, 0, nullptr); |
| 187 | + lzt::append_image_copy_to_mem(cmd_bundle.list, png_img_dest.raw_data(), |
| 188 | + ze_img, nullptr); |
| 189 | + lzt::append_barrier(cmd_bundle.list, nullptr, 0, nullptr); |
| 190 | + lzt::close_command_list(cmd_bundle.list); |
| 191 | + lzt::execute_and_sync_command_bundle(cmd_bundle, UINT64_MAX); |
| 192 | + |
| 193 | + EXPECT_EQ(png_img_src, png_img_dest); |
| 194 | + } |
| 195 | + |
| 196 | + static constexpr size_t size = 1024; |
| 197 | + ze_device_handle_t device, remote_device; |
| 198 | + lzt::zeCommandBundle cmd_bundle; |
| 199 | +}; |
| 200 | + |
| 201 | +LZT_TEST_P( |
| 202 | + SharedSystemRemoteDeviceTests, |
| 203 | + GivenMultipleDevicesAndSharedSystemMemoryWhenAppendingMemoryFillThenIsSuccessAndValuesAreCorrect) { |
| 204 | + run_append_memory_fill_test(false); |
| 205 | +} |
| 206 | + |
| 207 | +LZT_TEST_P( |
| 208 | + SharedSystemRemoteDeviceTests, |
| 209 | + GivenMultipleDevicesAndSharedSystemMemoryAdvisedToRemoteDeviceWhenAppendingMemoryFillThenIsSuccessAndValuesAreCorrect) { |
| 210 | + run_append_memory_fill_test(true); |
| 211 | +} |
| 212 | + |
| 213 | +LZT_TEST_P( |
| 214 | + SharedSystemRemoteDeviceTests, |
| 215 | + GivenMultipleDevicesAndSrcSharedSystemMemoryAdvisedToRemoteDeviceWhenAppendingMemoryCopyThenIsSuccessAndValuesAreCorrect) { |
| 216 | + run_append_memory_copy_test(true, false); |
| 217 | +} |
| 218 | + |
| 219 | +LZT_TEST_P( |
| 220 | + SharedSystemRemoteDeviceTests, |
| 221 | + GivenMultipleDevicesAndDstSharedSystemMemoryAdvisedToRemoteDeviceWhenAppendingMemoryCopyThenIsSuccessAndValuesAreCorrect) { |
| 222 | + run_append_memory_copy_test(false, true); |
| 223 | +} |
| 224 | + |
| 225 | +LZT_TEST_P( |
| 226 | + SharedSystemRemoteDeviceTests, |
| 227 | + GivenMultipleDevicesAndSrcAndDstSharedSystemMemoryAdvisedToRemoteDeviceWhenAppendingMemoryCopyThenIsSuccessAndValuesAreCorrect) { |
| 228 | + run_append_memory_copy_test(true, true); |
| 229 | +} |
| 230 | + |
| 231 | +LZT_TEST_P( |
| 232 | + SharedSystemRemoteDeviceTests, |
| 233 | + GivenMultipleDevicesAndSrcSharedSystemMemoryAdvisedToRemoteDeviceWhenAppendingImageCopyToAndFromMemoryThenIsSuccessAndImageIsCorrect) { |
| 234 | + if (!(lzt::image_support(device))) { |
| 235 | + GTEST_SKIP() << "Test requires image support."; |
| 236 | + } |
| 237 | + run_append_memory_image_copy_test(true, false); |
| 238 | +} |
| 239 | + |
| 240 | +LZT_TEST_P( |
| 241 | + SharedSystemRemoteDeviceTests, |
| 242 | + GivenMultipleDevicesAndDstSharedSystemMemoryAdvisedToRemoteDeviceWhenAppendingImageCopyToAndFromMemoryThenIsSuccessAndImageIsCorrect) { |
| 243 | + if (!(lzt::image_support(device))) { |
| 244 | + GTEST_SKIP() << "Test requires image support."; |
| 245 | + } |
| 246 | + run_append_memory_image_copy_test(false, true); |
| 247 | +} |
| 248 | + |
| 249 | +LZT_TEST_P( |
| 250 | + SharedSystemRemoteDeviceTests, |
| 251 | + GivenMultipleDevicesAndSrcAndDstSharedSystemMemoryAdvisedToRemoteDeviceWhenAppendingImageCopyToAndFromMemoryThenIsSuccessAndImageIsCorrect) { |
| 252 | + if (!(lzt::image_support(device))) { |
| 253 | + GTEST_SKIP() << "Test requires image support."; |
| 254 | + } |
| 255 | + run_append_memory_image_copy_test(true, true); |
| 256 | +} |
| 257 | + |
| 258 | +INSTANTIATE_TEST_SUITE_P(SharedSystemRemoteDeviceTestParam, |
| 259 | + SharedSystemRemoteDeviceTests, ::testing::Bool()); |
| 260 | + |
| 261 | +} // namespace |
0 commit comments