Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions libs/core/executors/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ set(executors_headers
hpx/executors/execution_policy_parameters.hpp
hpx/executors/execution_policy_scheduling_property.hpp
hpx/executors/execution_policy.hpp
hpx/executors/executor_scheduler.hpp
hpx/executors/executor_scheduler_fwd.hpp
hpx/executors/explicit_scheduler_executor.hpp
hpx/executors/fork_join_executor.hpp
hpx/executors/limiting_executor.hpp
Expand Down
142 changes: 142 additions & 0 deletions libs/core/executors/include/hpx/executors/executor_scheduler.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// Copyright (c) 2026 The STE||AR-Group
//
// SPDX-License-Identifier: BSL-1.0
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

/// \file parallel/executors/executor_scheduler.hpp

#pragma once

#include <hpx/config.hpp>
#include <hpx/executors/executor_scheduler_fwd.hpp>
#include <hpx/modules/errors.hpp>
#include <hpx/modules/execution.hpp>
#include <hpx/modules/execution_base.hpp>

#include <concepts>
#include <exception>
#include <type_traits>
#include <utility>

namespace hpx::execution::experimental {
///////////////////////////////////////////////////////////////////////////
HPX_CXX_CORE_EXPORT template <typename Executor, typename Receiver>
struct executor_operation_state
{
HPX_NO_UNIQUE_ADDRESS std::decay_t<Executor> exec_;
HPX_NO_UNIQUE_ADDRESS std::decay_t<Receiver> receiver_;

template <typename Exec, typename Recv>
executor_operation_state(Exec&& exec, Recv&& recv)
: exec_(HPX_FORWARD(Exec, exec))
, receiver_(HPX_FORWARD(Recv, recv))
{
}

executor_operation_state(executor_operation_state&&) = delete;
executor_operation_state(executor_operation_state const&) = delete;
executor_operation_state& operator=(
executor_operation_state&&) = delete;
executor_operation_state& operator=(
executor_operation_state const&) = delete;

~executor_operation_state() = default;

friend void tag_invoke(start_t, executor_operation_state& os) noexcept
{
hpx::detail::try_catch_exception_ptr(
[&]() {
hpx::parallel::execution::post(os.exec_, [&os]() mutable {
hpx::execution::experimental::set_value(
HPX_MOVE(os.receiver_));
});
},
[&](std::exception_ptr ep) {
hpx::execution::experimental::set_error(
HPX_MOVE(os.receiver_), HPX_MOVE(ep));
});
}
};

///////////////////////////////////////////////////////////////////////////
HPX_CXX_CORE_EXPORT template <typename Executor>
struct executor_sender
{
using sender_concept = hpx::execution::experimental::sender_t;

HPX_NO_UNIQUE_ADDRESS std::decay_t<Executor> exec_;

using completion_signatures =
hpx::execution::experimental::completion_signatures<
hpx::execution::experimental::set_value_t(),
hpx::execution::experimental::set_error_t(std::exception_ptr)>;

template <typename Env>
friend auto tag_invoke(
hpx::execution::experimental::get_completion_signatures_t,
executor_sender const&, Env) noexcept -> completion_signatures;

friend constexpr auto tag_invoke(
hpx::execution::experimental::get_completion_scheduler_t<
hpx::execution::experimental::set_value_t>,
executor_sender const& s) noexcept
{
return executor_scheduler<Executor>{s.exec_};
}

template <typename Receiver>
friend executor_operation_state<Executor, Receiver> tag_invoke(
connect_t, executor_sender&& s, Receiver&& receiver)
{
return {HPX_MOVE(s.exec_), HPX_FORWARD(Receiver, receiver)};
}

template <typename Receiver>
friend executor_operation_state<Executor, Receiver> tag_invoke(
connect_t, executor_sender const& s, Receiver&& receiver)
{
return {s.exec_, HPX_FORWARD(Receiver, receiver)};
}
};

///////////////////////////////////////////////////////////////////////////
HPX_CXX_CORE_EXPORT template <typename Executor>
struct executor_scheduler
{
using executor_type = std::decay_t<Executor>;

HPX_NO_UNIQUE_ADDRESS executor_type exec_;

constexpr executor_scheduler() = default;

template <typename Exec>
requires(!std::is_same_v<std::decay_t<Exec>, executor_scheduler>)
explicit executor_scheduler(Exec&& exec) noexcept
: exec_(HPX_FORWARD(Exec, exec))
{
}

constexpr bool operator==(executor_scheduler const& rhs) const noexcept
{
return exec_ == rhs.exec_;
}

constexpr bool operator!=(executor_scheduler const& rhs) const noexcept
{
return !(*this == rhs);
}

friend executor_sender<Executor> tag_invoke(
schedule_t, executor_scheduler&& sched)
{
return {HPX_MOVE(sched.exec_)};
}

friend executor_sender<Executor> tag_invoke(
schedule_t, executor_scheduler const& sched)
{
return {sched.exec_};
}
};
} // namespace hpx::execution::experimental
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) 2026 The STE||AR-Group
//
// SPDX-License-Identifier: BSL-1.0
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

/// \file parallel/executors/executor_scheduler_fwd.hpp

#pragma once

#include <hpx/config.hpp>

namespace hpx::execution::experimental {

// Forward declarations, see executor_scheduler.hpp
HPX_CXX_CORE_EXPORT template <typename Executor>
struct executor_scheduler;

HPX_CXX_CORE_EXPORT template <typename Executor>
struct executor_sender;

HPX_CXX_CORE_EXPORT template <typename Executor, typename Receiver>
struct executor_operation_state;

} // namespace hpx::execution::experimental
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to place this header into its own directory. It already has _fwd in its name.

Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include <hpx/config.hpp>
#include <hpx/assert.hpp>
#include <hpx/executors/executor_scheduler.hpp>
#include <hpx/executors/parallel_executor.hpp>
#include <hpx/modules/async_base.hpp>
#include <hpx/modules/concurrency.hpp>
Expand Down Expand Up @@ -1240,6 +1241,14 @@ namespace hpx::execution::experimental {
HPX_FORWARD(F, f), HPX_FORWARD(Fs, fs)...);
}

template <typename F, typename... Ts>
friend void tag_invoke(hpx::parallel::execution::post_t,
fork_join_executor const& exec, F&& f, Ts&&... ts)
{
exec.shared_data_->sync_invoke(
HPX_FORWARD(F, f), HPX_FORWARD(Ts, ts)...);
}

template <typename F, typename... Fs>
requires(std::invocable<F> && (std::invocable<Fs> && ...))
friend decltype(auto) tag_invoke(
Expand Down Expand Up @@ -1373,10 +1382,29 @@ namespace hpx::execution::experimental {
/// \endcond
};

// P2300 get_scheduler bridge — defined outside the class so that
// executor_scheduler<fork_join_executor> is fully defined at point of use.
// This allows fork_join_executor to participate in P2300 sender/receiver
// pipelines via schedule(get_scheduler(exec)).
inline auto tag_invoke(hpx::execution::experimental::get_scheduler_t,
fork_join_executor const& exec) noexcept
-> hpx::execution::experimental::executor_scheduler<fork_join_executor>
{
return hpx::execution::experimental::executor_scheduler<
fork_join_executor>(exec);
}

HPX_CXX_CORE_EXPORT HPX_CORE_EXPORT std::ostream& operator<<(
std::ostream& os, fork_join_executor::loop_schedule schedule);

/// \cond NOINTERNAL

template <>
struct is_never_blocking_one_way_executor<fork_join_executor>
: std::true_type
{
};

template <>
struct is_bulk_one_way_executor<fork_join_executor> : std::true_type
{
Expand Down
29 changes: 29 additions & 0 deletions libs/core/executors/tests/unit/fork_join_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <hpx/execution.hpp>
#include <hpx/future.hpp>
#include <hpx/init.hpp>
#include <hpx/modules/executors.hpp>
#include <hpx/modules/testing.hpp>
#include <hpx/thread.hpp>

Expand Down Expand Up @@ -511,11 +512,39 @@ void test_fork_join_static_large_range()
HPX_TEST_EQ(sum.load(), expected_sum);
}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
void test_get_scheduler()
{
namespace ex = hpx::execution::experimental;
namespace tt = hpx::this_thread::experimental;

std::cerr << "test_get_scheduler\n";

// Test 1: get_scheduler returns a valid scheduler
fork_join_executor exec{};
auto sched = ex::get_scheduler(exec);

// Test 2: scheduler can be used to schedule work via then + sync_wait
auto result = hpx::get<0>(
*(tt::sync_wait(ex::then(ex::schedule(sched), []() { return 42; }))));
HPX_TEST_EQ(result, 42);

// Test 3: scheduled work runs on an HPX worker thread
hpx::thread::id scheduled_thread_id{};
tt::sync_wait(ex::then(ex::schedule(sched),
[&]() { scheduled_thread_id = hpx::this_thread::get_id(); }));
HPX_TEST(scheduled_thread_id != hpx::thread::id{});
}

///////////////////////////////////////////////////////////////////////////////
int hpx_main()
{
static_check_executor();

// P2300 get_scheduler bridge test
test_get_scheduler();

// Call regression test for #6922
test_fork_join_static_large_range();

Expand Down
Loading