-
-
Notifications
You must be signed in to change notification settings - Fork 430
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
Use thread-safe cache in thread_local_caching_allocator #6539
Open
Pansysk75
wants to merge
4
commits into
STEllAR-GROUP:master
Choose a base branch
from
Pansysk75:fix-thread-local-caching-allocator
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 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 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 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
123 changes: 123 additions & 0 deletions
123
libs/core/allocator_support/src/thread_local_caching_allocator.cpp
This file contains 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,123 @@ | ||
// Copyright (c) 2023-2024 Hartmut Kaiser | ||
// | ||
// SPDX-License-Identifier: BSL-1.0 | ||
// Distributed under the Boost Software License, Version 1.0. (See accompanying | ||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
|
||
#include <hpx/config.hpp> | ||
#include <hpx/allocator_support/config/defines.hpp> | ||
|
||
#if defined(HPX_ALLOCATOR_SUPPORT_HAVE_CACHING) && \ | ||
!((defined(HPX_HAVE_CUDA) && defined(__CUDACC__)) || \ | ||
defined(HPX_HAVE_HIP)) | ||
|
||
#include <hpx/allocator_support/thread_local_caching_allocator.hpp> | ||
#include <hpx/assert.hpp> | ||
#include <hpx/type_support/static_reinit_interface.hpp> | ||
|
||
#include <cstddef> | ||
#include <functional> | ||
#include <stack> | ||
#include <utility> | ||
|
||
namespace hpx::util::detail { | ||
|
||
/////////////////////////////////////////////////////////////////////////// | ||
struct allocated_cache | ||
{ | ||
explicit allocated_cache() noexcept = default; | ||
|
||
void init(std::function<void()>&& clear) | ||
{ | ||
if (!clear_cache) // initialize once | ||
{ | ||
clear_cache = HPX_MOVE(clear); | ||
util::reinit_register(std::function<void()>(), clear_cache); | ||
} | ||
} | ||
|
||
allocated_cache(allocated_cache const&) = delete; | ||
allocated_cache(allocated_cache&&) = delete; | ||
allocated_cache& operator=(allocated_cache const&) = delete; | ||
allocated_cache& operator=(allocated_cache&&) = delete; | ||
|
||
~allocated_cache() | ||
{ | ||
if (clear_cache) | ||
{ | ||
clear_cache(); | ||
} | ||
} | ||
|
||
std::pair<void*, std::size_t> allocate() noexcept | ||
{ | ||
std::pair<void*, std::size_t> p{nullptr, 0}; | ||
if (!data.empty()) | ||
{ | ||
p = data.top(); | ||
data.pop(); | ||
|
||
++allocated; | ||
} | ||
return p; | ||
} | ||
|
||
void deallocate(void* p, std::size_t n) | ||
{ | ||
data.emplace(p, n); | ||
if (++deallocated > 2 * (allocated + 16)) | ||
{ | ||
if (clear_cache) | ||
{ | ||
clear_cache(); | ||
} | ||
|
||
allocated = 0; | ||
deallocated = 0; | ||
} | ||
} | ||
|
||
[[nodiscard]] bool empty() const noexcept | ||
{ | ||
return data.empty(); | ||
} | ||
|
||
private: | ||
std::stack<std::pair<void*, std::size_t>> data; | ||
std::size_t allocated = 0; | ||
std::size_t deallocated = 0; | ||
std::function<void()> clear_cache; | ||
}; | ||
|
||
/////////////////////////////////////////////////////////////////////////// | ||
allocated_cache& cache(std::size_t n) | ||
{ | ||
HPX_ASSERT(n < max_number_of_caches); | ||
|
||
thread_local allocated_cache allocated_data[max_number_of_caches]; | ||
return allocated_data[n]; | ||
} | ||
|
||
void init_allocator_cache( | ||
std::size_t n, std::function<void()>&& clear_cache) | ||
{ | ||
cache(n).init(HPX_MOVE(clear_cache)); | ||
} | ||
|
||
std::pair<void*, std::size_t> allocate_from_cache(std::size_t n) noexcept | ||
{ | ||
return cache(n).allocate(); | ||
} | ||
|
||
void return_to_cache(std::size_t n, void* p, std::size_t const size) | ||
{ | ||
cache(n).deallocate(p, size); | ||
} | ||
|
||
bool cache_empty(std::size_t n) noexcept | ||
{ | ||
return cache(n).empty(); | ||
} | ||
} // namespace hpx::util::detail | ||
|
||
#endif |
This file contains 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
Oops, something went wrong.
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.
Check notice
Code scanning / CodeQL
Unused local variable Note