rayon-rs / rayon

Rayon: A data parallelism library for Rust
Apache License 2.0
10.82k stars 493 forks source link

general purpose WASM support? #1122

Open fr-an-k opened 7 months ago

fr-an-k commented 7 months ago

I tried to build the example as wasm32-unknown-unknown but it requires the js feature. There is no browser API or wasm-bindgen in a general purpose WASM module.

It would be nice if this would also be supported, or a mention that it will not be supported and why.

In my case I will probably just use rayon normally for native functions that invoke a WASM function. So the WASM function just handles one job and returns when done. The native function is wrapped and threaded by Rayon as usual. Perhaps that's how it should be done.

For web and native compatibility on the same WASM modules, I would probably have to compile the wasm modules using wasm-bindgen-rayon and not use those exports on a native runtime.

It's difficult to get your head around these things so it would be nice to have this explained or demonstrated for everyone.

This also relates to the issues linked here: https://github.com/rayon-rs/rayon/issues/93

cuviper commented 7 months ago

Rayon can't fully run in general wasm32-unknown-unknown because there's no threading support. We do at least have a single-threaded fallback, so code written with rayon can still work for the most part, but you don't need to do any special setup for that.

If rayon is running natively, then it's orthogonal that the workload may be a wasm module. Rayon won't know anything about it, but you'll need to load the module to run in whatever wasm runtime you've chosen, just as you would if rayon wasn't involved at all.