let mut sphere_indices: Vec<usize> = vec![0; self.current_spheres.len()];
self.current_spheres
.par_iter()
.enumerate()
.for_each(|(i, sphere)| {
let index = hash(cell_coord(sphere.position)) % hash_table_length;
let index =
hash_table[index].fetch_sub(1, std::sync::atomic::Ordering::Relaxed);
sphere_indices[index] = i; // I know that the index here is always unique, how can i do this?
});
#Writing to a Vec from multiple threads with unique indices
5 messages · Page 1 of 1 (latest)
Zip the iterator with &mut sphere_indicies
If you are sure that the indices are always unique, you can make sphere_indices a Vec<UnsafeCell<usize>> and use unsafe *sphere_indices[index].get() = i;
Alternately, a safer approach would be to make sphere_indices a Vec<AtomicUsize> and use sphere_indices[index].store(i, Relaxed);, which will be the same as the UnsafeCell<usize> version at the machine level on most architectures (amd64 and aarch64 at least), but doesn't require unsafe.
It doesn't look like index == i in general
Oh right