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
use crate::utils::{DefaultRandom, Random, ThreadPool, Timer};
use std::sync::Arc;
pub type InfoLogger = Arc<dyn Fn(&str) + Send + Sync>;
pub trait Quota: Send + Sync {
fn is_reached(&self) -> bool;
}
#[derive(Clone)]
pub struct Environment {
pub random: Arc<dyn Random + Send + Sync>,
pub quota: Option<Arc<dyn Quota + Send + Sync>>,
pub parallelism: Parallelism,
pub logger: InfoLogger,
pub is_experimental: bool,
}
impl Environment {
pub fn new_with_time_quota(max_time: Option<usize>) -> Self {
Self {
quota: max_time.map::<Arc<dyn Quota + Send + Sync>, _>(|time| Arc::new(TimeQuota::new(time as f64))),
..Self::default()
}
}
pub fn new(
random: Arc<dyn Random + Send + Sync>,
quota: Option<Arc<dyn Quota + Send + Sync>>,
parallelism: Parallelism,
logger: InfoLogger,
is_experimental: bool,
) -> Self {
Self { random, quota, parallelism, logger, is_experimental }
}
}
impl Default for Environment {
fn default() -> Self {
Environment::new(
Arc::new(DefaultRandom::default()),
None,
Parallelism::default(),
Arc::new(|msg| println!("{}", msg)),
false,
)
}
}
pub struct TimeQuota {
start: Timer,
limit_in_secs: f64,
}
impl TimeQuota {
pub fn new(limit_in_secs: f64) -> Self {
Self { start: Timer::start(), limit_in_secs }
}
}
impl Quota for TimeQuota {
fn is_reached(&self) -> bool {
self.start.elapsed_secs_as_f64() > self.limit_in_secs
}
}
#[derive(Clone)]
pub struct Parallelism {
available_cpus: usize,
#[allow(clippy::rc_buffer)]
thread_pools: Option<Arc<Vec<ThreadPool>>>,
}
impl Default for Parallelism {
fn default() -> Self {
Self { available_cpus: get_cpus(), thread_pools: None }
}
}
impl Parallelism {
pub fn new(num_thread_pools: usize, threads_per_pool: usize) -> Self {
let thread_pools = (0..num_thread_pools).map(|_| ThreadPool::new(threads_per_pool)).collect();
Self { available_cpus: get_cpus(), thread_pools: Some(Arc::new(thread_pools)) }
}
pub fn available_cpus(&self) -> usize {
self.available_cpus
}
pub fn thread_pool_execute<OP, R>(&self, idx: usize, op: OP) -> R
where
OP: FnOnce() -> R + Send,
R: Send,
{
if let Some(thread_pool) = self.thread_pools.as_ref().and_then(|tps| tps.get(idx)) {
thread_pool.execute(op)
} else {
op()
}
}
pub fn thread_pool_size(&self) -> usize {
self.thread_pools.as_ref().map_or(0, |tp| tp.len())
}
}
#[cfg(not(target_arch = "wasm32"))]
fn get_cpus() -> usize {
num_cpus::get()
}
#[cfg(target_arch = "wasm32")]
fn get_cpus() -> usize {
1
}