Search Unity

Enabling and disabling burst is changing the output and deterministibility of my jobs

Discussion in 'Burst' started by Abbrew, Jan 25, 2019.

  1. Abbrew

    Abbrew

    Joined:
    Jan 1, 2018
    Posts:
    417
    Has anyone ever encountered a situation where they not only got incorrect output from jobs, but the results seem to change between executions? I'm writing unit tests and some of them are passing and failing on a whim. Disabling burst compilation causes them all to pass (They still fail if the code I'm testing is incorrect, but enabling burst causes the same correct code to produce the wrong output). Enabling it causes this erratic behavior
     
    Last edited: Jan 25, 2019
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    You need to post some code. There has been at least one instance in the past where there was an issue with burst which produced the wrong results. They can't really fix anything unless they know what's wrong.
     
  3. Abbrew

    Abbrew

    Joined:
    Jan 1, 2018
    Posts:
    417
    Code (CSharp):
    1.  
    2.     [BurstCompile]
    3.     private struct CheckRequestCompletionJob : IJobNativeMultiHashMapVisitKeyValue<ResultOrder, Request>
    4.     {
    5.         [ReadOnly]
    6.         public ResultCache<Request,Result>.ReadOnly results;
    7.         [WriteOnly]
    8.         public NativeQueue<ResultOrder>.Concurrent fulfilledRequests;
    9.         [WriteOnly]
    10.         public NativeHashMap<ResultLine<Request, Result>, int>.Concurrent existingResults;
    11.  
    12.         public void ExecuteNext(ResultOrder order, Request request)
    13.         {
    14.             if(results.TryGetFirstValue(request,out var result,out var iterator))
    15.             {
    16.                 if (existingResults.TryAdd(new ResultLine<Request, Result>(
    17.                     request,
    18.                     result,
    19.                     order),
    20.                     0))
    21.                 {
    22.                     fulfilledRequests.Enqueue(order);
    23.                 }
    24.                 while (results.TryGetNextValue(out result,ref iterator))
    25.                 {
    26.  
    27.                     if (existingResults.TryAdd(new ResultLine<Request, Result>(
    28.                     request,
    29.                     result,
    30.                     order),
    31.                     0))
    32.                     {
    33.                         fulfilledRequests.Enqueue(order);
    34.                     }
    35.                 }
    36.             }
    37.         }
    38.     }
    This is the job I'm suspecting is causing the issue.
    Code (CSharp):
    1. fulfilledRequests.Enqueue(order);
    is being executed an inconsistent number of times. Some other strange behavior: Restarting Unity, I can run all the tests multiple times without a single failing test, with burst enabled. On the 5th or so run, the same tests fail. The recently released IJobNativeMultiHashMapVisitKeyValue might be the culprit
     
  4. Abbrew

    Abbrew

    Joined:
    Jan 1, 2018
    Posts:
    417
    Yep, the issue is with IJobNativeMultiHashMapVisitKeyValue being burst compiled. I ran the tests multiple times with the [BurstCompile] tag commented out, and multiple times with it included. Every time it failed when burst compiled, and every time it passed when not. I think IJobNativeMultiHashMapVisitKeyValue has a race condition bug when burst compiled
     
  5. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    You don't have multiple copies of the same Request added to the native hash map you're iterating do you?