-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmax_num_threads_config.rs
More file actions
59 lines (51 loc) · 1.61 KB
/
max_num_threads_config.rs
File metadata and controls
59 lines (51 loc) · 1.61 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
/*
1. to run the computation without any limits on max number of threads:
cargo run --release --example max_num_threads_config
OR
ORX_PARALLEL_MAX_NUM_THREADS=0 cargo run --release --example max_num_threads_config
2. to allow parallel computation at most 4 threads:
ORX_PARALLEL_MAX_NUM_THREADS=4 cargo run --release --example max_num_threads_config
*/
use orx_parallel::*;
fn fib(n: &u64) -> u64 {
// just some work
let mut a = 0;
let mut b = 1;
for _ in 0..*n {
let c = a + b;
a = b;
b = c;
}
a
}
// A: what should name of the variable be?
const MAX_NUM_THREADS_ENV_VARIABLE: &str = "ORX_PARALLEL_MAX_NUM_THREADS";
fn max_num_threads_by_env_variable() -> Option<usize> {
match std::env::var(MAX_NUM_THREADS_ENV_VARIABLE) {
Ok(s) => match s.parse::<usize>() {
Ok(0) => None,
Ok(x) => Some(x),
Err(_e) => None,
},
Err(_e) => None,
}
}
fn main() {
match max_num_threads_by_env_variable() {
Some(x) => {
println!(
"Environment variable {MAX_NUM_THREADS_ENV_VARIABLE} is set to {x}\n -> this will be the hard limit on the maximum number of threads that can be used by parallelization"
)
}
None => {
println!(
"Environment variable {MAX_NUM_THREADS_ENV_VARIABLE} is not set\n -> all available threads might be used"
)
}
}
let n = 1 << 31;
let input = 0..n;
// default -> might use all threads
let sum = input.par().map(|i| fib(&(i as u64 % 42)) % 42).sum();
println!("sum = {sum}");
}