From 2b0bb2c5f3163e0a30bd33616b10be809536719c Mon Sep 17 00:00:00 2001 From: Hartmut Kaiser Date: Mon, 16 Sep 2024 19:12:17 -0500 Subject: [PATCH] Factoring out allocator cache --- libs/core/allocator_support/CMakeLists.txt | 5 +- .../thread_local_caching_allocator.hpp | 161 +++++++++--------- .../src/thread_local_caching_allocator.cpp | 120 +++++++++++++ libs/core/async_base/CMakeLists.txt | 4 +- .../include/hpx/async_base/dataflow.hpp | 9 +- libs/core/async_combinators/CMakeLists.txt | 3 +- .../hpx/async_combinators/when_all.hpp | 4 +- .../include/hpx/concurrency/stack.hpp | 8 +- libs/core/concurrency/tests/unit/stack.cpp | 8 +- libs/core/execution/CMakeLists.txt | 3 +- .../hpx/execution/detail/future_exec.hpp | 9 +- libs/core/executors/CMakeLists.txt | 3 +- .../hpx/executors/parallel_executor.hpp | 9 +- libs/core/futures/CMakeLists.txt | 3 +- .../futures/include/hpx/futures/future.hpp | 37 ++-- .../include/hpx/futures/futures_factory.hpp | 7 +- .../hpx/futures/packaged_continuation.hpp | 4 +- libs/core/lcos_local/CMakeLists.txt | 3 +- .../include/hpx/lcos_local/and_gate.hpp | 7 +- libs/full/async_distributed/CMakeLists.txt | 2 +- .../detail/async_implementations.hpp | 48 +++--- 21 files changed, 267 insertions(+), 190 deletions(-) create mode 100644 libs/core/allocator_support/src/thread_local_caching_allocator.cpp diff --git a/libs/core/allocator_support/CMakeLists.txt b/libs/core/allocator_support/CMakeLists.txt index 1e5bcb0f10ab..9464a7dd3392 100644 --- a/libs/core/allocator_support/CMakeLists.txt +++ b/libs/core/allocator_support/CMakeLists.txt @@ -42,7 +42,7 @@ set(allocator_support_compat_headers ) # cmake-format: on -set(allocator_support_sources) +set(allocator_support_sources thread_local_caching_allocator.cpp) include(HPX_AddModule) add_hpx_module( @@ -52,6 +52,7 @@ add_hpx_module( HEADERS ${allocator_support_headers} COMPAT_HEADERS ${allocator_support_compat_headers} DEPENDENCIES hpx_dependencies_allocator - MODULE_DEPENDENCIES hpx_concepts hpx_config hpx_preprocessor hpx_type_support + MODULE_DEPENDENCIES hpx_assertion hpx_concepts hpx_config hpx_preprocessor + hpx_type_support CMAKE_SUBDIRS examples tests ) diff --git a/libs/core/allocator_support/include/hpx/allocator_support/thread_local_caching_allocator.hpp b/libs/core/allocator_support/include/hpx/allocator_support/thread_local_caching_allocator.hpp index 0afd89a7c3fe..144542ba1b40 100644 --- a/libs/core/allocator_support/include/hpx/allocator_support/thread_local_caching_allocator.hpp +++ b/libs/core/allocator_support/include/hpx/allocator_support/thread_local_caching_allocator.hpp @@ -8,8 +8,10 @@ #include #include +#include #include +#include #include #include #include @@ -21,15 +23,41 @@ namespace hpx::util { !((defined(HPX_HAVE_CUDA) && defined(__CUDACC__)) || \ defined(HPX_HAVE_HIP)) + namespace detail { + + HPX_CORE_EXPORT void init_allocator_cache( + std::size_t, std::function&& clear_cache); + HPX_CORE_EXPORT std::pair allocate_from_cache( + std::size_t) noexcept; + [[nodiscard]] HPX_CORE_EXPORT bool cache_empty(std::size_t) noexcept; + HPX_CORE_EXPORT void return_to_cache( + std::size_t, void* p, std::size_t n) noexcept; + + // maximal number of caches [0...max) + inline constexpr int max_number_of_caches = 16; + + /////////////////////////////////////////////////////////////////////// + constexpr int next_power_of_two(std::int64_t n) noexcept + { + int i = 0; + for (--n; n > 0; n >>= 1) + { + ++i; + } + return i; + } + } // namespace detail + /////////////////////////////////////////////////////////////////////////// - template