|
Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
|
Tracks cooperation patterns between workers for locality-aware stealing. More...
#include <work_affinity_tracker.h>
Public Member Functions | |
| work_affinity_tracker (std::size_t worker_count, std::size_t history_size=16) | |
| Construct a work affinity tracker. | |
| work_affinity_tracker ()=default | |
| Default constructor - creates an empty tracker. | |
| work_affinity_tracker (work_affinity_tracker &&other) noexcept | |
| Move constructor. | |
| auto | operator= (work_affinity_tracker &&other) noexcept -> work_affinity_tracker & |
| Move assignment operator. | |
| work_affinity_tracker (const work_affinity_tracker &)=delete | |
| auto | operator= (const work_affinity_tracker &) -> work_affinity_tracker &=delete |
| ~work_affinity_tracker ()=default | |
| Destructor. | |
| void | record_cooperation (std::size_t thief_id, std::size_t victim_id) |
| Record a cooperation event between two workers. | |
| auto | get_affinity (std::size_t worker_a, std::size_t worker_b) const -> double |
| Get the affinity score between two workers. | |
| auto | get_preferred_victims (std::size_t worker_id, std::size_t max_count) const -> std::vector< std::size_t > |
| Get preferred victims for a worker, sorted by affinity. | |
| void | reset () |
| Reset all affinity data. | |
| auto | worker_count () const -> std::size_t |
| Get the number of workers being tracked. | |
| auto | history_size () const -> std::size_t |
| Get the configured history size. | |
| auto | total_cooperations () const -> std::uint64_t |
| Get total cooperation events recorded. | |
Static Public Attributes | |
| static constexpr std::size_t | MAX_TRACKED_WORKERS = 32 |
| Maximum number of workers tracked for affinity. | |
Private Member Functions | |
| auto | get_matrix_index (std::size_t worker_a, std::size_t worker_b) const -> std::size_t |
| Get the matrix index for a worker pair. | |
Static Private Member Functions | |
| static auto | normalize_pair (std::size_t a, std::size_t b) -> std::pair< std::size_t, std::size_t > |
| Normalize worker IDs to ensure a < b. | |
Private Attributes | |
| std::size_t | worker_count_ {0} |
| std::size_t | tracked_count_ {0} |
| std::size_t | history_size_ {16} |
| std::size_t | matrix_size_ {0} |
| std::unique_ptr< std::atomic< std::uint64_t >[]> | cooperation_matrix_ |
| std::atomic< std::uint64_t > | total_cooperations_ {0} |
Tracks cooperation patterns between workers for locality-aware stealing.
This class maintains a cooperation matrix that records successful work-stealing interactions between worker threads. Workers that frequently exchange work develop higher affinity scores, making them preferred victims for future steals.
When workers frequently cooperate (steal from each other successfully), they likely share related work that benefits from cache locality. By preferring to steal from high-affinity workers, we improve cache utilization and reduce memory access latency.
All methods are thread-safe. The cooperation matrix uses atomic operations for lock-free updates and reads.
record_cooperation(): Uses relaxed ordering (eventual consistency is acceptable)get_affinity(): Uses relaxed ordering (snapshot view)get_preferred_victims(): Uses acquire ordering for consistent rankingThe tracker implements a simple decay mechanism: new interactions are weighted more heavily than older ones. This ensures the affinity scores reflect recent behavior rather than historical patterns that may no longer be relevant.
Definition at line 69 of file work_affinity_tracker.h.
|
explicit |
Construct a work affinity tracker.
| worker_count | Number of workers to track |
| history_size | Size of history to consider for affinity calculations |
Definition at line 12 of file work_affinity_tracker.cpp.
References cooperation_matrix_, matrix_size_, and tracked_count_.
|
default |
Default constructor - creates an empty tracker.
|
noexcept |
Move constructor.
Definition at line 37 of file work_affinity_tracker.cpp.
|
delete |
|
default |
Destructor.
|
nodiscard |
Get the affinity score between two workers.
| worker_a | First worker ID |
| worker_b | Second worker ID |
The affinity is symmetric: get_affinity(a, b) == get_affinity(b, a)
The returned score is normalized based on the history size, making it comparable across different tracker configurations.
Definition at line 92 of file work_affinity_tracker.cpp.
|
nodiscardprivate |
Get the matrix index for a worker pair.
Definition at line 184 of file work_affinity_tracker.cpp.
Referenced by record_cooperation().
|
nodiscard |
Get preferred victims for a worker, sorted by affinity.
| worker_id | The worker seeking victims |
| max_count | Maximum number of victims to return |
Returns workers with the highest affinity scores, excluding the requesting worker itself. Workers with zero affinity may be included if there aren't enough high-affinity workers.
Definition at line 117 of file work_affinity_tracker.cpp.
|
nodiscard |
Get the configured history size.
Definition at line 174 of file work_affinity_tracker.cpp.
References history_size_.
|
staticprivate |
Normalize worker IDs to ensure a < b.
Definition at line 196 of file work_affinity_tracker.cpp.
|
delete |
|
noexcept |
Move assignment operator.
Definition at line 52 of file work_affinity_tracker.cpp.
| void kcenon::thread::work_affinity_tracker::record_cooperation | ( | std::size_t | thief_id, |
| std::size_t | victim_id ) |
Record a cooperation event between two workers.
| thief_id | Worker that stole work (the one receiving work) |
| victim_id | Worker that provided work (the one being stolen from) |
This method is typically called after a successful steal operation. It updates the cooperation matrix to reflect the interaction.
Definition at line 75 of file work_affinity_tracker.cpp.
References cooperation_matrix_, get_matrix_index(), matrix_size_, total_cooperations_, and tracked_count_.
| void kcenon::thread::work_affinity_tracker::reset | ( | ) |
Reset all affinity data.
Clears all cooperation history, resetting all affinities to zero. Useful when worker roles change significantly.
Definition at line 157 of file work_affinity_tracker.cpp.
References cooperation_matrix_, matrix_size_, and total_cooperations_.
|
nodiscard |
Get total cooperation events recorded.
Definition at line 179 of file work_affinity_tracker.cpp.
References total_cooperations_.
|
nodiscard |
Get the number of workers being tracked.
Definition at line 169 of file work_affinity_tracker.cpp.
References worker_count_.
|
private |
Definition at line 205 of file work_affinity_tracker.h.
Referenced by record_cooperation(), reset(), and work_affinity_tracker().
|
private |
|
private |
Definition at line 201 of file work_affinity_tracker.h.
Referenced by record_cooperation(), reset(), and work_affinity_tracker().
|
staticconstexpr |
Maximum number of workers tracked for affinity.
When the pool has more workers than this limit, only the first MAX_TRACKED_WORKERS are tracked in the cooperation matrix to prevent O(n^2) memory growth. Workers beyond this cap can still participate in work stealing but won't have affinity data.
Definition at line 80 of file work_affinity_tracker.h.
|
private |
Definition at line 208 of file work_affinity_tracker.h.
Referenced by record_cooperation(), reset(), and total_cooperations().
|
private |
Definition at line 199 of file work_affinity_tracker.h.
Referenced by record_cooperation(), and work_affinity_tracker().
|
private |