Skip to content

Commit

Permalink
Merge pull request #663 from veselypeta/petr/590/remaining-virtual-mem
Browse files Browse the repository at this point in the history
[UR] Test remaining Virtual Memory entry-points
  • Loading branch information
veselypeta authored Jul 3, 2023
2 parents 25abfe6 + c9faea7 commit 2eae79a
Show file tree
Hide file tree
Showing 9 changed files with 350 additions and 16 deletions.
123 changes: 120 additions & 3 deletions test/conformance/testing/include/uur/fixtures.h
Original file line number Diff line number Diff line change
Expand Up @@ -636,12 +636,33 @@ template <class T> struct urUSMPoolTestWithParam : urContextTestWithParam<T> {
ur_usm_pool_handle_t pool;
};

struct urPhysicalMemTest : urContextTest {
struct urVirtualMemGranularityTest : urContextTest {
void SetUp() override {
UUR_RETURN_ON_FATAL_FAILURE(urContextTest::SetUp());
ASSERT_SUCCESS(urVirtualMemGranularityGetInfo(
context, device, UR_VIRTUAL_MEM_GRANULARITY_INFO_MINIMUM,
sizeof(granularity), &granularity, nullptr));
}
size_t granularity;
};

template <class T>
struct urVirtualMemGranularityTestWithParam : urContextTestWithParam<T> {
void SetUp() override {
UUR_RETURN_ON_FATAL_FAILURE(urContextTestWithParam<T>::SetUp());
ASSERT_SUCCESS(urVirtualMemGranularityGetInfo(
this->context, this->device,
UR_VIRTUAL_MEM_GRANULARITY_INFO_MINIMUM, sizeof(granularity),
&granularity, nullptr));
ASSERT_NE(granularity, 0);
}

size_t granularity = 0;
};

struct urPhysicalMemTest : urVirtualMemGranularityTest {
void SetUp() override {
UUR_RETURN_ON_FATAL_FAILURE(urVirtualMemGranularityTest::SetUp());
size = granularity * 256;
ur_physical_mem_properties_t props{
UR_STRUCTURE_TYPE_PHYSICAL_MEM_PROPERTIES,
Expand All @@ -657,14 +678,110 @@ struct urPhysicalMemTest : urContextTest {
if (physical_mem) {
EXPECT_SUCCESS(urPhysicalMemRelease(physical_mem));
}
UUR_RETURN_ON_FATAL_FAILURE(urContextTest::TearDown());
UUR_RETURN_ON_FATAL_FAILURE(urVirtualMemGranularityTest::TearDown());
}

size_t size = 0;
ur_physical_mem_handle_t physical_mem = nullptr;
};

template <class T>
struct urPhysicalMemTestWithParam : urVirtualMemGranularityTestWithParam<T> {
void SetUp() override {
UUR_RETURN_ON_FATAL_FAILURE(
urVirtualMemGranularityTestWithParam<T>::SetUp());
size = this->granularity * 256;
ur_physical_mem_properties_t props{
UR_STRUCTURE_TYPE_PHYSICAL_MEM_PROPERTIES,
nullptr,
0 /*flags*/,
};
ASSERT_SUCCESS(urPhysicalMemCreate(this->context, this->device, size,
&props, &physical_mem));
ASSERT_NE(physical_mem, nullptr);
}

void TearDown() override {
if (physical_mem) {
EXPECT_SUCCESS(urPhysicalMemRelease(physical_mem));
}
UUR_RETURN_ON_FATAL_FAILURE(
urVirtualMemGranularityTestWithParam<T>::TearDown());
}

size_t granularity = 0;
size_t size = 0;
ur_physical_mem_handle_t physical_mem = nullptr;
};

struct urVirtualMemTest : urPhysicalMemTest {
void SetUp() override {
UUR_RETURN_ON_FATAL_FAILURE(urPhysicalMemTest::SetUp());
ASSERT_SUCCESS(
urVirtualMemReserve(context, nullptr, size, &virtual_ptr));
ASSERT_NE(virtual_ptr, nullptr);
}

void TearDown() override {
if (virtual_ptr) {
EXPECT_SUCCESS(urVirtualMemFree(context, virtual_ptr, size));
}
UUR_RETURN_ON_FATAL_FAILURE(urPhysicalMemTest::TearDown());
}

void *virtual_ptr = nullptr;
};

template <class T>
struct urVirtualMemTestWithParam : urPhysicalMemTestWithParam<T> {
void SetUp() override {
UUR_RETURN_ON_FATAL_FAILURE(urPhysicalMemTestWithParam<T>::SetUp());
ASSERT_SUCCESS(urVirtualMemReserve(this->context, nullptr, this->size,
&virtual_ptr));
}

void TearDown() override {
if (virtual_ptr) {
EXPECT_SUCCESS(
urVirtualMemFree(this->context, virtual_ptr, this->size));
}
UUR_RETURN_ON_FATAL_FAILURE(urPhysicalMemTestWithParam<T>::TearDown());
}

void *virtual_ptr = nullptr;
};

struct urVirtualMemMappedTest : urVirtualMemTest {

void SetUp() override {
UUR_RETURN_ON_FATAL_FAILURE(urVirtualMemTest::SetUp());
ASSERT_SUCCESS(urVirtualMemMap(context, virtual_ptr, size, physical_mem,
0,
UR_VIRTUAL_MEM_ACCESS_FLAG_READ_WRITE));
}

void TearDown() override {
EXPECT_SUCCESS(urVirtualMemUnmap(context, virtual_ptr, size));
UUR_RETURN_ON_FATAL_FAILURE(urVirtualMemTest::TearDown());
}
};

template <class T>
struct urVirtualMemMappedTestWithParam : urVirtualMemTestWithParam<T> {

void SetUp() override {
UUR_RETURN_ON_FATAL_FAILURE(urVirtualMemTestWithParam<T>::SetUp());
ASSERT_SUCCESS(urVirtualMemMap(this->context, this->virtual_ptr,
this->size, this->physical_mem, 0,
UR_VIRTUAL_MEM_ACCESS_FLAG_READ_WRITE));
}

void TearDown() override {
EXPECT_SUCCESS(
urVirtualMemUnmap(this->context, this->virtual_ptr, this->size));
UUR_RETURN_ON_FATAL_FAILURE(urVirtualMemTestWithParam<T>::TearDown());
}
};

template <class T>
struct urUSMDeviceAllocTestWithParam : urQueueTestWithParam<T> {

Expand Down
4 changes: 4 additions & 0 deletions test/conformance/testing/include/uur/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

namespace uur {

inline size_t RoundUpToNearestFactor(size_t num, size_t factor) {
return ((num + factor - 1) / factor) * factor;
}

/// @brief Make a string a valid identifier for gtest.
/// @param str The string to sanitize.
inline std::string GTestSanitizeString(const std::string &str) {
Expand Down
19 changes: 6 additions & 13 deletions test/conformance/virtual_memory/urPhysicalMemCreate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,21 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include <uur/fixtures.h>

using urPhysicalMemCreateTestParams = size_t;
struct urPhysicalMemCreateTest
: uur::urContextTestWithParam<urPhysicalMemCreateTestParams> {
: uur::urVirtualMemGranularityTestWithParam<size_t> {

void SetUp() override {
UUR_RETURN_ON_FATAL_FAILURE(uur::urContextTestWithParam<
urPhysicalMemCreateTestParams>::SetUp());
ASSERT_SUCCESS(urVirtualMemGranularityGetInfo(
context, device, UR_VIRTUAL_MEM_GRANULARITY_INFO_MINIMUM,
sizeof(granularity), &granularity, nullptr));
ASSERT_NE(granularity, 0);
UUR_RETURN_ON_FATAL_FAILURE(
uur::urVirtualMemGranularityTestWithParam<size_t>::SetUp());
size = getParam() * granularity;
}

size_t granularity;
size_t size;
};

UUR_TEST_SUITE_P(
urPhysicalMemCreateTest,
::testing::Values(1, 2, 3, 7, 12, 44, 1024, 4000, 12345),
uur::deviceTestWithParamPrinter<urPhysicalMemCreateTestParams>);
UUR_TEST_SUITE_P(urPhysicalMemCreateTest,
::testing::Values(1, 2, 3, 7, 12, 44, 1024, 4000, 12345),
uur::deviceTestWithParamPrinter<size_t>);

TEST_P(urPhysicalMemCreateTest, Success) {
ur_physical_mem_handle_t physical_mem = nullptr;
Expand Down
19 changes: 19 additions & 0 deletions test/conformance/virtual_memory/urVirtualMemFree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,22 @@
// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions.
// See LICENSE.TXT
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include <uur/fixtures.h>

using urVirtualMemFreeTest = uur::urVirtualMemTest;
UUR_INSTANTIATE_DEVICE_TEST_SUITE_P(urVirtualMemFreeTest);

TEST_P(urVirtualMemFreeTest, Success) {
ASSERT_SUCCESS(urVirtualMemFree(context, virtual_ptr, size));
virtual_ptr = nullptr; // set to nullptr to prevent double-free
}

TEST_P(urVirtualMemFreeTest, InvalidNullHandleContext) {
ASSERT_EQ_RESULT(urVirtualMemFree(nullptr, virtual_ptr, size),
UR_RESULT_ERROR_INVALID_NULL_HANDLE);
}

TEST_P(urVirtualMemFreeTest, InvalidNullPointerStart) {
ASSERT_EQ_RESULT(urVirtualMemFree(context, nullptr, size),
UR_RESULT_ERROR_INVALID_NULL_HANDLE);
}
59 changes: 59 additions & 0 deletions test/conformance/virtual_memory/urVirtualMemGetInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,62 @@
// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions.
// See LICENSE.TXT
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include <uur/fixtures.h>

using urVirtualMemGetInfoTestWithParam =
uur::urVirtualMemMappedTestWithParam<ur_virtual_mem_info_t>;
UUR_TEST_SUITE_P(urVirtualMemGetInfoTestWithParam,
::testing::Values(UR_VIRTUAL_MEM_INFO_ACCESS_MODE),
uur::deviceTestWithParamPrinter<ur_virtual_mem_info_t>);

TEST_P(urVirtualMemGetInfoTestWithParam, Success) {
size_t info_size = 0;
ur_virtual_mem_info_t info = getParam();
ASSERT_SUCCESS(urVirtualMemGetInfo(context, virtual_ptr, size, info, 0,
nullptr, &info_size));
ASSERT_NE(info_size, 0);

std::vector<uint8_t> data(info_size);
ASSERT_SUCCESS(urVirtualMemGetInfo(context, virtual_ptr, size, info,
data.size(), data.data(), nullptr));

switch (info) {
case UR_VIRTUAL_MEM_INFO_ACCESS_MODE: {
ASSERT_EQ(sizeof(ur_virtual_mem_access_flags_t), data.size());
ur_virtual_mem_access_flags_t flags =
*reinterpret_cast<ur_virtual_mem_access_flags_t *>(data.data());
ASSERT_TRUE(flags & UR_VIRTUAL_MEM_ACCESS_FLAG_READ_WRITE);
} break;

default:
FAIL() << "Unhandled ur_virtual_mem_info_t enumeration: " << info;
break;
}
}

using urVirtualMemGetInfoTest = uur::urVirtualMemMappedTest;
UUR_INSTANTIATE_DEVICE_TEST_SUITE_P(urVirtualMemGetInfoTest);

TEST_P(urVirtualMemGetInfoTest, InvalidNullHandleContext) {
ur_virtual_mem_access_flags_t flags = 0;
ASSERT_EQ_RESULT(urVirtualMemGetInfo(nullptr, virtual_ptr, size,
UR_VIRTUAL_MEM_INFO_ACCESS_MODE,
sizeof(flags), &flags, nullptr),
UR_RESULT_ERROR_INVALID_NULL_HANDLE);
}

TEST_P(urVirtualMemGetInfoTest, InvalidNullPointerStart) {
ur_virtual_mem_access_flags_t flags = 0;
ASSERT_EQ_RESULT(urVirtualMemGetInfo(context, nullptr, size,
UR_VIRTUAL_MEM_INFO_ACCESS_MODE,
sizeof(flags), &flags, nullptr),
UR_RESULT_ERROR_INVALID_NULL_POINTER);
}

TEST_P(urVirtualMemGetInfoTest, InvalidEnumerationInfo) {
size_t info_size = 0;
ASSERT_EQ_RESULT(urVirtualMemGetInfo(context, virtual_ptr, size,
UR_VIRTUAL_MEM_INFO_FORCE_UINT32, 0,
nullptr, &info_size),
UR_RESULT_ERROR_INVALID_ENUMERATION);
}
35 changes: 35 additions & 0 deletions test/conformance/virtual_memory/urVirtualMemMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,38 @@
// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions.
// See LICENSE.TXT
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include <uur/fixtures.h>

using urVirtualMemMapTest = uur::urVirtualMemTest;
UUR_INSTANTIATE_DEVICE_TEST_SUITE_P(urVirtualMemMapTest);

TEST_P(urVirtualMemMapTest, Success) {
ASSERT_SUCCESS(urVirtualMemMap(context, virtual_ptr, size, physical_mem, 0,
UR_VIRTUAL_MEM_ACCESS_FLAG_READ_WRITE));
EXPECT_SUCCESS(urVirtualMemUnmap(context, virtual_ptr, size));
}

TEST_P(urVirtualMemMapTest, InvalidNullHandleContext) {
ASSERT_EQ_RESULT(urVirtualMemMap(nullptr, virtual_ptr, size, physical_mem,
0, UR_VIRTUAL_MEM_ACCESS_FLAG_READ_WRITE),
UR_RESULT_ERROR_INVALID_NULL_HANDLE);
}

TEST_P(urVirtualMemMapTest, InvalidNullHandlePhysicalMem) {
ASSERT_EQ_RESULT(urVirtualMemMap(context, virtual_ptr, size, nullptr, 0,
UR_VIRTUAL_MEM_ACCESS_FLAG_READ_WRITE),
UR_RESULT_ERROR_INVALID_NULL_HANDLE);
}

TEST_P(urVirtualMemMapTest, InvalidNullPointerStart) {
ASSERT_EQ_RESULT(urVirtualMemMap(context, nullptr, size, physical_mem, 0,
UR_VIRTUAL_MEM_ACCESS_FLAG_READ_WRITE),
UR_RESULT_ERROR_INVALID_NULL_POINTER);
}

TEST_P(urVirtualMemMapTest, InvalidEnumerationFlags) {
ASSERT_EQ_RESULT(urVirtualMemMap(context, virtual_ptr, size, physical_mem,
0,
UR_VIRTUAL_MEM_ACCESS_FLAG_FORCE_UINT32),
UR_RESULT_ERROR_INVALID_ENUMERATION);
}
58 changes: 58 additions & 0 deletions test/conformance/virtual_memory/urVirtualMemReserve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,61 @@
// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions.
// See LICENSE.TXT
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include <uur/fixtures.h>

using urVirtualMemReserveTestWithParam =
uur::urVirtualMemGranularityTestWithParam<size_t>;
UUR_TEST_SUITE_P(urVirtualMemReserveTestWithParam,
::testing::Values(2, 4, 8, 16, 32, 64, 128, 256, 512, 1024,
2048, 5000, 100000),
uur::deviceTestWithParamPrinter<size_t>);

TEST_P(urVirtualMemReserveTestWithParam, SuccessNoStartPointer) {
// round up to nearest granularity
size_t virtual_mem_size =
uur::RoundUpToNearestFactor(getParam(), granularity);
void *virtual_mem_start = nullptr;
ASSERT_SUCCESS(urVirtualMemReserve(context, nullptr, virtual_mem_size,
&virtual_mem_start));
ASSERT_NE(virtual_mem_start, nullptr);

EXPECT_SUCCESS(
urVirtualMemFree(context, virtual_mem_start, virtual_mem_size));
}

TEST_P(urVirtualMemReserveTestWithParam, SuccessWithStartPointer) {
// roundup to nearest granularity
size_t page_size = uur::RoundUpToNearestFactor(getParam(), granularity);
void *origin_ptr = nullptr;
ASSERT_SUCCESS(
urVirtualMemReserve(context, nullptr, page_size, &origin_ptr));
ASSERT_NE(origin_ptr, nullptr);

// try to reserve at the end of origin_ptr
void *virtual_mem_ptr = nullptr;
void *pStart = (uint8_t *)origin_ptr + page_size;
ASSERT_SUCCESS(
urVirtualMemReserve(context, pStart, page_size, &virtual_mem_ptr));
ASSERT_NE(virtual_mem_ptr, nullptr);

// both pointers have to be freed
EXPECT_SUCCESS(urVirtualMemFree(context, origin_ptr, page_size));
EXPECT_SUCCESS(urVirtualMemFree(context, virtual_mem_ptr, page_size));
}

using urVirtualMemReserveTest = uur::urVirtualMemGranularityTest;
UUR_INSTANTIATE_DEVICE_TEST_SUITE_P(urVirtualMemReserveTest);

TEST_P(urVirtualMemReserveTest, InvalidNullHandleContext) {
size_t page_size = uur::RoundUpToNearestFactor(1024, granularity);
void *virtual_ptr = nullptr;
ASSERT_EQ_RESULT(
urVirtualMemReserve(nullptr, nullptr, page_size, &virtual_ptr),
UR_RESULT_ERROR_INVALID_NULL_HANDLE);
}

TEST_P(urVirtualMemReserveTest, InvalidNullPointer) {
size_t page_size = uur::RoundUpToNearestFactor(1024, granularity);
ASSERT_EQ_RESULT(urVirtualMemReserve(context, nullptr, page_size, nullptr),
UR_RESULT_ERROR_INVALID_NULL_POINTER);
}
Loading

0 comments on commit 2eae79a

Please sign in to comment.