RReverser / wasm-bindgen-rayon

An adapter for enabling Rayon-based concurrency on the Web with WebAssembly.
https://docs.rs/wasm-bindgen-rayon
Apache License 2.0
117 stars 8 forks source link

Support for rayon::spawn and rayon::scope #2

Closed hammadtq closed 6 months ago

hammadtq commented 6 months ago

I am using rayon::scope and rayon::spawn in my wasm library with #[wasm_bindgen], I use wasm-pack build --target web to compile and my code structure is:

rayon::scope(|s| {
        s.spawn(move |_| {
            //thread
        });
       s.spawn(move |_| {
            //thread
        });
       s.spawn(move |_| {
            //thread
        });
});

I have used following js code to initialize number of threads through worker.js:

import init, {benchmarkFunction, initThreadPool} from "./pkg/wasm.js";
init().then(_ => {
    initThreadPool(navigator.hardwareConcurrency).then(async _ => {
        console.time("benchmarkFunction")
        await benchmarkFunction();
        console.timeEnd("benchmarkFunction")
    })
})

I am using M2 machine with 10 cores and I have tried entering cores manually as well. It compiles and loads, I can see multiple workerHelpers.js loading in the network tab along with my main wasm.js repeatedly. Seems like cores are being used but the end result is same benchmark time if I run it using single thread or multi-threards or don't use wasm-bindgen-rayon at all. Also from the source I could not understand if this adapter is written for all kinds of rayon threads like the ones I am using above or is it for specific use-cases.

I will be grateful for any pointers.

RReverser commented 6 months ago

Also from the source I could not understand if this adapter is written for all kinds of rayon threads like the ones I am using above or is it for specific use-cases.

No, it should work for any Rayon work, including rayon::spawn.

But then, it completely depends on your program whether it's using all those threads efficiently. If each spawn doesn't do much work and finishes quickly, it's entirely possible that execution will be still almost serial. It only makes sense to spawn off long-running, expensive tasks.

RReverser commented 6 months ago

Until there's more information (e.g. clearly reproducible benchmark with expected vs actual behaviour), I don't think there's a bug here as those functions should work.