Skip to content

Commit

Permalink
Add pool manager tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kswiecicki committed Sep 7, 2023
1 parent 1c98f6e commit a970190
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 9 deletions.
7 changes: 5 additions & 2 deletions test/usm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,20 @@ function(add_usm_test name)
add_ur_executable(${TEST_TARGET_NAME}
${UR_USM_TEST_DIR}/../conformance/source/environment.cpp
${UR_USM_TEST_DIR}/../conformance/source/main.cpp
${UR_USM_TEST_DIR}/../unified_malloc_framework/common/provider.c
${ARGN})
target_link_libraries(${TEST_TARGET_NAME}
PRIVATE
${PROJECT_NAME}::common
${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")
set_tests_properties(usm-${name} PROPERTIES
LABELS "usm"
ENVIRONMENT "UR_ADAPTERS_FORCE_LOAD=\"$<TARGET_FILE:ur_adapter_null>\"")
target_compile_definitions("usm_test-${name}" PRIVATE DEVICES_ENVIRONMENT)
endfunction()

Expand Down
48 changes: 48 additions & 0 deletions test/usm/pool.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
*
* 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 <gtest/gtest.h>

#include <umf/base.h>
#include <umf/memory_provider.h>

#include "umf_helpers.hpp"

namespace usm_test {

struct null_pool {
umf_result_t initialize(umf_memory_provider_handle_t *, size_t) noexcept {
return UMF_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; }
enum umf_result_t free(void *) noexcept { return UMF_RESULT_SUCCESS; }
size_t malloc_usable_size(void *) noexcept { return 0; }
enum umf_result_t get_last_allocation_error() noexcept {
return UMF_RESULT_SUCCESS;
}
};

umf::pool_unique_handle_t
nullPoolCreate(umf_memory_provider_handle_t *provider) {
auto [ret, pool] = umf::poolMakeUnique<null_pool>(provider, 1);
EXPECT_EQ(ret, UMF_RESULT_SUCCESS);

return std::move(pool);
}

} // namespace usm_test

#endif /* USM_TEST_POOL_HPP */
75 changes: 68 additions & 7 deletions test/usm/usmPoolManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@
// See LICENSE.TXT
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include "../unified_malloc_framework/common/pool.hpp"
#include "../unified_malloc_framework/common/provider.hpp"
#include "ur_pool_manager.hpp"

#include <uur/fixtures.h>
#include "../unified_malloc_framework/common/provider.h"
#include "pool.hpp"

#include <unordered_set>
#include <uur/fixtures.h>

struct urUsmPoolManagerTest
struct urUsmPoolDescriptorTest
: public uur::urMultiDeviceContextTest,
::testing::WithParamInterface<ur_usm_pool_handle_t> {};

TEST_P(urUsmPoolManagerTest, poolIsPerContextTypeAndDevice) {
TEST_P(urUsmPoolDescriptorTest, poolIsPerContextTypeAndDevice) {
auto &devices = uur::DevicesEnvironment::instance->devices;
auto poolHandle = this->GetParam();

Expand Down Expand Up @@ -49,7 +48,69 @@ 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 : public uur::urContextTest {
void SetUp() override {
UUR_RETURN_ON_FATAL_FAILURE(urContextTest::SetUp());
auto [ret, descs] = usm::pool_descriptor::create(nullptr, context);
ASSERT_EQ(ret, UR_RESULT_SUCCESS);
poolDescriptors = descs;
}

std::vector<usm::pool_descriptor> poolDescriptors;
};

TEST_P(urUsmPoolManagerTest, poolManagerPopulate) {
auto [ret, manager] = usm::pool_manager<usm::pool_descriptor>::create();
ASSERT_EQ(ret, UMF_RESULT_SUCCESS);

for (auto &desc : poolDescriptors) {
// Populate the pool manager
auto provider = nullProviderCreate();
auto poolUnique = usm_test::nullPoolCreate(&provider);
ASSERT_NE(poolUnique, nullptr);
ret = manager.addPool(desc, poolUnique);
ASSERT_EQ(ret, UMF_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);
}
}

TEST_P(urUsmPoolManagerTest, poolManagerInsertExisting) {
auto [ret, manager] = usm::pool_manager<usm::pool_descriptor>::create();
ASSERT_EQ(ret, UMF_RESULT_SUCCESS);

auto desc = poolDescriptors[0];

auto provider = nullProviderCreate();
auto poolUnique = usm_test::nullPoolCreate(&provider);
ASSERT_NE(poolUnique, nullptr);

ret = manager.addPool(desc, poolUnique);
ASSERT_EQ(ret, UMF_RESULT_SUCCESS);

// Inserting an existing key should return an error
ret = manager.addPool(desc, poolUnique);
ASSERT_EQ(ret, UMF_RESULT_ERROR_INVALID_ARGUMENT);
}

TEST_P(urUsmPoolManagerTest, poolManagerGetNonexistant) {
auto [ret, manager] = usm::pool_manager<usm::pool_descriptor>::create();
ASSERT_EQ(ret, UMF_RESULT_SUCCESS);

for (auto &desc : poolDescriptors) {
auto hPool = manager.getPool(desc);
ASSERT_FALSE(hPool.has_value());
}
}

UUR_INSTANTIATE_DEVICE_TEST_SUITE_P(urUsmPoolManagerTest);

0 comments on commit a970190

Please sign in to comment.