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
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ namespace hpx::local::detail {
matches("local-priority-tuned-fifo") ||
matches("local-workrequesting-fifo") ||
matches("local-workrequesting-lifo") ||
matches("local-workrequesting-mc"))
matches("local-workrequesting-mc") ||
matches("local-workrequesting-tuned-fifo"))
{
return true;
}
Expand Down Expand Up @@ -526,6 +527,7 @@ namespace hpx::local::detail {
"--hpx:queuing=local-workrequesting-fifo, "
"--hpx:queuing=local-workrequesting-lifo, "
"--hpx:queuing=local-workrequesting-mc, "
"--hpx:queuing=local-workrequesting-tuned-fifo, "
"--hpx:queuing=abp-priority-fifo, and "
"--hpx:queuing=abp-priority-lifo only");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,8 @@ namespace hpx::local::detail {
"'local-priority-lifo', 'local-priority-tuned-fifo', "
"'abp-priority-fifo', 'abp-priority-lifo', 'static', "
"'static-priority', 'local-workrequesting-fifo', "
"'local-workrequesting-lifo', and 'local-workrequesting-mc' "
"'local-workrequesting-lifo', 'local-workrequesting-mc', and "
"'local-workrequesting-tuned-fifo' "
"(default: 'local-priority-fifo'; all option values can be "
"abbreviated)")
("hpx:high-priority-threads", value<std::size_t>(),
Expand All @@ -593,6 +594,7 @@ namespace hpx::local::detail {
"--hpx:queuing=local-workrequesting-fifo, "
"--hpx:queuing=local-workrequesting-lifo, "
"--hpx:queuing=local-workrequesting-mc, "
"--hpx:queuing=local-workrequesting-tuned-fifo, "
"--hpx:queuing=abp-priority-fifo, and "
"--hpx:queuing=abp-priority-lifo only)")
("hpx:numa-sensitive", value<std::size_t>()->implicit_value(1),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ namespace hpx::resource {
local_workrequesting_lifo = 9,
local_workrequesting_mc = 10,
local_priority_tuned_fifo = 12,
local_workrequesting_tuned_fifo = 13,
};

#define HPX_SCHEDULING_POLICY_UNSCOPED_ENUM_DEPRECATION_MSG \
Expand Down
8 changes: 8 additions & 0 deletions libs/core/resource_partitioner/src/detail_partitioner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,13 @@ namespace hpx::resource {
return "local_workrequesting_lifo";
case resource::scheduling_policy::local_workrequesting_mc:
return "local_workrequesting_mc";
case resource::scheduling_policy::local_workrequesting_tuned_fifo:
return "local_workrequesting_tuned_fifo";
#else
case resource::scheduling_policy::local_workrequesting_fifo:
case resource::scheduling_policy::local_workrequesting_lifo:
case resource::scheduling_policy::local_workrequesting_mc:
case resource::scheduling_policy::local_workrequesting_tuned_fifo:
return "unknown";
#endif
case resource::scheduling_policy::static_:
Expand Down Expand Up @@ -119,6 +122,11 @@ namespace hpx::resource {
{
return scheduling_policy::local_workrequesting_mc;
}
if (0 ==
std::string("local-workrequesting-tuned-fifo").find(scheduler_name))
{
return scheduling_policy::local_workrequesting_tuned_fifo;
}
#endif
if (0 == std::string("static").find(scheduler_name))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ int main(int argc, char* argv[])
test_queuing_string(argc, argv, "local-priority-tuned-fifo",
hpx::resource::scheduling_policy::local_priority_tuned_fifo);

#if defined(HPX_HAVE_WORK_REQUESTING_SCHEDULERS)
// Must resolve to local_workrequesting_tuned_fifo, and must not be
// short-circuited by shorter local-workrequesting-* branches.
test_queuing_string(argc, argv, "local-workrequesting-tuned-fifo",
hpx::resource::scheduling_policy::local_workrequesting_tuned_fifo);
#endif

// A value that is not a prefix of any known scheduler name.
test_invalid_queuing_string(argc, argv, "not-a-real-scheduler");

Expand Down
5 changes: 3 additions & 2 deletions libs/core/schedulers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ set(schedulers_headers
)

if(HPX_WITH_WORK_REQUESTING_SCHEDULERS)
set(schedulers_headers ${schedulers_headers}
hpx/schedulers/local_workrequesting_scheduler.hpp
set(schedulers_headers
${schedulers_headers} hpx/schedulers/local_workrequesting_scheduler.hpp
hpx/schedulers/local_workrequesting_tuned_scheduler.hpp
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
)
endif()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,9 @@ namespace hpx::threads::policies {
typename PendingQueuing = lockfree_fifo,
typename StagedQueuing = concurrentqueue_fifo,
typename TerminatedQueuing =
default_local_workrequesting_scheduler_terminated_queue>
class local_workrequesting_scheduler final : public scheduler_base
default_local_workrequesting_scheduler_terminated_queue,
typename Rules = fixed_scheduling_rules>
class local_workrequesting_scheduler : public scheduler_base
{
public:
using has_periodic_maintenance = std::false_type;
Expand Down Expand Up @@ -498,6 +499,7 @@ namespace hpx::threads::policies {
, numa_sensitive_(init.numa_sensitive_)
, num_queues_(init.num_queues_)
, num_high_priority_queues_(init.num_high_priority_queues_)
, rules_(init.num_queues_)
{
HPX_ASSERT(init.num_queues_ != 0);
HPX_ASSERT(num_high_priority_queues_ != 0);
Expand Down Expand Up @@ -533,6 +535,13 @@ namespace hpx::threads::policies {
return "local_workrequesting_scheduler";
}

// the idle callback of the workers, with rules 6 to 8 as Rules has
// them (see scheduler_base::idle_callback_with_rules)
bool idle_callback(std::size_t const num_thread)
{
return this->idle_callback_with_rules(num_thread, rules_);
}

#ifdef HPX_HAVE_THREAD_CREATION_AND_CLEANUP_RATES
std::uint64_t get_creation_time(bool reset) override
{
Expand Down Expand Up @@ -2496,6 +2505,10 @@ namespace hpx::threads::policies {
numa_sensitive_mode const numa_sensitive_;
std::size_t const num_queues_;
std::size_t const num_high_priority_queues_;

// The rules for idle backoff and worker wakeups (see idle_callback).
// fixed_scheduling_rules is empty and takes no space.
HPX_NO_UNIQUE_ADDRESS Rules rules_;
};
} // namespace hpx::threads::policies

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// Copyright (c) 2026 Hartmut Kaiser
//
// 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 local_workrequesting_tuned_scheduler.hpp
/// \brief The local workrequesting scheduler with tunable rules
/// (local-workrequesting-tuned-fifo).

#pragma once

#include <hpx/config.hpp>

#if defined(HPX_HAVE_WORK_REQUESTING_SCHEDULERS)

#include <hpx/modules/threading_base.hpp>
#include <hpx/schedulers/local_workrequesting_scheduler.hpp>
#include <hpx/schedulers/lockfree_queue_backends.hpp>

#include <memory>
#include <mutex>
#include <string_view>

#include <hpx/config/warnings_prefix.hpp>

namespace hpx::threads::policies {

/// The local workrequesting scheduler with tunable rules
/// (local-workrequesting-tuned-fifo). It places, queues, and requests
/// tasks as the local workrequesting scheduler does, while taking the rules
/// that control idle backoff behavior from scheduling_policy. The numbers
/// and switches of these rules are tunable settings (see
/// scheduler_tuning_parameters), which the tuner selected with
/// hpx.scheduler_tuner may change while the scheduler runs.
HPX_CXX_CORE_EXPORT template <typename Mutex = std::mutex,
typename PendingQueuing = lockfree_fifo,
typename StagedQueuing = concurrentqueue_fifo,
typename TerminatedQueuing =
default_local_workrequesting_scheduler_terminated_queue>
class local_workrequesting_tuned_scheduler final
: public local_workrequesting_scheduler<Mutex, PendingQueuing,
StagedQueuing, TerminatedQueuing, tuned_scheduling_rules>
{
public:
using base_type = local_workrequesting_scheduler<Mutex, PendingQueuing,
StagedQueuing, TerminatedQueuing, tuned_scheduling_rules>;

// the parameters of the local workrequesting scheduler, and the
// values the settings of the rules start with
struct init_parameter : base_type::init_parameter_type
{
init_parameter(base_type::init_parameter_type const& init,
scheduling_policy_init_parameters const& policy_init =
scheduling_policy_init_parameters{}) noexcept
: base_type::init_parameter_type(init)
, policy_init_(policy_init)
{
}

scheduling_policy_init_parameters policy_init_;
};
using init_parameter_type = init_parameter;

explicit local_workrequesting_tuned_scheduler(
init_parameter_type const& init,
bool const deferred_initialization = true)
: base_type(init, deferred_initialization)
{
this->rules_.register_parameters(
this->thread_queue_init_, init.policy_init_);
this->rules_.set_tuner(create_scheduler_tuner("static"));
}

static constexpr std::string_view get_scheduler_name() noexcept
{
return "local_workrequesting_tuned_scheduler";
}

/// Replace the tuner that decides the values of the tunable settings
/// (see hpx.scheduler_tuner). Called before the workers start.
void set_tuner(std::unique_ptr<scheduler_tuner> tuner)
{
this->rules_.set_tuner(HPX_MOVE(tuner));
}

/// The tunable settings of the rules of this scheduler.
[[nodiscard]] scheduler_tuning_parameters&
get_tuning_parameters() noexcept
{
return this->rules_.parameters();
}
[[nodiscard]] scheduler_tuning_parameters const& get_tuning_parameters()
const noexcept
{
return this->rules_.parameters();
}
};
} // namespace hpx::threads::policies

#include <hpx/config/warnings_suffix.hpp>

#endif
52 changes: 45 additions & 7 deletions libs/core/schedulers/tests/unit/scheduler_tuning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
namespace policies = hpx::threads::policies;

using tuned_scheduler = policies::local_priority_tuned_scheduler<>;
#if defined(HPX_HAVE_WORK_REQUESTING_SCHEDULERS)
using wr_tuned_scheduler = policies::local_workrequesting_tuned_scheduler<>;
#endif

std::atomic<int> attached{0};

Expand All @@ -47,22 +50,24 @@ struct test_tuner final : policies::scheduler_tuner
}
};

template <typename Scheduler = tuned_scheduler>
policies::scheduler_tuning_parameters& get_parameters()
{
auto* scheduler = dynamic_cast<tuned_scheduler*>(
auto* scheduler = dynamic_cast<Scheduler*>(
hpx::threads::get_self_id_data()->get_scheduler_base());
HPX_TEST(scheduler != nullptr);
return scheduler->get_tuning_parameters();
}

// the tested settings belong to the local-priority-tuned-fifo scheduler, make
// sure it is used regardless of --hpx:queuing
void use_tuned_scheduler(hpx::local::init_params& init_args)
// the tested settings belong to the tuned scheduler under test, make sure it is
// used regardless of --hpx:queuing
void use_tuned_scheduler(hpx::local::init_params& init_args,
hpx::resource::scheduling_policy const policy =
hpx::resource::scheduling_policy::local_priority_tuned_fifo)
{
init_args.rp_callback = [](hpx::resource::partitioner& rp,
init_args.rp_callback = [policy](hpx::resource::partitioner& rp,
hpx::program_options::variables_map const&) {
rp.create_thread_pool("default",
hpx::resource::scheduling_policy::local_priority_tuned_fifo);
rp.create_thread_pool("default", policy);
};
}

Expand Down Expand Up @@ -356,5 +361,38 @@ int main(int argc, char* argv[])
hpx::local::init(test_custom_tuner, argc, argv, init_args), 0);
}

#if defined(HPX_HAVE_WORK_REQUESTING_SCHEDULERS)
{
auto const run_wr_tuned = []() -> int {
#if defined(HPX_HAVE_THREAD_MANAGER_IDLE_BACKOFF)
auto& parameters = get_parameters<wr_tuned_scheduler>();
std::size_t const interval =
parameters.find("hpx.idle_backoff_steal_check_interval");
HPX_TEST_NEQ(interval, policies::scheduler_tuning_parameters::npos);
HPX_TEST_EQ(parameters.get<float>(0, interval), 3.0f);

// Verify runtime updates and clamping through
// get_tuning_parameters()
parameters.set(0, interval, -1.0f);
HPX_TEST_EQ(parameters.get<float>(0, interval), 0.0f);
parameters.set(0, interval, 12.5f);
HPX_TEST_EQ(parameters.get<float>(0, interval), 12.5f);
#endif
return hpx::local::finalize();
};

int const prev_attached = attached.load();
policies::register_scheduler_tuner(
"test_wr", [] { return std::make_unique<test_tuner>(); });

hpx::local::init_params init_args;
init_args.cfg = {"hpx.os_threads=2", "hpx.scheduler_tuner=test_wr"};
use_tuned_scheduler(init_args,
hpx::resource::scheduling_policy::local_workrequesting_tuned_fifo);
HPX_TEST_EQ(hpx::local::init(run_wr_tuned, argc, argv, init_args), 0);
HPX_TEST_EQ(attached.load(), prev_attached + 1);
}
#endif

return hpx::util::report_errors();
}
5 changes: 5 additions & 0 deletions libs/core/thread_pools/src/scheduled_thread_pool_tuned.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@

template class HPX_CORE_EXPORT hpx::threads::detail::scheduled_thread_pool<
hpx::threads::policies::local_priority_tuned_scheduler<>>;

#if defined(HPX_HAVE_WORK_REQUESTING_SCHEDULERS)
template class HPX_CORE_EXPORT hpx::threads::detail::scheduled_thread_pool<
hpx::threads::policies::local_workrequesting_tuned_scheduler<>>;
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,9 @@ namespace hpx::threads {
void create_scheduler_local_workrequesting_mc(
thread_pool_init_parameters const&,
policies::thread_queue_init_parameters const&, std::size_t);
void create_scheduler_local_workrequesting_tuned_fifo(
thread_pool_init_parameters const&,
policies::thread_queue_init_parameters const&, std::size_t);

mutable mutex_type mtx_; // mutex protecting the members

Expand Down
5 changes: 5 additions & 0 deletions libs/core/threadmanager/src/threadmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,11 @@ namespace hpx::threads {
thread_pool_init, thread_queue_init, numa_sensitive);
break;

case resource::scheduling_policy::local_workrequesting_tuned_fifo:
create_scheduler_local_workrequesting_tuned_fifo(
thread_pool_init, thread_queue_init, numa_sensitive);
break;

case resource::scheduling_policy::abp_priority_fifo:
create_scheduler_abp_priority_fifo(
thread_pool_init, thread_queue_init, numa_sensitive);
Expand Down
Loading
Loading