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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How do I create nested loop IJobParallelFor?

Discussion in 'Entity Component System' started by IC_, Aug 18, 2018.

  1. IC_

    IC_

    Joined:
    Jan 27, 2016
    Posts:
    61
    Hello. I want to create a grid of some entities with ecs. Each entity should have component GridComponent that contains two ints x and y. How do I implement a job and jobcomponentsystem for it
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,592
  3. IC_

    IC_

    Joined:
    Jan 27, 2016
    Posts:
    61
  4. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,647
    one works in parallel with itself and the other doesn't. Ideally you use IJobParallelFor whenever you can for performance.

    IJob allows you to schedule a single job that runs in parallel to other jobs and the main thread.
    https://docs.unity3d.com/2018.1/Documentation/ScriptReference/Unity.Jobs.IJob.html

    Parallel-for jobs allow you to perform the same independent operation for each element of a native container or for a fixed number of iterations.
    When Scheduled the job's Execute(int index) method will be invoked on multiple worker threads in parallel to each other.
    https://docs.unity3d.com/2018.1/Documentation/ScriptReference/Unity.Jobs.IJobParallelFor.html
     
    IC_ likes this.
  5. Zooltan

    Zooltan

    Joined:
    Jun 14, 2013
    Posts:
    19
    You can do the first for loop jobbed, and then do the inner for normally.

    You will probably want to make a flat array, as you can't have a nested NativeArray.

    This code is just written from memory, but should give you an idea of how it works.
    Code (CSharp):
    1. struct VelocityJob : IJobParallelFor
    2.     {
    3.        
    4.         public NativeArray<GridComponent> yourArray;
    5.  
    6.         // The code actually running on the job
    7.         public void Execute(int i)
    8.         {
    9.             int size = yourArray.length;
    10.             for(int j = 0; j < size; j++)
    11.             {
    12.                GridComponent component = yourArray[i * size + j];
    13.             }
    14.         }
    15.     }
    But you might run into some problems, where you are not allowed to read from some of the entities, as each thread is only allowed access to a part of the whole data.
    You can get around that, by making your array ReadOnly, but that only works if you don't need to write to them.

    I don't have a solution to all this right now, but I hope this will help you get started.
     
    DalamarTD7 and Chmyke like this.
  6. DalamarTD7

    DalamarTD7

    Joined:
    Jan 21, 2016
    Posts:
    3
    READ ONLY FTW!! Efing brilliant! Thank you.