Search Unity

[Bug?Help?] Burst and custom jobs

Discussion in 'Burst' started by tertle, Aug 12, 2019.

  1. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    -edit2-

    Solved it by adding a dummy TValue parameter to the interface but I don't know why this is required.

    Code (CSharp):
    1.     [JobProducerType(typeof(JobNativeMultiHashMapVisitUniqueKey.NativeMultiHashMapVisitUniqueKeyJobStruct<,,>))]
    2.     public interface IJobNativeMultiHashMapVisitUniqueKey<TKey, TValue>
    3.         where TKey : struct, IEquatable<TKey>
    4.         where TValue : struct
    5.     {
    6.         void ExecuteNext(TKey key);
    7.     }
    I feel like i'm either overlooking something or something is a little off.

    -original-

    I wrote a simple custom job,
    IJobNativeMultiHashMapVisitUniqueKey<T>

    Which is basically just a copy of
    IJobNativeMultiHashMapVisitKeyValue except only it only visits the first key

    Code (CSharp):
    1. // <copyright file="IJobNativeMultiHashMapVisitUniqueKey.cs" company="BovineLabs">
    2. //     Copyright (c) BovineLabs. All rights reserved.
    3. // </copyright>
    4.  
    5. namespace BovineLabs.Common.Collections
    6. {
    7.     using System;
    8.     using Unity.Collections;
    9.     using Unity.Collections.LowLevel.Unsafe;
    10.     using Unity.Jobs;
    11.     using Unity.Jobs.LowLevel.Unsafe;
    12.  
    13.     [JobProducerType(typeof(JobNativeMultiHashMapVisitUniqueKey.NativeMultiHashMapVisitUniqueKeyJobStruct<,,>))]
    14.     public interface IJobNativeMultiHashMapVisitUniqueKey<in TKey>
    15.         where TKey : struct, IEquatable<TKey>
    16.     {
    17.         void ExecuteNext(TKey key);
    18.     }
    19.  
    20.     public static class JobNativeMultiHashMapVisitUniqueKey
    21.     {
    22.         internal struct NativeMultiHashMapVisitUniqueKeyJobStruct<TJob, TKey, TValue>
    23.             where TJob : struct, IJobNativeMultiHashMapVisitUniqueKey<TKey>
    24.             where TKey : struct, IEquatable<TKey>
    25.             where TValue : struct
    26.         {
    27.             [ReadOnly]
    28.             public NativeMultiHashMapImposter<TKey, TValue> HashMap;
    29.  
    30.             public TJob JobData;
    31.  
    32.             // ReSharper disable once StaticMemberInGenericType
    33.             private static IntPtr JobReflectionData;
    34.  
    35.             public static IntPtr Initialize()
    36.             {
    37.                 if (JobReflectionData == IntPtr.Zero)
    38.                 {
    39.                     JobReflectionData = JobsUtility.CreateJobReflectionData(
    40.                         typeof(NativeMultiHashMapVisitUniqueKeyJobStruct<TJob, TKey, TValue>),
    41.                         typeof(TJob),
    42.                         JobType.ParallelFor,
    43.                         (ExecuteJobFunction)Execute);
    44.                 }
    45.  
    46.                 return JobReflectionData;
    47.             }
    48.  
    49.             public delegate void ExecuteJobFunction(
    50.                 ref NativeMultiHashMapVisitUniqueKeyJobStruct<TJob, TKey, TValue> fullData,
    51.                 IntPtr additionalPtr,
    52.                 IntPtr bufferRangePatchData,
    53.                 ref JobRanges ranges,
    54.                 int jobIndex);
    55.  
    56.             public static unsafe void Execute(
    57.                 ref NativeMultiHashMapVisitUniqueKeyJobStruct<TJob, TKey, TValue> fullData,
    58.                 IntPtr additionalPtr,
    59.                 IntPtr bufferRangePatchData,
    60.                 ref JobRanges ranges,
    61.                 int jobIndex)
    62.             {
    63.                 while (true)
    64.                 {
    65.                     if (!JobsUtility.GetWorkStealingRange(ref ranges, jobIndex, out var begin, out var end))
    66.                     {
    67.                         return;
    68.                     }
    69.  
    70.                     var buckets = (int*)fullData.HashMap.Buffer->Buckets;
    71.                     var keys = fullData.HashMap.Buffer->Keys;
    72.  
    73.                     for (int i = begin; i < end; i++)
    74.                     {
    75.                         int entryIndex = buckets[i];
    76.  
    77.                         if (entryIndex != -1)
    78.                         {
    79.                             var key = UnsafeUtility.ReadArrayElement<TKey>(keys, entryIndex);
    80.                             fullData.JobData.ExecuteNext(key);
    81.                         }
    82.                     }
    83.                 }
    84.             }
    85.         }
    86.  
    87.         public static unsafe JobHandle Schedule<TJob, TKey, TValue>(
    88.             this TJob jobData,
    89.             NativeMultiHashMap<TKey, TValue> hashMap,
    90.             int minIndicesPerJobCount,
    91.             JobHandle dependsOn = default)
    92.             where TJob : struct, IJobNativeMultiHashMapVisitUniqueKey<TKey>
    93.             where TKey : struct, IEquatable<TKey>, IComparable<TKey>
    94.             where TValue : struct
    95.         {
    96.             var imposter = (NativeMultiHashMapImposter<TKey, TValue>)hashMap;
    97.  
    98.             var fullData = new NativeMultiHashMapVisitUniqueKeyJobStruct<TJob, TKey, TValue>
    99.             {
    100.                 HashMap = imposter,
    101.                 JobData = jobData,
    102.             };
    103.  
    104.             var scheduleParams = new JobsUtility.JobScheduleParameters(
    105.                 UnsafeUtility.AddressOf(ref fullData),
    106.                 NativeMultiHashMapVisitUniqueKeyJobStruct<TJob, TKey, TValue>.Initialize(),
    107.                 dependsOn,
    108.                 ScheduleMode.Batched);
    109.  
    110.             return JobsUtility.ScheduleParallelFor(
    111.                 ref scheduleParams,
    112.                 imposter.Buffer->BucketCapacityMask + 1,
    113.                 minIndicesPerJobCount);
    114.         }
    115.     }
    116. }
    It works fine in editor; appears to burst compile fine but as soon as I try to make a proper build burst throws errors even on an empty job

    Code (CSharp):
    1.     [BurstCompile]
    2.     public struct TESTJOB : IJobNativeMultiHashMapVisitUniqueKey<int>
    3.     {
    4.         public void ExecuteNext(int key)
    5.         {
    6.         }
    7.     }
    with a very unhelpful error.

    Code (CSharp):
    1. BuildFailedException: Burst compiler (1.1.2) failed running
    2.  
    3. stdout:
    4. Unexpected exception while processing type `BovineLabs.Vision.Jobs.TESTJOB`
    5. stderr:
    6. System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
    7.    at Mono.Collections.Generic.Collection`1.get_Item(Int32 index)
    8.    at Burst.Compiler.IL.Syntax.GenericContext.Resolve(TypeReference typeReference)
    9.    at Burst.Compiler.IL.Syntax.GenericContext.Resolve(TypeReference typeReference)
    10.    at Burst.Compiler.IL.Syntax.GenericContext.Resolve(TypeReference typeReference)
    11.    at Burst.Bcl.BclApp.FullyResolve(TypeReference tyRef, GenericContext ctx)
    12.    at Burst.Bcl.BclApp.FullyResolve(MethodReference methodRef)
    13.    at Burst.Bcl.BclApp.HashMethod(MethodReference methodRef)
    14.    at Burst.Bcl.BclApp.FindExecuteMethods(List`1 rootAssemblyPaths)
    15.  
    Can anyone spot anything I'm doing wrong?

    -edit-

    got a slightly more detailed error opening burst inspector

    Code (CSharp):
    1. ArgumentException: The number of generic arguments provided doesn't equal the arity of the generic type definition.
    2. Parameter name: instantiation
    3. System.RuntimeType.MakeGenericType (System.Type[] instantiation) (at <ad04dee02e7e4a85a1299c7ee81c79f6>:0)
    4. Unity.Burst.Editor.BurstReflection.FindExecuteMethods (UnityEditor.Compilation.AssembliesType assemblyTypes) (at Library/PackageCache/com.unity.burst@1.1.2/Editor/BurstReflection.cs:185)
    5. UnityEngine.Debug:LogException(Exception)
    6. Unity.Burst.Editor.BurstReflection:FindExecuteMethods(AssembliesType) (at Library/PackageCache/com.unity.burst@1.1.2/Editor/BurstReflection.cs:203)
    7. Unity.Burst.Editor.BurstInspectorGUI:OnGUI() (at Library/PackageCache/com.unity.burst@1.1.2/Editor/BurstInspectorGUI.cs:187)
    8. UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr)
    9.  
     
    Last edited: Aug 12, 2019