Search Unity

Job not running in parallell more than once

Discussion in 'Entity Component System' started by martinjonsson01, Jun 2, 2020.

  1. martinjonsson01

    martinjonsson01

    Joined:
    Jan 2, 2016
    Posts:
    7
    I'm working on my own recreation of the boids demo by Unity using DOTS, and I have run into a problem. The IJobNativeMultiHashMapMergedSharedKeyIndices has been deprecated and Unity now recommends users to copy the deprecated job-code into our own if we still want to use it, so I did that. (the code was posted in this thread)

    I believe I might be using the job wrong, though. The job does not seem to run in parallel more than once, and then it does all of the rest of the work on a single worker thread, instead of spreading it out over all of them (even though they are idle). This is what my inspector looks like:


    The code where I schedule the job inside of BoidComputeNeighboursSystem (more specifically on line 13):
    Code (CSharp):
    1. /* Calculate bin-shared values. */
    2. BinIndices = new NativeArray<int>(boidCount, Allocator.TempJob);
    3. NativeArray<int> binIndices = BinIndices;
    4. NativeMultiHashMap<int, int> binnedBoids = BinnedBoids;
    5. var calculateBinValuesJob = new CalculateBinValues
    6. {
    7.     binIndices = binIndices,
    8.     binBoidCounts = binBoidCounts,
    9.     binPositionSums = binPositionSums,
    10.     binVelocitySums = binVelocitySums
    11. };
    12. // TODO: Find out why this does not run in parallel more than once per thread.
    13. JobHandle calculateBinSharedValuesHandle = calculateBinValuesJob.Schedule(binnedBoids, 64, Dependency);
    14. // Combine CalculateBinValues dependency with other dependencies.
    15. Dependency = JobHandle.CombineDependencies(calculateBinSharedValuesHandle, Dependency);
    The job itself looks like this:
    Code (CSharp):
    1. [BurstCompile]
    2. private struct CalculateBinValues : IJobNativeMultiHashMapMergedSharedKeyIndices
    3. {
    4.     public NativeArray<int> binIndices;
    5.     public NativeArray<float3> binVelocitySums;
    6.     public NativeArray<float3> binPositionSums;
    7.     public NativeArray<int> binBoidCounts;
    8.     // No need to compute anything more, since the initial array values
    9.     // have already taken this first boid into account.
    10.     public void ExecuteFirst(int entityInQueryIndex)
    11.     {
    12.         binIndices[entityInQueryIndex] = entityInQueryIndex;
    13.     }
    14.     // Sums the velocities and positions of the actual index being considered and stores
    15.     // the index of this first value where we're storing the bins.
    16.     public void ExecuteNext(int binIndex, int entityInQueryIndex)
    17.     {
    18.         binBoidCounts[binIndex] += 1;
    19.         binVelocitySums[binIndex] = binVelocitySums[binIndex] + binVelocitySums[entityInQueryIndex];
    20.         binPositionSums[binIndex] = binPositionSums[binIndex] + binPositionSums[entityInQueryIndex];
    21.         binIndices[entityInQueryIndex] = binIndex;
    22.     }
    23. }
    I fail to see what's wrong with the code, so why does the job (mainly) run on one thread?