Search Unity

  1. Want to see 2020.1b in action? Sign up for our Beta 2020.1 Overview Webinar on April 20th for a live presentation from our evangelists and a Q&A session with guests from R&D.
    Dismiss Notice
  2. Interested in giving us feedback? Join our online research interviews on a broad range of topics and share your insights with us.
    Dismiss Notice
  3. We're hosting a webinar for the new Input System where you'll be able to get in touch with the devs. Sign up now and share your questions with us in preparation for the session on April 15.
    Dismiss Notice

Burst don't allow me to create a NativeArray

Discussion in 'Data Oriented Technology Stack' started by georgeq, Sep 15, 2018.

  1. georgeq

    georgeq

    Joined:
    Mar 5, 2014
    Posts:
    558
    The documentations says:

    1) Burst supports the following primitive types:
    • bool
    • char
    • sbyte/byte
    • short/ushort
    • int/uint
    • long/ulong
    • float
    • double

    2) Burst supports regular structs with any field with supported types.

    3) Managed arrays are not supported by burst. You should use instead a native container, NativeArray<T> for instance.

    Based on that I'm trying to create a native array like this:

    Code (CSharp):
    1.  
    2.  [BurstCompile]
    3.       struct MainJob : IJobParallelFor {
    4.  
    5.          struct Obstacle{
    6.             public int   Distance;
    7.             public float X1;
    8.             public float X2;
    9.             public float Speed;
    10.          }
    11.  
    12.          public int length;
    13.  
    14.          public void Execute(int index) {
    15.  
    16.                NativeArray<Obstacle> LeftObst = new NativeArray<Obstacle>(length,Allocator.TempJob);
    17.  
    18.                LeftObst.Dispose();
    19.          }
    20.    }
    21.  
    but I'm getting this error:

    C:\buildslave\unity\build\Runtime\Export\NativeArray\DisposeSentinel.cs(49,9): error: Using managed `null` is not supported by burst

    While processing function `System.Void Unity.Collections.LowLevel.Unsafe.DisposeSentinel::Create(Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle&,Unity.Collections.LowLevel.Unsafe.DisposeSentinel&,System.Int32,Unity.Collections.Allocator)`

    While compiling job: System.Void Unity.Jobs.IJobParallelForExtensions/ParallelForJobStruct`1<Casablanca.ECS.SysAvoidCollision/MainJob>::Execute(T&,System.IntPtr,System.IntPtr,Unity.Jobs.LowLevel.Unsafe.JobRanges&,System.Int32)

    Compiler exception: System.NotSupportedException: Using managed `null` is not supported by burst
    at Burst.Compiler.IL.ILVisitor.NotSupported (Burst.Compiler.IL.Syntax.ILInstruction inst) [0x0016b] in <1ba7ce5f379b46f08028b383607c2c83>:0
    at Burst.Compiler.IL.ILVisitor.CompileInternal (Burst.Compiler.IL.Syntax.ILInstruction inst) [0x00523] in <1ba7ce5f379b46f08028b383607c2c83>:0
    at Burst.Compiler.IL.ILVerifier.CompileInternal (Burst.Compiler.IL.Syntax.ILInstruction inst) [0x00000] in <1ba7ce5f379b46f08028b383607c2c83>:0
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    4,882
    We are working on making NativeArray be allocatable from inside a job when Allocator.Temp label is used in C# jobs, including burst support.

    At the moment it is not supported.
     
  3. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    1,815
    Like Joachim says - is not supported yet, and documentation says about public fields for pushing data outside job in to him, not about allocation inside job.
     
  4. georgeq

    georgeq

    Joined:
    Mar 5, 2014
    Posts:
    558
    Obviously what you say is true, however I read again the documentation and I didn't find any reference about such limitation.
     
  5. VildNinja

    VildNinja

    Joined:
    Jan 17, 2013
    Posts:
    7
    Not sure if this has been fixed yet for the latest preview, otherwise there is an unsafe workaround if you want to use it for short arrays or slices:

    Code (CSharp):
    1.  
    2. const int maxPolyCount = 32;
    3. var polyPtr = stackalloc PolygonId[maxPolyCount];
    4. var polyPath = NativeSliceUnsafeUtility.ConvertExistingDataToNativeSlice<PolygonId>(polyPtr, sizeof(PolygonId), maxPolyCount);
    5. NativeSliceUnsafeUtility.SetAtomicSafetyHandle(ref polyPath, AtomicSafetyHandle.GetTempUnsafePtrSliceHandle());
    6.  
    /Jannek
     
    Razmot and recursive like this.
  6. xoofx

    xoofx

    Unity Technologies

    Joined:
    Nov 5, 2016
    Posts:
    268
    Indeed, that's an oversight. We will add this to the documentation. Currently, NativeContainer constructors are using managed objects (DisposeSentinel to track their lifetime) which is not supported by burst.
     
  7. whiskers434

    whiskers434

    Joined:
    Sep 23, 2014
    Posts:
    22
    Hi, sorry for poking an old thread

    was there any updated to this?

    is it supported in a newer burst version?

    or what is the correct way to use temp NativeArrays inside a job?

    getting

    Code (CSharp):
    1. C:\buildslave\unity\build\Runtime\Export\NativeArray\DisposeSentinel.cs(54,28): error: Creating a managed object `Unity.Collections.LowLevel.Unsafe.DisposeSentinel..ctor(...)` is not supported by burst
    2. at Unity.Collections.LowLevel.Unsafe.DisposeSentinel.Create(ref Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle safety, ref Unity.Collections.LowLevel.Unsafe.DisposeSentinel sentinel, int callSiteStackDepth, Unity.Collections.Allocator allocator) (at C:\buildslave\unity\build\Runtime\Export\NativeArray\DisposeSentinel.cs:54)
    3. at Unity.Collections.NativeList`1<Passenger_Manager_Jobs.class_origin_planet_picklist>..ctor(Unity.Collections.NativeList`1<Passenger_Manager_Jobs.class_origin_planet_picklist>* this, int capacity, Unity.Collections.Allocator i_label, int stackDepth) (at C:\Users\chris\Documents\InterstellarTransportCompany\Library\PackageCache\com.unity.collections@0.0.9-preview.12\Unity.Collections\NativeList.cs:71)
    4. at Unity.Collections.NativeList`1<Passenger_Manager_Jobs.class_origin_planet_picklist>..ctor(Unity.Collections.NativeList`1<Passenger_Manager_Jobs.class_origin_planet_picklist>* this, Unity.Collections.Allocator i_label) (at C:\Users\chris\Documents\InterstellarTransportCompany\Library\PackageCache\com.unity.collections@0.0.9-preview.12\Unity.Collections\NativeList.cs:38)
    5. at Passenger_Manager_Jobs.SpawnWeeklyPassengersForPlanetoidJob.Execute(Passenger_Manager_Jobs.SpawnWeeklyPassengersForPlanetoidJob* this, int index) (at C:\Users\chris\Documents\InterstellarTransportCompany\Assets\Scripts\Passenger_Manager_Jobs.cs:179)
    6. at Unity.Jobs.IJobParallelForExtensions.ParallelForJobStruct`1<Passenger_Manager_Jobs.SpawnWeeklyPassengersForPlanetoidJob>.Execute(ref Passenger_Manager_Jobs.SpawnWeeklyPassengersForPlanetoidJob jobData, System.IntPtr additionalPtr, System.IntPtr bufferRangePatchData, ref Unity.Jobs.LowLevel.Unsafe.JobRanges ranges, int jobIndex) (at C:\buildslave\unity\build\Runtime\Jobs\Managed\IJobParallelFor.cs:43)
    7.  
    8.  
    9. While compiling job: System.Void Unity.Jobs.IJobParallelForExtensions/ParallelForJobStruct`1<Passenger_Manager_Jobs/SpawnWeeklyPassengersForPlanetoidJob>::Execute(T&,System.IntPtr,System.IntPtr,Unity.Jobs.LowLevel.Unsafe.JobRanges&,System.Int32)
    10.  
     
  8. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    1,815
    Show code. NativeArrays allocated with temp allocator inside job works fine in bursted job. And on which unity and dots packages versions you are?
     
  9. whiskers434

    whiskers434

    Joined:
    Sep 23, 2014
    Posts:
    22
    unity 2018.4.14 LTS
    burst 1.2.0-preview.11

    sample job code, works without temp_array but adding any temp array gives burst compile error

    Code (CSharp):
    1. [BurstCompile]
    2.     struct SampleParallelJob : IJobParallelFor
    3.     {
    4.         [ReadOnly]
    5.         public NativeArray<JobValues> job_values;
    6.  
    7.         [ReadOnly]
    8.         public NativeList<MoreValues> more_values;
    9.  
    10.         [WriteOnly]
    11.         public NativeMultiHashMap<int, JobResult>.Concurrent job_results;
    12.  
    13.         public void Execute(int index)
    14.         {
    15.             var temp_array = new NativeList<MoreValues>(Allocator.Temp);
    16.  
    17.             var job_value = job_values[index];
    18.  
    19.             for (int i = job_value.more_values_array_start_index; i < job_value.more_values_array_start_index + job_value.more_values_array_length; i++)
    20.             {
    21.                 var result = more_values[i].other_value;
    22.                 var other_result = more_values[i].value;
    23.                 job_results.Add(index, new JobResult(result, other_result));
    24.             }
    25.  
    26.             temp_array.Dispose();
    27.         }
    28.     }
     
  10. PublicEnumE

    PublicEnumE

    Joined:
    Feb 3, 2019
    Posts:
    203
    I believe NativeList is one native container type which still has this restriction. Using a NativeArray should work, though.
     
  11. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    1,815
    You on old version of unity and packages. It’s not supported, use 2019.2 and above, better is 2019.3 and latest DOTS packages.
     
  12. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    1,815
    NativeList works fine, NativeQueue allocation inside job not supported.
     
    PublicEnumE likes this.
  13. whiskers434

    whiskers434

    Joined:
    Sep 23, 2014
    Posts:
    22
    will wait out the 2019 LTS version
     
unityunity