Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How to schedule parallel job for more than 4 frames?

Discussion in 'Entity Component System' started by Zylkowski_a, Sep 10, 2020.

  1. Zylkowski_a

    Zylkowski_a

    Joined:
    Jul 27, 2019
    Posts:
    157
    I have pretty heavy job chain in my code and I would like main thread to still be responsive, so I schedule my jobs and just check every frame if they are completed, if yes then I do something with them. I already struggled with combining dependencies because you can't combine them if jobs would take more than 4 frames(WTF) and now I am struggling with .ScheduleParallel() on IJobFor because I get errors with arrays living longer than 4 frames. I understand that it's using Allocator.Temp somewhere but that's just stupid not giving users options to run jobs longer. So is there any way of running IJobFor parallel for longer than 4 frames?
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,984
    If there is no TempJob anywhere in your project and you are getting this error, file a bug report. That is a bug.
     
  3. burningmime

    burningmime

    Joined:
    Jan 25, 2014
    Posts:
    845
    Long-running jobs can jam up job threads. Jobs are never pre-empted so keep running until they're done, not allowing anything else to use that thread. If all processors are filled up with these jobs, Unity will stop running internal jobs (like rendering...) until one finishes.

    For long-running background tasks, you can spawn a managed thread (eg using tasks or just
    new Thread()
    ).
     
  4. Zylkowski_a

    Zylkowski_a

    Joined:
    Jul 27, 2019
    Posts:
    157
    Okay, I am aware of that but the thing is how can I schedule parallel job for more frames?
     
  5. dave_sf_42

    dave_sf_42

    Joined:
    May 28, 2019
    Posts:
    40
    What @burningmime is saying.. is that if you run a long-running parallel job, it can jam up all the workers and hurt your ability to run other jobs necessary for rendering. So he's saying, if you really want to run a long-running parallel job with 5 threads, then spawn 5 managed threads using new Thread(), and do your own background parallel work on there... probably after setting the thread priority to ThreadPriority.BelowNormal.
     
    nyanpath likes this.
  6. nyanpath

    nyanpath

    Joined:
    Feb 9, 2018
    Posts:
    77
    Have you tried Allocator.Persistent? I have hefty jobs too and use them when I know something is going to take a while. It's been ok so far, but I am running a hopelessly mangled hybrid mess currently using Monobehaviour until I get the opportunity to translate my scriptures to ECS.

    System.Threading.Task comes with a tiny bit of overhead and is also limited in what you can do with it (mostly non-UI), but works great for working with external third party APIs like Azure and such of cloud computing solutions. Generally not gaming-related in my experience, though.