Skip to content
Merged
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 @@ -464,6 +464,7 @@ namespace hpx::threads::policies {
// adaptive stealing
std::uint16_t num_recent_steals_ = 0;
std::uint16_t num_recent_tasks_executed_ = 0;
std::uint8_t consecutive_high_priority_tasks_ = 0;
bool stealhalf_ = true;

#if defined(HPX_HAVE_WORKREQUESTING_LAST_VICTIM)
Expand Down Expand Up @@ -1294,9 +1295,41 @@ namespace hpx::threads::policies {

auto& d = data_[num_thread].data_;

if ((num_thread < num_high_priority_queues_ &&
get_thread(d.high_priority_queue_, thrd)) ||
get_thread(d.bound_queue_, thrd) || get_thread(d.queue_, thrd))
// After 16 high-priority threads, try bound and normal work first.
// This also prevents starvation under a steady high-priority load.
// Bound work cannot be stolen by another worker.
constexpr std::uint8_t max_consecutive_high_priority_tasks = 16;
auto const get_other_thread = [&] {
return get_thread(d.bound_queue_, thrd) ||
get_thread(d.queue_, thrd);
};

bool found = true;
if (d.consecutive_high_priority_tasks_ >=
max_consecutive_high_priority_tasks &&
get_other_thread())
{
d.consecutive_high_priority_tasks_ = 0;
}
else if (num_thread < num_high_priority_queues_ &&
get_thread(d.high_priority_queue_, thrd))
{
if (d.consecutive_high_priority_tasks_ <
max_consecutive_high_priority_tasks)
{
++d.consecutive_high_priority_tasks_;
}
}
else if (get_other_thread())
{
d.consecutive_high_priority_tasks_ = 0;
}
else
{
found = false;
}

if (found)
{
// We found a task to run, however before running it we handle
// steal requests (assuming that there is more work left that
Expand Down
2 changes: 2 additions & 0 deletions libs/core/schedulers/tests/unit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ if(HPX_WITH_WORK_REQUESTING_SCHEDULERS)
tests
workrequesting_numa
workrequesting_numa_hint
workrequesting_pending_boost
workrequesting_random_victim
workrequesting_run_as_child
workrequesting_steal
Expand All @@ -35,6 +36,7 @@ if(HPX_WITH_WORK_REQUESTING_SCHEDULERS)
workrequesting_suspend_resume
)
set(workrequesting_staged_stealing_PARAMETERS THREADS_PER_LOCALITY 4)
set(workrequesting_pending_boost_PARAMETERS THREADS_PER_LOCALITY 1)
endif()

set(workrequesting_numa_hint_PARAMETERS THREADS_PER_LOCALITY 4)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (c) 2026 Divyanshi Talreja
//
// 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)

// A thread repeatedly yielding with pending_boost must not starve normal work
// on the same worker. The bounded yield count prevents the test from hanging.

#include <hpx/future.hpp>
#include <hpx/init.hpp>
#include <hpx/modules/execution_base.hpp>
#include <hpx/modules/testing.hpp>

#include <atomic>
#include <cstddef>
#include <string>
#include <vector>

int hpx_main()
{
std::atomic<bool> normal_task_ran{false};
auto normal_task = hpx::async([&normal_task_ran]() {
normal_task_ran.store(true, std::memory_order_relaxed);
});

for (std::size_t i = 0;
i != 64 && !normal_task_ran.load(std::memory_order_relaxed); ++i)
{
hpx::execution_base::this_thread::yield_k(17);
}

bool const completed_while_boosted =
normal_task_ran.load(std::memory_order_relaxed);
normal_task.wait();
HPX_TEST(completed_while_boosted);

return hpx::local::finalize();
}

int main(int, char* argv[])
{
for (char const* scheduler :
{"local-workrequesting-fifo", "local-workrequesting-mc"})
{
std::string const queuing = std::string("--hpx:queuing=") + scheduler;
std::vector<char*> av = {argv[0], const_cast<char*>(queuing.c_str())};

hpx::local::init_params init_args;
init_args.cfg = {"hpx.os_threads=1"};

HPX_TEST_EQ(hpx::local::init(hpx_main, static_cast<int>(av.size()),
av.data(), init_args),
0);
}

return hpx::util::report_errors();
}
Loading