brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
63 / 64
98.4%
Branches:
33 / 42
78.6%
Functions:
8 / 8
100.0%

src/cpu/thread_pool.cpp
Line Branch Exec Source
1 #include <brotensor/detail/cpu/thread_pool.h>
2
3 namespace brotensor::detail::cpu {
4
5 namespace {
6 // Set while the calling thread (original caller or a worker) is inside a
7 // job's fn_ calls. Guards against the reentrancy hazard where fn_ itself
8 // invokes parallel_for again: without this, every thread currently
9 // executing fn_ would race to claim the singleton's shared job state for
10 // the nested call. A nested call instead runs as a plain sequential loop
11 // on whichever thread hits it.
12 thread_local bool t_active = false;
13 } // namespace
14
15 96 ThreadPool::ThreadPool() {
16 48 unsigned hw = std::thread::hardware_concurrency();
17
1/2
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
48 if (hw == 0) hw = 1;
18 // Reserve one logical core for the calling thread, which also drains
19 // work instead of idling while it waits for the pool.
20
1/2
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
48 const unsigned num_workers = hw > 1 ? hw - 1 : 0;
21
1/2
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
48 workers_.reserve(num_workers);
22
2/2
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 48 times.
144 for (unsigned i = 0; i < num_workers; ++i) {
23
1/2
✓ Branch 0 taken 96 times.
✗ Branch 1 not taken.
192 workers_.emplace_back([this] { worker_loop(); });
24 96 }
25 48 }
26
27 96 ThreadPool::~ThreadPool() {
28
1/2
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
48 shutdown();
29 96 }
30
31 49 void ThreadPool::shutdown() {
32 49 shutdown_.store(true, std::memory_order_relaxed);
33 49 generation_.fetch_add(1, std::memory_order_release);
34 49 generation_.notify_all();
35
2/2
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 49 times.
145 for (auto& t : workers_) {
36
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 96 times.
96 if (t.joinable()) t.join();
37 }
38 // Idempotent: an empty vector makes a second call (e.g. the eventual
39 // destructor, after an explicit early shutdown()) a no-op.
40 49 workers_.clear();
41 49 }
42
43 namespace {
44 std::atomic<ThreadPool*> g_made{nullptr};
45 } // namespace
46
47 3920 ThreadPool& ThreadPool::instance() {
48
4/6
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 3872 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 48 times.
✓ Branch 4 taken 48 times.
✗ Branch 5 not taken.
3920 static ThreadPool pool;
49 3920 g_made.store(&pool, std::memory_order_release);
50 3920 return pool;
51 ✗ }
52
53 2 ThreadPool* ThreadPool::existing() {
54 2 return g_made.load(std::memory_order_acquire);
55 }
56
57 96 void ThreadPool::worker_loop() {
58 96 int seen = 0;
59 6635 for (;;) {
60 6635 generation_.wait(seen);
61 6635 seen = generation_.load(std::memory_order_acquire);
62
2/2
✓ Branch 0 taken 6539 times.
✓ Branch 1 taken 96 times.
6635 if (shutdown_.load(std::memory_order_relaxed)) return;
63
64 6539 t_active = true;
65 411164 for (;;) {
66 411164 const std::size_t i = cursor_.fetch_add(1, std::memory_order_relaxed);
67
2/2
✓ Branch 0 taken 6539 times.
✓ Branch 1 taken 404625 times.
411164 if (i >= n_) break;
68 404625 (*fn_)(i);
69 }
70 6539 t_active = false;
71
2/2
✓ Branch 0 taken 3268 times.
✓ Branch 1 taken 3271 times.
6539 if (outstanding_.fetch_sub(1, std::memory_order_acq_rel) == 1) {
72 3271 outstanding_.notify_all();
73 3271 }
74 }
75 }
76
77 3716 void ThreadPool::run(std::size_t n, const std::function<void(std::size_t)>& fn) {
78
2/2
✓ Branch 0 taken 3715 times.
✓ Branch 1 taken 1 time.
3716 if (n == 0) return;
79
5/6
✓ Branch 0 taken 3707 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 3707 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 436 times.
✓ Branch 5 taken 3271 times.
3715 if (t_active || workers_.empty() || n == 1) {
80 // Either nested (this thread is already draining a job's fn_, so
81 // the shared job state is in use — see class comment) or not worth
82 // dispatching (no spare cores, or a single item).
83
2/2
✓ Branch 0 taken 444 times.
✓ Branch 1 taken 556 times.
1000 for (std::size_t i = 0; i < n; ++i) fn(i);
84 444 return;
85 }
86
87 3271 fn_ = &fn;
88 3271 n_ = n;
89 3271 cursor_.store(0, std::memory_order_relaxed);
90 3271 outstanding_.store(static_cast<int>(workers_.size()), std::memory_order_relaxed);
91
92 3271 generation_.fetch_add(1, std::memory_order_release);
93 3271 generation_.notify_all();
94
95 // The calling thread drains work too instead of idling.
96 3271 t_active = true;
97 648213 for (;;) {
98 648213 const std::size_t i = cursor_.fetch_add(1, std::memory_order_relaxed);
99
2/2
✓ Branch 0 taken 644942 times.
✓ Branch 1 taken 3271 times.
648213 if (i >= n_) break;
100 644942 fn(i);
101 }
102 3271 t_active = false;
103
104 6657 for (;;) {
105 6657 const int rem = outstanding_.load(std::memory_order_acquire);
106
2/2
✓ Branch 0 taken 3386 times.
✓ Branch 1 taken 3271 times.
6657 if (rem == 0) break;
107 3386 outstanding_.wait(rem);
108 }
109 3716 }
110
111 } // namespace brotensor::detail::cpu
112