13 std::size_t history_size)
14 : worker_count_(worker_count)
15 , tracked_count_(
std::min(worker_count, MAX_TRACKED_WORKERS))
16 , history_size_(history_size)
18 , total_cooperations_(0)
27 std::make_unique<std::atomic<std::uint64_t>[]>(
matrix_size_);
38 : worker_count_(other.worker_count_)
39 , tracked_count_(other.tracked_count_)
40 , history_size_(other.history_size_)
41 , matrix_size_(other.matrix_size_)
42 , cooperation_matrix_(std::move(other.cooperation_matrix_))
43 , total_cooperations_(other.total_cooperations_.load(std::memory_order_relaxed))
45 other.worker_count_ = 0;
46 other.tracked_count_ = 0;
47 other.history_size_ = 0;
48 other.matrix_size_ = 0;
49 other.total_cooperations_.store(0, std::memory_order_relaxed);
57 worker_count_ = other.worker_count_;
58 tracked_count_ = other.tracked_count_;
59 history_size_ = other.history_size_;
60 matrix_size_ = other.matrix_size_;
61 cooperation_matrix_ = std::move(other.cooperation_matrix_);
62 total_cooperations_.store(
63 other.total_cooperations_.load(std::memory_order_relaxed),
64 std::memory_order_relaxed);
66 other.worker_count_ = 0;
67 other.tracked_count_ = 0;
68 other.history_size_ = 0;
69 other.matrix_size_ = 0;
70 other.total_cooperations_.store(0, std::memory_order_relaxed);
76 std::size_t victim_id)
93 std::size_t worker_b)
const ->
double
95 if (worker_a >= tracked_count_ || worker_b >= tracked_count_ ||
96 worker_a == worker_b || !cooperation_matrix_)
101 auto idx = get_matrix_index(worker_a, worker_b);
102 if (idx >= matrix_size_)
107 auto cooperation_count = cooperation_matrix_[idx].load(std::memory_order_relaxed);
108 if (cooperation_count == 0)
114 return static_cast<double>(cooperation_count) /
static_cast<double>(history_size_);
118 std::size_t max_count)
const
119 -> std::vector<std::size_t>
121 if (worker_id >= tracked_count_ || max_count == 0)
127 std::vector<std::pair<double, std::size_t>> affinities;
128 affinities.reserve(tracked_count_ - 1);
130 for (std::size_t i = 0; i < tracked_count_; ++i)
134 double affinity = get_affinity(worker_id, i);
135 affinities.emplace_back(affinity, i);
140 std::sort(affinities.begin(), affinities.end(),
141 [](
const auto& lhs,
const auto& rhs) {
142 return lhs.first > rhs.first;
146 std::vector<std::size_t>
result;
147 result.reserve(std::min(max_count, affinities.size()));
149 for (std::size_t i = 0; i < std::min(max_count, affinities.size()); ++i)
151 result.push_back(affinities[i].second);
185 std::size_t worker_b)
const
188 auto [i, j] = normalize_pair(worker_a, worker_b);
193 return (i * tracked_count_) - ((i * (i + 1)) / 2) + j - i - 1;
197 -> std::pair<std::size_t, std::size_t>
A template class representing either a value or an error.
Tracks cooperation patterns between workers for locality-aware stealing.
auto get_affinity(std::size_t worker_a, std::size_t worker_b) const -> double
Get the affinity score between two workers.
work_affinity_tracker()=default
Default constructor - creates an empty tracker.
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.
std::size_t history_size_
void record_cooperation(std::size_t thief_id, std::size_t victim_id)
Record a cooperation event between two workers.
std::atomic< std::uint64_t > total_cooperations_
std::size_t tracked_count_
std::size_t worker_count_
std::unique_ptr< std::atomic< std::uint64_t >[]> cooperation_matrix_
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 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.
auto operator=(work_affinity_tracker &&other) noexcept -> work_affinity_tracker &
Move assignment operator.
void reset()
Reset all affinity data.
auto total_cooperations() const -> std::uint64_t
Get total cooperation events recorded.
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.
Core threading foundation of the thread system library.
Tracks cooperation patterns between workers for locality-aware stealing.