Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
numa_work_stealer.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
12#pragma once
13
15#include "numa_topology.h"
18#include "work_stealing_stats.h"
19
20#include <cstddef>
21#include <functional>
22#include <memory>
23#include <random>
24#include <vector>
25
26namespace kcenon::thread
27{
28
29// Forward declarations
30class thread_worker;
31class job;
32
33namespace lockfree
34{
35template <typename T>
37} // namespace lockfree
38
99{
100public:
106 using deque_accessor_fn = std::function<lockfree::work_stealing_deque<job*>*(std::size_t)>;
107
113 using cpu_accessor_fn = std::function<int(std::size_t)>;
114
124 numa_work_stealer(std::size_t worker_count,
125 deque_accessor_fn deque_accessor,
126 cpu_accessor_fn cpu_accessor,
128
133
134 // Non-copyable, non-movable (contains atomics)
139
153 [[nodiscard]] auto steal_for(std::size_t worker_id) -> job*;
154
168 [[nodiscard]] auto steal_batch_for(std::size_t worker_id, std::size_t max_count)
169 -> std::vector<job*>;
170
175 [[nodiscard]] auto get_stats() const -> const work_stealing_stats&;
176
181 [[nodiscard]] auto get_stats_snapshot() const -> work_stealing_stats_snapshot;
182
186 void reset_stats();
187
192 [[nodiscard]] auto get_topology() const -> const numa_topology&;
193
198 [[nodiscard]] auto get_config() const -> const enhanced_work_stealing_config&;
199
207 void set_config(const enhanced_work_stealing_config& config);
208
209private:
216 [[nodiscard]] auto select_victims(std::size_t requester_id, std::size_t count)
217 -> std::vector<std::size_t>;
218
222 [[nodiscard]] auto select_victims_random(std::size_t requester_id, std::size_t count)
223 -> std::vector<std::size_t>;
224
228 [[nodiscard]] auto select_victims_round_robin(std::size_t requester_id, std::size_t count)
229 -> std::vector<std::size_t>;
230
234 [[nodiscard]] auto select_victims_adaptive(std::size_t requester_id, std::size_t count)
235 -> std::vector<std::size_t>;
236
240 [[nodiscard]] auto select_victims_numa_aware(std::size_t requester_id, std::size_t count)
241 -> std::vector<std::size_t>;
242
246 [[nodiscard]] auto select_victims_locality_aware(std::size_t requester_id, std::size_t count)
247 -> std::vector<std::size_t>;
248
252 [[nodiscard]] auto select_victims_hierarchical(std::size_t requester_id, std::size_t count)
253 -> std::vector<std::size_t>;
254
258 [[nodiscard]] auto calculate_batch_size(std::size_t victim_queue_size) const -> std::size_t;
259
263 [[nodiscard]] auto get_worker_cpu(std::size_t worker_id) const -> int;
264
268 [[nodiscard]] auto workers_on_same_node(std::size_t worker_a, std::size_t worker_b) const
269 -> bool;
270
274 void record_steal(std::size_t thief_id, std::size_t victim_id);
275
284
285 // Per-thread random generators for random victim selection
286 mutable std::mt19937_64 rng_;
287
288 // Round-robin state
289 mutable std::atomic<std::size_t> round_robin_index_{0};
290};
291
292} // namespace kcenon::thread
Calculates backoff delays for work-stealing operations.
Represents a unit of work (task) to be executed, typically by a job queue.
Definition job.h:136
Lock-free work-stealing deque based on Chase-Lev algorithm.
NUMA (Non-Uniform Memory Access) topology information.
NUMA-aware work stealer with enhanced victim selection policies.
auto select_victims_adaptive(std::size_t requester_id, std::size_t count) -> std::vector< std::size_t >
Select victims using adaptive (queue-size based) policy.
numa_work_stealer(numa_work_stealer &&)=delete
auto select_victims_round_robin(std::size_t requester_id, std::size_t count) -> std::vector< std::size_t >
Select victims using round-robin policy.
auto get_config() const -> const enhanced_work_stealing_config &
Get the current configuration.
auto select_victims_locality_aware(std::size_t requester_id, std::size_t count) -> std::vector< std::size_t >
Select victims using locality-aware policy.
std::atomic< std::size_t > round_robin_index_
auto get_stats() const -> const work_stealing_stats &
Get the current statistics.
auto get_stats_snapshot() const -> work_stealing_stats_snapshot
Get a snapshot of current statistics.
void set_config(const enhanced_work_stealing_config &config)
Update the configuration.
void record_steal(std::size_t thief_id, std::size_t victim_id)
Record a successful steal for affinity tracking.
numa_work_stealer(std::size_t worker_count, deque_accessor_fn deque_accessor, cpu_accessor_fn cpu_accessor, enhanced_work_stealing_config config={})
Construct a NUMA-aware work stealer.
auto workers_on_same_node(std::size_t worker_a, std::size_t worker_b) const -> bool
Check if two workers are on the same NUMA node.
numa_work_stealer & operator=(const numa_work_stealer &)=delete
auto get_worker_cpu(std::size_t worker_id) const -> int
Get the CPU ID for a worker.
auto get_topology() const -> const numa_topology &
Get the NUMA topology information.
numa_work_stealer(const numa_work_stealer &)=delete
std::function< int(std::size_t)> cpu_accessor_fn
Function type for getting worker's CPU affinity.
~numa_work_stealer()=default
Destructor.
auto select_victims_random(std::size_t requester_id, std::size_t count) -> std::vector< std::size_t >
Select victims using random policy.
enhanced_work_stealing_config config_
numa_work_stealer & operator=(numa_work_stealer &&)=delete
auto select_victims_hierarchical(std::size_t requester_id, std::size_t count) -> std::vector< std::size_t >
Select victims using hierarchical policy.
auto select_victims_numa_aware(std::size_t requester_id, std::size_t count) -> std::vector< std::size_t >
Select victims using NUMA-aware policy.
std::function< lockfree::work_stealing_deque< job * > *(std::size_t)> deque_accessor_fn
Function type for accessing worker's local deque.
auto steal_for(std::size_t worker_id) -> job *
Attempt to steal work for a worker.
auto steal_batch_for(std::size_t worker_id, std::size_t max_count) -> std::vector< job * >
Attempt to steal multiple jobs for a worker.
std::unique_ptr< work_affinity_tracker > affinity_tracker_
void reset_stats()
Reset all statistics to zero.
auto calculate_batch_size(std::size_t victim_queue_size) const -> std::size_t
Calculate batch size based on configuration and victim queue depth.
auto select_victims(std::size_t requester_id, std::size_t count) -> std::vector< std::size_t >
Select victim workers based on the configured policy.
std::unique_ptr< backoff_calculator > backoff_calculator_
Tracks cooperation patterns between workers for locality-aware stealing.
Configuration for enhanced work-stealing with NUMA awareness.
Core threading foundation of the thread system library.
Definition thread_impl.h:17
STL namespace.
NUMA node topology detection and information.
Backoff strategies for work-stealing operations.
Configuration for enhanced work-stealing with NUMA awareness.
Non-atomic snapshot of work-stealing statistics.
Statistics for work-stealing operations.
Tracks cooperation patterns between workers for locality-aware stealing.
Statistics snapshot for work-stealing performance monitoring.