Search Unity

Parallel Jobs are nice and spikey

Discussion in 'Entity Component System' started by laurentlavigne, Jan 18, 2018.

  1. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,363
    this is a build





    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Unity.Jobs;
    5. using Unity.Collections;
    6.  
    7. public class JobSimple : MonoBehaviour {
    8.  
    9.     struct FillTheArray : IJobParallelFor
    10.     {
    11.         public NativeArray<float> output;
    12.  
    13.         public void Execute(int i)
    14.         {
    15.             output[i] = Mathf.Log10( i);
    16.         }
    17.     }
    18.  
    19.     struct CalculateThings : IJobParallelFor
    20.     {
    21.         [ReadOnly]
    22.         public NativeArray<float> input;
    23.  
    24.         public NativeArray<float> output;
    25.  
    26.         public void Execute(int i)
    27.         {
    28.             output[i] = Mathf.Sin( input[i]);
    29.         }
    30.     }
    31.  
    32.     void OnEnable()
    33.     {
    34.         StartCoroutine(JobCompute());
    35.     }
    36.  
    37.     public int computeSize=1000000, batchSize = 100;
    38.     IEnumerator JobCompute()
    39.     {
    40.         while (true)
    41.         {
    42.             var input = new NativeArray<float>(computeSize, Allocator.Persistent);
    43.             var output = new NativeArray<float>(computeSize, Allocator.Persistent);
    44.  
    45.             var time = Time.time;
    46.  
    47.             var jobFiller = new FillTheArray()
    48.             {
    49.                 output = input
    50.             };
    51.             var handleFiller = jobFiller.Schedule(computeSize, batchSize);
    52.  
    53.             var job = new CalculateThings()
    54.             {
    55.                 input = input,
    56.                 output = output
    57.             };
    58.             var handleCalculate = job.Schedule(input.Length, batchSize, handleFiller);
    59.             yield return new WaitWhile(() => handleCalculate.IsCompleted);
    60.             Debug.Log(Time.time - time);
    61.             handleCalculate.Complete();
    62.             input.Dispose();
    63.             output.Dispose();
    64.         }
    65.     }
    66. }
    67.  
     
    dyox likes this.
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    1. you are allocating and filling 4mb of data on each of your native arrays
    2. you are waiting on the job immediately after scheduling because yield return new WaitWhile(() => handleCalculate.IsCompleted); is inverted
     
    laurentlavigne likes this.
  3. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,363
    Thanks!
    It's now smooth like the dunes of the Sahara desert.