Search Unity

Cacheline contention

Discussion in 'Entity Component System' started by JooleanLogic, Feb 2, 2019.

  1. JooleanLogic

    JooleanLogic

    Joined:
    Mar 1, 2018
    Posts:
    447
    I'm running multiple jobs in parallel which share a NativeArray and each job stores a unique index it can read/write to. Is it worth ensuring one item per cacheline to avoid invalidating that line?

    I ask because I noticed in the NativeHashMap source they're using an IntsPerCacheLine value throughout. I haven't looked in detail but seems to be used for cacheline per thread data which I assume is to avoid contention.

    Code (CSharp):
    1. struct ComputeData {
    2.     Entity entity;
    3.     float mass;
    4. }
    5.  
    6. Job : IJob {
    7.     int writeIndex;
    8.     [NativeDisableContainerSafetyRestriction]
    9.     NativeArray<ComputeData> sharedData;
    10.  
    11.     void Execute() {
    12.         // ... do something
    13.         sharedData[writeIndex] = new ComputeData {..};
    14.     }
    15. }
    16.  
    17. void OnUpdate() {
    18.     var sharedData = new NativeArray<ComputeData>(numJobs, Allocator.TempJob);
    19.  
    20.     for (int i = 0; i < numJobs; i++)
    21.     {
    22.         new Job {
    23.             writeIndex = i,
    24.             sharedData = sharedData
    25.         }.Schedule(inputDeps)
    26.     }
    27.     ...
    28. }
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    The parallel for indices will generally be split up in ranges per thread, thus contention on a native array will be only visible on boundaries.

    If you are performing random access like on a hashtable the answer can be different. Tools like VTune can tell you what cache line contention between threads is.
     
  3. JooleanLogic

    JooleanLogic

    Joined:
    Mar 1, 2018
    Posts:
    447
    Ok thankyou.

    My next question then would be are NativeArray allocations cacheline aligned by default?

    - Edit - Not the component ones in chunks, the ones we allocate ourselves. I.e.
    var array = new NativeArray<int>(Allocator.TempJob)
     
    Last edited: Feb 3, 2019