Skip to content

Commit

Permalink
Resolve circular dependency between concurrency and allocator_support…
Browse files Browse the repository at this point in the history
… modules
  • Loading branch information
hkaiser committed Sep 11, 2024
1 parent c008395 commit 459cf67
Show file tree
Hide file tree
Showing 18 changed files with 115 additions and 74 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -8,7 +8,6 @@

#include <hpx/config.hpp>
#include <hpx/allocator_support/config/defines.hpp>
#include <hpx/concurrency/stack.hpp>

#include <cstddef>
#include <memory>
Expand All @@ -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 <typename T = char, typename Allocator = std::allocator<T>>
template <template <typename, typename> class Stack, typename T = char,
typename Allocator = std::allocator<T>>
struct thread_local_caching_allocator
{
HPX_NO_UNIQUE_ADDRESS Allocator alloc;
Expand All @@ -38,7 +39,7 @@ namespace hpx::util {
template <typename U>
struct rebind
{
using other = thread_local_caching_allocator<U,
using other = thread_local_caching_allocator<Stack, U,
typename traits::template rebind_alloc<U>>;
};

Expand Down Expand Up @@ -113,7 +114,7 @@ namespace hpx::util {
}

HPX_NO_UNIQUE_ADDRESS Allocator alloc;
hpx::lockfree::stack<std::pair<T*, size_type>> data;
Stack<std::pair<T*, size_type>, Allocator> data;
std::size_t allocated = 0;
std::size_t deallocated = 0;
};
Expand All @@ -134,7 +135,7 @@ namespace hpx::util {

template <typename U, typename Alloc>
explicit thread_local_caching_allocator(
thread_local_caching_allocator<U, Alloc> const&
thread_local_caching_allocator<Stack, U, Alloc> const&
rhs) noexcept(noexcept(std::
is_nothrow_copy_constructible_v<Alloc>))
: alloc(rhs.alloc)
Expand Down Expand Up @@ -198,7 +199,8 @@ namespace hpx::util {
}
};
#else
template <typename T = char, typename Allocator = std::allocator<T>>
template <template <typename, typename> class Stack, typename T = char,
typename Allocator = std::allocator<T>>
using thread_local_caching_allocator = Allocator;
#endif
} // namespace hpx::util
4 changes: 2 additions & 2 deletions libs/core/async_base/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2020-2022 The STE||AR-Group
# Copyright (c) 2020-2024 The STE||AR-Group
#
# SPDX-License-Identifier: BSL-1.0
# Distributed under the Boost Software License, Version 1.0. (See accompanying
Expand Down Expand Up @@ -34,6 +34,6 @@ add_hpx_module(
COMPAT_HEADERS ${async_base_compat_headers}
SOURCES ${async_base_sources}
MODULE_DEPENDENCIES hpx_allocator_support hpx_concepts hpx_config
hpx_coroutines hpx_tag_invoke
hpx_concurrency hpx_coroutines hpx_tag_invoke
CMAKE_SUBDIRS examples tests
)
9 changes: 6 additions & 3 deletions libs/core/async_base/include/hpx/async_base/dataflow.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2007-2022 Hartmut Kaiser
// Copyright (c) 2007-2024 Hartmut Kaiser
//
// SPDX-License-Identifier: BSL-1.0
// Distributed under the Boost Software License, Version 1.0. (See accompanying
Expand Down Expand Up @@ -29,6 +29,7 @@ namespace hpx {
#else

#include <hpx/config.hpp>
#include <hpx/concurrency/stack.hpp>
#include <hpx/modules/allocator_support.hpp>
#include <hpx/modules/concepts.hpp>
#include <hpx/modules/tag_invoke.hpp>
Expand Down Expand Up @@ -60,12 +61,14 @@ namespace hpx {
// clang-format on
friend constexpr HPX_FORCEINLINE auto tag_fallback_invoke(
dataflow_t tag, F&& f, Ts&&... ts)
-> decltype(tag(hpx::util::thread_local_caching_allocator<char,
-> decltype(tag(hpx::util::thread_local_caching_allocator<
hpx::lockfree::fixed_size_stack, char,
hpx::util::internal_allocator<>>{},
HPX_FORWARD(F, f), HPX_FORWARD(Ts, ts)...))
{
using allocator_type =
hpx::util::thread_local_caching_allocator<char,
hpx::util::thread_local_caching_allocator<
hpx::lockfree::fixed_size_stack, char,
hpx::util::internal_allocator<>>;
return hpx::functional::tag_invoke(tag, allocator_type{},
HPX_FORWARD(F, f), HPX_FORWARD(Ts, ts)...);
Expand Down
3 changes: 2 additions & 1 deletion libs/core/async_combinators/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2019-2023 The STE||AR-Group
# Copyright (c) 2019-2024 The STE||AR-Group
#
# SPDX-License-Identifier: BSL-1.0
# Distributed under the Boost Software License, Version 1.0. (See accompanying
Expand Down Expand Up @@ -43,6 +43,7 @@ add_hpx_module(
EXCLUDE_FROM_GLOBAL_HEADER "hpx/async_combinators/future_wait.hpp"
MODULE_DEPENDENCIES
hpx_async_base
hpx_concurrency
hpx_config
hpx_errors
hpx_futures
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2007-2022 Hartmut Kaiser
// Copyright (c) 2007-2024 Hartmut Kaiser
// Copyright (c) 2013 Agustin Berge
// Copyright (c) 2017 Denis Blank
//
Expand Down Expand Up @@ -131,6 +131,7 @@ namespace hpx {
#include <hpx/config.hpp>
#include <hpx/allocator_support/internal_allocator.hpp>
#include <hpx/allocator_support/thread_local_caching_allocator.hpp>
#include <hpx/concurrency/stack.hpp>
#include <hpx/datastructures/tuple.hpp>
#include <hpx/functional/tag_invoke.hpp>
#include <hpx/futures/detail/future_data.hpp>
Expand Down Expand Up @@ -204,7 +205,7 @@ namespace hpx::lcos::detail {
template <typename T, typename N>
auto operator()(hpx::util::async_traverse_detach_tag, T&& current,
N&& next) -> decltype(async_detach_future(HPX_FORWARD(T, current),
HPX_FORWARD(N, next)))
HPX_FORWARD(N, next)))
{
return async_detach_future(
HPX_FORWARD(T, current), HPX_FORWARD(N, next));
Expand All @@ -226,7 +227,8 @@ namespace hpx::lcos::detail {
using frame_type = async_when_all_frame<result_type>;
using no_addref = typename frame_type::base_type::init_no_addref;

using allocator_type = hpx::util::thread_local_caching_allocator<char,
using allocator_type = hpx::util::thread_local_caching_allocator<
hpx::lockfree::fixed_size_stack, char,
hpx::util::internal_allocator<>>;
auto frame = hpx::util::traverse_pack_async_allocator(allocator_type{},
hpx::util::async_traverse_in_place_tag<frame_type>{}, no_addref{},
Expand Down
8 changes: 7 additions & 1 deletion libs/core/concurrency/include/hpx/concurrency/stack.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (C) 2008-2013 Tim Blechmann
// Copyright (c) 2022-2023 Hartmut Kaiser
// Copyright (c) 2022-2024 Hartmut Kaiser
//
// SPDX-License-Identifier: BSL-1.0
// Distributed under the Boost Software License, Version 1.0. (See accompanying
Expand Down Expand Up @@ -770,4 +770,10 @@ namespace hpx::lockfree {

pool_t pool;
};

template <typename T, typename Allocator = std::allocator<T>>
class fixed_size_stack : public stack<T, Allocator>
{
using stack<T, Allocator>::stack;
};
} // namespace hpx::lockfree
3 changes: 2 additions & 1 deletion libs/core/execution/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2019-2022 The STE||AR-Group
# Copyright (c) 2019-2024 The STE||AR-Group
#
# SPDX-License-Identifier: BSL-1.0
# Distributed under the Boost Software License, Version 1.0. (See accompanying
Expand Down Expand Up @@ -160,6 +160,7 @@ add_hpx_module(
MODULE_DEPENDENCIES
hpx_async_base
hpx_async_combinators
hpx_concurrency
hpx_config
hpx_threading
hpx_pack_traversal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2007-2022 Hartmut Kaiser
// Copyright (c) 2007-2024 Hartmut Kaiser
// Copyright (c) 2013 Agustin Berge
//
// SPDX-License-Identifier: BSL-1.0
Expand All @@ -13,6 +13,7 @@
#include <hpx/assert.hpp>
#include <hpx/async_base/launch_policy.hpp>
#include <hpx/async_base/traits/is_launch_policy.hpp>
#include <hpx/concurrency/stack.hpp>
#include <hpx/coroutines/thread_enums.hpp>
#include <hpx/execution/traits/executor_traits.hpp>
#include <hpx/execution/traits/future_then_result_exec.hpp>
Expand Down Expand Up @@ -64,9 +65,9 @@ namespace hpx::lcos::detail {
using continuation_result_type =
hpx::util::invoke_result_t<F, Future>;

using allocator_type =
hpx::util::thread_local_caching_allocator<char,
hpx::util::internal_allocator<>>;
using allocator_type = hpx::util::thread_local_caching_allocator<
hpx::lockfree::fixed_size_stack, char,
hpx::util::internal_allocator<>>;

hpx::traits::detail::shared_state_ptr_t<result_type> p =
detail::make_continuation_alloc<continuation_result_type>(
Expand Down
3 changes: 2 additions & 1 deletion libs/core/executors/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2020-2022 The STE||AR-Group
# Copyright (c) 2020-2024 The STE||AR-Group
#
# SPDX-License-Identifier: BSL-1.0
# Distributed under the Boost Software License, Version 1.0. (See accompanying
Expand Down Expand Up @@ -97,6 +97,7 @@ add_hpx_module(
hpx_allocator_support
hpx_async_base
hpx_concepts
hpx_concurrency
hpx_config
hpx_errors
hpx_execution
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2019-2020 ETH Zurich
// Copyright (c) 2007-2023 Hartmut Kaiser
// Copyright (c) 2007-2024 Hartmut Kaiser
// Copyright (c) 2019 Agustin Berge
//
// SPDX-License-Identifier: BSL-1.0
Expand All @@ -12,6 +12,7 @@
#include <hpx/allocator_support/internal_allocator.hpp>
#include <hpx/allocator_support/thread_local_caching_allocator.hpp>
#include <hpx/async_base/launch_policy.hpp>
#include <hpx/concurrency/stack.hpp>
#include <hpx/execution/detail/async_launch_policy_dispatch.hpp>
#include <hpx/execution/detail/future_exec.hpp>
#include <hpx/execution/detail/post_policy_dispatch.hpp>
Expand Down Expand Up @@ -389,9 +390,9 @@ namespace hpx::execution {
hpx::bind_back(HPX_FORWARD(F, f), HPX_FORWARD(Ts, ts)...));
#endif

using allocator_type =
hpx::util::thread_local_caching_allocator<char,
hpx::util::internal_allocator<>>;
using allocator_type = hpx::util::thread_local_caching_allocator<
hpx::lockfree::fixed_size_stack, char,
hpx::util::internal_allocator<>>;
hpx::traits::detail::shared_state_ptr_t<result_type> p =
lcos::detail::make_continuation_alloc_nounwrap<result_type>(
allocator_type{}, HPX_FORWARD(Future, predecessor),
Expand Down
3 changes: 2 additions & 1 deletion libs/core/futures/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2019-2022 The STE||AR-Group
# Copyright (c) 2019-2024 The STE||AR-Group
#
# SPDX-License-Identifier: BSL-1.0
# Distributed under the Boost Software License, Version 1.0. (See accompanying
Expand Down Expand Up @@ -68,6 +68,7 @@ add_hpx_module(
hpx_assertion
hpx_async_base
hpx_concepts
hpx_concurrency
hpx_config
hpx_errors
hpx_functional
Expand Down
37 changes: 23 additions & 14 deletions libs/core/futures/include/hpx/futures/future.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <hpx/assert.hpp>
#include <hpx/async_base/launch_policy.hpp>
#include <hpx/concepts/concepts.hpp>
#include <hpx/concurrency/stack.hpp>
#include <hpx/errors/try_catch_exception_ptr.hpp>
#include <hpx/functional/detail/invoke.hpp>
#include <hpx/functional/experimental/scope_exit.hpp>
Expand Down Expand Up @@ -378,13 +379,14 @@ namespace hpx::lcos::detail {
template <typename F>
static auto then(Derived&& fut, F&& f, error_code& ec = throws)
-> decltype(future_then_dispatch<std::decay_t<F>>::call_alloc(
hpx::util::thread_local_caching_allocator<char,
hpx::util::thread_local_caching_allocator<
hpx::lockfree::fixed_size_stack, char,
hpx::util::internal_allocator<>>{},
HPX_MOVE(fut), HPX_FORWARD(F, f)))
{
using allocator_type =
hpx::util::thread_local_caching_allocator<char,
hpx::util::internal_allocator<>>;
using allocator_type = hpx::util::thread_local_caching_allocator<
hpx::lockfree::fixed_size_stack, char,
hpx::util::internal_allocator<>>;

using result_type =
decltype(future_then_dispatch<std::decay_t<F>>::call_alloc(
Expand All @@ -404,13 +406,14 @@ namespace hpx::lcos::detail {
template <typename F, typename T0>
static auto then(Derived&& fut, T0&& t0, F&& f, error_code& ec = throws)
-> decltype(future_then_dispatch<std::decay_t<T0>>::call_alloc(
hpx::util::thread_local_caching_allocator<char,
hpx::util::thread_local_caching_allocator<
hpx::lockfree::fixed_size_stack, char,
hpx::util::internal_allocator<>>{},
HPX_MOVE(fut), HPX_FORWARD(T0, t0), HPX_FORWARD(F, f)))
{
using allocator_type =
hpx::util::thread_local_caching_allocator<char,
hpx::util::internal_allocator<>>;
using allocator_type = hpx::util::thread_local_caching_allocator<
hpx::lockfree::fixed_size_stack, char,
hpx::util::internal_allocator<>>;

using result_type =
decltype(future_then_dispatch<std::decay_t<T0>>::call_alloc(
Expand Down Expand Up @@ -1246,7 +1249,8 @@ namespace hpx {
std::is_constructible_v<T, Ts&&...> || std::is_void_v<T>, future<T>>
make_ready_future(Ts&&... ts)
{
using allocator_type = hpx::util::thread_local_caching_allocator<char,
using allocator_type = hpx::util::thread_local_caching_allocator<
hpx::lockfree::fixed_size_stack, char,
hpx::util::internal_allocator<>>;
return make_ready_future_alloc<T>(
allocator_type{}, HPX_FORWARD(Ts, ts)...);
Expand All @@ -1270,7 +1274,8 @@ namespace hpx {
HPX_FORCEINLINE future<hpx::util::decay_unwrap_t<T>> make_ready_future(
T&& init)
{
using allocator_type = hpx::util::thread_local_caching_allocator<char,
using allocator_type = hpx::util::thread_local_caching_allocator<
hpx::lockfree::fixed_size_stack, char,
hpx::util::internal_allocator<>>;
return hpx::make_ready_future_alloc<hpx::util::decay_unwrap_t<T>>(
allocator_type{}, HPX_FORWARD(T, init));
Expand Down Expand Up @@ -1351,7 +1356,8 @@ namespace hpx {
// extension: create a pre-initialized future object
HPX_FORCEINLINE future<void> make_ready_future()
{
using allocator_type = hpx::util::thread_local_caching_allocator<char,
using allocator_type = hpx::util::thread_local_caching_allocator<
hpx::lockfree::fixed_size_stack, char,
hpx::util::internal_allocator<>>;
return make_ready_future_alloc<void>(allocator_type{}, util::unused);
}
Expand Down Expand Up @@ -1593,7 +1599,8 @@ namespace hpx::lcos {
std::enable_if_t<std::is_constructible_v<T, Ts&&...> || std::is_void_v<T>,
hpx::future<T>> make_ready_future(Ts&&... ts)
{
using allocator_type = hpx::util::thread_local_caching_allocator<char,
using allocator_type = hpx::util::thread_local_caching_allocator<
hpx::lockfree::fixed_size_stack, char,
hpx::util::internal_allocator<>>;
return hpx::make_ready_future_alloc<T>(
allocator_type{}, HPX_FORWARD(Ts, ts)...);
Expand All @@ -1616,7 +1623,8 @@ namespace hpx::lcos {
"hpx::make_ready_future instead.")
hpx::future<hpx::util::decay_unwrap_t<T>> make_ready_future(T&& init)
{
using allocator_type = hpx::util::thread_local_caching_allocator<char,
using allocator_type = hpx::util::thread_local_caching_allocator<
hpx::lockfree::fixed_size_stack, char,
hpx::util::internal_allocator<>>;
return hpx::make_ready_future_alloc<hpx::util::decay_unwrap_t<T>>(
allocator_type{}, HPX_FORWARD(T, init));
Expand Down Expand Up @@ -1675,7 +1683,8 @@ namespace hpx::lcos {
"hpx::make_ready_future instead.")
inline hpx::future<void> make_ready_future()
{
using allocator_type = hpx::util::thread_local_caching_allocator<char,
using allocator_type = hpx::util::thread_local_caching_allocator<
hpx::lockfree::fixed_size_stack, char,
hpx::util::internal_allocator<>>;
return hpx::make_ready_future_alloc<void>(
allocator_type{}, util::unused);
Expand Down
Loading

0 comments on commit 459cf67

Please sign in to comment.