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