Search Unity

[Safety System] Schedule 2 jobs in parallel if one modifies an irrelevant part of a struct?

Discussion in 'Entity Component System' started by burningmime, Sep 21, 2021.

  1. burningmime

    burningmime

    Joined:
    Jan 25, 2014
    Posts:
    845
    Say I have a struct and 2 jobs like this:

    Code (CSharp):
    1. struct Vertex
    2. {
    3.     public float3 position;
    4.     public float2 uv;
    5. }
    6.  
    7. struct ColliderJob : IJob
    8. {
    9.     [ReadOnly] public NativeArray<Vertex> vertices;
    10.     [WriteOnly] public NativeReference<PMeshCollider> output;
    11.     public void Execute() { /* read from position, ignore UV, write to output */ }
    12. }
    13.  
    14. struct UVJob : IJob
    15. {
    16.     public NativeArray<Vertex> vertices;
    17.     public void Execute() { /* read from position, write to UV */ }
    18. }
    These jobs should be able to run in parallel since the ColliderJob does not read from the UV field, which is the only thing the UVJob will modify. However the safety system (rightly) complains since I am actually modifying the contents of an array while in use by another job. Is there a way to tell the safety system to just take a chill pill for a bit and not make these jobs depend on one another?
     
    Last edited: Sep 21, 2021
    Lukas_Kastern likes this.
  2. CookieStealer2

    CookieStealer2

    Joined:
    Jun 25, 2018
    Posts:
    119
    Try putting this attribute in front of the vertex array:
    [NativeDisableContainerSafetyRestriction]
     
    burningmime likes this.
  3. M_R

    M_R

    Joined:
    Apr 15, 2015
    Posts:
    559
    this will trash the cache, as writing the uv invalidates the entire line (false sharing). it may be faster to run them serially.

    you should have an array for positions and one for uvs
     
  4. burningmime

    burningmime

    Joined:
    Jan 25, 2014
    Posts:
    845
    And then another job to combine them into their final form? Hmmm... this might work.