andreakarasho / TinyEcs

A tiny bevy-like archetype-style ECS library for dotnet
MIT License
113 stars 1 forks source link

Schedule Parallelization #24

Closed xentripetal closed 4 months ago

xentripetal commented 5 months ago

It would be great to eventually support automatically parallelizing systems in schedules based on their resource access.

I started work on porting Bevy's ECS scheduler to C# but put it on hold for now. I plan to come back to in the future, but if you're interested my draft state is here https://github.com/xentripetal/PolyGame/tree/73b5596a2ecb85016f9edc937b091c7ce05c1117/PolyECS/Scheduling

andreakarasho commented 5 months ago

I don't know how bevy manages multithreading systems, but i made something working here https://github.com/andreakarasho/TinyEcs/tree/feat/scheduler-multithread The logic should [didn't test so much lol] split all systems which are not using borrowed/already-accessed resources.

In addition, systems are spawned with the ThreadingType.Auto as default value. You can change that ofc and force the running behaviour.

scheduler.AddSystem(() => Console.WriteLine("Auto"), threadingType: ThreadingMode.Auto);
scheduler.AddSystem(() => Console.WriteLine("Single"), threadingType: ThreadingMode.Single);
scheduler.AddSystem(() => Console.WriteLine("Multi"), threadingType: ThreadingMode.Multi);
xentripetal commented 5 months ago

Interesting strategy, thanks for the quick solution!

Bevy looks at the tables each system will read or write, run systems in parallel based on it, and automatically inserts sync points/end defers between write-reads.

andreakarasho commented 4 months ago

merged https://github.com/andreakarasho/TinyEcs/pull/25