Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to combine Rayon and Faster?

Rayon looks great for algorithm parallelization of collections, and Faster is great for vectorization (SIMD) on the x86 platform for collections like Vec<f32>. I've tried to combine them and the iterators don't seem to like each other. Is there a way to make use of these two libraries for algorithms which would benefit from both vectorization and parallelization? Like this one from the Faster example:

let lots_of_3s = (&[-123.456f32; 128][..]).iter()
    .map(|v| {
        9.0 * v.abs().sqrt().sqrt().recip().ceil().sqrt() - 4.0 - 2.0
    })
    .collect::<Vec<f32>>();
like image 415
Stan Prokop Avatar asked Jul 09 '18 20:07

Stan Prokop


People also ask

How does rayon work rust?

Rayon is a data-parallelism library for Rust. It is extremely lightweight and makes it easy to convert a sequential computation into a parallel one. It also guarantees data-race freedom.

Does rust support parallelism?

Rust provides excellent support for safe parallel programming, which can lead to large performance improvements. There are a variety of ways to introduce parallelism into a program and the best way for any program will depend greatly on its design.


1 Answers

You can just use Rayon’s par_chunks and process each chunk with Faster.

let lots_of_3s = (&[-123.456f32; 1000000][..])
    .par_chunks(128)
    .flat_map(|chunk| {
        chunk
            .simd_iter(f32s(0.0))
            .simd_map(|v| {
                f32s(9.0) * v.abs().sqrt().rsqrt().ceil().sqrt() - f32s(4.0) - f32s(2.0)
            })
            .scalar_collect()
    })
    .collect::<Vec<f32>>();
like image 196
Anders Kaseorg Avatar answered Sep 29 '22 02:09

Anders Kaseorg