Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Resolved array NativeHashMap error jobparallel

Discussion in 'Entity Component System' started by RendergonPolygons, Jan 24, 2022.

  1. RendergonPolygons

    RendergonPolygons

    Joined:
    Oct 9, 2019
    Posts:
    97
    Hi

    I am trying to use an array of NativeHashMap in a job, this container meets the job documentation requirements. I am getting this error. Is it because an array of native container is not allowed for jobs or maybe something else?

    InvalidOperationException: thisJParallelJob._all_Groups_PixelHashMap is not a value type. Job structs may not contain any reference types.

    I'm using Burst, sorry if this is not the right place to post as I'm not using entities but i'm using mono and jobs. I'm on Unity 2020.3.22, Burst 1.4.1, Collection 0.15.0 prev .21, Entities 0.17.0-prev.42, Jobs 0.8.0prev.23

    Code (CSharp):
    1. void AFunc()
    2. {
    3. thisImg.A_Groups_PixelDataHashMap = new NativeHashMap<float3, float4>[512 * 512];
    4. var thisJHandle = new thisJParallelJob()
    5.             {
    6. _all_Groups_PixelHashMap = thisImg.A_Groups_PixelDataHashMap,
    7. _pixels =new float4(0,0,0,0),
    8. _locat=new float3(1,1,1,1),
    9. };
    10. thisJHandle.Schedule(512, 64).Complete();
    11. }
    12. public struct thisJParallelJob: IJobParallelFor
    13.         {
    14. [NativeDisableParallelForRestriction] public NativeHashMap<float3, float4>[] _all_Groups_PixelHashMap;
    15. float4 _pixels;
    16. float3 _locat;
    17. public void Execute(int y)
    18.             {
    19. _all_Groups_PixelHashMap[0].Add(_locat,_pixels);
    20. }
    21. }
    thanks
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,447
    NativeHashMap<float3, float4>[]

    you can't have an array in a job (that isn't static readonly)
     
    RendergonPolygons likes this.
  3. RendergonPolygons

    RendergonPolygons

    Joined:
    Oct 9, 2019
    Posts:
    97
    Very helpful @tertle , thanks!

    Took the turn of up bucketing pixels and using NativeMultiHashMap<thebucket,PixelData> for this.

    Code (CSharp):
    1. public struct PixelData
    2.     {
    3.         public Color pixelColor;
    4.         public float3 pixelLocat;
    5.  
    6.         public PixelData(Color thisColor, float3 thisLocat)
    7.         {
    8.             pixelColor = thisColor;
    9.             pixelLocat = thisLocat;
    10.         }
    11.     }
    I had tried this before but I had not set Parallelwrite() correctly until I found this thread and discarded the use of Concurrent. Now in AFunc() I init the NativeMultiHashMap:

    var multiHashMap = new NativeMultiHashMap<int, PixelData>(texW*texH, Allocator.Persistent);

    and in struct thisHandle assign the ParallelWriter:
    _multiHashMap= multiHashMap.AsParallelWriter(),

    The other got'cha I found in case it helps anyone, is that in the job I didn't label the NativeMultiHashMap as [NativeDisableParallelForRestriction], it was just like :

    Code (CSharp):
    1. public struct thisJParallelJob: IJobParallelFor
    2. {
    3. public NativeMultiHashMap<int, PixelData>.ParallelWriter _all_Groups_PixelHashMap;
    and works :)
     
    Last edited: Jan 26, 2022