Search Unity

NativeDisableParallelForRestriction and Burst Performance

Discussion in 'Burst' started by JakHussain, Jun 9, 2020.

  1. JakHussain

    JakHussain

    Joined:
    Oct 20, 2016
    Posts:
    318
    Just a quick sanity check question. In Unity's recent blog post on the Burst Compiler, they wrote:

    I assume they're referring to native collections in this sentence. But in a lot of my jobs I'm using flattened 2D arrays as native arrays and deriving an i and j index for each "index" value passed into my job so I can access my data AS IF it were a 2D array. This is only made possible using the NativeDisableParallelForRestriction attribute.

    My question is, does the use of this attribute actually hinder the performance of burst compiled code since it can no longer make that assumption? And if so, is there another pattern I could use that plays more nicely with burst?
     
  2. CleverAI

    CleverAI

    Joined:
    Jun 4, 2017
    Posts:
    38
    I would like to know this too.
     
  3. florinel2102

    florinel2102

    Joined:
    May 21, 2019
    Posts:
    76
    Since there is no information about what this tag actually does (behind the scene) , my assumption is that is something similar to pthread_mutex from C (more here
    ) , basically every other thread must wait if another one is writing and yeah there is a waste of performance . If that is the case I suggest to combine the result after the job is finished which is much better in most cases , although you have to allocate more memory . Even if my assumption is 'not perfect' , there is certainly a waste of performance there , because some threads must wait in order to avoid race condition .
     
    Last edited: Apr 14, 2022
  4. Per-Morten

    Per-Morten

    Joined:
    Aug 23, 2019
    Posts:
    119
    AFAIK, The only thing the attribute does is to turn off a particular part of the safety system for that container. The code doesn't change based on the attribute, and unity is certainly not introducing locks for you, because there is no way they can do that while guaranteeing they get the correct result.

    We use that attribute all the time, it's completely valid to use it, you just need to ensure that your code is correct because you're telling Unity that you know what you're doing and that it can't detect race conditions on that particular collection.
     
    DragonCoder likes this.
  5. Neto_Kokku

    Neto_Kokku

    Joined:
    Feb 15, 2018
    Posts:
    1,751
    As @Per-Morten said, that tag only disables the run-time native container safety checks, which only detect invalid uses and prevent the job from running if the collections don't meet the requirements and don't actually inject any sort of actual safety mechanisms into the code.

    You also don't have to guess: the generated native burst ASM is available for reading in the burst inspector, in its full glory.
     
    Per-Morten likes this.
  6. florinel2102

    florinel2102

    Joined:
    May 21, 2019
    Posts:
    76
    Ok , then I misunderstood what is used for , I saw in some tutorials/threads that it's used for avoiding race condition not just to prevent messages about it .
     
  7. Zuntatos

    Zuntatos

    Joined:
    Nov 18, 2012
    Posts:
    612
    https://docs.unity3d.com/2022.2/Documentation/ScriptReference/Unity.Jobs.IJobParallelFor.html

    > Execute(int index) will be executed once for each index from 0 to the provided length. Each iteration must be independent from other iterations (The safety system enforces this rule for you)

    "the safety system enforces this rule" is what you disable with NativeDisableParallelForRestriction
    the safety system checks if you only access containers with write access using the same index as the one passed by the ParallelForJob.
    If you have some math/logic on the index to get to the one you write to, you'll get the safety system yelling at you despite it being a safe operation, so you can bypass it with this attribute.
     
    DragonCoder likes this.