Search Unity

[Feedback] safety checks errors missing schedule location

Discussion in 'Entity Component System' started by Baste, Jun 22, 2018.

  1. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,336
    If you access a NativeArray that's being written to by a non-completed job, you get an error. That's very helpful! But, if you're scheduling the same job struct several times with different data, the information about which of those scheduled jobs you forgot to complete.

    Here's a very contrived example:
    Code (csharp):
    1. public class TestScript : MonoBehaviour {
    2.  
    3.     struct TransferJob : IJobParallelFor {
    4.         public NativeArray<int> input;
    5.         public NativeArray<int> output;
    6.  
    7.         public void Execute(int index) {
    8.             output[index] = input[index];
    9.         }
    10.     }
    11.  
    12.     void Update() {
    13.         var input1  = new NativeArray<int>(100, Allocator.Temp);
    14.         var input2  = new NativeArray<int>(100, Allocator.Temp);
    15.         var output1 = new NativeArray<int>(100, Allocator.Temp);
    16.         var output2 = new NativeArray<int>(100, Allocator.Temp);
    17.  
    18.         FillWithRandomValues(input1);
    19.         FillWithRandomValues(input2);
    20.  
    21.         var job1 = new TransferJob {
    22.             input = input1,
    23.             output = output1
    24.         }.Schedule(100, 64);
    25.  
    26.         var job2 = new TransferJob {
    27.             input = input2,
    28.             output = output2
    29.         }.Schedule(100, 64);
    30.  
    31.         job1.Complete();
    32.         //WHOOPS FORGOT TO COMPLETE JOB 2
    33.  
    34.         int sum = 0;
    35.         foreach (var value in output1) {
    36.             sum += value;
    37.         }
    38.         foreach (var value in output2) {
    39.             sum += value;
    40.         }
    41.  
    42.         input1.Dispose();
    43.         input2.Dispose();
    44.         output1.Dispose();
    45.         output2.Dispose();
    46.     }
    47.  
    48.     private void FillWithRandomValues(NativeArray<int> array) {
    49.         for (int i = 0; i < array.Length; i++) {
    50.             array[i] = Random.Range(-1000, 1000);
    51.         }
    52.     }
    53. }

    The error given is:

    It would be very nice if the error message included where that job was scheduled, as there could be several places where the same job type is scheduled (with different kinds of data). In this example, it's pretty easy to find, but if it's a file where that job is scheduled in several places, and the dependency chains between jobs are somewhat long, it gets somewhat hard to figure out what you've done wrong.

    Something like:
    "InvalidOperationException: The previously scheduled job TestScript:TransferJob (at line 30 in TestScript) writes to etc."
    Could help a bunch with debugging.
     
    SugoiDev likes this.