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

Support for multi threading in WASM? #2630

Open
ibudisteanu opened this issue Feb 11, 2022 · 24 comments
Open

Support for multi threading in WASM? #2630

ibudisteanu opened this issue Feb 11, 2022 · 24 comments
Labels
enhancement New feature or request wasm WebAssembly

Comments

@ibudisteanu
Copy link

Does Tinygo support multi threading in web assembly? If not, is there any date when tinygo would add support for multi threading ? WebAssembly is useful for CPU intensive, but it such a pity that golang doesn't support multi threading in wasm...

@dgryski
Copy link
Member

dgryski commented Feb 14, 2022

The current tinygo implementation supports cooperative goroutines using asyncify on wasm. Does that solve your problem?

@ibudisteanu
Copy link
Author

Wait what ? Could you give us some links to read more about it?
Does it mean, that multiple goroutines run multi threaded in tinygo wasm ?

@dgryski
Copy link
Member

dgryski commented Feb 14, 2022

Yes. The tinygo 0.22 release includes asyncify support which solved all the memory corruption issues with the coroutine scheduler.

@ibudisteanu
Copy link
Author

ibudisteanu commented Feb 15, 2022

Thanks for your reply! I really appreciate it! From my understanding multi threading in wasm is done running multiple and separate WebWorkers and exchange data either using SharedArrayBuffer or using processMessage() . From my understanding asyncify doesn't make the goroutine run multi threaded. Am I wrong ?

@dgryski
Copy link
Member

dgryski commented Feb 15, 2022

Correct, there is still only a single wasm "core".

@ibudisteanu
Copy link
Author

ibudisteanu commented Feb 15, 2022

Then, there is no multi threading support right now in tinygo in wasm. Any plans of adding support for multi threading wasm ? Take a look here https://github.com/w3reality/wasm-mt

Our open source library does a lot of heavy computations (zero knowledge proofs) and it takes 20 seconds single threaded. Using multi threading, we can reduce it down to 3-4 seconds?

@deadprogram deadprogram added enhancement New feature or request wasm WebAssembly labels Feb 19, 2022
@dkegel-fastly
Copy link
Contributor

Would "multicore support" be a better title?

@ibudisteanu
Copy link
Author

It would be amazing of having multicore support wasm bundles.

@ibudisteanu
Copy link
Author

I think the easiest way to integrate it is via SharedArrayBuffer which was re-enabled in Google Chrome v67

@fgsch
Copy link
Contributor

fgsch commented Feb 19, 2022

The threading proposal for Wasm is not finished, and wasm-mt is a library while TinyGo is a compiler, so really you cannot compare them. Personally I don't think there is anything to do here for the time being.

@edwinm
Copy link

edwinm commented Jun 9, 2023

The threading proposal for Wasm is not finished

In the meantime, the threading proposal for Wasm is finished.

It would be very interesting (really very interesting) to use tinygo to write multithreaded wasm programs.

Please consider this.

https://webassembly.org/roadmap/
https://github.com/WebAssembly/threads/blob/master/proposals/threads/Overview.md
https://web.dev/webassembly-threads/

@aykevl
Copy link
Member

aykevl commented Jun 11, 2023

Unfortunately, the "threading proposal" doesn't actually provide a way to start a thread. It's only about shared memory and atomics. No idea who thought that would be a good name.

So that proposal is useless in itself, it only becomes useful together with an embedder specified way of starting threads (Web Workers for example). Last time I checked, WASI didn't have a way of starting threads making it pretty useless there.

Also, wasm threading depends on general support for parallelism is TinyGo, which doesn't exist at the moment: #2446

@edwinm
Copy link

edwinm commented Jun 11, 2023

Unfortunately, the "threading proposal" doesn't actually provide a way to start a thread. It's only about shared memory and atomics.

The last article I mentioned addresses a list of problems (with solutions) and one of them is the inability to start a thread.

"In C, particularly on Unix-like systems, the common way to use threads is via POSIX Threads provided by the pthread library. Emscripten provides an API-compatible implementation of the pthread library built atop Web Workers, shared memory and atomics, so that the same code can work on the web without changes."

If it's available in C, it should be available in TinyGo, I suppose.

Also, wasm threading depends on general support for parallelism is TinyGo, which doesn't exist at the moment

Oh, that's too bad, I didn't know. I'll stick with C, then.

I don't want to sound harsh. I think TinyGo is an interesting project, both for wasm and for microcontrollers. I'm looking forward to the moment parallelism is added.

@dkegel-fastly
Copy link
Contributor

This looks apropos: https://bytecodealliance.org/articles/wasi-threads

@esimkowitz
Copy link

Hi folks, any plans to address this now that the WASM threads proposal has entered Stage 4 of standardization and is supported in all modern browsers? https://github.com/webassembly/threads

There's a similar issue in the main Go repo that was stalled pending standardization of the proposal, it's far enough along that there's little chance of spec change at this point.

This feature is blocking Go's utility in the WASM world, since one of the main benefits of Go (the versatility of goroutines) is kneecapped without proper procs. golang/go#28631

@dkegel-fastly
Copy link
Contributor

The standard still doesn't specify how to create a thread, so it seems incomplete...?
https://github.com/WebAssembly/threads/blob/main/proposals/threads/Overview.md

@esimkowitz
Copy link

esimkowitz commented Oct 16, 2024

Maybe a bit naive but the spec sounds to me like an abstraction of Web Workers and there's a pthread implementation that interfaces with this to create threads so why can't TinyGo utilize this?

@aykevl
Copy link
Member

aykevl commented Oct 16, 2024

TinyGo does not support multithreading yet, on any platform.
It's not as simple as just running every goroutine in a separate thread. For an overview, see: #2446. But in short: things like the sync package, channels, and the GC also need to be able to live in a multithreaded world (for example, the GC can't assume that if it runs, no other threads are running).

The standard still doesn't specify how to create a thread, so it seems incomplete...?
https://github.com/WebAssembly/threads/blob/main/proposals/threads/Overview.md

Indeed. On the web it's typical to use Web Workers. For WASI there's the wasi-threads proposal that has been deprecated already. The consensus seems to be that both should be replaced with shared-everything threads, which will be built into WebAssembly instead of being specific to every host API.

@esimkowitz
Copy link

Ah, didn't realize it wasn't supported by TinyGo period, that would certainly be a blocker. I was imagining it would be more like a thread pool like in other runtime langs, but yeah if it's not supported at all that's a different story. Will pursue in the main Go repo...

@dkegel-fastly
Copy link
Contributor

It's possible that even big Go will have trouble here. Often tinygo leads on wasm support, and big go follows.

@aykevl
Copy link
Member

aykevl commented Oct 17, 2024

It's possible that even big Go will have trouble here.

As far as I'm aware, they don't support threading either.

@esimkowitz
Copy link

esimkowitz commented Oct 17, 2024

As far as I'm aware, they don't support threading either.

Big Go definitely supports concurrency via procs, which queue on threads...

@aykevl
Copy link
Member

aykevl commented Oct 17, 2024

As far as I'm aware, they don't support threading either.

Big Go definitely supports concurrency via procs, which queue on threads...

According to this issue, they don't support multithreading in WebAssembly: golang/go#28631

@esimkowitz
Copy link

esimkowitz commented Oct 17, 2024

Right, that's what I want to push them on 😉

AFAICT all the prerequisite features should be there now so it's just a matter of implementation. Not discounting that that is a tall order, but it feels weird that no effort is even being made...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request wasm WebAssembly
Projects
None yet
Development

No branches or pull requests

8 participants