AdamNiederer / faster

SIMD for humans
Mozilla Public License 2.0
1.56k stars 51 forks source link

Support for Rayon integration #33

Open rohitjoshi opened 6 years ago

rohitjoshi commented 6 years ago

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?

AdamNiederer commented 6 years ago

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

rohitjoshi commented 6 years ago

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>>
andersk commented 5 years ago

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

Titaniumtown commented 3 years ago

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?

andersk commented 3 years ago

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.)

AdamNiederer commented 3 years ago

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.