rayon-rs / rayon

Rayon: A data parallelism library for Rust

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to dynamically change the number of threads during runtime?

BA8F0D39 opened this issue · comments

Some parts of my code runs faster with 20 threads, while other parts of my code runs faster with 4 threads. For example, loading and parsing large files is faster with 4 threads, while taking the sqrt of a vector is faster with 20 threads.

How do you dynamically change the number of threads at runtime?

Changing the number of threads at runtime segment faults.

rayon::ThreadPoolBuilder::new().num_threads(20).build_global().unwrap();

    println!("Main thread: {:?}", thread::current().id());
    let ids: Vec<_> = (0..10)
        .into_par_iter()
        .map(|_| thread::current().id())
        .collect();
        
    println!("Iterations: {:?}", ids);
    
    rayon::ThreadPoolBuilder::new().num_threads(4).build_global().unwrap();

    println!("Main thread: {:?}", thread::current().id());
    let ids: Vec<_> = (0..10)
        .into_par_iter()
        .map(|_| thread::current().id())
        .collect();
        
    println!("Iterations: {:?}", ids);
    

How do you dynamically change the number of threads at runtime?

There's no way to do that in the global pool, but you can create an independent pool with build() instead of build_global(), and give that a different number of threads. Save that to a name like pool, then you can pool.install(|| ...) to run code in there.

Changing the number of threads at runtime segment faults.

Hopefully not! I suspect you mean that it panics when your unwrap() encounters a Result::Err from attempting to build the global pool twice. That's normal, but you can also add more graceful handling of that error, whatever makes sense in your situation.

Thanks for the help
Threadpool .build() and .install() worked for me