Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing a parallel version of array::from_fn() #1222

Open
CrabbyDisk opened this issue Dec 30, 2024 · 3 comments
Open

Implementing a parallel version of array::from_fn() #1222

CrabbyDisk opened this issue Dec 30, 2024 · 3 comments

Comments

@CrabbyDisk
Copy link

I would like to know if a parallel implementation of array::from_fn() could be added to rayon. If not, are there any ways to use it in a parallel manner with some lower level functions?

@adamreichold
Copy link
Collaborator

Could you expand a bit on the use case you have for this? Arrays are usually relatively small so that parallelism wont help if the elements are cheap to compute. If the elements are indeed expensive to compute, I suspect the overhead of collecting a Vec and turning it into an array to be neglible.

@CrabbyDisk
Copy link
Author

Currently, I'm using a large 2 dimensional array as the main storage for my application. And, I frequently need to recompute the entire array. So I think it would be good to have a "nicer" way of doing this instead of collecting into a vec. (Especially when they're nested)

@cuviper
Copy link
Member

cuviper commented Jan 1, 2025

For 2d arrays, you will probably fare better with a serialized inner loop, unless the work is very imbalanced so you benefit from work-stealing. So you would be collecting Vec<[T; N]> or Box<[[T; N]]> with parallelism only on the outer index, then convert to a local array -- but it might be better to leave it on the heap anyway.

Another way to do it on the stack is to start with a MaybeUninit array, initialize it via par_iter_mut(), and then assume_init(). I think this is roughly what a native rayon::array::from_fn would do, but it gets tricky trying to ensure that drops happen if it's only partially initialized due to a panic. In your specific domain, you might be able to ignore that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants