Skip to content

Commit

Permalink
Add parameterization to USM alloc tests for testing pools.
Browse files Browse the repository at this point in the history
Also fix a couple of minor issues in tests:
* check correct code for invalid size in GetMemAllocInfo test
* fix shadowing in urUSMPoolTestWithParam fixture
* ensure we allocate with a pool for UR_USM_ALLOC_INFO_POOL
  GetMemAllocInfo test
  • Loading branch information
aarongreig committed Aug 2, 2023
1 parent 4834f8b commit 40a7789
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 49 deletions.
8 changes: 6 additions & 2 deletions test/conformance/testing/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

add_ur_library(ur_testing STATIC
source/utils.cpp)
source/utils.cpp
source/fixtures.cpp)
target_include_directories(ur_testing PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_link_libraries(ur_testing PRIVATE gtest_main unified-runtime::headers)
target_link_libraries(ur_testing PRIVATE
gtest_main
${PROJECT_NAME}::common
${PROJECT_NAME}::headers)
add_library(${PROJECT_NAME}::testing ALIAS ur_testing)
28 changes: 26 additions & 2 deletions test/conformance/testing/include/uur/fixtures.h
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,6 @@ template <class T> struct urUSMPoolTestWithParam : urContextTestWithParam<T> {
UUR_RETURN_ON_FATAL_FAILURE(urContextTestWithParam<T>::SetUp());
ur_usm_pool_desc_t pool_desc{UR_STRUCTURE_TYPE_USM_POOL_DESC, nullptr,
UR_USM_POOL_FLAG_ZERO_INITIALIZE_BLOCK};
ur_usm_pool_handle_t pool = nullptr;
ASSERT_SUCCESS(urUSMPoolCreate(this->context, &pool_desc, &pool));
}

Expand Down Expand Up @@ -823,8 +822,12 @@ struct urUSMDeviceAllocTestWithParam : urQueueTestWithParam<T> {
if (!device_usm) {
GTEST_SKIP() << "Device USM in not supported";
}
if (use_pool) {
ur_usm_pool_desc_t pool_desc = {};
ASSERT_SUCCESS(urUSMPoolCreate(this->context, &pool_desc, &pool));
}
ASSERT_SUCCESS(urUSMDeviceAlloc(this->context, this->device, nullptr,
nullptr, allocation_size, &ptr));
pool, allocation_size, &ptr));
ur_event_handle_t event = nullptr;

uint8_t fillPattern = 0;
Expand All @@ -839,11 +842,16 @@ struct urUSMDeviceAllocTestWithParam : urQueueTestWithParam<T> {

void TearDown() override {
ASSERT_SUCCESS(urUSMFree(this->context, ptr));
if (pool) {
ASSERT_SUCCESS(urUSMPoolRelease(pool));
}
uur::urQueueTestWithParam<T>::TearDown();
}

size_t allocation_size = sizeof(int);
void *ptr = nullptr;
bool use_pool = false;
ur_usm_pool_handle_t pool = nullptr;
};

/// @brief
Expand All @@ -861,6 +869,22 @@ std::string deviceTestWithParamPrinter(
return uur::GetPlatformAndDeviceName(device) + "__" + ss.str();
}

// Helper struct to allow bool param tests with meaningful names.
struct BoolTestParam {
std::string name;
bool value;

// For use with testing::ValuesIn to generate the param values.
static std::vector<BoolTestParam> makeBoolParam(std::string name) {
return std::vector<BoolTestParam>({{name, true}, {name, false}});
}
};

template <>
std::string deviceTestWithParamPrinter<BoolTestParam>(
const ::testing::TestParamInfo<
std::tuple<ur_device_handle_t, BoolTestParam>> &info);

struct urProgramTest : urQueueTest {
void SetUp() override {
UUR_RETURN_ON_FATAL_FAILURE(urQueueTest::SetUp());
Expand Down
20 changes: 20 additions & 0 deletions test/conformance/testing/source/fixtures.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (C) 2023 Intel Corporation
// 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"

namespace uur {
template <>
std::string deviceTestWithParamPrinter<BoolTestParam>(
const ::testing::TestParamInfo<
std::tuple<ur_device_handle_t, BoolTestParam>> &info) {
auto device = std::get<0>(info.param);
auto param = std::get<1>(info.param);

std::stringstream ss;
ss << param.name << (param.value ? "Enabled" : "Disabled");
return uur::GetPlatformAndDeviceName(device) + "__" + ss.str();
}
} // namespace uur
48 changes: 34 additions & 14 deletions test/conformance/usm/urUSMDeviceAlloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,43 @@

#include <uur/fixtures.h>

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

if (getParam().value) {
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::BoolTestParam>::TearDown());
}

ur_usm_pool_handle_t pool = nullptr;
};
UUR_INSTANTIATE_DEVICE_TEST_SUITE_P(urUSMDeviceAllocTest);

UUR_TEST_SUITE_P(
urUSMDeviceAllocTest,
testing::ValuesIn(uur::BoolTestParam::makeBoolParam("UsePool")),
uur::deviceTestWithParamPrinter<uur::BoolTestParam>);

TEST_P(urUSMDeviceAllocTest, Success) {
void *ptr = nullptr;
size_t allocation_size = sizeof(int);
ASSERT_SUCCESS(urUSMDeviceAlloc(context, device, nullptr, nullptr,
ASSERT_SUCCESS(urUSMDeviceAlloc(context, device, nullptr, pool,
allocation_size, &ptr));
ASSERT_NE(ptr, nullptr);

Expand All @@ -47,7 +67,7 @@ TEST_P(urUSMDeviceAllocTest, SuccessWithDescriptors) {
/* alignment */ 0};
void *ptr = nullptr;
size_t allocation_size = sizeof(int);
ASSERT_SUCCESS(urUSMDeviceAlloc(context, device, &usm_desc, nullptr,
ASSERT_SUCCESS(urUSMDeviceAlloc(context, device, &usm_desc, pool,
allocation_size, &ptr));

ur_event_handle_t event = nullptr;
Expand All @@ -64,27 +84,27 @@ TEST_P(urUSMDeviceAllocTest, InvalidNullHandleContext) {
void *ptr = nullptr;
ASSERT_EQ_RESULT(
UR_RESULT_ERROR_INVALID_NULL_HANDLE,
urUSMDeviceAlloc(nullptr, device, nullptr, nullptr, sizeof(int), &ptr));
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, nullptr,
sizeof(int), &ptr));
ASSERT_EQ_RESULT(
UR_RESULT_ERROR_INVALID_NULL_HANDLE,
urUSMDeviceAlloc(context, nullptr, nullptr, pool, sizeof(int), &ptr));
}

TEST_P(urUSMDeviceAllocTest, InvalidNullPtrResult) {
ASSERT_EQ_RESULT(UR_RESULT_ERROR_INVALID_NULL_POINTER,
urUSMDeviceAlloc(context, device, nullptr, nullptr,
sizeof(int), nullptr));
ASSERT_EQ_RESULT(
UR_RESULT_ERROR_INVALID_NULL_POINTER,
urUSMDeviceAlloc(context, device, nullptr, pool, sizeof(int), nullptr));
}

TEST_P(urUSMDeviceAllocTest, InvalidUSMSize) {
void *ptr = nullptr;
ASSERT_EQ_RESULT(
UR_RESULT_ERROR_INVALID_USM_SIZE,
urUSMDeviceAlloc(context, device, nullptr, nullptr, -1, &ptr));
urUSMDeviceAlloc(context, device, nullptr, pool, -1, &ptr));
}

TEST_P(urUSMDeviceAllocTest, InvalidValueAlignPowerOfTwo) {
Expand All @@ -94,5 +114,5 @@ TEST_P(urUSMDeviceAllocTest, InvalidValueAlignPowerOfTwo) {
desc.align = 5;
ASSERT_EQ_RESULT(
UR_RESULT_ERROR_INVALID_VALUE,
urUSMDeviceAlloc(context, device, &desc, nullptr, sizeof(int), &ptr));
urUSMDeviceAlloc(context, device, &desc, pool, sizeof(int), &ptr));
}
12 changes: 9 additions & 3 deletions test/conformance/usm/urUSMGetMemAllocInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@
#include <map>
#include <uur/fixtures.h>

using urUSMAllocInfoTest =
uur::urUSMDeviceAllocTestWithParam<ur_usm_alloc_info_t>;
struct urUSMAllocInfoTest
: uur::urUSMDeviceAllocTestWithParam<ur_usm_alloc_info_t> {
void SetUp() override {
use_pool = getParam() == UR_USM_ALLOC_INFO_POOL;
UUR_RETURN_ON_FATAL_FAILURE(
uur::urUSMDeviceAllocTestWithParam<ur_usm_alloc_info_t>::SetUp());
}
};

UUR_TEST_SUITE_P(urUSMAllocInfoTest,
::testing::Values(UR_USM_ALLOC_INFO_TYPE,
Expand Down Expand Up @@ -71,7 +77,7 @@ TEST_P(urUSMGetMemAllocInfoTest, InvalidEnumeration) {

TEST_P(urUSMGetMemAllocInfoTest, InvalidValuePropSize) {
ur_usm_type_t USMType;
ASSERT_EQ_RESULT(UR_RESULT_ERROR_INVALID_VALUE,
ASSERT_EQ_RESULT(UR_RESULT_ERROR_INVALID_SIZE,
urUSMGetMemAllocInfo(context, ptr, UR_USM_ALLOC_INFO_TYPE,
sizeof(ur_usm_type_t) - 1, &USMType,
nullptr));
Expand Down
43 changes: 30 additions & 13 deletions test/conformance/usm/urUSMHostAlloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,36 @@
#include <cstring>
#include <uur/fixtures.h>

struct urUSMHostAllocTest : uur::urQueueTest {
struct urUSMHostAllocTest : uur::urQueueTestWithParam<uur::BoolTestParam> {
void SetUp() override {
UUR_RETURN_ON_FATAL_FAILURE(uur::urQueueTest::SetUp());
UUR_RETURN_ON_FATAL_FAILURE(
uur::urQueueTestWithParam<uur::BoolTestParam>::SetUp());
ur_device_usm_access_capability_flags_t hostUSMSupport = 0;
ASSERT_SUCCESS(uur::GetDeviceUSMHostSupport(device, hostUSMSupport));
if (!hostUSMSupport) {
GTEST_SKIP() << "Device USM is not supported.";
}
if (getParam().value) {
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::BoolTestParam>::TearDown());
}

ur_usm_pool_handle_t pool = nullptr;
};
UUR_INSTANTIATE_DEVICE_TEST_SUITE_P(urUSMHostAllocTest);

UUR_TEST_SUITE_P(
urUSMHostAllocTest,
testing::ValuesIn(uur::BoolTestParam::makeBoolParam("UsePool")),
uur::deviceTestWithParamPrinter<uur::BoolTestParam>);

TEST_P(urUSMHostAllocTest, Success) {
ur_device_usm_access_capability_flags_t hostUSMSupport = 0;
Expand All @@ -27,7 +46,7 @@ TEST_P(urUSMHostAllocTest, Success) {

size_t allocation_size = sizeof(int);
int *ptr = nullptr;
ASSERT_SUCCESS(urUSMHostAlloc(context, nullptr, nullptr, sizeof(int),
ASSERT_SUCCESS(urUSMHostAlloc(context, nullptr, pool, sizeof(int),
reinterpret_cast<void **>(&ptr)));
ASSERT_NE(ptr, nullptr);

Expand Down Expand Up @@ -68,7 +87,7 @@ TEST_P(urUSMHostAllocTest, SuccessWithDescriptors) {
void *ptr = nullptr;
size_t allocation_size = sizeof(int);
ASSERT_SUCCESS(
urUSMHostAlloc(context, &usm_desc, nullptr, allocation_size, &ptr));
urUSMHostAlloc(context, &usm_desc, pool, allocation_size, &ptr));

ur_event_handle_t event = nullptr;
uint8_t pattern = 0;
Expand All @@ -82,29 +101,27 @@ TEST_P(urUSMHostAllocTest, SuccessWithDescriptors) {

TEST_P(urUSMHostAllocTest, InvalidNullHandleContext) {
void *ptr = nullptr;
ASSERT_EQ_RESULT(
UR_RESULT_ERROR_INVALID_NULL_HANDLE,
urUSMHostAlloc(nullptr, nullptr, nullptr, sizeof(int), &ptr));
ASSERT_EQ_RESULT(UR_RESULT_ERROR_INVALID_NULL_HANDLE,
urUSMHostAlloc(nullptr, nullptr, pool, sizeof(int), &ptr));
}

TEST_P(urUSMHostAllocTest, InvalidNullPtrMem) {
ASSERT_EQ_RESULT(
UR_RESULT_ERROR_INVALID_NULL_POINTER,
urUSMHostAlloc(context, nullptr, nullptr, sizeof(int), nullptr));
urUSMHostAlloc(context, nullptr, pool, sizeof(int), nullptr));
}

TEST_P(urUSMHostAllocTest, InvalidUSMSize) {
void *ptr = nullptr;
ASSERT_EQ_RESULT(UR_RESULT_ERROR_INVALID_USM_SIZE,
urUSMHostAlloc(context, nullptr, nullptr, -1, &ptr));
urUSMHostAlloc(context, nullptr, pool, -1, &ptr));
}

TEST_P(urUSMHostAllocTest, InvalidValueAlignPowerOfTwo) {
void *ptr = nullptr;
ur_usm_desc_t desc = {};
desc.stype = UR_STRUCTURE_TYPE_USM_DESC;
desc.align = 5;
ASSERT_EQ_RESULT(
UR_RESULT_ERROR_INVALID_VALUE,
urUSMHostAlloc(context, &desc, nullptr, sizeof(int), &ptr));
ASSERT_EQ_RESULT(UR_RESULT_ERROR_INVALID_VALUE,
urUSMHostAlloc(context, &desc, pool, sizeof(int), &ptr));
}
Loading

0 comments on commit 40a7789

Please sign in to comment.