Skip to content

Commit

Permalink
Improvements to align CTS and Spec for USM:
Browse files Browse the repository at this point in the history
- Additional error case checking
- More validation for urUSMPoolRelease
- Refactored urUSMDevice/Shared/HostAlloc test structs into a shared parent struct
- Added ur_usm_advice_flags_t param to parameterised Alloc tests
  • Loading branch information
martygrant committed Feb 3, 2025
1 parent af4f331 commit e4b71e4
Show file tree
Hide file tree
Showing 5 changed files with 250 additions and 195 deletions.
71 changes: 60 additions & 11 deletions test/conformance/usm/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,40 @@

namespace uur {

using USMDeviceAllocParams = std::tuple<uur::BoolTestParam, uint32_t, size_t>;
using USMDeviceAllocParams =
std::tuple<uur::BoolTestParam, uint32_t, size_t, ur_usm_advice_flags_t>;

struct urUSMAllocTest : uur::urQueueTestWithParam<uur::USMDeviceAllocParams> {
void SetUp() override {
UUR_RETURN_ON_FATAL_FAILURE(
uur::urQueueTestWithParam<uur::USMDeviceAllocParams>::SetUp());
if (usePool) {
ur_bool_t poolSupport = false;
ASSERT_SUCCESS(uur::GetDeviceUSMPoolSupport(device, poolSupport));
if (!poolSupport) {
GTEST_SKIP() << "USM pools are not supported.";
}
ur_usm_pool_desc_t pool_desc = {};
ASSERT_SUCCESS(urUSMPoolCreate(context, &pool_desc, &pool));
}
}

void TearDown() override {
if (pool) {
ASSERT_SUCCESS(urUSMPoolRelease(pool));
}
UUR_RETURN_ON_FATAL_FAILURE(
uur::urQueueTestWithParam<uur::USMDeviceAllocParams>::TearDown());
}

ur_usm_pool_handle_t pool = nullptr;
const bool usePool = std::get<0>(getParam()).value;
ur_device_usm_access_capability_flags_t USMSupport = 0;
const uint32_t alignment = std::get<1>(getParam());
size_t allocation_size = std::get<2>(getParam());
const ur_usm_advice_flags_t advice_flags = std::get<3>(getParam());
void *ptr = nullptr;
};

template <typename T>
inline std::string printUSMAllocTestString(
Expand All @@ -24,20 +57,36 @@ inline std::string printUSMAllocTestString(
const auto &BoolParam = std::get<0>(usmDeviceAllocParams);

std::stringstream ss;
ss << BoolParam.name << (BoolParam.value ? "Enabled" : "Disabled");

const auto alignment = std::get<1>(usmDeviceAllocParams);
const auto size = std::get<2>(usmDeviceAllocParams);
if (alignment && size > 0) {
ss << "_";
ss << std::get<1>(usmDeviceAllocParams);
ss << "_";
ss << std::get<2>(usmDeviceAllocParams);
}
ss << BoolParam.name << (BoolParam.value ? "Enabled" : "Disabled"); // UsePool
ss << "_";
ss << std::get<1>(usmDeviceAllocParams); // alignment
ss << "_";
ss << std::get<2>(usmDeviceAllocParams); // size
ss << "_";
ss << std::get<3>(usmDeviceAllocParams); // ur_usm_advice_flags_t

return platform_device_name + "__" + ss.str();
}

static std::vector<ur_usm_advice_flags_t> usm_alloc_test_parameters{
UR_USM_ADVICE_FLAG_DEFAULT,
UR_USM_ADVICE_FLAG_SET_READ_MOSTLY,
UR_USM_ADVICE_FLAG_CLEAR_READ_MOSTLY,
UR_USM_ADVICE_FLAG_SET_PREFERRED_LOCATION,
UR_USM_ADVICE_FLAG_CLEAR_PREFERRED_LOCATION,
UR_USM_ADVICE_FLAG_SET_NON_ATOMIC_MOSTLY,
UR_USM_ADVICE_FLAG_CLEAR_NON_ATOMIC_MOSTLY,
UR_USM_ADVICE_FLAG_BIAS_CACHED,
UR_USM_ADVICE_FLAG_BIAS_UNCACHED,
UR_USM_ADVICE_FLAG_SET_ACCESSED_BY_DEVICE,
UR_USM_ADVICE_FLAG_CLEAR_ACCESSED_BY_DEVICE,
UR_USM_ADVICE_FLAG_SET_ACCESSED_BY_HOST,
UR_USM_ADVICE_FLAG_CLEAR_ACCESSED_BY_HOST,
UR_USM_ADVICE_FLAG_SET_PREFERRED_LOCATION_HOST,
UR_USM_ADVICE_FLAG_CLEAR_PREFERRED_LOCATION_HOST,
UR_USM_ADVICE_FLAG_SET_NON_COHERENT_MEMORY,
UR_USM_ADVICE_FLAG_CLEAR_NON_COHERENT_MEMORY};

} // namespace uur

#endif // UUR_ENQUEUE_RECT_HELPERS_H_INCLUDED
108 changes: 51 additions & 57 deletions test/conformance/usm/urUSMDeviceAlloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,18 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "helpers.h"
#include "uur/utils.h"
#include <limits>
#include <uur/fixtures.h>
#include <uur/known_failure.h>

struct urUSMDeviceAllocTest
: uur::urQueueTestWithParam<uur::USMDeviceAllocParams> {
struct urUSMDeviceAllocTest : uur::urUSMAllocTest {
void SetUp() override {
UUR_RETURN_ON_FATAL_FAILURE(
uur::urQueueTestWithParam<uur::USMDeviceAllocParams>::SetUp());
ur_device_usm_access_capability_flags_t deviceUSMSupport = 0;
ASSERT_SUCCESS(uur::GetDeviceUSMDeviceSupport(device, deviceUSMSupport));
if (!deviceUSMSupport) {
UUR_RETURN_ON_FATAL_FAILURE(uur::urUSMAllocTest::SetUp());
ASSERT_SUCCESS(uur::GetDeviceUSMDeviceSupport(device, USMSupport));
if (!USMSupport) {
GTEST_SKIP() << "Device USM is not supported.";
}

if (usePool) {
ur_bool_t poolSupport = false;
ASSERT_SUCCESS(uur::GetDeviceUSMPoolSupport(device, poolSupport));
if (!poolSupport) {
GTEST_SKIP() << "USM pools are not supported.";
}
ur_usm_pool_desc_t pool_desc = {};
ASSERT_SUCCESS(urUSMPoolCreate(context, &pool_desc, &pool));
}
}

void TearDown() override {
if (pool) {
ASSERT_SUCCESS(urUSMPoolRelease(pool));
}
UUR_RETURN_ON_FATAL_FAILURE(
uur::urQueueTestWithParam<uur::USMDeviceAllocParams>::TearDown());
}

ur_usm_pool_handle_t pool = nullptr;
bool usePool = std::get<0>(getParam()).value;
};

// The 0 value parameters are not relevant for urUSMDeviceAllocTest tests, they
Expand All @@ -49,14 +26,14 @@ UUR_DEVICE_TEST_SUITE_WITH_PARAM(
urUSMDeviceAllocTest,
testing::Combine(
testing::ValuesIn(uur::BoolTestParam::makeBoolParam("UsePool")),
testing::Values(0), testing::Values(0)),
testing::Values(0), testing::Values(0),
::testing::ValuesIn(uur::usm_alloc_test_parameters)),
uur::printUSMAllocTestString<urUSMDeviceAllocTest>);

TEST_P(urUSMDeviceAllocTest, Success) {
UUR_KNOWN_FAILURE_ON(uur::NativeCPU{});

void *ptr = nullptr;
size_t allocation_size = sizeof(int);
allocation_size = sizeof(int);
ASSERT_SUCCESS(
urUSMDeviceAlloc(context, device, nullptr, pool, allocation_size, &ptr));
ASSERT_NE(ptr, nullptr);
Expand All @@ -73,16 +50,15 @@ TEST_P(urUSMDeviceAllocTest, Success) {
}

TEST_P(urUSMDeviceAllocTest, SuccessWithDescriptors) {
const ur_usm_device_desc_t usm_device_desc{UR_STRUCTURE_TYPE_USM_DEVICE_DESC,
nullptr,
/* device flags */ 0};

ur_usm_device_desc_t usm_device_desc{UR_STRUCTURE_TYPE_USM_DEVICE_DESC,
nullptr,
/* device flags */ 0};
const ur_usm_desc_t usm_desc{UR_STRUCTURE_TYPE_USM_DESC, &usm_device_desc,
advice_flags, alignment};

allocation_size = sizeof(int);

ur_usm_desc_t usm_desc{UR_STRUCTURE_TYPE_USM_DESC, &usm_device_desc,
/* mem advice flags */ UR_USM_ADVICE_FLAG_DEFAULT,
/* alignment */ 0};
void *ptr = nullptr;
size_t allocation_size = sizeof(int);
ASSERT_SUCCESS(urUSMDeviceAlloc(context, device, &usm_desc, pool,
allocation_size, &ptr));
ASSERT_NE(ptr, nullptr);
Expand All @@ -98,14 +74,12 @@ TEST_P(urUSMDeviceAllocTest, SuccessWithDescriptors) {
}

TEST_P(urUSMDeviceAllocTest, InvalidNullHandleContext) {
void *ptr = nullptr;
ASSERT_EQ_RESULT(
UR_RESULT_ERROR_INVALID_NULL_HANDLE,
urUSMDeviceAlloc(nullptr, device, nullptr, pool, sizeof(int), &ptr));
}

TEST_P(urUSMDeviceAllocTest, InvalidNullHandleDevice) {
void *ptr = nullptr;
ASSERT_EQ_RESULT(
UR_RESULT_ERROR_INVALID_NULL_HANDLE,
urUSMDeviceAlloc(context, nullptr, nullptr, pool, sizeof(int), &ptr));
Expand All @@ -121,43 +95,63 @@ TEST_P(urUSMDeviceAllocTest, InvalidUSMSize) {
UUR_KNOWN_FAILURE_ON(uur::CUDA{}, uur::HIP{}, uur::LevelZero{},
uur::NativeCPU{});

void *ptr = nullptr;
ASSERT_EQ_RESULT(UR_RESULT_ERROR_INVALID_USM_SIZE,
urUSMDeviceAlloc(context, device, nullptr, pool, -1, &ptr));
size_t size = 0;

ASSERT_EQ_RESULT(
UR_RESULT_ERROR_INVALID_USM_SIZE,
urUSMDeviceAlloc(context, device, nullptr, pool, size, &ptr));

size = std::numeric_limits<size_t>::max() + 1;
ASSERT_EQ_RESULT(
UR_RESULT_ERROR_INVALID_USM_SIZE,
urUSMDeviceAlloc(context, device, nullptr, pool, size, &ptr));
}

TEST_P(urUSMDeviceAllocTest, InvalidValueAlignPowerOfTwo) {
void *ptr = nullptr;
TEST_P(urUSMDeviceAllocTest, InvalidValue) {
ur_usm_desc_t desc = {};
desc.stype = UR_STRUCTURE_TYPE_USM_DESC;
desc.align = 5;
desc.pNext = nullptr;
desc.hints = UR_USM_ADVICE_FLAG_DEFAULT;
ASSERT_EQ_RESULT(
UR_RESULT_ERROR_INVALID_VALUE,
urUSMDeviceAlloc(context, device, &desc, pool, sizeof(int), &ptr));

desc.align = std::numeric_limits<uint32_t>::max();
ASSERT_EQ_RESULT(
UR_RESULT_ERROR_INVALID_VALUE,
urUSMDeviceAlloc(context, device, &desc, pool, sizeof(int), &ptr));
}

TEST_P(urUSMDeviceAllocTest, InvalidEnumeration) {
ur_usm_desc_t desc = {};
desc.stype = UR_STRUCTURE_TYPE_USM_DESC;
desc.align = 5;
desc.pNext = nullptr;
desc.hints = UR_USM_ADVICE_FLAG_FORCE_UINT32;
ASSERT_EQ_RESULT(
UR_RESULT_ERROR_INVALID_ENUMERATION,
urUSMDeviceAlloc(context, device, &desc, pool, sizeof(int), &ptr));
}

using urUSMDeviceAllocAlignmentTest = urUSMDeviceAllocTest;

UUR_DEVICE_TEST_SUITE_WITH_PARAM(
urUSMDeviceAllocAlignmentTest,
testing::Combine(
testing::ValuesIn(uur::BoolTestParam::makeBoolParam("UsePool")),
testing::Values(4, 8, 16, 32, 64), testing::Values(8, 512, 2048)),
testing::Values(4, 8, 16, 32, 64), testing::Values(8, 512, 2048),
testing::ValuesIn(uur::usm_alloc_test_parameters)),
uur::printUSMAllocTestString<urUSMDeviceAllocAlignmentTest>);

TEST_P(urUSMDeviceAllocAlignmentTest, SuccessAlignedAllocations) {
uint32_t alignment = std::get<1>(getParam());
size_t allocation_size = std::get<2>(getParam());

ur_usm_device_desc_t usm_device_desc{UR_STRUCTURE_TYPE_USM_DEVICE_DESC,
nullptr,
/* device flags */ 0};
const ur_usm_device_desc_t usm_device_desc{UR_STRUCTURE_TYPE_USM_DEVICE_DESC,
nullptr,
/* device flags */ 0};

ur_usm_desc_t usm_desc{UR_STRUCTURE_TYPE_USM_DESC, &usm_device_desc,
/* mem advice flags */ UR_USM_ADVICE_FLAG_DEFAULT,
alignment};
const ur_usm_desc_t usm_desc{UR_STRUCTURE_TYPE_USM_DESC, &usm_device_desc,
advice_flags, alignment};

void *ptr = nullptr;
ASSERT_SUCCESS(urUSMDeviceAlloc(context, device, &usm_desc, pool,
allocation_size, &ptr));
ASSERT_NE(ptr, nullptr);
Expand Down
Loading

0 comments on commit e4b71e4

Please sign in to comment.