Repository navigation
Conversation
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>
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the
|
Up to standards ✅🟢 Issues
|
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 afork_join_executorexists, 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
fork_join_executoris 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 theboundpriority, so they resume on their own core.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::finalizenever 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 tohpx::register_pre_shutdown_function. Using the executor afterwards throwsinvalid_status.User-visible change: the local runtime's
finalizestored 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 makesinterval_timer/pool_timerstop 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_executorcan't be used by several threads at the same time or from inside its own regions.shared_suspending_fork_join_executorcan:parallel_executorinstead.sync_execute,async_execute,then_execute,post) andbulk_then_executerun as aparallel_executorruns them. Mapping a policy with this executor to a task policy (to_task, as inpar(task)) gives it aparallel_executor: a task policy runs an algorithm asynchronously, which the fork-join worker threads can't do.constexprvariable (both executors keep theirstd::shared_ptrs in lazily constructed storage, asstd::shared_ptr's destructor is not constexpr).The new CMake option
HPX_WITH_SUSPENDING_FORK_JOIN_EXECUTOR_AS_DEFAULT(defaultOFF) makesshared_suspending_fork_join_executorthe executor ofparandpar_unseq.par(task)andpar_unseq(task)keep usingparallel_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:Tests
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 externalstd::threadcaller, 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.parandpar_unseqas well, among them sort, stable_sort, inclusive_scan, merge and reduce); gcc 12 and clang 23 before the shared executor was added.