// Copyright (c) 2007-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) #include #include #include #include #include #include #include #include #include #if defined(HPX_HAVE_SCHEDULER_LOCAL_STORAGE) #include #endif #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include /////////////////////////////////////////////////////////////////////////////// namespace hpx::threads::policies { #if defined(HPX_HAVE_THREAD_MANAGER_IDLE_BACKOFF) namespace { // The number of spin rounds per microsecond, measured once per // process (the best of three runs of 1000 rounds, each with a load of // an atomic for the look at a queue). A pause takes very different // times on different CPUs, and the spin counts rounds instead of // reading the clock, which can cost as much as a round. double spin_rounds_per_microsecond() noexcept { static double const rounds_per_us = [] { constexpr int rounds = 1000; std::atomic queue_length{0}; double best = (std::numeric_limits::max)(); for (int run = 0; run != 3; ++run) { auto const start = std::chrono::steady_clock::now(); for (int i = 0; i != rounds; ++i) { detail::spin_round(); if (queue_length.load(std::memory_order_relaxed) != 0) { break; } } best = (std::min) (best, std::chrono::duration( std::chrono::steady_clock::now() - start) .count()); } // HPX_SMT_PAUSE may be empty, keep the rate finite return rounds / (std::max) (best, 0.001); }(); return rounds_per_us; } } // namespace #endif scheduler_base::scheduler_base(std::size_t num_threads, char const* description, thread_queue_init_parameters const& thread_queue_init, scheduler_mode mode) : modes_(num_threads) #if defined(HPX_HAVE_THREAD_MANAGER_IDLE_BACKOFF) , max_idle_backoff_time_(thread_queue_init.max_idle_backoff_time_) , min_idle_backoff_time_(thread_queue_init.min_idle_backoff_time_) , spin_rounds_per_us_(spin_rounds_per_microsecond()) , idle_backoff_steal_check_rounds_((std::max) (std::uint64_t(1), static_cast(std::llround( thread_queue_init.idle_backoff_steal_check_interval_ * spin_rounds_per_us_)))) , wait_count_data_(num_threads) #endif , suspend_mtxs_(num_threads) , suspend_conds_(num_threads) , pu_mtxs_(num_threads) , states_(num_threads) , description_(description) , thread_queue_init_(thread_queue_init) , parent_pool_(nullptr) , background_thread_count_(0) , polling_function_mpi_(&null_polling_function) , polling_function_cuda_(&null_polling_function) , polling_function_sycl_(&null_polling_function) , polling_work_count_function_mpi_(&null_polling_work_count_function) , polling_work_count_function_cuda_(&null_polling_work_count_function) , polling_work_count_function_sycl_(&null_polling_work_count_function) { for (std::size_t i = 0; i != num_threads; ++i) { modes_[i].data_.store(mode, std::memory_order::relaxed); states_[i].data_.store( hpx::state::initialized, std::memory_order::relaxed); } #if defined(HPX_HAVE_THREAD_MANAGER_IDLE_BACKOFF) sleep_mask_ = std::vector>>( (num_threads + 63) / 64); #endif } #if defined(HPX_HAVE_THREAD_MANAGER_IDLE_BACKOFF) // is there work this worker could run (its own queues, or the queues it // steals from if it may steal)? Called by idle workers. bool scheduler_base::has_visible_work(std::size_t num_thread) const { return has_work_for(num_thread, (modes_[num_thread].data_.load(std::memory_order_relaxed) & policies::scheduler_mode::enable_stealing) != 0); } // wake a worker that announced itself as sleeping. Taking the mutex makes // sure the sleeper is already waiting (it holds the mutex from its // announcement until wait_for releases it). void scheduler_base::notify_worker(std::size_t i) { auto& data = wait_count_data_[i].data_; { std::lock_guard l(data.wait_mtx); data.wait_count.store(0, std::memory_order_relaxed); } data.wait_cond.notify_one(); } // claim and wake one sleeping worker, the hinted one if it sleeps, // otherwise the next one after it bool scheduler_base::wake_one(std::size_t hint) { signal_sentinel(); std::size_t const size = wait_count_data_.size(); if (hint >= size) { hint = wake_start_.fetch_add(1, std::memory_order_relaxed) % size; } // No fence between queuing the new thread and reading the sleep mask // below. A seq_cst fence here (paired with one in idle_callback) // would make sure that either this sees the announcement of a worker // going to sleep or that worker sees the new thread in its last // check. It would run for every new nano and small thread, also when // no worker sleeps, and costs those up to several percent of their // throughput. Without it, the store that queued the thread may become // visible only after this load (the memory model allows a store to be // reordered with a later load, and x86 and AArch64 both do it), so a // worker that announces itself at the same time may be missed here // while it misses the thread. The thread is not lost: the worker that // queued it, or any worker that looks at that queue, still runs it, // and the sleeper wakes up after its backoff period (at most // max_idle_backoff_time) and finds it. This is the race do_some_work // accepts. // start at the hint's word, prefer the hint's bit, then the lowest // set bit std::size_t const words = sleep_mask_.size(); for (std::size_t k = 0; k != words; ++k) { std::size_t const w = (hint / 64 + k) % words; auto& word = sleep_mask_[w].data_; std::uint64_t bits = word.load(std::memory_order_relaxed); while (bits != 0) { std::uint64_t bit = bits & (~bits + 1); if (k == 0) { std::uint64_t const hint_bit = std::uint64_t(1) << (hint % 64); if (bits & hint_bit) bit = hint_bit; } // the claim only needs to be atomic: two wakers must not // claim the same sleeper, no other memory is ordered by it std::uint64_t const old = word.fetch_and(~bit, std::memory_order_relaxed); if (old & bit) { notify_worker(w * 64 + static_cast(std::countr_zero(bit))); return true; } bits = old & ~bit; } } return false; } // claim and wake the given worker if it sleeps bool scheduler_base::wake_if_sleeping(std::size_t num_thread) { signal_sentinel(); // No fence between queuing the new thread and reading the sleep mask // (see wake_one): if the target announces itself at the same time, // this may miss it while the target misses the new thread. The // target then sleeps for its backoff period (at most // max_idle_backoff_time) before it runs the thread, unless another // worker steals it first. The claim is relaxed for the same reason // as in wake_one. std::uint64_t const bit = std::uint64_t(1) << (num_thread % 64); auto& word = sleep_mask_[num_thread / 64].data_; if ((word.load(std::memory_order_relaxed) & bit) != 0 && (word.fetch_and(~bit, std::memory_order_relaxed) & bit) != 0) { notify_worker(num_thread); return true; } return false; } // The sentinel spins for the period it would have slept, in rounds of // the pauses of step, as other spinning workers do. It looks at its own // queues every round, and at the queues it steals from right after a // thread was queued anywhere (sentinel_.new_threads), otherwise every // step.steal_check_rounds rounds (hpx.idle_backoff_steal_check_interval // microseconds). A thread queued while this worker becomes the sentinel // may not be counted; it is found at the next of these regular looks. // Out of line, so that idle_callback keeps its size. HPX_NOINLINE void scheduler_base::spin_as_sentinel( std::size_t const num_thread, idle_step const& step) { HPX_ASSERT(step.pauses_per_round != 0 && step.steal_check_rounds != 0); auto& data = sentinel_.data_; // a round of more pauses lasts longer than the measured one double const rounds_per_us = spin_rounds_per_us_ * scheduling_policy::calibration_pauses_per_round / step.pauses_per_round; auto const rounds = (std::max) (std::uint64_t(1), static_cast(std::llround( static_cast(step.period.count()) * rounds_per_us))); bool const calibration_round = step.pauses_per_round == scheduling_policy::calibration_pauses_per_round; std::uint64_t seen = data.new_threads.load(std::memory_order_relaxed); for (std::uint64_t round = 1; round <= rounds; ++round) { if (calibration_round) { detail::spin_round(); } else { detail::spin_round(step.pauses_per_round); } if (get_queue_length(num_thread) != 0) { break; } std::uint64_t const now = data.new_threads.load(std::memory_order_relaxed); if ((now != seen || round % step.steal_check_rounds == 0) && has_visible_work(num_thread)) { break; } seen = now; } data.worker.store(0, std::memory_order_relaxed); } #endif #if defined(HPX_HAVE_THREAD_MANAGER_IDLE_BACKOFF) // Short periods are spent spinning: a timed wait lasts at least the time // the OS takes to wake the thread up (a millisecond or more on Windows, // the timer slack of 50 us on Linux), so a short sleep would be a long // one. A spinning worker looks at its own queues every round, but at the // queues it steals from only every hpx.idle_backoff_steal_check_interval // microseconds, since that reads the length of the queue of every victim. // It counts rounds instead of reading the clock: the period and the check // interval are converted to rounds with the rate measured at startup, // because a round lasts from a few nanoseconds (yield on AArch64) to most // of a microsecond (pause on recent Intel cores). Inlined into the // instance of idle_callback_with_rules below, which calls it under the // lock of the worker. HPX_FORCEINLINE idle_step scheduler_base::idle_step_for( std::size_t, std::uint32_t const count) const { // exponential back-off with a maximum sleep time constexpr double max_exponent = std::numeric_limits::max_exponent - 1; double const exponent = (std::min) (static_cast(count), max_exponent); idle_step step{}; step.period = std::chrono::microseconds(std::lround( (std::min) (max_idle_backoff_time_, std::pow(2.0, exponent)))); step.pauses_per_round = scheduling_policy::calibration_pauses_per_round; step.steal_check_rounds = idle_backoff_steal_check_rounds_; if (static_cast(step.period.count()) >= min_idle_backoff_time_) { return step; // sleep, or spin as the sentinel } step.spin_rounds = (std::max) (std::uint64_t(1), static_cast( std::llround(static_cast(step.period.count()) * spin_rounds_per_us_))); return step; } #endif bool scheduler_base::idle_callback(std::size_t num_thread) { return idle_callback_with_rules(num_thread, fixed_scheduling_rules(0)); } template HPX_CORE_EXPORT bool scheduler_base::idle_callback_with_rules( std::size_t, fixed_scheduling_rules const&); /// This function gets called by the thread-manager whenever new work /// has been added, allowing the scheduler to reactivate one or more of /// possibly idling OS threads void scheduler_base::do_some_work([[maybe_unused]] std::size_t num_thread) { #if defined(HPX_HAVE_THREAD_MANAGER_IDLE_BACKOFF) HPX_TRACING_MARK_EVENT("do_some_work"); // The vectors don't change while the scheduler runs. Indexing them in // the loops reloads their begin and end on every step (the exchange // below may change any memory), from a line of the scheduler that the // calling core often has to fetch again. auto* const wait_data = wait_count_data_.data(); auto const* const modes = modes_.data(); // Only touch the wait data of threads that are actually backing off. // Unconditionally writing wait_count would pull the cache line of // every (busy) worker into the calling core on each new work item. A // thread that is about to start backing off (wait_count == 0) is not // notified; its first backoff period is one microsecond. // // As before, the notification is sent without holding wait_mtx: if it // arrives while idle_callback is between incrementing wait_count and // waiting, it is lost and that thread sleeps for the period it has // already computed (at most max_idle_backoff_time). auto const wake_up = [wait_data](std::size_t i) noexcept { // read first: most workers are not backing off, keep their cache // lines shared in that case auto& data = wait_data[i].data_; if (data.wait_count.load(std::memory_order_relaxed) != 0 && data.wait_count.exchange(0, std::memory_order_relaxed) != 0) { data.wait_cond.notify_one(); } }; auto const size = wait_count_data_.size(); HPX_ASSERT(modes_.size() >= size); if (static_cast(-1) == num_thread) { signal_sentinel(); for (std::size_t i = 0; i != size; ++i) { if (modes[i].data_.load(std::memory_order_relaxed) & policies::scheduler_mode::enable_idle_backoff) { wake_up(i); } } } else if (modes_[num_thread].data_.load(std::memory_order_relaxed) & policies::scheduler_mode::enable_idle_backoff) { signal_sentinel(); for (std::size_t i = 0; i != size; ++i) { wake_up(i); } } #endif } void scheduler_base::suspend(std::size_t num_thread) { HPX_ASSERT(num_thread < suspend_conds_.size()); std::unique_lock l(suspend_mtxs_[num_thread]); hpx::state expected_pre_sleep = hpx::state::pre_sleep; if (states_[num_thread].data_.compare_exchange_strong( expected_pre_sleep, hpx::state::sleeping)) { suspend_conds_[num_thread].wait(l, [this, num_thread]() noexcept { return states_[num_thread].data_.load() != hpx::state::sleeping; }); } [[maybe_unused]] hpx::state const current_state = states_[num_thread].data_.load(); HPX_ASSERT(current_state == hpx::state::pre_sleep || current_state == hpx::state::running || current_state == hpx::state::stopping || current_state == hpx::state::terminating); } void scheduler_base::resume(std::size_t num_thread) { auto resume_pu = [this](std::size_t pu) { std::lock_guard l(suspend_mtxs_[pu]); hpx::state expected = hpx::state::sleeping; if (!states_[pu].data_.compare_exchange_strong( expected, hpx::state::running)) { expected = hpx::state::pre_sleep; states_[pu].data_.compare_exchange_strong( expected, hpx::state::running); } // Always notify under the lock. The state may have changed to // stopping or terminating while the worker was waiting. suspend_conds_[pu].notify_one(); }; if (num_thread == static_cast(-1)) { for (std::size_t i = 0; i < suspend_conds_.size(); ++i) { resume_pu(i); } } else { HPX_ASSERT(num_thread < suspend_conds_.size()); resume_pu(num_thread); } } std::size_t scheduler_base::select_active_pu( std::size_t num_thread, bool allow_fallback) { if (modes_[num_thread].data_.load(std::memory_order_relaxed) & threads::policies::scheduler_mode::enable_elasticity) { std::size_t states_size = states_.size(); if (!allow_fallback) { // Try indefinitely as long as at least one thread is available // for scheduling. Increase allowed state if no threads are // available for scheduling. auto max_allowed_state = hpx::state::suspended; hpx::util::yield_while([this, states_size, &num_thread, &max_allowed_state]() { std::size_t num_allowed_threads = 0; for (std::size_t offset = 0; offset < states_size; ++offset) { std::size_t const num_thread_local = (num_thread + offset) % states_size; { std::unique_lock l( pu_mtxs_[num_thread_local], std::try_to_lock); if (l.owns_lock()) { if (states_[num_thread_local].data_.load( std::memory_order_relaxed) <= max_allowed_state) { num_thread = num_thread_local; return false; } } } if (states_[num_thread_local].data_.load( std::memory_order_relaxed) <= max_allowed_state) { ++num_allowed_threads; } } if (0 == num_allowed_threads) { if (max_allowed_state <= hpx::state::suspended) { max_allowed_state = hpx::state::sleeping; } else if (max_allowed_state <= hpx::state::sleeping) { max_allowed_state = hpx::state::stopping; } else { // All threads are terminating or stopped. Just // return num_thread to avoid infinite loop. return false; } } // Yield after trying all pus, then try again return true; }); return num_thread; } // Try all pus only once if fallback is allowed HPX_ASSERT(num_thread != static_cast(-1)); for (std::size_t offset = 0; offset < states_size; ++offset) { std::size_t const num_thread_local = (num_thread + offset) % states_size; std::unique_lock l( pu_mtxs_[num_thread_local], std::try_to_lock); if (l.owns_lock() && states_[num_thread_local].data_.load( std::memory_order_relaxed) <= hpx::state::suspended) { return num_thread_local; } } } return num_thread; } // allow to access/manipulate states std::atomic& scheduler_base::get_state(std::size_t num_thread) { HPX_ASSERT(num_thread < states_.size()); return states_[num_thread].data_; } std::atomic const& scheduler_base::get_state( std::size_t num_thread) const { HPX_ASSERT(num_thread < states_.size()); return states_[num_thread].data_; } void scheduler_base::set_all_states(hpx::state s) { for (auto& state : states_) { state.data_.store(s); } } void scheduler_base::set_all_states_at_least(hpx::state s) { for (auto& state : states_) { if (state.data_.load(std::memory_order_relaxed) < s) { state.data_.store(s, std::memory_order_release); } } } // return whether all states are at least at the given one bool scheduler_base::has_reached_state(hpx::state s) const { for (auto const& state : states_) { if (state.data_.load(std::memory_order_relaxed) < s) return false; } return true; } bool scheduler_base::is_state(hpx::state s) const { for (auto const& state : states_) { if (state.data_.load(std::memory_order_relaxed) != s) return false; } return true; } std::pair scheduler_base::get_minmax_state() const { std::pair result( hpx::state::last_valid_runtime_state, hpx::state::first_valid_runtime_state); for (auto const& state_iter : states_) { hpx::state s = state_iter.data_.load(std::memory_order_relaxed); result.first = (std::min) (result.first, s); result.second = (std::max) (result.second, s); } return result; } bool scheduler_base::worker_matches_pu_mask(std::size_t worker_idx, hpx::threads::mask_cref_type pu_mask) const noexcept { HPX_ASSERT(parent_pool_ != nullptr); auto const worker_mask = parent_pool_->get_used_processing_unit(worker_idx); return threads::bit_and(worker_mask, pu_mask); } template void scheduler_base::apply_to_matching_modes( hpx::threads::mask_cref_type pu_mask, F&& f) noexcept { if (parent_pool_ != nullptr) { for (std::size_t j = 0; j != modes_.size(); ++j) { if (worker_matches_pu_mask(j, pu_mask)) { f(modes_[j].data_); } } } else { std::size_t const size = hpx::threads::mask_size(pu_mask); for (std::size_t i = 0, j = 0; i != size && j < modes_.size(); ++i) { if (hpx::threads::test(pu_mask, i)) { f(modes_[j++].data_); } } } } // get/set scheduler mode void scheduler_base::set_scheduler_mode( scheduler_mode mode, hpx::threads::mask_cref_type pu_mask) noexcept { apply_to_matching_modes(pu_mask, [this, mode](auto& mode_atomic) { mode_atomic.store( adjust_scheduler_mode(mode), std::memory_order_release); }); do_some_work(static_cast(-1)); } void scheduler_base::add_scheduler_mode( scheduler_mode mode, hpx::threads::mask_cref_type pu_mask) noexcept { apply_to_matching_modes(pu_mask, [this, mode](auto& mode_atomic) { auto old_mode = mode_atomic.load(std::memory_order_relaxed); while (!mode_atomic.compare_exchange_weak(old_mode, adjust_scheduler_mode(old_mode | mode), std::memory_order_release, std::memory_order_relaxed)) { } }); } void scheduler_base::remove_scheduler_mode( scheduler_mode mode, hpx::threads::mask_cref_type pu_mask) noexcept { apply_to_matching_modes(pu_mask, [this, mode](auto& mode_atomic) { auto old_mode = mode_atomic.load(std::memory_order_relaxed); while (!mode_atomic.compare_exchange_weak(old_mode, adjust_scheduler_mode( static_cast(old_mode & ~mode)), std::memory_order_release, std::memory_order_relaxed)) { } }); } void scheduler_base::add_remove_scheduler_mode(scheduler_mode to_add_mode, scheduler_mode to_remove_mode, hpx::threads::mask_cref_type pu_mask) noexcept { apply_to_matching_modes( pu_mask, [this, to_add_mode, to_remove_mode](auto& mode_atomic) { auto old_mode = mode_atomic.load(std::memory_order_relaxed); while (!mode_atomic.compare_exchange_weak(old_mode, adjust_scheduler_mode(static_cast( (old_mode | to_add_mode) & ~to_remove_mode)), std::memory_order_release, std::memory_order_relaxed)) { } }); } void scheduler_base::update_scheduler_mode(scheduler_mode mode, bool set, hpx::threads::mask_cref_type pu_mask) noexcept { if (set) { add_scheduler_mode(mode, pu_mask); } else { remove_scheduler_mode(mode, pu_mask); } } /////////////////////////////////////////////////////////////////////////// std::int64_t scheduler_base::get_background_thread_count() const noexcept { return background_thread_count_; } void scheduler_base::enter_scheduling_loop() noexcept { // publishes nothing, the count only has to cover this worker workers_in_loop_.fetch_add(1, std::memory_order_relaxed); } bool scheduler_base::leave_scheduling_loop() { // release: makes the work this worker scheduled on other queues // visible to the last worker; acquire: the last worker synchronizes // with all earlier decrements (they form one release sequence), so // the thread counts below include that work std::uint64_t const before = workers_in_loop_.fetch_sub(1, std::memory_order_acq_rel); if ((before & workers_in_loop_mask) != 1) return true; // This is the last worker. All others are idle and have left, so no // HPX thread of this pool can schedule new work anymore. A creation // from outside the workers that began before may not have queued its // thread yet: wait for it (it is short, and every later one sees // that no worker is left and gives up). Acquire: synchronizes with // end_outside_creation, so the thread counts below include its // thread. Whatever is left on any queue then is left there for good // unless this worker runs it. if (before >= outside_creation_unit) { hpx::util::yield_while([this]() { return workers_in_loop_.load(std::memory_order_acquire) >= outside_creation_unit; }); } if (get_thread_count() <= get_background_thread_count()) return true; // take the work over, see scheduling_loop workers_in_loop_.fetch_add(1, std::memory_order_relaxed); return false; } void scheduler_base::abandon_scheduling_loop() noexcept { // release: as in leave_scheduling_loop workers_in_loop_.fetch_sub(1, std::memory_order_release); } bool scheduler_base::begin_outside_creation() noexcept { // The creations and the workers are counted in the same atomic, so // either this comes first and the last worker to leave waits for it, // or the last worker has left before and this gives up std::uint64_t const before = workers_in_loop_.fetch_add( outside_creation_unit, std::memory_order_relaxed); if ((before & workers_in_loop_mask) == 0 && has_reached_state(hpx::state::stopping)) { workers_in_loop_.fetch_sub( outside_creation_unit, std::memory_order_relaxed); return false; } return true; } void scheduler_base::end_outside_creation() noexcept { // release: the last worker that waits for this sees the new thread workers_in_loop_.fetch_sub( outside_creation_unit, std::memory_order_release); } void scheduler_base::increment_background_thread_count() noexcept { ++background_thread_count_; } void scheduler_base::decrement_background_thread_count() noexcept { --background_thread_count_; } #if defined(HPX_HAVE_SCHEDULER_LOCAL_STORAGE) coroutines::detail::tss_data_node* scheduler_base::find_tss_data( void const* key) { if (!thread_data_) return nullptr; return thread_data_->find(key); } void scheduler_base::add_new_tss_node(void const* key, std::shared_ptr const& func, void* tss_data) { if (!thread_data_) { thread_data_ = std::make_shared(); } thread_data_->insert(key, func, tss_data); } void scheduler_base::erase_tss_node(void const* key, bool cleanup_existing) { if (thread_data_) thread_data_->erase(key, cleanup_existing); } void* scheduler_base::get_tss_data(void const* key) { if (coroutines::detail::tss_data_node* const current_node = find_tss_data(key)) { return current_node->get_value(); } return nullptr; } void scheduler_base::set_tss_data(void const* key, std::shared_ptr const& func, void* tss_data, bool cleanup_existing) { if (coroutines::detail::tss_data_node* const current_node = find_tss_data(key)) { if (func || (tss_data != 0)) current_node->reinit(func, tss_data, cleanup_existing); else erase_tss_node(key, cleanup_existing); } else if (func || (tss_data != 0)) { add_new_tss_node(key, func, tss_data); } } #endif std::ptrdiff_t scheduler_base::get_stack_size( threads::thread_stacksize stacksize) const noexcept { if (stacksize == thread_stacksize::current) { stacksize = get_self_stacksize_enum(); } HPX_ASSERT(stacksize != thread_stacksize::current); switch (stacksize) { case thread_stacksize::small_: return thread_queue_init_.small_stacksize_; case thread_stacksize::medium: return thread_queue_init_.medium_stacksize_; case thread_stacksize::large: return thread_queue_init_.large_stacksize_; case thread_stacksize::huge: return thread_queue_init_.huge_stacksize_; case thread_stacksize::nostack: return (std::numeric_limits::max)(); default: HPX_ASSERT_MSG( false, util::format("Invalid stack size {1}", stacksize)); break; } return thread_queue_init_.small_stacksize_; } void scheduler_base::set_mpi_polling_functions( polling_function_ptr mpi_func, polling_work_count_function_ptr mpi_work_count_func) { polling_function_mpi_.store(mpi_func, std::memory_order_relaxed); polling_work_count_function_mpi_.store( mpi_work_count_func, std::memory_order_relaxed); } void scheduler_base::clear_mpi_polling_function() { polling_function_mpi_.store( &null_polling_function, std::memory_order_relaxed); polling_work_count_function_mpi_.store( &null_polling_work_count_function, std::memory_order_relaxed); } void scheduler_base::set_cuda_polling_functions( polling_function_ptr cuda_func, polling_work_count_function_ptr cuda_work_count_func) { polling_function_cuda_.store(cuda_func, std::memory_order_relaxed); polling_work_count_function_cuda_.store( cuda_work_count_func, std::memory_order_relaxed); } void scheduler_base::clear_cuda_polling_function() { polling_function_cuda_.store( &null_polling_function, std::memory_order_relaxed); polling_work_count_function_cuda_.store( &null_polling_work_count_function, std::memory_order_relaxed); } void scheduler_base::set_sycl_polling_functions( polling_function_ptr sycl_func, polling_work_count_function_ptr sycl_work_count_func) { polling_function_sycl_.store(sycl_func, std::memory_order_relaxed); polling_work_count_function_sycl_.store( sycl_work_count_func, std::memory_order_relaxed); } void scheduler_base::clear_sycl_polling_function() { polling_function_sycl_.store( &null_polling_function, std::memory_order_relaxed); polling_work_count_function_sycl_.store( &null_polling_work_count_function, std::memory_order_relaxed); } detail::polling_status scheduler_base::custom_polling_function() const { detail::polling_status status = detail::polling_status::idle; #if defined(HPX_HAVE_MODULE_ASYNC_MPI) if ((*polling_function_mpi_.load(std::memory_order_relaxed))() == detail::polling_status::busy) { status = detail::polling_status::busy; } #endif #if defined(HPX_HAVE_MODULE_ASYNC_CUDA) if ((*polling_function_cuda_.load(std::memory_order_relaxed))() == detail::polling_status::busy) { status = detail::polling_status::busy; } #endif #if defined(HPX_HAVE_MODULE_ASYNC_SYCL) if ((*polling_function_sycl_.load(std::memory_order_relaxed))() == detail::polling_status::busy) { status = detail::polling_status::busy; } #endif return status; } std::size_t scheduler_base::get_polling_work_count() const { std::size_t work_count = 0; #if defined(HPX_HAVE_MODULE_ASYNC_MPI) work_count += polling_work_count_function_mpi_.load(std::memory_order_relaxed)(); #endif #if defined(HPX_HAVE_MODULE_ASYNC_CUDA) work_count += polling_work_count_function_cuda_.load(std::memory_order_relaxed)(); #endif #if defined(HPX_HAVE_MODULE_ASYNC_SYCL) work_count += polling_work_count_function_sycl_.load(std::memory_order_relaxed)(); #endif return work_count; } std::ostream& operator<<(std::ostream& os, scheduler_base const& scheduler) { os << scheduler.get_description() << "(" << &scheduler << ")"; return os; } } // namespace hpx::threads::policies