Skip to content

Reimplement base_alloc test in cpp #208

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,14 @@ if(UMF_BUILD_OS_MEMORY_PROVIDER AND LINUX) # OS-specific functions are implement
LIBS umf_utils numa)
endif()

# TODO: add Windows tests
if(LINUX)
# the base_alloc test uses linux pthreads
add_umf_test(NAME base_alloc
SRCS ${BA_SOURCES} test_base_alloc.c
LIBS umf_utils)
add_umf_test(NAME base_alloc_linear
SRCS ${BA_SOURCES} test_base_alloc_linear.c
LIBS umf_utils)
if(UMF_BUILD_SHARED_LIBRARY)
# if build as shared library, ba symbols won't be visible in tests
set(BA_SOURCES_FOR_TEST ${BA_SOURCES})
endif()

add_umf_test(NAME base_alloc
SRCS ${BA_SOURCES_FOR_TEST} test_base_alloc.cpp
LIBS umf_utils)
add_umf_test(NAME base_alloc_linear
SRCS ${BA_SOURCES_FOR_TEST} test_base_alloc_linear.cpp
LIBS umf_utils)
73 changes: 0 additions & 73 deletions test/test_base_alloc.c

This file was deleted.

53 changes: 53 additions & 0 deletions test/test_base_alloc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (C) 2024 Intel Corporation
*
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/

#include <cstdio>
#include <cstdlib>
#include <thread>

#include "base_alloc.h"

#include "base.hpp"
#include "test_helpers.h"

using umf_test::test;

TEST_F(test, baseAllocMultiThreadedAllocMemset) {
static constexpr int NTHREADS = 10;
static constexpr int ITERATIONS = 1000;
static constexpr int ALLOCATION_SIZE = 16;

auto pool = std::shared_ptr<umf_ba_pool_t>(umf_ba_create(ALLOCATION_SIZE),
umf_ba_destroy);

auto poolAlloc = [](int TID, umf_ba_pool_t *pool) {
std::vector<std::shared_ptr<unsigned char>> ptrs;

for (int i = 0; i < ITERATIONS; i++) {
ptrs.emplace_back(
(unsigned char *)umf_ba_alloc(pool),
[pool = pool](unsigned char *ptr) { umf_ba_free(pool, ptr); });

memset(ptrs.back().get(), (i + TID) & 0xFF, ALLOCATION_SIZE);
}

for (int i = 0; i < ITERATIONS; i++) {
for (int k = 0; k < ALLOCATION_SIZE; k++) {
UT_ASSERTeq(*(ptrs[i].get() + k), ((i + TID) & 0xFF));
}
}
};

std::vector<std::thread> threads;
for (int i = 0; i < NTHREADS; i++) {
threads.emplace_back(poolAlloc, i, pool.get());
}

for (auto &thread : threads) {
thread.join();
}
}
81 changes: 0 additions & 81 deletions test/test_base_alloc_linear.c

This file was deleted.

61 changes: 61 additions & 0 deletions test/test_base_alloc_linear.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (C) 2024 Intel Corporation
*
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/

#include <cstdio>
#include <cstdlib>
#include <thread>

#include "base_alloc_linear.h"

#include "base.hpp"
#include "test_helpers.h"

using umf_test::test;

TEST_F(test, baseAllocLinearMultiThreadedAllocMemset) {
static constexpr int NTHREADS = 10;
static constexpr int ITERATIONS = 1000;
static constexpr int MAX_ALLOCATION_SIZE = 1024;

srand(0);

auto pool = std::shared_ptr<umf_ba_linear_pool_t>(
umf_ba_linear_create(NTHREADS * ITERATIONS * MAX_ALLOCATION_SIZE),
umf_ba_linear_destroy);

auto poolAlloc = [](int TID, umf_ba_linear_pool_t *pool) {
struct buffer_t {
unsigned char *ptr;
size_t size;
} buffer[ITERATIONS];

for (int i = 0; i < ITERATIONS; i++) {
buffer[i].size =
(size_t)((rand() / (double)RAND_MAX) * MAX_ALLOCATION_SIZE);
buffer[i].ptr =
(unsigned char *)umf_ba_linear_alloc(pool, buffer[i].size);
UT_ASSERTne(buffer[i].ptr, NULL);
memset(buffer[i].ptr, (i + TID) & 0xFF, buffer[i].size);
}

for (int i = 0; i < ITERATIONS; i++) {
UT_ASSERTne(buffer[i].ptr, NULL);
for (size_t k = 0; k < buffer[i].size; k++) {
UT_ASSERTeq(*(buffer[i].ptr + k), (i + TID) & 0xFF);
}
}
};

std::vector<std::thread> threads;
for (int i = 0; i < NTHREADS; i++) {
threads.emplace_back(poolAlloc, i, pool.get());
}

for (auto &thread : threads) {
thread.join();
}
}