Search Unity

Question IJonsParallelFor Works On Single Core

Discussion in 'Entity Component System' started by tahsinXYZ, Nov 25, 2021.

  1. tahsinXYZ

    tahsinXYZ

    Joined:
    Aug 14, 2019
    Posts:
    70
    I have encountered a problem: I have implemented a IJobParallelFor for FarmGrowingJob, the crops grown but growth process works on single core and other workers are idle. How can I solve this ?
    Here My Code:
    Code (CSharp):
    1.     private void ExecuteFarmGrow()
    2.     {
    3.         FarmableResourceNode.FarmGrowJob farmGrowJob = new FarmableResourceNode.FarmGrowJob();
    4.         farmGrowJob.deltaTime = Time.deltaTime;
    5.         farmGrowJob.datas = new NativeArray<FarmableResourceNode.Data>(farmableResourceNodes.Count, Allocator.TempJob);
    6.         for (int i = 0; i < farmableResourceNodes.Count; i++)
    7.         {
    8.             farmGrowJob.datas[i] = farmableResourceNodes[i].data;
    9.         }
    10.         farmGrowJob.results = new NativeArray<FarmableResourceNode.Data>(farmableResourceNodes.Count, Allocator.TempJob);
    11.         JobHandle jobHandle = farmGrowJob.Schedule(farmableResourceNodes.Count, 100);
    12.         jobHandle.Complete();
    13.         farmGrowJob.datas.Dispose();
    14.         for (int i = 0; i < farmableResourceNodes.Count; i++)
    15.         {
    16.             farmableResourceNodes[i].data = farmGrowJob.results[i];
    17.             farmableResourceNodes[i].ChangeLODMeshByStage();
    18.         }
    19.         farmGrowJob.results.Dispose();
    20.     }
    Code (CSharp):
    1.     [BurstCompile]
    2.     public struct FarmGrowJob : IJobParallelFor
    3.     {
    4.         public float deltaTime;
    5.         public NativeArray<Data> datas;
    6.         public NativeArray<Data> results;
    7.         public void Execute(int index)
    8.         {
    9.             Data temp = datas[index];
    10.             temp.oldRate = datas[index].currentGrowthRate;
    11.             if(temp.currentGrowthRate < 100) temp.currentGrowthRate = temp.oldRate + temp.growthSpeed * deltaTime * temp.chanceFactor;
    12.             results[index] = temp;
    13.         }
    14.     }
     
  2. Tony_Max

    Tony_Max

    Joined:
    Feb 7, 2017
    Posts:
    353
    I think in line
    JobHandle jobHandle = farmGrowJob.Schedule(farmableResourceNodes.Count, 100);
    the second parameter is batch count and you pass 100, so only greater then 100 growing entities would be computed with more then one core AFAIK.
     
  3. tahsinXYZ

    tahsinXYZ

    Joined:
    Aug 14, 2019
    Posts:
    70
    So I need decrease the number ? BTW Thanks for reply.
     
  4. Tony_Max

    Tony_Max

    Joined:
    Feb 7, 2017
    Posts:
    353
    You can try decrease number and see how it behaves in profiler. This parameter is to control calculation batching, becasue when you want for example do tons of cheap calculations it is more optimal to not schedule it so much, because scheduling is overhead, instead you want batch it. I often see 32 and 64 numbers for common calculations like fill some array or sort something.