rustparallel-processingrust-tokiorust-async-std

How to use async to parallelize heavy computation?


I would like to perform the following processing in multi-threads using tokio or async-std. I have read tutorials but I haven't seen any mention of parallelizing a for loop. In my program, all threads refer to the same array but will access different locations:

let input_array: Array2<f32>;  
let output_array: Array2<f32>;
for i in 0..roop_num {
  let res = do_some_func(&input_array, idx);
  output_array.slice_mut(s![idx, ...]) .assign(&res);
}

I would like to change the for loop to use parallel processing.


Solution

  • Tokio or async-std deal with concurrency, not parallelism. If you need the data parallelism then rayon is a better choice. If you are using an Iterator, then .chunks() method is good. For more imperative approach you can use .par_chunks_mut().