From 069b26814360843967bc48852bd99df47434a0fd Mon Sep 17 00:00:00 2001 From: Krzysztof Swiecicki Date: Thu, 29 Jun 2023 10:25:06 +0200 Subject: [PATCH] Add tests for the pool manager --- test/usm/CMakeLists.txt | 2 +- test/usm/pool.hpp | 83 +++++++++++++++++++++++++++++++++++++ test/usm/usmPoolManager.cpp | 45 +++++++++++++++++--- 3 files changed, 124 insertions(+), 6 deletions(-) create mode 100644 test/usm/pool.hpp diff --git a/test/usm/CMakeLists.txt b/test/usm/CMakeLists.txt index e0cb5fd943..6c57241fb2 100644 --- a/test/usm/CMakeLists.txt +++ b/test/usm/CMakeLists.txt @@ -17,7 +17,7 @@ function(add_usm_test name) ${PROJECT_NAME}::loader ur_testing GTest::gtest_main) - add_test(NAME usm-${name} + add_test(NAME usm-${name} COMMAND ${TEST_TARGET_NAME} WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) set_tests_properties(usm-${name} PROPERTIES LABELS "usm") diff --git a/test/usm/pool.hpp b/test/usm/pool.hpp new file mode 100644 index 0000000000..f79522c56b --- /dev/null +++ b/test/usm/pool.hpp @@ -0,0 +1,83 @@ +/* + * + * 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 + * + */ + +#ifndef USM_TEST_POOL_HPP +#define USM_TEST_POOL_HPP 1 + +#include + +#include +#include + +#include "uma_helpers.hpp" + +namespace usm_test { + +struct null_provider { + uma_result_t initialize() noexcept { return UMA_RESULT_SUCCESS; }; + enum uma_result_t alloc(size_t, size_t, void **) noexcept { + return UMA_RESULT_SUCCESS; + } + enum uma_result_t free(void *ptr, size_t size) noexcept { + return UMA_RESULT_SUCCESS; + } + enum uma_result_t get_last_result(const char **) noexcept { + return UMA_RESULT_SUCCESS; + } + enum uma_result_t get_recommended_page_size(size_t size, + size_t *pageSize) noexcept { + return UMA_RESULT_SUCCESS; + } + enum uma_result_t get_min_page_size(void *ptr, size_t *pageSize) noexcept { + return UMA_RESULT_SUCCESS; + } + enum uma_result_t purge_lazy(void *ptr, size_t size) noexcept { + return UMA_RESULT_SUCCESS; + } + enum uma_result_t purge_force(void *ptr, size_t size) noexcept { + return UMA_RESULT_SUCCESS; + } + + void get_name(const char **ppName) noexcept { *ppName = "null"; } +}; + +uma::provider_unique_handle_t nullProviderCreate() { + auto [ret, provider] = uma::memoryProviderMakeUnique(); + EXPECT_EQ(ret, UMA_RESULT_SUCCESS); + + return std::move(provider); +} + +struct null_pool { + uma_result_t initialize(uma_memory_provider_handle_t *, size_t) noexcept { + return UMA_RESULT_SUCCESS; + }; + void *malloc(size_t) noexcept { return nullptr; } + void *calloc(size_t, size_t) noexcept { return nullptr; } + void *realloc(void *, size_t) noexcept { return nullptr; } + void *aligned_malloc(size_t, size_t) noexcept { return nullptr; } + void free(void *) noexcept { return; } + size_t malloc_usable_size(void *) noexcept { return 0; } + enum uma_result_t get_last_result(const char **) noexcept { + return UMA_RESULT_ERROR_UNKNOWN; + } +}; + +uma::pool_unique_handle_t +nullPoolCreate(uma_memory_provider_handle_t *provider) { + auto [ret, pool] = uma::poolMakeUnique(provider, 1); + EXPECT_EQ(ret, UMA_RESULT_SUCCESS); + + return std::move(pool); +} + +} // namespace usm_test + +#endif /* USM_TEST_POOL_HPP */ diff --git a/test/usm/usmPoolManager.cpp b/test/usm/usmPoolManager.cpp index b244a89c83..840a7bf6ce 100644 --- a/test/usm/usmPoolManager.cpp +++ b/test/usm/usmPoolManager.cpp @@ -3,19 +3,19 @@ // See LICENSE.TXT // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -#include "../unified_memory_allocation/common/pool.hpp" -#include "../unified_memory_allocation/common/provider.hpp" +#include "pool.hpp" #include "ur_pool_manager.hpp" #include +#include #include -struct urUsmPoolManagerTest +struct urUsmPoolDescriptorTest : public uur::urMultiDeviceContextTest, ::testing::WithParamInterface {}; -TEST_P(urUsmPoolManagerTest, poolIsPerContextTypeAndDevice) { +TEST_P(urUsmPoolDescriptorTest, poolIsPerContextTypeAndDevice) { auto &devices = uur::DevicesEnvironment::instance->devices; auto poolHandle = this->GetParam(); @@ -49,7 +49,42 @@ TEST_P(urUsmPoolManagerTest, poolIsPerContextTypeAndDevice) { ASSERT_EQ(sharedPools, devices.size() * 2); } -INSTANTIATE_TEST_SUITE_P(urUsmPoolManagerTest, urUsmPoolManagerTest, +INSTANTIATE_TEST_SUITE_P(urUsmPoolDescriptorTest, urUsmPoolDescriptorTest, ::testing::Values(nullptr)); // TODO: add test with sub-devices + +struct urUsmPoolManagerTest : ::testing::Test { + void SetUp() override { + auto [ret, descs] = usm::pool_descriptor::create(nullptr, nullptr); + ASSERT_EQ(ret, UR_RESULT_SUCCESS); + poolDescriptors = descs; + } + + std::vector poolDescriptors; +}; + +TEST_F(urUsmPoolManagerTest, poolManagerPopulate) { + constexpr size_t seed = 0xC0FFEE; + srand(seed); + + auto [ret, manager] = usm::pool_manager::create(); + ASSERT_EQ(ret, UMA_RESULT_SUCCESS); + + for (auto &desc : poolDescriptors) { + // Populate the pool manager + auto providerUnique = usm_test::nullProviderCreate(); + auto provider = providerUnique.get(); + auto poolUnique = usm_test::nullPoolCreate(&provider); + ASSERT_NE(poolUnique, nullptr); + ret = manager.addPool(desc, poolUnique); + ASSERT_EQ(ret, UMA_RESULT_SUCCESS); + } + + for (auto &desc : poolDescriptors) { + // Confirm that there is a pool for each descriptor + auto hPoolOpt = manager.getPool(desc); + ASSERT_TRUE(hPoolOpt.has_value()); + ASSERT_NE(hPoolOpt.value(), nullptr); + } +}