Skip to content

Commit f6f2f85

Browse files
committed
fix: add multi-mapping virtual host memory test
Signed-off-by: Wenbin Lu <wenbin.lu@intel.com>
1 parent 38ae470 commit f6f2f85

File tree

2 files changed

+146
-8
lines changed

2 files changed

+146
-8
lines changed

conformance_tests/core/test_memory/src/test_virtual_memory.cpp

Lines changed: 145 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
/*
22
*
3-
* Copyright (C) 2022-2024 Intel Corporation
3+
* Copyright (C) 2022-2025 Intel Corporation
44
*
55
* SPDX-License-Identifier: MIT
66
*
77
*/
88

9-
#include <chrono>
10-
#include <thread>
11-
129
#include "gtest/gtest.h"
1310

1411
#include "utils/utils.hpp"
1512
#include "test_harness/test_harness.hpp"
16-
#include "logging/logging.hpp"
1713

1814
#ifdef __linux__
1915
#include <unistd.h>
@@ -36,7 +32,7 @@ class zeVirtualMemoryTests : public ::testing::Test {
3632
public:
3733
ze_context_handle_t context;
3834
ze_device_handle_t device;
39-
size_t pageSize = 0;
35+
size_t pageSize = 1ul << 21;
4036
size_t allocationSize = (1024 * 1024);
4137
void *reservedVirtualMemory = nullptr;
4238
ze_physical_mem_handle_t reservedPhysicalDeviceMemory = nullptr;
@@ -148,7 +144,10 @@ TEST_F(
148144
zeVirtualMemoryTests,
149145
GivenPageAlignedSizeThenVirtualAndPhysicalHostMemoryReservedSuccessfully) {
150146
#ifdef __linux__
151-
pageSize = sysconf(_SC_PAGE_SIZE);
147+
const long os_page_size = sysconf(_SC_PAGE_SIZE);
148+
if (os_page_size > 0) {
149+
pageSize = static_cast<size_t>(os_page_size);
150+
}
152151
allocationSize = lzt::create_page_aligned_size(allocationSize, pageSize);
153152
lzt::physical_host_memory_allocation(context, allocationSize,
154153
&reservedPhysicalHostMemory);
@@ -201,7 +200,10 @@ void RunGivenMappedReadWriteMemoryThenFillAndCopyWithMappedVirtualMemory(
201200

202201
if (is_host_memory) {
203202
#ifdef __linux__
204-
test.pageSize = sysconf(_SC_PAGE_SIZE);
203+
const long os_page_size = sysconf(_SC_PAGE_SIZE);
204+
if (os_page_size > 0) {
205+
test.pageSize = static_cast<size_t>(os_page_size);
206+
}
205207
#endif
206208
} else {
207209
lzt::query_page_size(test.context, test.device, test.allocationSize,
@@ -693,4 +695,139 @@ TEST_F(
693695
MemoryReservationTestType::MEMORY_RESERVATION_MULTI_ROOT_DEVICES, true);
694696
}
695697

698+
class zeVirtualMemoryMultiMappingTests
699+
: public ::testing::Test,
700+
public ::testing::WithParamInterface<std::tuple<ze_memory_type_t, bool>> {
701+
protected:
702+
void SetUp() override {
703+
device = lzt::get_default_device(lzt::get_default_driver());
704+
context = lzt::get_default_context();
705+
}
706+
void TearDown() override {}
707+
708+
public:
709+
ze_context_handle_t context = nullptr;
710+
ze_device_handle_t device = nullptr;
711+
};
712+
713+
TEST_P(
714+
zeVirtualMemoryMultiMappingTests,
715+
givenSinglePhysicalHostMemoryMappedToMultipleVirtualMemoryRangeThenReadAndWriteResultsAreCorrect) {
716+
#ifdef __linux__
717+
const ze_memory_type_t aux_buffer_type = std::get<0>(GetParam());
718+
const bool is_immediate = std::get<1>(GetParam());
719+
720+
constexpr size_t alloc_size = 1ul << 26;
721+
722+
void *aux_buffer = nullptr;
723+
switch (aux_buffer_type) {
724+
case ZE_MEMORY_TYPE_HOST:
725+
aux_buffer = lzt::allocate_host_memory(alloc_size);
726+
break;
727+
case ZE_MEMORY_TYPE_DEVICE:
728+
aux_buffer = lzt::allocate_device_memory(alloc_size);
729+
break;
730+
default:
731+
aux_buffer = lzt::allocate_shared_memory(alloc_size);
732+
break;
733+
}
734+
EXPECT_NE(nullptr, aux_buffer);
735+
736+
ze_physical_mem_handle_t physical_host_memory = nullptr;
737+
lzt::physical_host_memory_allocation(context, alloc_size,
738+
&physical_host_memory);
739+
EXPECT_NE(nullptr, physical_host_memory);
740+
741+
void *virtual_memory_0 = nullptr;
742+
void *virtual_memory_1 = nullptr;
743+
void *virtual_memory_2 = nullptr;
744+
lzt::virtual_memory_reservation(context, nullptr, alloc_size,
745+
&virtual_memory_0);
746+
lzt::virtual_memory_reservation(context, nullptr, alloc_size,
747+
&virtual_memory_1);
748+
lzt::virtual_memory_reservation(context, nullptr, alloc_size,
749+
&virtual_memory_2);
750+
EXPECT_NE(nullptr, virtual_memory_0);
751+
EXPECT_NE(nullptr, virtual_memory_1);
752+
EXPECT_NE(nullptr, virtual_memory_2);
753+
EXPECT_NE(virtual_memory_0, virtual_memory_1);
754+
EXPECT_NE(virtual_memory_0, virtual_memory_2);
755+
EXPECT_NE(virtual_memory_1, virtual_memory_2);
756+
757+
lzt::virtual_memory_map(context, virtual_memory_0, alloc_size,
758+
physical_host_memory, 0,
759+
ZE_MEMORY_ACCESS_ATTRIBUTE_READWRITE);
760+
lzt::virtual_memory_map(context, virtual_memory_1, alloc_size,
761+
physical_host_memory, 0,
762+
ZE_MEMORY_ACCESS_ATTRIBUTE_READWRITE);
763+
764+
std::fill_n(static_cast<uint8_t *>(virtual_memory_0), alloc_size, 0);
765+
std::fill_n(static_cast<uint8_t *>(virtual_memory_1), alloc_size, 0);
766+
767+
// Simple read-write test with cross check
768+
static_cast<int64_t *>(virtual_memory_0)[(alloc_size / sizeof(int64_t)) / 2] =
769+
0xdeadbeef;
770+
EXPECT_EQ(0xdeadbeef,
771+
static_cast<int64_t *>(
772+
virtual_memory_1)[(alloc_size / sizeof(int64_t)) / 2]);
773+
774+
static_cast<int64_t *>(virtual_memory_1)[(alloc_size / sizeof(int64_t)) / 3] =
775+
0xcafecafe;
776+
EXPECT_EQ(0xcafecafe,
777+
static_cast<int64_t *>(
778+
virtual_memory_0)[(alloc_size / sizeof(int64_t)) / 3]);
779+
780+
// GPU copy test with cross check
781+
int8_t seven = 7;
782+
auto bundle = lzt::create_command_bundle(device, is_immediate);
783+
lzt::append_memory_fill(bundle.list, aux_buffer, &seven, sizeof(seven),
784+
alloc_size, nullptr);
785+
lzt::append_barrier(bundle.list, nullptr, 0, nullptr);
786+
lzt::append_memory_copy(bundle.list, virtual_memory_0, aux_buffer, alloc_size,
787+
nullptr, 0, nullptr);
788+
lzt::close_command_list(bundle.list);
789+
lzt::execute_and_sync_command_bundle(bundle, UINT64_MAX);
790+
lzt::destroy_command_bundle(bundle);
791+
792+
for (size_t i = 0; i < alloc_size; i++) {
793+
if (static_cast<int8_t *>(virtual_memory_1)[i] != seven) {
794+
FAIL() << "Verification failed";
795+
break;
796+
}
797+
}
798+
799+
lzt::virtual_memory_unmap(context, virtual_memory_0, alloc_size);
800+
lzt::virtual_memory_free(context, virtual_memory_0, alloc_size);
801+
802+
lzt::virtual_memory_unmap(context, virtual_memory_1, alloc_size);
803+
lzt::virtual_memory_free(context, virtual_memory_1, alloc_size);
804+
805+
// Make sure data in physical host memory is persistent
806+
lzt::virtual_memory_map(context, virtual_memory_2, alloc_size,
807+
physical_host_memory, 0,
808+
ZE_MEMORY_ACCESS_ATTRIBUTE_READONLY);
809+
for (size_t i = 0; i < alloc_size; i++) {
810+
if (static_cast<int8_t *>(virtual_memory_2)[i] != seven) {
811+
FAIL() << "Verification failed";
812+
break;
813+
}
814+
}
815+
lzt::virtual_memory_unmap(context, virtual_memory_2, alloc_size);
816+
lzt::virtual_memory_free(context, virtual_memory_2, alloc_size);
817+
818+
lzt::physical_memory_destroy(context, physical_host_memory);
819+
820+
lzt::free_memory(context, aux_buffer);
821+
#else
822+
GTEST_SKIP() << "Physical host memory is unsupported on Windows";
823+
#endif
824+
}
825+
826+
INSTANTIATE_TEST_SUITE_P(
827+
VirtualHostMemoryMultiMappingParams, zeVirtualMemoryMultiMappingTests,
828+
::testing::Combine(::testing::Values(ZE_MEMORY_TYPE_HOST,
829+
ZE_MEMORY_TYPE_DEVICE,
830+
ZE_MEMORY_TYPE_SHARED),
831+
::testing::Bool()));
832+
696833
} // namespace

scripts/level_zero_report_utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ def assign_test_feature_tag(test_feature: str, test_name: str, test_section: str
161161
(test_name.find("ParamAppendMemCopy")!= -1) or \
162162
(test_name.find("ParamSharedSystemMemCopy")!= -1) or \
163163
(test_name.find("zeVirtualMemoryTests")!= -1) or \
164+
(test_name.find("zeVirtualMemoryMultiMappingTests")!= -1) or \
164165
(test_name.find("Cooperative")!= -1) or \
165166
(test_name.find("zeMemFreeExtTests")!= -1) or \
166167
(test_name.find("zeMemFreeExtMultipleTests")!= -1) or \

0 commit comments

Comments
 (0)