Repository navigation
Let executors ask for their own minimal chunk time #8258
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| // Copyright (c) 2026 TheHPXProject | ||
| // | ||
| // 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 get_min_chunk_time.hpp | ||
| /// \page hpx::execution::experimental::get_min_chunk_time | ||
| /// \headerfile hpx/execution.hpp | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <hpx/config.hpp> | ||
| #include <hpx/execution/detail/task_size_chunk_size_defaults.hpp> | ||
| #include <hpx/modules/async_base.hpp> | ||
|
|
||
| #include <chrono> | ||
|
|
||
| namespace hpx::execution::experimental { | ||
|
|
||
| /// \cond NOINTERNAL | ||
| namespace detail { | ||
|
|
||
| HPX_CXX_CORE_EXPORT struct get_min_chunk_time_fallback | ||
| { | ||
| template <typename Target, typename... Args> | ||
| HPX_FORCEINLINE std::chrono::nanoseconds operator()( | ||
| Target&&, Args&&...) const noexcept | ||
| { | ||
| return get_task_size_chunk_size_defaults().min_chunk_time; | ||
| } | ||
| }; | ||
| } // namespace detail | ||
| /// \endcond | ||
|
|
||
| /// The minimal run time of a chunk for the loops of the executor: the | ||
| /// default minimal run time of \a task_size_chunk_size (see | ||
| /// \a hpx.task_size_chunk_size.min_chunk_time_us) unless the executor asks | ||
| /// for another one. \a task_size_chunk_size uses it unless a minimal run | ||
| /// time was given to the \a task_size_chunk_size object: an executor that | ||
| /// starts a chunk with less overhead than \a parallel_executor (a task and | ||
| /// a future for each chunk) can run smaller chunks. | ||
| /// | ||
| /// The executors of HPX return the value configured with | ||
| /// \a hpx.task_size_chunk_size.<executor>.<pool size>.min_chunk_time_us, | ||
| /// or else with \a hpx.task_size_chunk_size.<executor>.min_chunk_time_us | ||
| /// (in microseconds), where \a <executor> is the name of the executor | ||
| /// (\a parallel for \a parallel_executor, \a fork_join for | ||
| /// \a fork_join_executor) and \a <pool size> the number of worker threads | ||
| /// it runs on. If neither is configured, they return the default: | ||
| /// | ||
| /// \code | ||
| /// [hpx.task_size_chunk_size.fork_join] | ||
| /// min_chunk_time_us = 0.5 | ||
| /// | ||
| /// [hpx.task_size_chunk_size.fork_join.32] | ||
| /// min_chunk_time_us = 0.8 | ||
| /// \endcode | ||
| /// | ||
| HPX_CXX_CORE_EXPORT inline constexpr struct get_min_chunk_time_t final | ||
| : hpx::execution::experimental::detail::query_first_tag_fallback< | ||
| get_min_chunk_time_t, detail::get_min_chunk_time_fallback> | ||
| { | ||
| constexpr get_min_chunk_time_t() = default; | ||
| } get_min_chunk_time{}; | ||
| } // namespace hpx::execution::experimental |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ | |
| #include <hpx/config.hpp> | ||
| #include <hpx/execution/detail/task_size_chunk_size_defaults.hpp> | ||
| #include <hpx/execution/executors/execution_parameters.hpp> | ||
| #include <hpx/execution/executors/get_min_chunk_time.hpp> | ||
| #include <hpx/modules/execution_base.hpp> | ||
| #include <hpx/modules/functional.hpp> | ||
| #include <hpx/modules/serialization.hpp> | ||
|
|
@@ -50,7 +51,10 @@ namespace hpx::execution::experimental { | |
| /// microseconds) and \a hpx.task_size_chunk_size.chunks_per_core. | ||
| /// The defaults are read when a loop is chunked, so an object | ||
| /// created before the runtime has started (for instance as part | ||
| /// of a global execution policy) uses the configured ones. | ||
| /// of a global execution policy) uses the configured ones. An | ||
| /// executor can ask for a minimal run time of its own (see | ||
| /// \a hpx::execution::experimental::get_min_chunk_time), which | ||
| /// replaces the default one for its loops. | ||
| /// | ||
| constexpr task_size_chunk_size() noexcept = default; | ||
|
|
||
|
|
@@ -109,7 +113,7 @@ namespace hpx::execution::experimental { | |
|
|
||
| // the iterations one core needs, computed without multiplying by | ||
| // count, which could overflow | ||
| auto const [min_chunk_time, chunks_per_core] = get_values(); | ||
| auto const [min_chunk_time, chunks_per_core] = get_values(exec); | ||
| auto const per_core = | ||
| min_chunk_time * static_cast<rep>(chunks_per_core); | ||
| auto const iterations_per_core = | ||
|
|
@@ -122,7 +126,7 @@ namespace hpx::execution::experimental { | |
|
|
||
| // chunks_per_core chunks per core, none shorter than min_chunk_time. | ||
| template <typename Executor> | ||
| std::size_t get_chunk_size(Executor&&, | ||
| std::size_t get_chunk_size(Executor&& exec, | ||
| hpx::chrono::steady_duration const& iteration_duration, | ||
| std::size_t const cores, std::size_t const count) const noexcept | ||
| { | ||
|
|
@@ -139,7 +143,7 @@ namespace hpx::execution::experimental { | |
| return count; | ||
| } | ||
|
|
||
| auto const [min_chunk_time, chunks_per_core] = get_values(); | ||
| auto const [min_chunk_time, chunks_per_core] = get_values(exec); | ||
| std::size_t const num_chunks = | ||
| chunks_per_core * (std::max) (cores, std::size_t(1)); | ||
| std::size_t const balanced = (count + num_chunks - 1) / num_chunks; | ||
|
|
@@ -158,21 +162,27 @@ namespace hpx::execution::experimental { | |
| using rep = duration::rep; | ||
|
|
||
| // The minimal run time of a chunk and the number of chunks per core, | ||
| // the current defaults for the ones that were not given. | ||
| std::pair<duration, std::size_t> get_values() const noexcept | ||
| // for the ones that were not given the executor's minimal run time | ||
| // (the current default unless it asks for another one) and the | ||
| // current default number of chunks per core. | ||
| template <typename Executor> | ||
| std::pair<duration, std::size_t> get_values( | ||
| Executor const& exec) const noexcept | ||
| { | ||
| if (min_chunk_time_ >= duration::zero() && chunks_per_core_ != 0) | ||
| { | ||
| return {min_chunk_time_, chunks_per_core_}; | ||
| } | ||
|
|
||
| auto const defaults = detail::get_task_size_chunk_size_defaults(); | ||
| return {min_chunk_time_ >= duration::zero() ? | ||
| min_chunk_time_ : | ||
| hpx::chrono::steady_duration(defaults.min_chunk_time) | ||
| hpx::chrono::steady_duration( | ||
| hpx::execution::experimental::get_min_chunk_time(exec)) | ||
|
Comment on lines
+179
to
+180
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prevent a throwing
Choose one fix and apply it consistently:
|
||
| .value(), | ||
| chunks_per_core_ != 0 ? chunks_per_core_ : | ||
| defaults.chunks_per_core}; | ||
| chunks_per_core_ != 0 ? | ||
| chunks_per_core_ : | ||
| detail::get_task_size_chunk_size_defaults() | ||
| .chunks_per_core}; | ||
| } | ||
|
|
||
| friend class hpx::serialization::access; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Preserve the executor's constness when resolving its query.
If an executor provides a non-const
query(get_min_chunk_time_t),get_min_chunk_time(exec)calls that query on a mutable executor. This helper addsconst, so query detection instead selects the global fallback. The checked dispatch preserves the target's qualification. (raw.githubusercontent.com)For a query returning 500ns, a global minimum of 2500ns, and
get_chunk_size(exec, 100ns, 2, 100), this produces 25 instead of 7. AcceptExecutor&here to preserve the caller's existing constness. Const-qualified executor queries will still work.