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
use std::env;
use std::sync::Arc;
use crate::{Error, Result};
/// Reusable CPU execution context carrying CPU parallelism policy.
///
/// `CpuContext` stores the requested thread count as a kernel-level
/// parallelism hint and owns the Rayon pool used by multi-threaded CPU work.
///
/// # Examples
///
/// ```
/// use tenferro_cpu::CpuContext;
///
/// let ctx = CpuContext::with_threads(1).unwrap();
/// let value = ctx.install(|| 1 + 1);
/// assert_eq!(value, 2);
/// assert_eq!(ctx.num_threads(), 1);
/// ```
#[derive(Clone, Debug)]
pub struct CpuContext {
num_threads: usize,
pool: Option<Arc<rayon::ThreadPool>>,
}
impl CpuContext {
/// Create a CPU context from `RAYON_NUM_THREADS`, or fall back to the
/// process-visible CPU count.
///
/// # Examples
///
/// ```
/// use tenferro_cpu::CpuContext;
///
/// let ctx = CpuContext::from_env();
/// assert!(ctx.num_threads() >= 1);
/// ```
pub fn from_env() -> Self {
Self::try_from_env().unwrap_or_else(|_| Self::single_threaded())
}
/// Try to create a CPU context from `RAYON_NUM_THREADS`.
///
/// # Examples
///
/// ```
/// use tenferro_cpu::CpuContext;
///
/// let ctx = CpuContext::try_from_env()
/// .unwrap_or_else(|_| CpuContext::with_threads(1).unwrap());
/// assert!(ctx.num_threads() >= 1);
/// ```
pub fn try_from_env() -> Result<Self> {
match env::var("RAYON_NUM_THREADS") {
Ok(value) => {
let num_threads = value.parse::<usize>().map_err(|err| Error::InvalidConfig {
op: "CpuContext::try_from_env",
message: format!("invalid RAYON_NUM_THREADS value {value:?}: {err}"),
})?;
Self::with_threads(num_threads).map_err(|err| match err {
Error::InvalidConfig { message, .. } => Error::InvalidConfig {
op: "CpuContext::try_from_env",
message: format!("invalid RAYON_NUM_THREADS value {value:?}: {message}"),
},
err => err,
})
}
Err(env::VarError::NotPresent) => {
Self::with_threads(super::affinity::available_parallelism())
}
Err(err) => Err(Error::InvalidConfig {
op: "CpuContext::try_from_env",
message: format!("failed to read RAYON_NUM_THREADS: {err}"),
}),
}
}
/// Create a CPU context with a fixed parallelism hint.
///
/// # Examples
///
/// ```
/// use tenferro_cpu::CpuContext;
///
/// let ctx = CpuContext::with_threads(2).unwrap();
/// assert_eq!(ctx.num_threads(), 2);
/// ```
///
/// # Errors
///
/// Returns an error when `num_threads` is zero or Rayon rejects the pool.
pub fn with_threads(num_threads: usize) -> Result<Self> {
if num_threads == 0 {
return Err(Error::InvalidConfig {
op: "CpuContext::with_threads",
message: "thread count must be at least 1".into(),
});
}
let pool = if num_threads == 1 {
None
} else {
Some(Arc::new(
rayon::ThreadPoolBuilder::new()
.num_threads(num_threads)
.build()
.map_err(|err| Error::InvalidConfig {
op: "CpuContext::with_threads",
message: format!("failed to build CPU thread pool: {err}"),
})?,
))
};
Ok(Self { num_threads, pool })
}
fn single_threaded() -> Self {
Self {
num_threads: 1,
pool: None,
}
}
/// Return this context's CPU parallelism hint.
///
/// # Examples
///
/// ```
/// use tenferro_cpu::CpuContext;
///
/// let ctx = CpuContext::with_threads(2).unwrap();
/// assert_eq!(ctx.num_threads(), 2);
/// ```
pub fn num_threads(&self) -> usize {
self.num_threads
}
/// Run a closure inside this context's CPU execution scope.
///
/// # Examples
///
/// ```
/// use tenferro_cpu::CpuContext;
///
/// let ctx = CpuContext::with_threads(1).unwrap();
/// let value = ctx.install(|| 1 + 1);
/// assert_eq!(value, 2);
/// ```
pub fn install<R: Send>(&self, op: impl FnOnce() -> R + Send) -> R {
match &self.pool {
Some(pool) => pool.install(op),
None => op(),
}
}
/// Return the faer parallelism policy for this context.
#[cfg(feature = "cpu-faer")]
#[doc(hidden)]
pub fn faer_par(&self) -> faer::Par {
if self.num_threads == 1 {
faer::Par::Seq
} else {
faer::Par::rayon(0)
}
}
}
#[cfg(test)]
mod tests;