-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy paththreadpool.rs
More file actions
296 lines (247 loc) · 8.98 KB
/
threadpool.rs
File metadata and controls
296 lines (247 loc) · 8.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
use std::{
ops::Index,
sync::{
Arc, Condvar, Mutex,
atomic::Ordering,
mpsc::{Receiver, SyncSender},
},
thread::Scope,
};
use crate::{
board::Board,
search::{self, Report},
thread::{SharedContext, Status, ThreadData},
time::TimeManager,
};
pub struct ThreadPool {
pub workers: Vec<WorkerThread>,
pub vector: Vec<ThreadData>,
}
impl ThreadPool {
pub fn available_threads() -> usize {
const MINIMUM_THREADS: usize = 512;
match std::thread::available_parallelism() {
Ok(threads) => (4 * threads.get()).max(MINIMUM_THREADS),
Err(_) => MINIMUM_THREADS,
}
}
pub fn new(shared: Arc<SharedContext>) -> Self {
let workers = make_worker_threads(1);
let data = make_thread_data(shared, &workers, Board::starting_position().into());
Self { workers, vector: data }
}
pub fn set_count(&mut self, threads: usize) {
let threads = threads.clamp(1, ThreadPool::available_threads());
let shared = self.vector[0].shared.clone();
let board = Arc::new(self.vector[0].board.clone());
self.workers.drain(..).for_each(WorkerThread::join);
self.workers = make_worker_threads(threads);
std::mem::drop(self.vector.drain(..));
self.vector = make_thread_data(shared, &self.workers, board);
}
pub fn main_thread(&mut self) -> &mut ThreadData {
&mut self.vector[0]
}
pub const fn len(&self) -> usize {
self.vector.len()
}
pub fn iter(&self) -> impl Iterator<Item = &ThreadData> {
self.vector.iter()
}
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut ThreadData> {
self.vector.iter_mut()
}
pub fn clear(&mut self) {
let shared = self.vector[0].shared.clone();
std::mem::drop(self.vector.drain(..));
self.vector = make_thread_data(shared, &self.workers, Board::starting_position().into());
}
pub fn execute_searches(
&mut self, time_manager: TimeManager, report: Report, shared: &Arc<SharedContext>, pondering: bool,
) {
shared.tt.increment_age();
shared.nodes.reset();
shared.tb_hits.reset();
shared.pondering.store(pondering, Ordering::Release);
shared.ponderhit.store(false, Ordering::Release);
shared.soft_stop_votes.store(0, Ordering::Release);
shared.status.set(Status::RUNNING);
shared.best_stats.iter().for_each(|x| {
x.store((self.main_thread().previous_best_score + 32768) as u32, Ordering::Release);
});
std::thread::scope(|scope| {
let mut handlers = Vec::new();
let thread_count = self.vector.len();
let (t1, rest) = self.vector.split_first_mut().unwrap();
let (w1, rest_workers) = self.workers.split_first().unwrap();
let tm = time_manager.clone();
handlers.push(scope.spawn_into(
move || {
t1.time_manager = tm;
search::start(t1, report, thread_count);
shared.status.set(Status::STOPPED);
},
w1,
));
for (index, (t, w)) in rest.iter_mut().zip(rest_workers).enumerate() {
let tm = time_manager.clone();
handlers.push(scope.spawn_into(
move || {
t.id = index + 1;
t.time_manager = tm;
search::start(t, Report::None, thread_count);
},
w,
));
}
for handler in handlers {
handler.join();
}
});
}
}
impl Index<usize> for ThreadPool {
type Output = ThreadData;
fn index(&self, index: usize) -> &Self::Output {
&self.vector[index]
}
}
pub struct WorkerThread {
handle: std::thread::JoinHandle<()>,
comms: WorkSender,
}
impl WorkerThread {
pub fn join(self) {
drop(self.comms); // Drop the sender to signal the worker thread to finish
self.handle.join().expect("Worker thread panicked");
}
}
// Handle for communicating with a worker thread.
// Contains a sender for sending messages to the worker thread,
// and a receiver for receiving messages from the worker thread.
struct WorkSender {
// INVARIANT: Each send must be matched by a receive.
sender: SyncSender<Box<dyn FnOnce() + Send>>,
completion_signal: Arc<(Mutex<bool>, Condvar)>,
}
/// Handle for the receiver side of a worker thread.
struct WorkReceiver {
receiver: Receiver<Box<dyn FnOnce() + Send>>,
completion_signal: Arc<(Mutex<bool>, Condvar)>,
}
fn make_work_channel() -> (WorkSender, WorkReceiver) {
let (sender, receiver) = std::sync::mpsc::sync_channel(0);
let completion_signal = Arc::new((Mutex::new(false), Condvar::new()));
(
WorkSender { sender, completion_signal: Arc::clone(&completion_signal) },
WorkReceiver { receiver, completion_signal },
)
}
pub struct ReceiverHandle<'scope> {
completion_signal: &'scope Arc<(Mutex<bool>, Condvar)>,
received: bool,
}
impl ReceiverHandle<'_> {
pub fn join(mut self) {
let (lock, cvar) = &**self.completion_signal;
let mut completed = lock.lock().unwrap();
while !*completed {
completed = cvar.wait(completed).unwrap();
}
drop(completed);
self.received = true;
}
}
impl Drop for ReceiverHandle<'_> {
fn drop(&mut self) {
// When the receiver handle is dropped, we ensure that we have received something.
assert!(self.received, "ReceiverHandle was dropped without receiving a value");
}
}
pub trait ScopeExt<'scope, 'env> {
fn spawn_into<F>(&'scope self, f: F, comms: &'scope WorkerThread) -> ReceiverHandle<'scope>
where
F: FnOnce() + Send + 'scope;
}
impl<'scope, 'env> ScopeExt<'scope, 'env> for Scope<'scope, 'env> {
fn spawn_into<'comms, F>(&'scope self, f: F, thread: &'scope WorkerThread) -> ReceiverHandle<'scope>
where
F: FnOnce() + Send + 'scope,
{
// Safety: This file is structured such that threads never hold the data longer than is permissible.
let f = unsafe {
std::mem::transmute::<Box<dyn FnOnce() + Send + 'scope>, Box<dyn FnOnce() + Send + 'static>>(Box::new(f))
};
// Reset the completion flag before sending the task
{
let (lock, _) = &*thread.comms.completion_signal;
let mut completed = lock.lock().unwrap();
*completed = false;
}
thread.comms.sender.send(f).expect("Failed to send function to worker thread");
ReceiverHandle {
completion_signal: &thread.comms.completion_signal,
// Important: We start with `received` as false.
received: false,
}
}
}
fn make_worker_thread(id: Option<usize>) -> WorkerThread {
let (sender, receiver) = make_work_channel();
let handle = std::thread::spawn(move || {
#[cfg(feature = "numa")]
if let Some(id) = id {
crate::numa::bind_thread(id);
}
#[cfg(not(feature = "numa"))]
let _ = id;
while let Ok(work) = receiver.receiver.recv() {
work();
let (lock, cvar) = &*receiver.completion_signal;
let mut completed = lock.lock().unwrap();
*completed = true;
drop(completed); // Release the lock before notifying
cvar.notify_one();
}
});
WorkerThread { handle, comms: sender }
}
fn make_worker_threads(num_threads: usize) -> Vec<WorkerThread> {
#[cfg(feature = "numa")]
{
let concurrency = std::thread::available_parallelism().map_or(1, |n| n.get());
(0..num_threads).map(|id| make_worker_thread((num_threads >= concurrency / 2).then_some(id))).collect()
}
#[cfg(not(feature = "numa"))]
{
(0..num_threads).map(|_| make_worker_thread(None)).collect()
}
}
fn make_thread_data(shared: Arc<SharedContext>, worker_threads: &[WorkerThread], board: Arc<Board>) -> Vec<ThreadData> {
std::thread::scope(|scope| -> Vec<ThreadData> {
let handles = worker_threads
.iter()
.map(|worker| {
let (tx, rx) = std::sync::mpsc::channel();
let shared = shared.clone();
let board = board.clone();
let join_handle = scope.spawn_into(
move || {
let mut td = Box::new(ThreadData::new(shared));
td.board = (*board).clone();
tx.send(td).unwrap();
},
worker,
);
(rx, join_handle)
})
.collect::<Vec<_>>();
let mut thread_data: Vec<ThreadData> = Vec::with_capacity(handles.len());
for (rx, handle) in handles {
let td = rx.recv().unwrap();
thread_data.push(*td);
handle.join();
}
thread_data
})
}