-
-
Notifications
You must be signed in to change notification settings - Fork 440
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
18 changed files
with
210 additions
and
165 deletions.
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
111 changes: 111 additions & 0 deletions
111
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,111 @@ | ||
// 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 <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) noexcept | ||
{ | ||
if (!clear_cache) // initialize once | ||
{ | ||
clear_cache = HPX_MOVE(clear); | ||
} | ||
} | ||
|
||
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() | ||
{ | ||
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) noexcept | ||
{ | ||
data.emplace(p, n); | ||
if (++deallocated > 2 * (allocated + 16)) | ||
{ | ||
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() | ||
{ | ||
thread_local allocated_cache allocated_data; | ||
return allocated_data; | ||
} | ||
|
||
void init_allocator_cache(std::function<void()>&& clear_cache) | ||
{ | ||
cache().init(HPX_MOVE(clear_cache)); | ||
} | ||
|
||
std::pair<void*, std::size_t> allocate_from_cache() noexcept | ||
{ | ||
return cache().allocate(); | ||
} | ||
|
||
void return_to_cache(void* p, std::size_t const n) noexcept | ||
{ | ||
cache().deallocate(p, n); | ||
} | ||
|
||
bool cache_empty() noexcept | ||
{ | ||
return cache().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
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
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
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.