From e4b71e4d9d76612e4ad9f7f175a6e6d62a672439 Mon Sep 17 00:00:00 2001 From: Martin Morrison-Grant Date: Wed, 29 Jan 2025 16:12:39 +0000 Subject: [PATCH] Improvements to align CTS and Spec for USM: - 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 --- test/conformance/usm/helpers.h | 71 ++++++++++-- test/conformance/usm/urUSMDeviceAlloc.cpp | 108 +++++++++---------- test/conformance/usm/urUSMHostAlloc.cpp | 115 +++++++++----------- test/conformance/usm/urUSMPoolRelease.cpp | 26 ++++- test/conformance/usm/urUSMSharedAlloc.cpp | 125 ++++++++++++---------- 5 files changed, 250 insertions(+), 195 deletions(-) diff --git a/test/conformance/usm/helpers.h b/test/conformance/usm/helpers.h index 307635eef9..6c9085cc8f 100644 --- a/test/conformance/usm/helpers.h +++ b/test/conformance/usm/helpers.h @@ -11,7 +11,40 @@ namespace uur { -using USMDeviceAllocParams = std::tuple; +using USMDeviceAllocParams = + std::tuple; + +struct urUSMAllocTest : uur::urQueueTestWithParam { + void SetUp() override { + UUR_RETURN_ON_FATAL_FAILURE( + uur::urQueueTestWithParam::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::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 inline std::string printUSMAllocTestString( @@ -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 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 diff --git a/test/conformance/usm/urUSMDeviceAlloc.cpp b/test/conformance/usm/urUSMDeviceAlloc.cpp index 4ac8aa474e..0e824893fc 100644 --- a/test/conformance/usm/urUSMDeviceAlloc.cpp +++ b/test/conformance/usm/urUSMDeviceAlloc.cpp @@ -5,41 +5,18 @@ // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception #include "helpers.h" #include "uur/utils.h" +#include #include #include -struct urUSMDeviceAllocTest - : uur::urQueueTestWithParam { +struct urUSMDeviceAllocTest : uur::urUSMAllocTest { void SetUp() override { - UUR_RETURN_ON_FATAL_FAILURE( - uur::urQueueTestWithParam::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::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 @@ -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); 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); @@ -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); @@ -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)); @@ -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::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::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); 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); diff --git a/test/conformance/usm/urUSMHostAlloc.cpp b/test/conformance/usm/urUSMHostAlloc.cpp index c2bc677b7b..ec95736712 100644 --- a/test/conformance/usm/urUSMHostAlloc.cpp +++ b/test/conformance/usm/urUSMHostAlloc.cpp @@ -6,41 +6,18 @@ #include "helpers.h" #include +#include #include #include -struct urUSMHostAllocTest - : uur::urQueueTestWithParam { +struct urUSMHostAllocTest : uur::urUSMAllocTest { void SetUp() override { - UUR_RETURN_ON_FATAL_FAILURE( - uur::urQueueTestWithParam::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 (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)); + UUR_RETURN_ON_FATAL_FAILURE(urUSMAllocTest::SetUp()); + ASSERT_SUCCESS(uur::GetDeviceUSMHostSupport(device, USMSupport)); + if (!USMSupport) { + GTEST_SKIP() << "Host USM is not supported."; } } - - void TearDown() override { - if (pool) { - ASSERT_SUCCESS(urUSMPoolRelease(pool)); - } - UUR_RETURN_ON_FATAL_FAILURE( - uur::urQueueTestWithParam::TearDown()); - } - - ur_usm_pool_handle_t pool = nullptr; - bool usePool = std::get<0>(getParam()).value; }; // The 0 value parameters are not relevant for urUSMHostAllocTest tests, they @@ -50,20 +27,14 @@ UUR_DEVICE_TEST_SUITE_WITH_PARAM( urUSMHostAllocTest, 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); TEST_P(urUSMHostAllocTest, Success) { UUR_KNOWN_FAILURE_ON(uur::NativeCPU{}); - ur_device_usm_access_capability_flags_t hostUSMSupport = 0; - ASSERT_SUCCESS(uur::GetDeviceUSMHostSupport(device, hostUSMSupport)); - if (!hostUSMSupport) { - GTEST_SKIP() << "Host USM is not supported."; - } - - size_t allocation_size = sizeof(int); - int *ptr = nullptr; + allocation_size = sizeof(int); ASSERT_SUCCESS(urUSMHostAlloc(context, nullptr, pool, sizeof(int), reinterpret_cast(&ptr))); ASSERT_NE(ptr, nullptr); @@ -77,7 +48,7 @@ TEST_P(urUSMHostAllocTest, Success) { EXPECT_SUCCESS(urQueueFlush(queue)); ASSERT_SUCCESS(urEventWait(1, &event)); EXPECT_SUCCESS(urEventRelease(event)); - ASSERT_EQ(*ptr, 0); + ASSERT_EQ(*reinterpret_cast(ptr), 0); // Set 1, in all bytes of int pattern = 1; @@ -86,23 +57,25 @@ TEST_P(urUSMHostAllocTest, Success) { EXPECT_SUCCESS(urQueueFlush(queue)); ASSERT_SUCCESS(urEventWait(1, &event)); EXPECT_SUCCESS(urEventRelease(event)); + // replicate it on host int set_data = 0; std::memset(&set_data, 1, allocation_size); - ASSERT_EQ(*ptr, set_data); + ASSERT_EQ(*reinterpret_cast(ptr), set_data); ASSERT_SUCCESS(urUSMFree(context, ptr)); } TEST_P(urUSMHostAllocTest, SuccessWithDescriptors) { - ur_usm_host_desc_t usm_host_desc{UR_STRUCTURE_TYPE_USM_HOST_DESC, nullptr, - /* host flags */ 0}; - - ur_usm_desc_t usm_desc{UR_STRUCTURE_TYPE_USM_DESC, &usm_host_desc, - /* mem advice flags */ UR_USM_ADVICE_FLAG_DEFAULT, - /* alignment */ 0}; - void *ptr = nullptr; - size_t allocation_size = sizeof(int); + const ur_usm_host_desc_t usm_host_desc{UR_STRUCTURE_TYPE_USM_HOST_DESC, + nullptr, + /* host flags */ 0}; + + const ur_usm_desc_t usm_desc{UR_STRUCTURE_TYPE_USM_DESC, &usm_host_desc, + advice_flags, alignment}; + + allocation_size = sizeof(int); + ASSERT_SUCCESS( urUSMHostAlloc(context, &usm_desc, pool, allocation_size, &ptr)); ASSERT_NE(ptr, nullptr); @@ -118,7 +91,6 @@ TEST_P(urUSMHostAllocTest, SuccessWithDescriptors) { } TEST_P(urUSMHostAllocTest, InvalidNullHandleContext) { - void *ptr = nullptr; ASSERT_EQ_RESULT(UR_RESULT_ERROR_INVALID_NULL_HANDLE, urUSMHostAlloc(nullptr, nullptr, pool, sizeof(int), &ptr)); } @@ -132,41 +104,58 @@ TEST_P(urUSMHostAllocTest, InvalidNullPtrMem) { TEST_P(urUSMHostAllocTest, InvalidUSMSize) { UUR_KNOWN_FAILURE_ON(uur::CUDA{}, uur::HIP{}, uur::NativeCPU{}); - void *ptr = nullptr; + size_t size = 0; + ASSERT_EQ_RESULT(UR_RESULT_ERROR_INVALID_USM_SIZE, - urUSMHostAlloc(context, nullptr, pool, -1, &ptr)); + urUSMHostAlloc(context, nullptr, pool, size, &ptr)); + + size = std::numeric_limits::max() + 1; + ASSERT_EQ_RESULT(UR_RESULT_ERROR_INVALID_USM_SIZE, + urUSMHostAlloc(context, nullptr, pool, size, &ptr)); } -TEST_P(urUSMHostAllocTest, InvalidValueAlignPowerOfTwo) { - void *ptr = nullptr; +TEST_P(urUSMHostAllocTest, InvalidValue) { ur_usm_desc_t desc = {}; desc.stype = UR_STRUCTURE_TYPE_USM_DESC; desc.align = 5; + desc.hints = UR_USM_ADVICE_FLAG_DEFAULT; + desc.pNext = nullptr; + ASSERT_EQ_RESULT(UR_RESULT_ERROR_INVALID_VALUE, + urUSMHostAlloc(context, &desc, pool, sizeof(int), &ptr)); + + desc.align = std::numeric_limits::max(); ASSERT_EQ_RESULT(UR_RESULT_ERROR_INVALID_VALUE, urUSMHostAlloc(context, &desc, pool, sizeof(int), &ptr)); } +TEST_P(urUSMHostAllocTest, InvalidEnumeration) { + ur_usm_desc_t desc = {}; + desc.stype = UR_STRUCTURE_TYPE_USM_DESC; + desc.align = 5; + desc.hints = UR_USM_ADVICE_FLAG_FORCE_UINT32; + desc.pNext = nullptr; + ASSERT_EQ_RESULT(UR_RESULT_ERROR_INVALID_ENUMERATION, + urUSMHostAlloc(context, &desc, pool, sizeof(int), &ptr)); +} + using urUSMHostAllocAlignmentTest = urUSMHostAllocTest; UUR_DEVICE_TEST_SUITE_WITH_PARAM( urUSMHostAllocAlignmentTest, 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); TEST_P(urUSMHostAllocAlignmentTest, SuccessAlignedAllocations) { - uint32_t alignment = std::get<1>(getParam()); - size_t allocation_size = std::get<2>(getParam()); - - ur_usm_host_desc_t usm_host_desc{UR_STRUCTURE_TYPE_USM_HOST_DESC, nullptr, - /* host flags */ 0}; + const ur_usm_host_desc_t usm_host_desc{UR_STRUCTURE_TYPE_USM_HOST_DESC, + nullptr, + /* host flags */ 0}; - ur_usm_desc_t usm_desc{UR_STRUCTURE_TYPE_USM_DESC, &usm_host_desc, - /* mem advice flags */ UR_USM_ADVICE_FLAG_DEFAULT, - alignment}; + const ur_usm_desc_t usm_desc{UR_STRUCTURE_TYPE_USM_DESC, &usm_host_desc, + advice_flags, alignment}; - void *ptr = nullptr; ASSERT_SUCCESS( urUSMHostAlloc(context, &usm_desc, pool, allocation_size, &ptr)); ASSERT_NE(ptr, nullptr); diff --git a/test/conformance/usm/urUSMPoolRelease.cpp b/test/conformance/usm/urUSMPoolRelease.cpp index ac6e71b2a4..1c88d570be 100644 --- a/test/conformance/usm/urUSMPoolRelease.cpp +++ b/test/conformance/usm/urUSMPoolRelease.cpp @@ -6,15 +6,31 @@ #include -using urUSMPoolDestroyTest = uur::urUSMPoolTest; -UUR_INSTANTIATE_DEVICE_TEST_SUITE(urUSMPoolDestroyTest); +using urUSMPoolReleaseTest = uur::urUSMPoolTest; +UUR_INSTANTIATE_DEVICE_TEST_SUITE(urUSMPoolReleaseTest); + +TEST_P(urUSMPoolReleaseTest, Success) { + uint32_t prevRefCount = 0; + ASSERT_SUCCESS(uur::GetObjectReferenceCount(pool, prevRefCount)); + + ASSERT_SUCCESS(urUSMPoolRetain(pool)); + + uint32_t refCount = 0; + ASSERT_SUCCESS(uur::GetObjectReferenceCount(pool, refCount)); + + ASSERT_LT(prevRefCount, refCount); + + EXPECT_SUCCESS(urUSMPoolRelease(pool)); + + uint32_t afterRefCount = 0; + ASSERT_SUCCESS(uur::GetObjectReferenceCount(pool, afterRefCount)); + + ASSERT_LT(afterRefCount, refCount); -TEST_P(urUSMPoolDestroyTest, Success) { - ASSERT_SUCCESS(urUSMPoolRelease(pool)); pool = nullptr; // prevent double-delete } -TEST_P(urUSMPoolDestroyTest, InvalidNullHandleContext) { +TEST_P(urUSMPoolReleaseTest, InvalidNullHandle) { ASSERT_EQ_RESULT(UR_RESULT_ERROR_INVALID_NULL_HANDLE, urUSMPoolRelease(nullptr)); } diff --git a/test/conformance/usm/urUSMSharedAlloc.cpp b/test/conformance/usm/urUSMSharedAlloc.cpp index afef082d5e..697012a592 100644 --- a/test/conformance/usm/urUSMSharedAlloc.cpp +++ b/test/conformance/usm/urUSMSharedAlloc.cpp @@ -5,14 +5,13 @@ // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception #include "helpers.h" +#include #include #include -struct urUSMSharedAllocTest - : uur::urQueueTestWithParam { +struct urUSMSharedAllocTest : uur::urUSMAllocTest { void SetUp() override { - UUR_RETURN_ON_FATAL_FAILURE( - uur::urQueueTestWithParam::SetUp()); + UUR_RETURN_ON_FATAL_FAILURE(uur::urUSMAllocTest::SetUp()); ur_device_usm_access_capability_flags_t shared_usm_cross = 0; ur_device_usm_access_capability_flags_t shared_usm_single = 0; @@ -24,29 +23,7 @@ struct urUSMSharedAllocTest if (!(shared_usm_cross || shared_usm_single)) { GTEST_SKIP() << "Shared USM is not supported by the device."; } - - 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::TearDown()); } - - ur_usm_pool_handle_t pool = nullptr; - bool usePool = std::get<0>(getParam()).value; - void *ptr = nullptr; }; // The 0 value parameters are not relevant for urUSMSharedAllocTest tests, they @@ -56,11 +33,13 @@ UUR_DEVICE_TEST_SUITE_WITH_PARAM( urUSMSharedAllocTest, 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); TEST_P(urUSMSharedAllocTest, Success) { - size_t allocation_size = sizeof(int); + allocation_size = sizeof(int); + ASSERT_SUCCESS( urUSMSharedAlloc(context, device, nullptr, pool, allocation_size, &ptr)); ASSERT_NE(ptr, nullptr); @@ -76,18 +55,19 @@ TEST_P(urUSMSharedAllocTest, Success) { } TEST_P(urUSMSharedAllocTest, SuccessWithDescriptors) { - ur_usm_device_desc_t usm_device_desc{UR_STRUCTURE_TYPE_USM_DEVICE_DESC, - nullptr, - /* device flags */ 0}; - - ur_usm_host_desc_t usm_host_desc{UR_STRUCTURE_TYPE_USM_HOST_DESC, - &usm_device_desc, - /* host flags */ 0}; - - ur_usm_desc_t usm_desc{UR_STRUCTURE_TYPE_USM_DESC, &usm_host_desc, - /* mem advice flags */ UR_USM_ADVICE_FLAG_DEFAULT, - /* alignment */ 0}; - size_t allocation_size = sizeof(int); + const ur_usm_device_desc_t usm_device_desc{UR_STRUCTURE_TYPE_USM_DEVICE_DESC, + nullptr, + /* device flags */ 0}; + + const ur_usm_host_desc_t usm_host_desc{UR_STRUCTURE_TYPE_USM_HOST_DESC, + &usm_device_desc, + /* host flags */ 0}; + + const ur_usm_desc_t usm_desc{UR_STRUCTURE_TYPE_USM_DESC, &usm_host_desc, + advice_flags, alignment}; + + allocation_size = sizeof(int); + ASSERT_SUCCESS(urUSMSharedAlloc(context, device, &usm_desc, pool, allocation_size, &ptr)); ASSERT_NE(ptr, nullptr); @@ -103,12 +83,14 @@ TEST_P(urUSMSharedAllocTest, SuccessWithDescriptors) { } TEST_P(urUSMSharedAllocTest, SuccessWithMultipleAdvices) { - ur_usm_desc_t usm_desc{ + const ur_usm_desc_t usm_desc{ UR_STRUCTURE_TYPE_USM_DESC, nullptr, /* mem advice flags */ UR_USM_ADVICE_FLAG_SET_READ_MOSTLY | UR_USM_ADVICE_FLAG_BIAS_CACHED, - /* alignment */ 0}; - size_t allocation_size = sizeof(int); + alignment}; + + allocation_size = sizeof(int); + ASSERT_SUCCESS(urUSMSharedAlloc(context, device, &usm_desc, pool, allocation_size, &ptr)); ASSERT_NE(ptr, nullptr); @@ -144,43 +126,68 @@ TEST_P(urUSMSharedAllocTest, InvalidNullPtrMem) { TEST_P(urUSMSharedAllocTest, InvalidUSMSize) { UUR_KNOWN_FAILURE_ON(uur::CUDA{}, uur::HIP{}, uur::NativeCPU{}); - ASSERT_EQ_RESULT(UR_RESULT_ERROR_INVALID_USM_SIZE, - urUSMSharedAlloc(context, device, nullptr, pool, -1, &ptr)); + size_t size = 0; + + void *ptr = nullptr; + ASSERT_EQ_RESULT( + UR_RESULT_ERROR_INVALID_USM_SIZE, + urUSMSharedAlloc(context, device, nullptr, pool, size, &ptr)); + + size = std::numeric_limits::max() + 1; + ASSERT_EQ_RESULT( + UR_RESULT_ERROR_INVALID_USM_SIZE, + urUSMSharedAlloc(context, device, nullptr, pool, size, &ptr)); } -TEST_P(urUSMSharedAllocTest, InvalidValueAlignPowerOfTwo) { +TEST_P(urUSMSharedAllocTest, 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, + urUSMSharedAlloc(context, device, &desc, pool, sizeof(int), &ptr)); + + desc.align = std::numeric_limits::max(); ASSERT_EQ_RESULT( UR_RESULT_ERROR_INVALID_VALUE, urUSMSharedAlloc(context, device, &desc, pool, sizeof(int), &ptr)); } +TEST_P(urUSMSharedAllocTest, InvalidEnumeration) { + void *ptr = nullptr; + 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, + urUSMSharedAlloc(context, device, &desc, pool, sizeof(int), &ptr)); +} + using urUSMSharedAllocAlignmentTest = urUSMSharedAllocTest; UUR_DEVICE_TEST_SUITE_WITH_PARAM( urUSMSharedAllocAlignmentTest, 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); TEST_P(urUSMSharedAllocAlignmentTest, 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_host_desc_t usm_host_desc{UR_STRUCTURE_TYPE_USM_HOST_DESC, - &usm_device_desc, - /* host flags */ 0}; + const ur_usm_host_desc_t usm_host_desc{UR_STRUCTURE_TYPE_USM_HOST_DESC, + &usm_device_desc, + /* host flags */ 0}; - ur_usm_desc_t usm_desc{UR_STRUCTURE_TYPE_USM_DESC, &usm_host_desc, - /* mem advice flags */ UR_USM_ADVICE_FLAG_DEFAULT, - alignment}; + const ur_usm_desc_t usm_desc{UR_STRUCTURE_TYPE_USM_DESC, &usm_host_desc, + advice_flags, alignment}; ASSERT_SUCCESS(urUSMSharedAlloc(context, device, &usm_desc, pool, allocation_size, &ptr));