-
Notifications
You must be signed in to change notification settings - Fork 35
Add MT benchmarks #204
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
Add MT benchmarks #204
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/* | ||
* | ||
* 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 "multithread.hpp" | ||
|
||
#include <umf/memory_pool.h> | ||
#include <umf/pools/pool_disjoint.h> | ||
#include <umf/pools/pool_jemalloc.h> | ||
#include <umf/pools/pool_scalable.h> | ||
#include <umf/providers/provider_os_memory.h> | ||
|
||
#include <iostream> | ||
#include <memory> | ||
#include <numeric> | ||
|
||
struct bench_params { | ||
// bench_params() = default; | ||
size_t n_repeats = 5; | ||
size_t n_iterations = 50000; | ||
size_t n_threads = 20; | ||
size_t alloc_size = 64; | ||
}; | ||
|
||
using poolCreateExtParams = std::tuple<umf_memory_pool_ops_t *, void *, | ||
umf_memory_provider_ops_t *, void *>; | ||
|
||
static auto poolCreateExtUnique(poolCreateExtParams params) { | ||
bratpiorka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
umf_memory_pool_handle_t hPool; | ||
auto [pool_ops, pool_params, provider_ops, provider_params] = params; | ||
|
||
umf_memory_provider_handle_t provider = nullptr; | ||
auto ret = | ||
umfMemoryProviderCreate(provider_ops, provider_params, &provider); | ||
if (ret != UMF_RESULT_SUCCESS) { | ||
std::cerr << "provider create failed" << std::endl; | ||
abort(); | ||
} | ||
|
||
ret = umfPoolCreate(pool_ops, provider, pool_params, | ||
UMF_POOL_CREATE_FLAG_OWN_PROVIDER, &hPool); | ||
if (ret != UMF_RESULT_SUCCESS) { | ||
std::cerr << "pool create failed" << std::endl; | ||
abort(); | ||
} | ||
|
||
return std::shared_ptr<umf_memory_pool_t>(hPool, &umfPoolDestroy); | ||
} | ||
|
||
static void mt_alloc_free(poolCreateExtParams params, | ||
const bench_params &bench = bench_params()) { | ||
auto pool = poolCreateExtUnique(params); | ||
|
||
std::vector<std::vector<void *>> allocs(bench.n_threads); | ||
std::vector<size_t> numFailures(bench.n_threads); | ||
for (auto &v : allocs) { | ||
v.reserve(bench.n_iterations); | ||
} | ||
|
||
auto values = umf_bench::measure<std::chrono::milliseconds>( | ||
bench.n_repeats, bench.n_threads, | ||
[&, pool = pool.get()](auto thread_id) { | ||
for (int i = 0; i < bench.n_iterations; i++) { | ||
allocs[thread_id].push_back( | ||
umfPoolMalloc(pool, bench.alloc_size)); | ||
if (!allocs[thread_id].back()) { | ||
numFailures[thread_id]++; | ||
} | ||
} | ||
|
||
for (int i = 0; i < bench.n_iterations; i++) { | ||
umfPoolFree(pool, allocs[thread_id][i]); | ||
} | ||
|
||
// clear the vector as this function might be called multiple times | ||
allocs[thread_id].clear(); | ||
}); | ||
|
||
std::cout << "mean: " << umf_bench::mean(values) | ||
ldorau marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<< " [ms] std_dev: " << umf_bench::std_dev(values) << " [ms]" | ||
<< " (total alloc failures: " | ||
<< std::accumulate(numFailures.begin(), numFailures.end(), 0) | ||
<< " out of " | ||
<< bench.n_iterations * bench.n_repeats * bench.n_threads << ")" | ||
<< std::endl; | ||
} | ||
|
||
int main() { | ||
#if defined(UMF_BUILD_OS_MEMORY_PROVIDER) | ||
PatKamin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
auto osParams = umfOsMemoryProviderParamsDefault(); | ||
#endif | ||
|
||
#if defined(UMF_BUILD_OS_MEMORY_PROVIDER) && \ | ||
defined(UMF_BUILD_LIBUMF_POOL_SCALABLE) | ||
|
||
// Increase iterations for scalable pool since it runs much faster than the remaining | ||
// ones. | ||
bench_params params; | ||
params.n_iterations *= 20; | ||
|
||
std::cout << "scalable_pool mt_alloc_free: "; | ||
mt_alloc_free(poolCreateExtParams{umfScalablePoolOps(), nullptr, | ||
umfOsMemoryProviderOps(), &osParams}, | ||
params); | ||
#else | ||
std::cout << "skipping scalable_pool mt_alloc_free" << std::endl; | ||
#endif | ||
|
||
#if defined(UMF_BUILD_OS_MEMORY_PROVIDER) && \ | ||
defined(UMF_BUILD_LIBUMF_POOL_JEMALLOC) | ||
std::cout << "jemalloc_pool mt_alloc_free: "; | ||
mt_alloc_free(poolCreateExtParams{umfJemallocPoolOps(), nullptr, | ||
umfOsMemoryProviderOps(), &osParams}); | ||
#else | ||
std::cout << "skipping jemalloc_pool mt_alloc_free" << std::endl; | ||
#endif | ||
|
||
bratpiorka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#if defined(UMF_BUILD_OS_MEMORY_PROVIDER) && \ | ||
defined(UMF_BUILD_LIBUMF_POOL_DISJOINT) | ||
auto disjointParams = umfDisjointPoolParamsDefault(); | ||
|
||
std::cout << "disjoint_pool mt_alloc_free: "; | ||
mt_alloc_free(poolCreateExtParams{umfDisjointPoolOps(), &disjointParams, | ||
umfOsMemoryProviderOps(), &osParams}); | ||
#else | ||
std::cout << "skipping disjoint_pool mt_alloc_free" << std::endl; | ||
#endif | ||
|
||
return 0; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.