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
use std::env;
use crossbeam_channel::{bounded, Receiver};
use lazy_static::lazy_static;
use yastl::Pool;
const MAX_VERIFIER_THREADS: usize = 6;
lazy_static! {
static ref NUM_CPUS: usize = env::var("BELLMAN_NUM_CPUS")
.ok()
.and_then(|num| num.parse().ok())
.unwrap_or_else(num_cpus::get);
pub static ref THREAD_POOL: Pool = Pool::new(*NUM_CPUS);
pub static ref VERIFIER_POOL: Pool = Pool::new(NUM_CPUS.max(MAX_VERIFIER_THREADS));
pub static ref RAYON_THREAD_POOL: rayon::ThreadPool = rayon::ThreadPoolBuilder::new()
.num_threads(*NUM_CPUS)
.build()
.expect("failed to build rayon threadpool");
}
#[derive(Clone, Default)]
pub struct Worker {}
impl Worker {
pub fn new() -> Worker {
Worker {}
}
pub fn log_num_cpus(&self) -> u32 {
log2_floor(*NUM_CPUS)
}
pub fn compute<F, R>(&self, f: F) -> Waiter<R>
where
F: FnOnce() -> R + Send + 'static,
R: Send + 'static,
{
let (sender, receiver) = bounded(1);
THREAD_POOL.spawn(move || {
let res = f();
sender.send(res).unwrap();
});
Waiter { receiver }
}
pub fn scope<'a, F, R>(&self, elements: usize, f: F) -> R
where
F: FnOnce(&yastl::Scope<'a>, usize) -> R,
{
let chunk_size = if elements < *NUM_CPUS {
1
} else {
elements / *NUM_CPUS
};
THREAD_POOL.scoped(|scope| f(scope, chunk_size))
}
}
pub struct Waiter<T> {
receiver: Receiver<T>,
}
impl<T> Waiter<T> {
pub fn wait(&self) -> T {
self.receiver.recv().unwrap()
}
pub fn done(val: T) -> Self {
let (sender, receiver) = bounded(1);
sender.send(val).unwrap();
Waiter { receiver }
}
}
fn log2_floor(num: usize) -> u32 {
assert!(num > 0);
let mut pow = 0;
while (1 << (pow + 1)) <= num {
pow += 1;
}
pow
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_log2_floor() {
assert_eq!(log2_floor(1), 0);
assert_eq!(log2_floor(3), 1);
assert_eq!(log2_floor(4), 2);
assert_eq!(log2_floor(5), 2);
assert_eq!(log2_floor(6), 2);
assert_eq!(log2_floor(7), 2);
assert_eq!(log2_floor(8), 3);
}
}