|
| 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 "test_harness/test_harness.hpp" |
| 12 | + |
| 13 | +namespace lzt = level_zero_tests; |
| 14 | + |
| 15 | +class SharedSystemMemoryTests |
| 16 | + : public testing::TestWithParam< |
| 17 | + std::tuple<std::pair<bool, bool>, bool, bool, size_t>> { |
| 18 | +protected: |
| 19 | + void SetUp() override { |
| 20 | + device = lzt::zeDevice::get_instance()->get_device(); |
| 21 | + module = lzt::create_module(device, "memory_add.spv"); |
| 22 | + } |
| 23 | + |
| 24 | + void TearDown() override { lzt::destroy_module(module); } |
| 25 | + |
| 26 | + ze_device_handle_t device; |
| 27 | + ze_module_handle_t module; |
| 28 | +}; |
| 29 | + |
| 30 | +LZT_TEST_P( |
| 31 | + SharedSystemMemoryTests, |
| 32 | + GivenSharedSystemMemoryAllocationsAsKernelArgumentsWhenKernelExecutesThenValuesAreCorrect) { |
| 33 | + bool is_dst_shared_system = std::get<0>(GetParam()).first; |
| 34 | + bool is_src_shared_system = std::get<0>(GetParam()).second; |
| 35 | + bool use_atomic_kernel = std::get<1>(GetParam()); |
| 36 | + bool use_immediate_cmdlist = std::get<2>(GetParam()); |
| 37 | + size_t buffer_size = std::get<3>(GetParam()); |
| 38 | + |
| 39 | + constexpr size_t group_size = 32; |
| 40 | + ASSERT_EQ(buffer_size % (sizeof(int) * group_size), 0); |
| 41 | + |
| 42 | + if (is_dst_shared_system || is_src_shared_system) { |
| 43 | + SKIP_IF_SHARED_SYSTEM_ALLOC_UNSUPPORTED(); |
| 44 | + } |
| 45 | + |
| 46 | + constexpr int source_value = 1234; |
| 47 | + constexpr int add_value = 5678; |
| 48 | + |
| 49 | + void *result = lzt::allocate_shared_memory_with_allocator_selector( |
| 50 | + buffer_size, 1, 0, 0, device, is_dst_shared_system); |
| 51 | + void *source = lzt::allocate_shared_memory_with_allocator_selector( |
| 52 | + buffer_size, 1, 0, 0, device, is_src_shared_system); |
| 53 | + |
| 54 | + memset(result, 0, buffer_size); |
| 55 | + int *source_as_int = reinterpret_cast<int *>(source); |
| 56 | + for (size_t i = 0; i < buffer_size / sizeof(int); i++) { |
| 57 | + source_as_int[i] = source_value; |
| 58 | + } |
| 59 | + |
| 60 | + const char *funcion_name = |
| 61 | + use_atomic_kernel ? "memory_atomic_add" : "memory_add"; |
| 62 | + ze_kernel_handle_t function = lzt::create_function(module, funcion_name); |
| 63 | + lzt::set_group_size(function, group_size, 1, 1); |
| 64 | + |
| 65 | + lzt::set_argument_value(function, 0, sizeof(result), &result); |
| 66 | + lzt::set_argument_value(function, 1, sizeof(source), &source); |
| 67 | + lzt::set_argument_value(function, 2, sizeof(add_value), &add_value); |
| 68 | + |
| 69 | + lzt::zeCommandBundle cmd_bundle = |
| 70 | + lzt::create_command_bundle(use_immediate_cmdlist); |
| 71 | + |
| 72 | + const uint32_t group_count_x = buffer_size / (sizeof(int) * group_size); |
| 73 | + ze_group_count_t thread_group_dimensions = {group_count_x, 1, 1}; |
| 74 | + lzt::append_launch_function(cmd_bundle.list, function, |
| 75 | + &thread_group_dimensions, nullptr, 0, nullptr); |
| 76 | + |
| 77 | + lzt::close_command_list(cmd_bundle.list); |
| 78 | + lzt::execute_and_sync_command_bundle(cmd_bundle, UINT64_MAX); |
| 79 | + |
| 80 | + int *result_as_int = reinterpret_cast<int *>(result); |
| 81 | + for (size_t i = 0; i < buffer_size / sizeof(int); i++) { |
| 82 | + EXPECT_EQ(result_as_int[i], source_value + add_value) << "index = " << i; |
| 83 | + } |
| 84 | + |
| 85 | + lzt::destroy_command_bundle(cmd_bundle); |
| 86 | + lzt::destroy_function(function); |
| 87 | + |
| 88 | + lzt::free_memory_with_allocator_selector(source, is_dst_shared_system); |
| 89 | + lzt::free_memory_with_allocator_selector(result, is_src_shared_system); |
| 90 | +} |
| 91 | + |
| 92 | +struct SharedSystemMemoryTestsNameSuffix { |
| 93 | + template <class ParamType> |
| 94 | + std::string operator()(const testing::TestParamInfo<ParamType> &info) const { |
| 95 | + std::stringstream ss; |
| 96 | + bool is_dst_shared_system = std::get<0>(info.param).first; |
| 97 | + bool is_src_shared_system = std::get<0>(info.param).second; |
| 98 | + bool use_atomic_kernel = std::get<1>(info.param); |
| 99 | + bool use_immediate_cmdlist = std::get<2>(info.param); |
| 100 | + size_t buffer_size = std::get<3>(info.param); |
| 101 | + |
| 102 | + const char *buffer_size_str = [](size_t size) -> const char * { |
| 103 | + switch (size) { |
| 104 | + case 0x80u: |
| 105 | + return "_128B"; |
| 106 | + case 0x1000u: |
| 107 | + return "_4KB"; |
| 108 | + case 0x1800u: |
| 109 | + return "_6KB"; |
| 110 | + case 0x100000u: |
| 111 | + return "_1MB"; |
| 112 | + case 0x100800u: |
| 113 | + return "_1MB2KB"; |
| 114 | + case 0x4000'0000u: |
| 115 | + return "_1GB"; |
| 116 | + case 0x4000'0800u: |
| 117 | + return "_1GB2KB"; |
| 118 | + } |
| 119 | + return ""; |
| 120 | + }(buffer_size); |
| 121 | + |
| 122 | + ss << (is_src_shared_system ? "SVM" : "USM"); |
| 123 | + ss << "to"; |
| 124 | + ss << (is_dst_shared_system ? "SVM" : "USM"); |
| 125 | + ss << (use_atomic_kernel ? "_Atomic" : "_NonAtomic"); |
| 126 | + ss << (use_immediate_cmdlist ? "_Immediate" : "_Regular"); |
| 127 | + ss << buffer_size_str; |
| 128 | + |
| 129 | + return ss.str(); |
| 130 | + } |
| 131 | +}; |
| 132 | + |
| 133 | +INSTANTIATE_TEST_SUITE_P( |
| 134 | + ParamSVMAllocationTests, SharedSystemMemoryTests, |
| 135 | + testing::Combine(testing::Values(std::make_pair(true, false), |
| 136 | + std::make_pair(false, true), |
| 137 | + std::make_pair(true, true)), |
| 138 | + testing::Bool(), testing::Bool(), |
| 139 | + testing::Values(0x80u, 0x1000u, 0x1800u, 0x100000u, |
| 140 | + 0x100800u, 0x4000'0000u, 0x4000'0800u)), |
| 141 | + SharedSystemMemoryTestsNameSuffix()); |
0 commit comments