Processing vec in parallel: how to do safely, or without using unstable features?

前端 未结 2 888
独厮守ぢ
独厮守ぢ 2020-12-02 00:14

I have a massive vector that I want to be able to load/act on in parallel, e.g. load first hundred thousand indices in one thread, next in another and so on. As this is goin

相关标签:
2条回答
  • 2020-12-02 00:32

    Today the rayon crate is the de facto standard for this sort of thing:

    use rayon::prelude::*;
    
    fn main() {
        let mut data = vec![1, 2, 3];
        data.par_iter_mut()
            .enumerate()
            .for_each(|(i, x)| *x = 10 + i as u32);
        assert_eq!(vec![10, 11, 12], data);
    }
    

    Note that this is just one line different from the single-threaded version using standard iterators, which would replace par_iter_mut with iter_mut.

    See also Writing a small ray tracer in Rust and Zig.

    0 讨论(0)
  • 2020-12-02 00:40

    One can use an external library for this, e.g. simple_parallel (disclaimer, I wrote it) allows one to write:

    extern crate simple_parallel;
    
    let mut data = vec![1u32, 2, 3, 4, 5];
    
    let mut pool = simple_parallel::Pool::new(4);
    
    pool.for_(data.chunks_mut(3), |target| {
        // do stuff with `target`
    })
    

    The chunks and chunks_mut methods are the perfect way to split a vector/slice of Ts into equally sized chunks: they respectively return an iterator over elements of type &[T] and &mut [T].

    0 讨论(0)
提交回复
热议问题