Hi,
Please see code below.
I would like to know if the for loops in the code can be parallelized. I believe the one with the rollsum can't but I might be wrong! The last one I am not sure. I am getting errors related to the variable vans
when I try.
Side note, any comment to improve the code below would be appriciated.
Thank you for your help.
use rand::Rng;
fn make_chunk(len: usize, n: usize) -> Vec<(usize, usize)> {
let m: usize = len / n;
let mut chks = ((0..len).step_by(m).zip((0..len).skip(m-1).step_by(m))).collect::<Vec<_>>();
if chks[n-1].1 != (len-1) {
chks[n-1].1 = len -1;
}
chks
}
fn main() {
let n: usize = 4;
let len: usize = 50;
let mut rng = rand::thread_rng();
let range = rand::distributions::Uniform::new(0, 25);
let x: Vec<u64> = (0..len).map(|_| rng.sample(range)).collect();
let chks: Vec<(usize, usize)> = make_chunk(len, n);
let mut counts: Vec<Vec<usize>> = vec![vec![0_usize; len]; n];// a matrix of dim len x n
std::thread::scope(|s| {
for (chk, count) in std::iter::zip(&chks, &mut counts) {
s.spawn(|| {
for i in (chk.0)..=(chk.1) {
count[x[i] as usize] += 1;
}
});
}
});
let mut rollsum: usize = 0;
for j in 0..len { // <- Can this be parallelized ?
for i in 0..n {
let tmp = counts[i][j];
counts[i][j] = rollsum;
rollsum += tmp;
}
}
let mut vans: Vec<u64> = vec![0_u64; len];
for i in 0..n { // <- Can this be parallelized ?
for j in (chks[i].0)..=(chks[i].1) {
let idx = x[j] as usize;
vans[counts[i][idx]] = x[j];
counts[i][idx] += 1;
}
}
println!("x = {:?}\n", x); // Original vector
println!("vans = {:?}", vans); // this should be sorted now!
}