Search Unity

IJobParallelFor add some native array without batching

Discussion in 'Data Oriented Technology Stack' started by eizenhorn, Apr 12, 2018.

  1. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    1,515
    Hello! Maybe I did not quite understand the logic of the work IJobParallelFor or something I missed, but I have the following question. Can we somehow transfer an NativeArray to job without batching. For example:
    Code (CSharp):
    1. struct SomeParallelJob : IJobParallelFor
    2.     {
    3.         public NativeArray<CustomStruct> dontBatchMe; //<-- I do not want to batching this array
    4.         public NativeArray<int>          batchMeBaby;
    5.  
    6.         public void Execute(int index)
    7.         {
    8.             int tmp = batchMeBaby[index];
    9.  
    10.             for (int i = 0; i < dontBatchMe.Length; i++)
    11.             {
    12.                 tmp += dontBatchMe[i].someFieldFromStruct;
    13.             }
    14.  
    15.             batchMeBaby[index] = tmp;
    16.         }
    17.     }
     
  2. mike_acton

    mike_acton

    Unity Technologies

    Joined:
    Nov 21, 2017
    Posts:
    109
    dontBatchMe should be marked [ReadOnly] in your example above.
     
    eizenhorn likes this.
  3. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    1,515
    Oh, I suspected it, but I did not have time to check it! Thank you for confirming my guess :)
     
  4. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    1,515
    And you can still 1 question, so as not to create a new thread. Can we get one output from Job? Approximately how here (Now I'm using a native array of long 1 for this):
    Code (CSharp):
    1. struct SomeJob : IJob
    2.     {
    3.         [ReadOnly] public NativeArray<int> data;
    4.         public int counter;
    5.         public void Execute()
    6.         {
    7.             for (int i = 0; i < data.Length; i++)
    8.             {
    9.                 counter++;
    10.             }
    11.         }
    12.     }
    13.     protected override void OnUpdate()
    14.     {
    15.         NativeArray<int> data = new NativeArray<int>(100000, Allocator.TempJob);
    16.         var someJob = new SomeJob
    17.         {
    18.             data = data,
    19.         };
    20.         JobHandle handle = someJob.Schedule(data.Length, 1250);
    21.         handle.Complete();
    22.         Debug.Log("Count: " + someJob.counter); // <--- this value always default int (0)
    23.         data.Dispose();
    24.     }
     
  5. mike_acton

    mike_acton

    Unity Technologies

    Joined:
    Nov 21, 2017
    Posts:
    109
    > Now I'm using a native array of long 1 for this

    That's an acceptable approach. (Which is just a pointer to some allocated, presumably TempJob, memory.) - Any solution that's specific to one int would just be identical to that anyway. (Although you could make an NativeInt container if you wanted, it's probably doesn't buy you anything.)
     
    eizenhorn likes this.
  6. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    1,515
    Ok :) Thank you for ansewers :)