Search Unity

Assets [Open Source] Svelto.Tasks - c# promises compliant multi-threaded task runner

Discussion in 'Works In Progress - Archive' started by sebas77, Oct 22, 2017.

  1. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,644
    Svelto.Tasks

    Run Asynchronous promise compliant tasks in Serial, Parallel and from other threads exploiting full continuation.

    FAQ:

    What is Svelto.Tasks?

    Svelto.Tasks is part of the Svelto Framework. It allows to run yielding IEnumerators in Serial, Parallel and between threads. It's much more than it sounds, as the unique continuation feature allows to switch seamlessly between schedulers so that tasks can run at different times following the same simple flow. The ITaskRoutine interface allows to use Promises compliant tasks too.

    Who's using Svelto?

    Svelto is the official framework of Freejam, so it's extensively used to run Robocraft, Robocraft Infinity and Cardlife (plus new upcoming titles). I know other companies are using it, but unluckily I am not getting much feedback. Please let me know if you use it, as I'd like to be in contact.

    Do you offer support for Svelto?

    Svelto is open source and offered as it is. Unluckily I don't have much time to invest on it beside what I need for our projects, but I will be glad to answer your questions as they come. Of course, being open source and hosted on github, feel free to send me pull requests.

    OK, but what can I do with it?

    It's not simple to explain in few words the potentiality of the framework as it can easily be confused with many similar libraries out there. Svelto.Tasks contains many unique features. I tried to show them through my articles and the set of examples and unit tests that ship with the library. However I understand that, without a proper documentation, is hard to wrap one's head around it at glance. I strongly suggest to experiment with it starting from what I offer, then I will be here to try to help whenever I can.

    What can I do that I can't do with Unity already?

    Better question. I will list the unique features, from the most obvious to the less ones:

    • Allows to run yielding IEnumerator (you should see them as asynchronous tasks) on classes that are not Monobehaviour. For example: MyEnumerator.Run(), will run the enumerator on the UpdateMonoRunner scheduler through an always available static extension method. Fire and forget it.
    • There are several Schedulers available: UpdateMonoRunner, LateMonoRunner, CoroutineMonoRunner, StaggeredMonoRunner, PhysicMonoRunner. You can explicitly choose the runner to run your IEnumerator on using the static extension RunOnSchedule. For example: MyEnumerator.RunOnSchedule(new LateMonoRunner()) or using the standard ones MyEnumerator.RunOnSchedule(StandardSchedulers.physicScheduler).
    • Because of the Marshalling that the normal StartCoroutine must perform, using Svelto.Tasks should be always faster in every occasion.
    • You can yield very complex flow of tasks exploiting continuation. You can yield a serial collection of tasks, from a parallel collection of task, from a normal IEnumerator.
    • You can yield everything from other threads using the MultiThreadRunner. It works exactly like the other runners. However you must use the ThreadSafeRunOnSchedule() method instead.
    • A task running from the standard UpdateMonoRunner can yield and wait for a task running on a MultiThreadRunner. Synchronization between threads become much easier to manage.
    • Through the interface ITaskRoutine, you can setup OnFail (exception happened) and OnStop (task stopped explicitly) callbacks. All the other promise-like features are available using the same logic that the .Net task library uses (more on the examples)
    • Well, as I said, much more can be done, but it must be tried to get it properly. Please check articles and example for more insights.
    • Designed to be fast, light and allocation free.

    Further Resources:

    My blog: http://www.sebaslab.com/

    Main Article: http://www.sebaslab.com/svelto-taskrunner-run-serial-and-parallel-asynchronous-tasks-in-unity3d/

    Article on optimizations: http://www.sebaslab.com/svelto-ecs-...ats-coming-and-optimization-related-thoughts/

    Github: https://github.com/sebas77/Svelto.Tasks (many examples of usage and unit tests included)
     
    Last edited: Dec 3, 2017
    andreiagmu and one_one like this.
  2. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,644
  3. sledgeman

    sledgeman

    Joined:
    Jun 23, 2014
    Posts:
    389
    Wow. I don´t exactly know what for it could be used....but what immidiantly comes in my mind: simulations, like fluid simulations. Would something like this benefit of using your framework ?
     
  4. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,644
    I don't blame you for having difficulties to find an application for this. TBH the demo is misleading, this kind of applications must actually run on the GPU this is what the GPU is for.

    Svelto.Tasks is about running about anything on other threads without worrying much about it as long as you understand how concurrent data access works. That's why the best is to use it with Svelto.ECS. If you write an Engine (system) totally encapsulated, you can just run its loop on another thread without any effort.

    For example you can run all the custom physic for the wings (like we do in robocraft) on another thread, just changing the runner of the task.
     
  5. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,644
  6. sledgeman

    sledgeman

    Joined:
    Jun 23, 2014
    Posts:
    389
    i got 950.000 on my 960m nVidia, intel 4720HQ
     
  7. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,644
    Very close to the max!
     
  8. LennartJohansen

    LennartJohansen

    Joined:
    Dec 1, 2014
    Posts:
    2,394
  9. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,644
  10. LennartJohansen

    LennartJohansen

    Joined:
    Dec 1, 2014
    Posts:
    2,394


    What does it actually test?
     
  11. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,644
    What kind of beast have you got?!?! This is a CPU benchmark, although the GPU may affect the final result.
     
  12. LennartJohansen

    LennartJohansen

    Joined:
    Dec 1, 2014
    Posts:
    2,394
    14 core 28 thread intel i9 overclocked to 4.1 ghz
     
  13. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,644
    I knew that! That machine can easily score 3M
     
  14. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,644
    do you want to know how far you can get? I can update it :) However your water cooling system will boil
     
  15. LennartJohansen

    LennartJohansen

    Joined:
    Dec 1, 2014
    Posts:
    2,394


    CPU was at 65%
     
  16. LennartJohansen

    LennartJohansen

    Joined:
    Dec 1, 2014
    Posts:
    2,394
    let us give it a try
     
  17. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,644
    well I can make it get to 100% if you are up for a challenge :)
     
  18. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,644
    I'll do it tonight ;)
     
  19. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,644
    here you go...4M particles!

    https://t.co/53Wl6kuGPT

    in reality the particles rendered are 1M, I just transform them 4 times. I can't increase the number of particles without affecting the performance due to the CPU->GPU data upload which is not part of what I want to benchmark.

    P.s: prepare to throw ice on the motherboard
     
  20. LennartJohansen

    LennartJohansen

    Joined:
    Dec 1, 2014
    Posts:
    2,394
    Ended up with 2247168

    Lennart
     
  21. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,644
    Cool thanks were all the cores working?
     
  22. LennartJohansen

    LennartJohansen

    Joined:
    Dec 1, 2014
    Posts:
    2,394
    yes. was at 96% total

    But did not scale as I was expecting. Could there be another bottleneck in the test? memory bandwidth?
    2 million was at 65% CPU
     
  23. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,644
    True it should be linear. I won't experiment more right now, but in future I would like to test more strategies as there are various variables that could change the result. Thanks for testing!
     
  24. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,644
  25. FiveFingers

    FiveFingers

    Joined:
    Oct 15, 2009
    Posts:
    541
    What a cup-of-coffee super-nerd talk this one :D
    I would like to boil my CPU too with the 1M example.
    Here my result:

    Out of curiosity: what version of unity did you use to compile that ?
    The dialog panel doesn't show 1920x1080 resolution which is my default desktop and gaming video res, the higher I get in the startup dialog is 1680x1050...weird.

    To answer Sledgeman, I have an application that needs to work in multithreading for speed, and it needs to launch timed instructions through coroutines. A thread can't start coroutines, then, it comes Svelto.Task. Problem solved! :)
     
  26. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,644
    Hey there, I didn't care about the resolution as the GPU shouldn't affect the result ;)