Skip to content

Add suspending_fork_join_executor - #8247

Open
hkaiser wants to merge 2 commits into
fork-join-dynamic-chunksfrom
suspending-fork-join-executor
Open

hkaiser wants to merge 2 commits into
fork-join-dynamic-chunksfrom
suspending-fork-join-executor

Conversation

@hkaiser

@hkaiser hkaiser commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Stacked on #8244 (its dynamic schedule pops chunks of indices, which this executor uses too); the base branch is fork-join-dynamic-chunks. Only the last two commits belong to this PR.

Why

fork_join_executor's worker threads spin between parallel regions, and the calling thread spins until a region has finished. While a fork_join_executor exists, its cores can't run other work and can't go to sleep: other work on those cores took about 100 ms instead of about 3 ms at 32 workers.

suspending_fork_join_executor

  • A new type; fork_join_executor is unchanged. Its waiting threads spin for a short time only (20 us by default, converted once into a number of spin rounds, no clock reads while spinning), and then suspend on a condition variable. While suspended they keep the bound priority, so they resume on their own core.
  • Each worker thread suspends on its own, and they are woken in a tree: the calling thread wakes two of them, each of these two more, and so on. With 32 workers this halves the time of a region that follows a pause of 100 us or more.
  • Constexpr default constructible: the worker threads are created on first use. Copies made on an HPX thread share the worker threads.
  • The calling thread takes part in a region only if it runs on one of the executor's PUs; otherwise all PUs get a worker thread and the calling thread suspends until the region has finished. This is decided by PU, not by worker thread number, so the case of Fix/fork join executor region data oob #8230 (a caller outside the executor's slots) can't index out of bounds.
  • The dynamic schedule pops chunks of indices like fork_join_executor's after fork_join_executor: pop chunks in the dynamic schedule #8244; regions with more than 2^32 elements fall back to the static schedule.

Shutdown, and a change to the local runtime

Suspended threads count as busy, so hpx::finalize never returned while an executor (for instance a global one) still existed. The executor stops its worker threads when the runtime starts to shut down, through a new hook in threading_base (hpx::threads::detail::register_pre_shutdown) that init_runtime_local connects to hpx::register_pre_shutdown_function. Using the executor afterwards throws invalid_status.

User-visible change: the local runtime's finalize stored the pre-shutdown and shutdown functions but never ran them (the distributed runtime did). It now runs both, in that order, also those registered while they run. This also makes interval_timer/pool_timer stop functions run with the local runtime. A new test, runtime_local/shutdown_functions, checks the order.

shared_suspending_fork_join_executor, and HPX_WITH_SUSPENDING_FORK_JOIN_EXECUTOR_AS_DEFAULT

suspending_fork_join_executor can't be used by several threads at the same time or from inside its own regions. shared_suspending_fork_join_executor can:

  • All shared executors with the same configuration use one set of worker threads per thread pool, created on first use by an HPX thread of the pool and stopped when the runtime starts to shut down.
  • A region runs on these worker threads if they are free. If they are busy (another thread's region, or a nested region), if the calling thread is not an HPX thread, or once they were stopped, the region runs through a parallel_executor instead.
  • Single tasks (sync_execute, async_execute, then_execute, post) and bulk_then_execute run as a parallel_executor runs them. Mapping a policy with this executor to a task policy (to_task, as in par(task)) gives it a parallel_executor: a task policy runs an algorithm asynchronously, which the fork-join worker threads can't do.
  • Copies never hold worker threads, so the executor can be a constexpr variable (both executors keep their std::shared_ptrs in lazily constructed storage, as std::shared_ptr's destructor is not constexpr).

The new CMake option HPX_WITH_SUSPENDING_FORK_JOIN_EXECUTOR_AS_DEFAULT (default OFF) makes shared_suspending_fork_join_executor the executor of par and par_unseq. par(task) and par_unseq(task) keep using parallel_executor. The macOS (Debug), Linux (Release, C++20 Modules) and Windows (Release, VS2022, C++20 Modules) workflows now build and test with the option on.

Benchmark

tests/performance/fork_join_executors_benchmark (new), 32 workers, ns per region, Windows / Linux:

test parallel_executor fork_join_executor (with #8244) suspending_fork_join_executor
empty region, back-to-back 4469 / 9413 8614 / 7455 4376 / 3187
64 elements, back-to-back 35746 / 29498 9521 / 8335 5495 / 5322
64 elements, 100 us between regions 29103 / 30690 10465 / 8940 11900 / 12959
10000 elements 70130 / 39856 20870 / 19921 13699 / 22740
1,000,000 elements 56068 / 53422 50626 / 64201 41641 / 54615
other work while the executor is idle (no executor: 3.21 / 3.49 ms) 100.3 / 105.8 ms 3.17 / 3.53 ms

Tests

  • New: executors/suspending_fork_join_executor (the fork_join_executor tests, plus lazy initialization of a global, callers outside the PU mask and on another worker thread, an external std::thread caller, nested use, nostack, and other work running on idle cores), executors/suspending_fork_join_executor_shutdown, executors/shared_suspending_fork_join_executor (nested, concurrent and external-thread use, exceptions, the parallel algorithms, the policy types), runtime_local/shutdown_functions.
  • Locally: MSVC 19.44 with the option on and off (with it on, 15 algorithm tests that use par and par_unseq as well, among them sort, stable_sort, inclusive_scan, merge and reduce); gcc 12 and clang 23 before the shared executor was added.

fork_join_executor's worker threads spin between parallel regions, and
the calling thread spins until a region has finished. While a
fork_join_executor exists, its cores can't run other work and can't go
to sleep: other work on those cores took 100 ms instead of 3 ms at 32
workers.

suspending_fork_join_executor does the same job, but its waiting threads
spin for a short time only (20 us by default, converted once to a number
of spin rounds), and then suspend on a condition variable. Each worker
thread suspends on its own, and they are woken in a tree: the calling
thread wakes two of them, each of these two more, and so on. The
executor is constexpr default constructible and creates its worker
threads on first use. The calling thread takes part in the regions only
if it runs on one of the executor's PUs; otherwise it suspends until the
region has finished. The dynamic schedule pops chunks of indices, like
fork_join_executor's.

Suspended worker threads count as busy, so the runtime couldn't stop
while an executor (for instance a global one) still existed. The
executor stops its worker threads when the runtime starts to shut down,
through a new hook in threading_base that init_runtime_local connects
to hpx::register_pre_shutdown_function. The local runtime's finalize
didn't run the pre-shutdown and shutdown functions so far (the
distributed runtime did); it runs both now.

32 workers, ns per region (Windows / Linux):
- empty region, back-to-back: suspending 4376 / 3187,
  fork_join_executor 8614 / 7455, parallel_executor 4469 / 9413
- 64 elements, 100 us between regions: suspending 11900 / 12959,
  fork_join_executor 10465 / 8940, parallel_executor 29103 / 30690
- 1,000,000 elements: suspending 41641 / 54615,
  fork_join_executor 50626 / 64201, parallel_executor 56068 / 53422
- other work while an executor is idle: 3.17 / 3.53 ms with the
  suspending executor, 100.3 / 105.8 ms with fork_join_executor,
  3.21 / 3.49 ms without an executor

Signed-off-by: Hartmut Kaiser <hartmut.kaiser@gmail.com>
suspending_fork_join_executor can't be used by several threads at the
same time or from inside its own regions, so it can't be the executor of
a default execution policy. shared_suspending_fork_join_executor can:

- All shared executors with the same configuration use one set of worker
  threads per thread pool, created on first use by an HPX thread of the
  pool and stopped when the runtime starts to shut down.
- A region runs on these worker threads if they are free. If they are
  busy (another thread's region, or a nested region), if the calling
  thread is not an HPX thread, or once they were stopped, the region runs
  through a parallel_executor instead.
- Single tasks (sync_execute, async_execute, then_execute, post) and
  bulk_then_execute run as a parallel_executor runs them; the parallel
  algorithms need them (dataflow in sort, async_execute in the scan
  partitioner). Mapping a policy with this executor to a task policy
  (to_task) gives it a parallel_executor, as a task policy runs an
  algorithm asynchronously.
- Copies never hold worker threads, and the executor can be a constexpr
  variable: suspending_fork_join_executor keeps its std::shared_ptrs in
  lazily constructed storage, as std::shared_ptr's destructor is not
  constexpr.

Stopping the worker threads waits for a running region to finish, and
keeps new ones from starting.

The new CMake option HPX_WITH_SUSPENDING_FORK_JOIN_EXECUTOR_AS_DEFAULT
(default OFF) makes shared_suspending_fork_join_executor the executor of
par and par_unseq. The task policies par(task) and par_unseq(task) keep
using parallel_executor. The macOS (Debug), Linux (Release, C++20
Modules) and Windows (Release, VS2022, C++20 Modules) workflows build
and test with the option on.

Signed-off-by: Hartmut Kaiser <hartmut.kaiser@gmail.com>
@coderabbitai

coderabbitai Bot commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 35330d2b-e1e2-4ab7-8412-0a41e7cbaf0b

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant