Search Unity

ReadWriteBuffers are restricted to only read & write the element at the job index.

Discussion in 'Entity Component System' started by Lorin_Atzberger, Jul 24, 2021.

  1. Lorin_Atzberger

    Lorin_Atzberger

    Joined:
    Dec 11, 2013
    Posts:
    119
    I'm trying to write a IJobParallelFor that processes different chunks of a grid(stored as a NativeArray).

    As an input, I have a NativeArray<Vector2Int>. For any given index of Execute, I take the Vector2Int from my input and based on that I compute the bounds of the chunk I need to update. Doing this I ensure that any instance of Execute is completely separated from the others, since the bounds never overlap.

    Doing this, however, leads me to errors like
    Code (CSharp):
    1. IndexOutOfRangeException: Index 19296 is out of restricted IJobParallelFor range [144...144] in ReadWriteBuffer.
    2. ReadWriteBuffers are restricted to only read & write the element at the job index. You can use double buffering strategies to avoid race conditions due to reading & writing in parallel to the same elements from a job.
    Am I doing something wrong? Is the text actually true in the sense that for whatever read/write array I might have, if the index of Execute is 43, then I HAVE to access the array at index 43? If so, this is incredibly limiting. Are there ways to circumvent the safety system or disable it for particular jobs?
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,271
    [NativeDisableParallelForRestriction]

    Be careful using it though, because it really does disable some safety checks and you could have hidden issues.
     
    cdiggins and Anthiese like this.
  3. Lorin_Atzberger

    Lorin_Atzberger

    Joined:
    Dec 11, 2013
    Posts:
    119
  4. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,271
    I've been in the DOTS community for a while now and this was one of the first things I learned. So unfortunately I can't point you to any particular resource.
     
  5. 8bitgoose

    8bitgoose

    Joined:
    Dec 28, 2014
    Posts:
    448
    So adding that means you can read and write to any point in the array. Which means if you are doing things in parallel, you may read and write things that are being read or written by another thread. Since the work isn't executed in the same order all the time, you may get inconsistent results. Only use it if you can be sure you aren't writing or reading in different parallel jobs.