faster icon indicating copy to clipboard operation
faster copied to clipboard

Support for Rayon integration

Open rohitjoshi opened this issue 6 years ago • 6 comments

Rayon supports parallel iterators/mapv function to process using multiple threads. How can we integrate with rayon so we can leverage both simd and thread parallel processing?

rohitjoshi avatar Apr 19 '18 20:04 rohitjoshi

You can do something like arr.par_iter(|chunk| chunk.simd_iter(|vec| ...)) to use both multithreading and SIMD

AdamNiederer avatar Apr 19 '18 20:04 AdamNiederer

I tried your suggestion and getting an error. e.g.

pub fn sqrt_par_simd(a: &[f64]) -> Vec<f64> {
   a.par_iter(|chunk| {
        chunk
            .simd_iter()
            .simd_map(f64s(0.0), |index| index.sqrt())
            .scalar_collect()
    }).collect()
}

Error:

error[E0061]: this function takes 0 parameters but 1 parameter was supplied
  --> src/prior.rs:34:7
   |
34 |     a.par_iter(|chunk| {
   |       ^^^^^^^^ expected 0 parameters

error[E0277]: the trait bound `std::vec::Vec<f64>: rayon::iter::FromParallelIterator<&f64>` is not satisfied
  --> src/prior.rs:39:8
   |
39 |     }).collect()
   |        ^^^^^^^ the trait `rayon::iter::FromParallelIterator<&f64>` is not implemented for `std::vec::Vec<f64>`
   |
   = help: the following implementations were found:
             <std::vec::Vec<T> as rayon::iter::FromParallelIterator<T>>

rohitjoshi avatar May 07 '18 13:05 rohitjoshi

The Rayon syntax you’re looking for is a.par_chunks(128).flat_map(|chunk| …).collect(). (Pick your favorite chunk size.)

andersk avatar Dec 31 '18 13:12 andersk

The Rayon syntax you’re looking for is a.par_chunks(128).flat_map(|chunk| …).collect(). (Pick your favorite chunk size.)

How would that translate if I wanted to filter elements instead of map?

Titaniumtown avatar Mar 24 '21 16:03 Titaniumtown

AFAIK Faster doesn’t currently provide a way to accelerate filter, with or without Rayon—so you’d just use the normal Rayon filter.

(If in the future some kind of filter is added to Faster, the same construction would work: you’d use Rayon’s .par_chunks().flat_map() around the hypothetical filter.)

andersk avatar Mar 24 '21 17:03 andersk

AFAIK Faster doesn’t currently provide a way to accelerate filter, with or without Rayon—so you’d just use the normal Rayon filter.

That's correct, and it's unlikely that it will without AVX-512; SSE and AVX don't really have the underlying instructions required to yield an appreciable performance improvement, save for specific cases which wouldn't work well in a general library.

AdamNiederer avatar Mar 25 '21 03:03 AdamNiederer