aya-lang / aya

Pocket sized programs
MIT License
54 stars 3 forks source link

Multithreading #92

Closed nick-paul closed 2 months ago

nick-paul commented 7 months ago

I removed all references to the global Aya thread and passed execution context down using the refactored blocks from this PR. With a bit more refactoring I was able to provide an API to spawn and manage multiple instances the Aya thread (now called AyaThread).

The API is very clunky and there are a handful of bugs I still need to work through but the implementation is quite stable now.

Examples

I used the raytracer implementation to test stability and performance since it was a fairly large aya project. I implemented a thread pool using the current version of the thread API. It is a bit clunky and needs some improvement but was very stable overall.

The performance is significantly better with more threads but there is still quite a bit of overhead. Rendering an image with 15 threads gave a ~6x speedup as seen below:

multithread_3s
15 threads, execution time ~3s

singlethread_18s 1 thread, execution time ~18s

Code for the multithreaded renderer is here: https://github.com/nick-paul/aya-raytrace/tree/thread

nick-paul commented 2 months ago

Most bugs have been sorted out and docs have been added here: https://github.com/aya-lang/aya/blob/fiber/manual/multithreading.md

Should be good to merge soon