Search Unity

Scheduling a long running job on worker threads.

Discussion in 'Data Oriented Technology Stack' started by Soaryn, Aug 19, 2019.

  1. Soaryn

    Soaryn

    Joined:
    Apr 17, 2015
    Posts:
    210
    Is there currently a way to allow / disallow jobs from running on main thread? Currently I have a long running job where its total estimated runtime per execution is roughly ~12ms and while I don't need it to complete in one frame, it causes issues when the job scheduler picks the main thread to perform its work.

    It would be nice to schedule a job with the intention of it running longer than a frame.
     
    Last edited: Aug 19, 2019
    TheGabelle and Jes28 like this.
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    1,665
  3. Sprocket

    Sprocket

    Joined:
    Sep 18, 2010
    Posts:
    30
    I am also very interested in this. I'm coding projects with runtime procedural generation where some computation happens each frame, but a lot of computation need to run in the background and won't necessarily finish in a single frame. Since it's a VR project it's pretty critical to ensure there's as few dropped frames as possible.

    Being able to run background jobs without interfering with per-frame performance must be useful for a lot of game features beyond procedural generation. AI comes to mind (especially computationally heavy techs like machine learning), but also things like pre-processing streamed assets and being able to quickly put a player into a low-detail scene while the high detail stuff is still being processed.

    It would be ideal with some built-in feature to let background jobs to idle while the per-frame code is running and then resume once there's idle cpu time. If that's not possible then at least being able to pause a job manually.

    Pseudo-code for doing it manually could be something like:

    void EarlyUpdate() {
    jobHandle.Pause();
    }

    void LateUpdate() {
    jobHandle.Resume();
    }

    Obviously this wouldn't be ideal, but at least it would ensure the background tasks don't slow down the per-frame code.
     
    dyox likes this.
  4. Soaryn

    Soaryn

    Joined:
    Apr 17, 2015
    Posts:
    210
    I think I found my issue. Two jobs. Scheduled independently from the entire system, but one as a follow up to the first.

    Both IJob based:
    Code (CSharp):
    1. var job1 = new LongJob().Schedule();
    2. var job2 = new LongJob().Schedule(job1); //This is where the main thread work can occur
    3.  
    4. job2.Complete();
    When job2 is cleared to run it has the potential to fire on the main thread. If job2 takes say 12 ms and has a potential to run on main thread, things bog down quick.

    If it were just one job with no existing dependencies then the job is run on a job thread, but that isn't practical in my scenario as the only thing burstable (that also should be bursted) is the first job which means I need two jobs and have it dependent.