gpt4 book ai didi

multithreading - 如何更改 Rayon 使用的线程数?

转载 作者:行者123 更新时间:2023-12-03 11:27:50 34 4
gpt4 key购买 nike

我正在使用 Rayon 库:

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);
}
我想使用不同数量的线程运行此代码:1、2、3 和 4。
我已阅读有关 How many threads will Rayon spawn? 的文档其中说:

By default, Rayon uses the same number of threads as the number of CPUs available. Note that on systems with hyperthreading enabled this equals the number of logical cores and not the physical ones.

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();

关于multithreading - 如何更改 Rayon 使用的线程数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59205184/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com