Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
work_affinity_tracker.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
14#include <atomic>
15#include <cstddef>
16#include <cstdint>
17#include <memory>
18#include <utility>
19#include <vector>
20
21namespace kcenon::thread
22{
23
70{
71public:
80 static constexpr std::size_t MAX_TRACKED_WORKERS = 32;
81
91 explicit work_affinity_tracker(std::size_t worker_count,
92 std::size_t history_size = 16);
93
98
103
107 auto operator=(work_affinity_tracker&& other) noexcept -> work_affinity_tracker&;
108
109 // Deleted copy operations (contains atomics)
112
117
128 void record_cooperation(std::size_t thief_id, std::size_t victim_id);
129
141 [[nodiscard]] auto get_affinity(std::size_t worker_a,
142 std::size_t worker_b) const -> double;
143
154 [[nodiscard]] auto get_preferred_victims(std::size_t worker_id,
155 std::size_t max_count) const
156 -> std::vector<std::size_t>;
157
164 void reset();
165
170 [[nodiscard]] auto worker_count() const -> std::size_t;
171
176 [[nodiscard]] auto history_size() const -> std::size_t;
177
182 [[nodiscard]] auto total_cooperations() const -> std::uint64_t;
183
184private:
189 [[nodiscard]] auto get_matrix_index(std::size_t worker_a,
190 std::size_t worker_b) const -> std::size_t;
191
195 static auto normalize_pair(std::size_t a, std::size_t b)
196 -> std::pair<std::size_t, std::size_t>;
197
198 std::size_t worker_count_{0};
199 std::size_t tracked_count_{0};
200 std::size_t history_size_{16};
201 std::size_t matrix_size_{0};
202
203 // Cooperation matrix stored as flattened upper triangular matrix
204 // Entry (i,j) where i < j is at index: i * worker_count - i*(i+1)/2 + j - i - 1
205 std::unique_ptr<std::atomic<std::uint64_t>[]> cooperation_matrix_;
206
207 // Total cooperation events for normalization
208 std::atomic<std::uint64_t> total_cooperations_{0};
209};
210
211} // namespace kcenon::thread
Tracks cooperation patterns between workers for locality-aware stealing.
auto operator=(const work_affinity_tracker &) -> work_affinity_tracker &=delete
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.
static constexpr std::size_t MAX_TRACKED_WORKERS
Maximum number of workers tracked for affinity.
auto history_size() const -> std::size_t
Get the configured 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_
work_affinity_tracker(const work_affinity_tracker &)=delete
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.
~work_affinity_tracker()=default
Destructor.
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.
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.
Definition thread_impl.h:17
STL namespace.