Search Unity

[Job System] output scalar value from job

Discussion in 'Data Oriented Technology Stack' started by M_R, Jan 11, 2018.

  1. M_R

    M_R

    Joined:
    Apr 15, 2015
    Posts:
    450
    I started playing around with the new job system.

    I tried to create a 'sum' job like this:
    Code (CSharp):
    1. struct SerialSumJob : IJob
    2.     {
    3.         [ReadOnly]
    4.         public NativeArray<float> data;
    5.         public float output;
    6.  
    7.         public void Execute ()
    8.         {
    9.             var len = data.Length;
    10.             output = 0;
    11.             for (int i = 0; i < len; i++) {
    12.                 output += data[i];
    13.             }
    14.         }
    15.     }
    and running it with
    Code (CSharp):
    1. var job = new SerialSumJob { data = data };
    2.             job.Schedule ().Complete();
    3.             Debug.LogFormat ("job complete: {0}", job.output);
    (data was initialized with Random values)

    after job was complete, output was still 0.

    it makes sense, as structs get copied, but is there a recommended way to enable it? (i can workaround with a single-element NativeArray)
     
  2. adriant

    adriant

    Unity Technologies

    Joined:
    Nov 1, 2016
    Posts:
    31
    You have to use the single-element NativeArray, there is no other way.
     
    GameDevCouple_I likes this.
  3. M_R

    M_R

    Joined:
    Apr 15, 2015
    Posts:
    450
    ok. slightly feels like a hack though.

    is there something planned for it? like
    Code (CSharp):
    1. public struct SumJob : IJob<float> {
    2.     [ReadOnly] public NativeArray<float> values;
    3.  
    4.     public float Execute() {return 42f;}
    5. }
    6. ...
    7. var result = new SumJob{values = ...}.Schedule().Complete();
     
  4. GameDevCouple_I

    GameDevCouple_I

    Joined:
    Oct 5, 2013
    Posts:
    1,748
    Could you post a code example of what you mean? Still trying to get my head around the nativearray use and job use for basics such as creating a job that will increment a value...
     
  5. GameDevCouple_I

    GameDevCouple_I

    Joined:
    Oct 5, 2013
    Posts:
    1,748
    Nevermind, worked it out :) lightning fast speeds, parallel operations are a great thing!
     
    hippocoder and richardkettlewell like this.
  6. adriant

    adriant

    Unity Technologies

    Joined:
    Nov 1, 2016
    Posts:
    31
    No, nothing is planned. Single-element arrays have been considered to be the best approach for the moment.
     
  7. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    2,312
    Would be nice to know what the threading model is here. Like can we mix what would be normal in a concurrent environment here with jobs.

    For example if output was declared outside of IJob, and not touched until the job was complete, that would be completely normal in any low latency concurrent app. If you don't actually need memory barriers, you don't create them just for the fun of it.

    Or if we can use api's like Interlocked in jobs.

    The threading model would basically answer all questions of that type.
     
  8. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    25,346
    Actually would be nice to have some serious docs and blog time on this subject, it's basically a ground-breaking feature in my view.

    Think for a moment. Threading this easy, without race conditions etc ? wow!
     
    SNS_Case likes this.
  9. Peter77

    Peter77

    Joined:
    Jun 12, 2013
    Posts:
    3,899
    yep, totally agree.

    I guess they're going to share details, documentation, etc... when they officially announce the release of the new tech. So far, even though it's included in Unity, the official statement is "Coming soon: the C# Job System".
     
  10. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    2,012
    I'm surprised the job system even allowed a public float non native array without a [ReadOnly] in front.
     
  11. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    4,635
    So the job is an actual struct. So what we do internally, is we take all data on the struct copy it out and give it to the job. So its perfectly fine to write back to the float on the job (Because the changed values are only visible on that thread and then discarded), this has no negative impact on performance or determinism.

    Arguably its a bit of a strange thing to do and maybe usually a mistakes. I guess we could argue that a static analysis tool should tell the user that writing to the float is likely not making any sense and at least output a warning.
     
  12. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    2,012
    it can make sense to write to a float, to count the area of occupation of one force in an RTS or the pollution level of the land.

    i dont understand how N thread writing to the same value is deterministic... i thought that if thread 1 and 2 do value++ then at the end of one round there is a chance that you get only value++ instead of the expected value=value+2
     
  13. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    4,635
    Each thread will receive its own copy of the original struct in the Execute method. So it is deterministic.

    In any case, I think we can agree that writing to value types on the jobs is undesirable and static analysis too should prevent users from doing it.
     
    GameDevCouple_I likes this.
  14. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    2,012
    Does that means a Job is able to calculate area of occupation or pollution level of a grid but ForJob will return an erroneous result?
    If that's the case then value types should be allowed on jobs but not on ForJobs
     
  15. GameDevCouple_I

    GameDevCouple_I

    Joined:
    Oct 5, 2013
    Posts:
    1,748
    I take it back, some examples would be great!