Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to allow native container for parallel writing after declearing it fron inside job ?

Discussion in 'Entity Component System' started by Rewaken, Apr 25, 2019.

  1. Rewaken

    Rewaken

    Joined:
    Mar 24, 2015
    Posts:
    126
    Hello,
    I am creating nativelist inside job like this(using 19.1)
    Code (CSharp):
    1. var p = new NativeList<int>(Allocator.Temp);
    I need to write and read from list but unity throwing an error that
    InvalidOperationException: The native container has been declared as [WriteOnly] in the job, but you are reading from it.
    But how can I assign field initializers inside a struct ?
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,753
    Show the full job
     
    julian-moschuering likes this.
  3. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    1,112
    Declaring native Containera from a job is not a good idea.

    Do not allocate managed memory in jobs
    Allocating managed memory in jobs is incredibly slow, and the job is not able to make use of the Unity Burst compiler to improve performance.
     
  4. julian-moschuering

    julian-moschuering

    Joined:
    Apr 15, 2014
    Posts:
    529
    This isn't managed memory.

    Quicktest using 2019.1.0f2, this works even with burst:
    Code (CSharp):
    1. using System.Linq;
    2. using Unity.Burst;
    3. using Unity.Collections;
    4. using Unity.Entities;
    5. using Unity.Jobs;
    6. using UnityEngine;
    7.  
    8. class AllocTest : JobComponentSystem
    9. {
    10.     [BurstCompile]
    11.     struct AllocJob : IJob
    12.     {
    13.         public NativeArray<int> result;
    14.  
    15.         public void Execute()
    16.         {
    17.             var p = new NativeList<int>(Allocator.Temp);
    18.             p.Add(0);
    19.             p.Add(1);
    20.             p.Add(2);
    21.             p.Add(3);
    22.             p.Add(4);
    23.             for (var i = 0; i < p.Length; i++)
    24.                 p[i] *= 2;
    25.             for (var i = 0; i < p.Length; i++)
    26.                 result[i] = p[i];
    27.         }
    28.     }
    29.  
    30.     protected override JobHandle OnUpdate(JobHandle inputDeps)
    31.     {
    32.         var result = new NativeArray<int>(5, Allocator.TempJob);
    33.         var job = new AllocJob
    34.         {
    35.             result = result
    36.         }.Schedule(inputDeps);
    37.         job.Complete();
    38.         Debug.Log(string.Join(",", result.Select(i => i.ToString())));
    39.         result.Dispose();
    40.         return job;
    41.     }
    42. }
     
    Tyler_Heers_BFG likes this.
  5. Rewaken

    Rewaken

    Joined:
    Mar 24, 2015
    Posts:
    126
    Hello, Thanks for the help but actually I made a small mistake in my code due to which unity was throwing an error.