I'm trying to parallelize an n-body simulation with rayon, by chunks so that concurrent threads never mutate bodies accessed by others.
I have an iterator of
rayon::ParallelIterator of iterators of iterators. (the first gives chunks of parallel iterators that can safely run concurrently, and each of them iterate on bodies)
There are two ways I've considered:
- the safe way: each iterator contains slices of the data belonging to it. Con: that's a lot of references I don't strictly need for, and more pointer arithmetic overhead. (and performance is important for O(N²) simulation)
- the unsafe way: each iterator contains a pointer to
Layout(the main struct containing all the data), and safety is only ensured by the algorithm.
I've tried the second way, but my data are not
Send, so even using
std::ptr::NonNull (to avoid multiple mutable references errors) this won't compile.
Is there a way to do this without making it slower? and why not, to make it safer?