Search Unity

Disable min/max write restriction without using [ReadOnly]

Discussion in 'Entity Component System' started by sient, Jun 24, 2018.

  1. sient

    sient

    Joined:
    Aug 9, 2013
    Posts:
    602
    Is it possible to disable the min/max write restriction for a given job without using [ReadOnly]?

    For example, here is an IJobParallelFor instance which stores a summation of an input array into an output array, where each element in the output array is only written to by a single thread. (Please ignore the false-sharing issues, this is a contrived example).

    Code (csharp):
    1.  
    2. public class Test {
    3.   struct ArrayWriter : IJobParallelFor {
    4.     public NativeArray<int> input;
    5.     [NativeSetThreadIndex]
    6.     public int m_ThreadIndex;
    7.     public NativeArray<int> summation;
    8.  
    9.     public void Execute(int index) {
    10.       summation[m_ThreadIndex] += index;
    11.     }
    12.   }
    13.  
    14.   [Test]
    15.   public void WriteToArray() {
    16.     var input = new NativeArray<int>(500, Allocator.Temp);
    17.     var output = new NativeArray<int>(JobsUtility.MaxThreadCount, Allocator.Temp);
    18.  
    19.     // Fill some dummy data.
    20.     for (int i = 0; i < input.Length; ++i)
    21.       input[i] = 1;
    22.  
    23.     new ArrayWriter {
    24.       input = input,
    25.       output = output
    26.     }.Schedule(input.Length, 1).Complete();
    27.  
    28.     input.Dispose();
    29.     output.Dispose();
    30.   }
    31. }
    32.  
     
  2. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,555
    [NativeDisableParallelForRestriction] public NativeArray<int> summation;
    ?
     
  3. sient

    sient

    Joined:
    Aug 9, 2013
    Posts:
    602
    Thanks! It looks like the docs do not mention that yet.