From 60ad8e4507511059acbbe0a2c51afe7db8f2ef60 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 | 163 +++++++++--------- .../src/thread_local_caching_allocator.cpp | 123 +++++++++++++ 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 +- .../src/init_runtime_local.cpp | 6 +- libs/core/lcos_local/CMakeLists.txt | 3 +- .../include/hpx/lcos_local/and_gate.hpp | 7 +- libs/core/static_reinit/CMakeLists.txt | 5 +- .../static_reinit/reinitializable_static.hpp | 6 +- .../hpx/static_reinit/static_reinit.hpp | 19 +- libs/core/static_reinit/src/static_reinit.cpp | 39 ++++- libs/core/type_support/CMakeLists.txt | 6 +- .../detail/static_reinit_functions.hpp | 24 +++ .../type_support/static_reinit_interface.hpp | 28 +++ .../src/detail/static_reinit_functions.cpp | 24 +++ .../src/static_reinit_interface.cpp | 36 ++++ libs/full/async_distributed/CMakeLists.txt | 2 +- .../detail/async_implementations.hpp | 48 +++--- libs/full/init_runtime/src/hpx_init.cpp | 2 + 32 files changed, 435 insertions(+), 222 deletions(-) create mode 100644 libs/core/allocator_support/src/thread_local_caching_allocator.cpp create mode 100644 libs/core/type_support/include/hpx/type_support/detail/static_reinit_functions.hpp create mode 100644 libs/core/type_support/include/hpx/type_support/static_reinit_interface.hpp create mode 100644 libs/core/type_support/src/detail/static_reinit_functions.cpp create mode 100644 libs/core/type_support/src/static_reinit_interface.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..ba9af8a4ae5a 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); + + // 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