我有一些使用Rayon库的简单Rust代码。以下是main.rs
文件中的代码
extern crate rayon;
const N: usize = 1_000_000_000;
const W: f64 = 1f64/(N as f64);
fn f(x: f64) -> f64 {
4.0/(1.0+x*x)
}
fn main() {
use rayon::prelude::*;
let sum : f64 = (0..N)
.into_par_iter()
.map(|i| f(W*((i as f64)+0.5)))
.sum::<f64>();
println!("pi = {}", W*sum);
}
问题是,我想使用不同数量的线程(T = 1、2、3和4)运行此代码。
我已阅读How many threads will Rayon spawn?
下的文档
https://github.com/rayon-rs/rayon/blob/master/FAQ.md
它说:
If you want to alter the number of threads spawned, you can set the environmental variable RAYON_NUM_THREADS to the desired number of threads or use the ThreadPoolBuilder::build_global function method.
但是,步骤对我来说并不明确。如何在Windows 10 PC上执行此操作?
仅包含在fn main()中。 num_threads接受线程数。
rayon::ThreadPoolBuilder::new().num_threads(4).build_global().unwrap();