Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Not sure if Job is executing efficiently

Discussion in 'Entity Component System' started by hellowill89, Apr 2, 2018.

  1. hellowill89

    hellowill89

    Joined:
    Jan 8, 2013
    Posts:
    45
    I have a project setup using IJobParallelForTransform which I create and schedule on Update. It looks like this:

    Code (CSharp):
    1.             foreach (var items in itemsSet) {
    2.                 Job job = MyJob();
    3.                 JobHandle handle = job.Schedule(items);
    4.             }
    When profiled, the jobs execute like this:



    The jobs don't seem to run in parallel even though they are distributed across cores. The performance is actually pretty good, but I thought in the profiler it would seem more parallel. There are no dependencies passed into the Schedule call, so I'm wondering if anything is irregular with the code.
     

    Attached Files:

  2. illinar

    illinar

    Joined:
    Apr 6, 2011
    Posts:
    863
    My understanding was that you need to pass dependencies so that jobs know when they can run in parallel and when they can't. Am I incorrect in that assumption? So If you don't pass dependencies, they have to run sequentially and maybe there would be some sync gaps between them. I might be wrong, but you could try.
     
    Last edited: Apr 2, 2018
  3. FastTurtle222

    FastTurtle222

    Joined:
    Dec 24, 2017
    Posts:
    28
    Something like this should work for you . The problem is you need to string your jobs as dependencies as one another. Please let me know if this fixes your problem with the idle gaps.

    Also this has nothing to do with your problem but declaring variables inside of loops can cause a lot of work for the Garbage collector.


    Code (csharp):
    1.  
    2.                        //List used to cache the jobs scheduled in this system.
    3.                        List<JobHandle> jobs = new List<JobHandle>();
    4.                        //loop through all itemSet entries, note this may need to be changed to itemSet.Count if it is stored as a list instead of an array.
    5.                        Job job = MyJob();
    6.                        for(int i = 0; i < itemsSet.Length; ++i)
    7.                       {
    8.                          
    9.                           if(i == 0)
    10.                           {
    11.                               jobs.Add( job.Schedule(items));
    12.                           }
    13.                           else
    14.                           {
    15.                                jobs.Add(job.Schedule(items, 250, jobs[i - 1]));
    16.                           }
    17.  
    18.  
    19.            }
    20.  
    21.  
     
  4. hellowill89

    hellowill89

    Joined:
    Jan 8, 2013
    Posts:
    45
    I've now tried it like you recommended (chaining dependencies) and there was no difference. I also tried chaining the dependencies followed by a `Complete` call to the last handle and there was no difference.

    My theory is that `IJobParallelForTransform` is behaving differently from `IJobParallelFor` because I have had success in a different location using the latter.
     
  5. Well, if you have no performance problems, then it does not matter. BTW I think you have this because your jobs are fairly short, no sweat to the CPU.
    The not chaining does not guarantee that the jobs will run in parallel or even that they will run on different threads. They could.
    If you chain your jobs that means they will be waiting for each other, no matter what. Even if you have longer jobs. So that is the opposite of the parallelism.

    IJobParallelForTransform -> you will have as many threads as many root transform you have (max), which means the hierarchies you're maintaining in Unity does matter, if you want three threads, then you will need to organize your transforms under three root transforms.