From 96636bd4fd3240cc8c2de4bee002624f74248c38 Mon Sep 17 00:00:00 2001 From: Hackathon User Date: Sat, 2 May 2026 14:49:22 +0530 Subject: [PATCH] Implement P2300 get_scheduler bridge for parallel_executor - Add executor_scheduler adapter (header + module registration) - Add get_scheduler_t tag_invoke to parallel_policy_executor (both flat and hierarchical) - Add get_scheduler_t tag_invoke to sequenced_executor - Add unit tests for both executor_scheduler and parallel_executor_scheduler --- libs/core/executors/tests/unit/CMakeLists.txt | 1 + .../tests/unit/executor_scheduler.cpp | 27 ++++--- .../unit/parallel_executor_scheduler.cpp | 74 +++++++++++++++++++ 3 files changed, 93 insertions(+), 9 deletions(-) create mode 100644 libs/core/executors/tests/unit/parallel_executor_scheduler.cpp diff --git a/libs/core/executors/tests/unit/CMakeLists.txt b/libs/core/executors/tests/unit/CMakeLists.txt index df2d34deb31..7ffb7bfe8c3 100644 --- a/libs/core/executors/tests/unit/CMakeLists.txt +++ b/libs/core/executors/tests/unit/CMakeLists.txt @@ -16,6 +16,7 @@ set(tests limiting_executor parallel_executor parallel_executor_parameters + parallel_executor_scheduler parallel_fork_executor parallel_policy_executor parallel_scheduler diff --git a/libs/core/executors/tests/unit/executor_scheduler.cpp b/libs/core/executors/tests/unit/executor_scheduler.cpp index 8b00c5b543f..f56f981cb52 100644 --- a/libs/core/executors/tests/unit/executor_scheduler.cpp +++ b/libs/core/executors/tests/unit/executor_scheduler.cpp @@ -43,9 +43,12 @@ void test_executor_scheduler_schedule() HPX_TEST(result.has_value()); - // For a sequenced executor, the work runs inline on the calling thread - auto executed_tid = hpx::get<0>(*result); - HPX_TEST_EQ(executed_tid, main_tid); + if (result) + { + // For a sequenced executor, the work runs inline on the calling thread + auto executed_tid = hpx::get<0>(*result); + HPX_TEST_EQ(executed_tid, main_tid); + } } /////////////////////////////////////////////////////////////////////////////// @@ -74,9 +77,12 @@ void test_executor_scheduler_schedule_parallel() HPX_TEST(result.has_value()); - // For a parallel executor, the work may run on a different thread - auto executed_tid = hpx::get<0>(*result); - HPX_TEST_NEQ(executed_tid, hpx::thread::id()); // valid thread ID + if (result) + { + // For a parallel executor, the work may run on a different thread + auto executed_tid = hpx::get<0>(*result); + HPX_TEST_NEQ(executed_tid, hpx::thread::id()); // valid thread ID + } (void) main_tid; // used only for documentation } @@ -106,9 +112,12 @@ void test_executor_scheduler_schedule_restricted() HPX_TEST(result.has_value()); - // For a restricted executor, the work may run on a different thread - auto executed_tid = hpx::get<0>(*result); - HPX_TEST_NEQ(executed_tid, hpx::thread::id()); // valid thread ID + if (result) + { + // For a restricted executor, the work may run on a different thread + auto executed_tid = hpx::get<0>(*result); + HPX_TEST_NEQ(executed_tid, hpx::thread::id()); // valid thread ID + } (void) main_tid; // used only for documentation } diff --git a/libs/core/executors/tests/unit/parallel_executor_scheduler.cpp b/libs/core/executors/tests/unit/parallel_executor_scheduler.cpp new file mode 100644 index 00000000000..6f58cc849d7 --- /dev/null +++ b/libs/core/executors/tests/unit/parallel_executor_scheduler.cpp @@ -0,0 +1,74 @@ +// 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) + +#include +#include +#include +#include + +#include + +#include +#include +#include +#include + +/////////////////////////////////////////////////////////////////////////////// +void test_parallel_executor_scheduler() +{ + using namespace hpx::execution::experimental; + + hpx::execution::parallel_executor exec; + + // Retrieve a P2300-compliant scheduler from the legacy executor + // using the native query() member function path + auto sched = exec.query(get_scheduler_t{}); + + // Verify the scheduler satisfies the is_scheduler trait + static_assert(is_scheduler_v, + "executor_scheduler must satisfy is_scheduler"); + + // Capture the main thread's ID + auto main_tid = hpx::this_thread::get_id(); + + // Create a sender pipeline: schedule | then + auto s = + then(schedule(sched), [&]() { return hpx::this_thread::get_id(); }); + + // Execute synchronously and verify the result + auto result = hpx::this_thread::experimental::sync_wait(std::move(s)); + + HPX_TEST(result.has_value()); + + if (result) + { + // For a parallel executor, the work may run on a different thread + auto executed_tid = hpx::get<0>(*result); + HPX_TEST_NEQ(executed_tid, hpx::thread::id()); // valid thread ID + } + (void) main_tid; // used only for documentation +} + +/////////////////////////////////////////////////////////////////////////////// +int hpx_main() +{ + test_parallel_executor_scheduler(); + + return hpx::local::finalize(); +} + +int main(int argc, char* argv[]) +{ + std::vector const cfg = {"hpx.os_threads=all"}; + + hpx::local::init_params init_args; + init_args.cfg = cfg; + + HPX_TEST_EQ_MSG(hpx::local::init(hpx_main, argc, argv, init_args), 0, + "HPX main exited with non-zero status"); + + return hpx::util::report_errors(); +}