Search Unity

Allocating temporary native containers in a Burst compiled job?

Discussion in 'Burst' started by koplapasi, May 24, 2018.

  1. koplapasi

    koplapasi

    Joined:
    Jun 30, 2016
    Posts:
    11
    Trying to allocate a temporary (
    Allocator.Temp
    )
    NativeArray
    in a job is preventing Burst compilation from happening. Based on the error log from the Burst Inspector, some code in the
    DisposeSentinel
    is the culprit.

    Will we be able to do such allocations at some point to some extent (even if it's only NativeArrays), or do we always have to allocate all containers outside of a Burst job?
     
    Mr-Mechanical likes this.
  2. koplapasi

    koplapasi

    Joined:
    Jun 30, 2016
    Posts:
    11
    It looks like if I'd be able to undefine
    ENABLE_UNITY_COLLECTIONS_CHECKS
    in the editor somehow temporarily, I would be able to get rid of the
    DisposeSentinel
    blocking Burst from compiling the jobs with NativeArray allocations in them. I've tried setting Jobs -> Leak Detection (Native Containers) off, but it always seems to sneak itself back on. :)
     
  3. timjohansson

    timjohansson

    Unity Technologies

    Joined:
    Jul 13, 2016
    Posts:
    473
    It is not possible right now, but we are investigating a fix. Essentially we are considering to change Allocator.Temp to be auto disposed when returning from managed back to native. By doing that we can guarantee that Temp containers never leak and there is no need to have a DiposeSentinel on them.
     
  4. Sylmerria

    Sylmerria

    Joined:
    Jul 2, 2012
    Posts:
    369
    Mr-Mechanical likes this.
  5. Mr-Mechanical

    Mr-Mechanical

    Joined:
    May 31, 2015
    Posts:
    507
    I agree that this is something of interest
     
  6. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    This is supported in 19.1
     
    starikcetin, Cynicat, FROS7 and 3 others like this.
  7. Abbrew

    Abbrew

    Joined:
    Jan 1, 2018
    Posts:
    417
    Will Allocator.TempJob inside a Burst-compiled Job also be supported in 19.1? If so, how significant is the performance penalty compared to Allocator.Temp?
     
  8. GilCat

    GilCat

    Joined:
    Sep 21, 2013
    Posts:
    676
    Only Allocator.Temp is supported
     
    Abbrew likes this.
  9. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    987
    How does it work? Can we allocate native containers now inside a job struct?
     
  10. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Yes you simply use Allocator.Temp on the container.
     
  11. iamarugin

    iamarugin

    Joined:
    Dec 17, 2014
    Posts:
    883
    Still fails on Dispose() of created array with Allocator.Temp inside the job (Unity 2019.1)

    Code (CSharp):
    1. C:\buildslave\unity\build\Runtime\Export\NativeArray\DisposeSentinel.cs(139,13): error: The IL opcode instruction `IL_0002: ldind.ref args(IL_0001)` is not supported by burst
    2. at Unity.Collections.LowLevel.Unsafe.DisposeSentinel.Clear(ref Unity.Collections.LowLevel.Unsafe.DisposeSentinel sentinel) (at C:\buildslave\unity\build\Runtime\Export\NativeArray\DisposeSentinel.cs:139)
    3. at Unity.Collections.LowLevel.Unsafe.DisposeSentinel.Dispose(ref Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle safety, ref Unity.Collections.LowLevel.Unsafe.DisposeSentinel sentinel) (at C:\buildslave\unity\build\Runtime\Export\NativeArray\DisposeSentinel.cs:75)
    4. at Unity.Collections.NativeArray`1<Jobs.QuadAddData>.Dispose(Unity.Collections.NativeArray`1<Jobs.QuadAddData>* this) (at C:\buildslave\unity\build\Runtime\Export\NativeArray\NativeArray.cs:160)
    5. at Jobs.SortQuadsJob.Sort(Jobs.SortQuadsJob* this, Unity.Collections.NativeList`1<Jobs.QuadAddData>* list, Jobs.ReverseComparerQuad comparer) (at G:\Unity\rocket_science\Assets\Scripts\Jobs\Planet\SortQuadsJob.cs:52)
    6. at Jobs.SortQuadsJob.Execute(Jobs.SortQuadsJob* this) (at G:\Unity\rocket_science\Assets\Scripts\Jobs\Planet\SortQuadsJob.cs:21)
    7. at Unity.Jobs.IJobExtensions.JobStruct`1<Jobs.SortQuadsJob>.Execute(ref Jobs.SortQuadsJob data, System.IntPtr additionalPtr, System.IntPtr bufferRangePatchData, ref Unity.Jobs.LowLevel.Unsafe.JobRanges ranges, int jobIndex) (at C:\buildslave\unity\build\Runtime\Jobs\Managed\IJob.cs:30)
    Or am I missing something?
     
  12. GilCat

    GilCat

    Joined:
    Sep 21, 2013
    Posts:
    676
    You don't need to call Dispose() on temp allocs it will dispose implicitly when out of scope.
     
  13. whiskers434

    whiskers434

    Joined:
    Sep 23, 2014
    Posts:
    28
    was this fix ported back to 2018 LTS? or its it only in 19.x ?