From 5ed299c053f3febb60f68cadda017ce948bd6ab2 Mon Sep 17 00:00:00 2001 From: Panos Sysk Date: Fri, 6 Sep 2024 19:34:48 -0500 Subject: [PATCH 1/4] Use thread-safe cache in thread_local_caching_allocator --- .../thread_local_caching_allocator.hpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) 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 f389213a8b53..b1c1ed5e24cc 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,11 +8,11 @@ #include #include +#include #include #include #include -#include #include #include @@ -56,6 +56,7 @@ namespace hpx::util { explicit allocated_cache(Allocator const& a) noexcept( noexcept(std::is_nothrow_copy_constructible_v)) : alloc(a) + , data(0) { } @@ -82,8 +83,9 @@ namespace hpx::util { } else { - p = data.top().first; - data.pop(); + std::pair pair; + data.pop(pair); + p = pair.first; } ++allocated; @@ -104,16 +106,15 @@ namespace hpx::util { private: void clear_cache() noexcept { - while (!data.empty()) + std::pair p; + while (data.pop(p)) { - traits::deallocate( - alloc, data.top().first, data.top().second); - data.pop(); + traits::deallocate(alloc, p.first, p.second); } } HPX_NO_UNIQUE_ADDRESS Allocator alloc; - std::stack> data; + hpx::lockfree::stack> data; std::size_t allocated = 0; std::size_t deallocated = 0; }; From c00839515557d52553219dce83554efba76dee7f Mon Sep 17 00:00:00 2001 From: Pansysk75 Date: Mon, 9 Sep 2024 12:15:09 -0500 Subject: [PATCH 2/4] Fix race condition --- .../thread_local_caching_allocator.hpp | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) 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 b1c1ed5e24cc..1dec447e3041 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 @@ -73,7 +73,12 @@ namespace hpx::util { pointer allocate(size_type n) { pointer p; - if (data.empty()) + std::pair pair; + if (data.pop(pair)) + { + p = pair.first; + } + else { p = traits::allocate(alloc, n); if (p == nullptr) @@ -81,12 +86,6 @@ namespace hpx::util { throw std::bad_alloc(); } } - else - { - std::pair pair; - data.pop(pair); - p = pair.first; - } ++allocated; return p; From f8d92b09381ce5d18a64f1908465fe99e94d5a1f Mon Sep 17 00:00:00 2001 From: Hartmut Kaiser Date: Wed, 11 Sep 2024 17:52:46 -0500 Subject: [PATCH 3/4] Resolve circular dependency between concurrency and allocator_support modules --- .../thread_local_caching_allocator.hpp | 16 ++++--- 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 | 10 ++-- .../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 | 12 +++-- 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 ++++++++++--------- 19 files changed, 122 insertions(+), 79 deletions(-) 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 1dec447e3041..0afd89a7c3fe 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 @@ -1,4 +1,4 @@ -// Copyright (c) 2023 Hartmut Kaiser +// Copyright (c) 2023-2024 Hartmut Kaiser // // SPDX-License-Identifier: BSL-1.0 // Distributed under the Boost Software License, Version 1.0. (See accompanying @@ -8,7 +8,6 @@ #include #include -#include #include #include @@ -21,8 +20,10 @@ namespace hpx::util { #if defined(HPX_ALLOCATOR_SUPPORT_HAVE_CACHING) && \ !((defined(HPX_HAVE_CUDA) && defined(__CUDACC__)) || \ defined(HPX_HAVE_HIP)) + /////////////////////////////////////////////////////////////////////////// - template > + template