Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Request for IJobChunk.ScheduleSingle

Discussion in 'Entity Component System' started by sient, Jan 27, 2019.

  1. sient

    sient

    Joined:
    Aug 9, 2013
    Posts:
    602
    Similar to IJobProcessComponentData.ScheduleSingle, can we also get a schedule function which runs single-threaded for IJobChunk?
     
  2. NoDumbQuestion

    NoDumbQuestion

    Joined:
    Nov 10, 2017
    Posts:
    186
    IJobChunk is run single threaded. If you pass down the job handle, you can tell other IJobChunk to wait for the previous one.
    Code (CSharp):
    1. for(i = 0; i > length; i++){
    2.      var job = new IJobChunk(); // Create Job
    3.      inputdeps = job.schedule(this, inputdeps); // Tell this job to wait for previous job in previous loop
    4. }
    I'm not tested yet but I think relatively job handle should work like that.
     
  3. sient

    sient

    Joined:
    Aug 9, 2013
    Posts:
    602
    IJobChunk runs as a parallel-for job. If you look into the source (IJobChunk.cs) you'll see this line

    Code (csharp):
    1. return JobsUtility.ScheduleParallelFor(ref scheduleParams, totalChunks, 1);
    A ScheduleSingle extension method would call JobsUtility.Schedule instead of ScheduleParallelFor.
     
  4. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,635
    Yeah that's not true. Chunks are spread across threads.
     
  5. NoDumbQuestion

    NoDumbQuestion

    Joined:
    Nov 10, 2017
    Posts:
    186
    Well my bad. That is badly written and I do not have that deep understand of ECS.
     
    Deleted User likes this.
  6. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,635
    Sorry didn't mean to come off so blunt.

    From memory, every job except IJob is parallel by default.
     
    NoDumbQuestion likes this.
  7. sient

    sient

    Joined:
    Aug 9, 2013
    Posts:
    602
    bump
     
  8. sient

    sient

    Joined:
    Aug 9, 2013
    Posts:
    602
    Since it might be a while for us to get this, I wrote a quick'n'dirty work-around

    Code (csharp):
    1. public static JobHandle ScheduleSingle<T>(this T job, EntityQuery query, JobHandle dependsOn)
    2.     where T : struct, IJobChunk {
    3.   var chunks = query.CreateArchetypeChunkArray(Allocator.TempJob, out JobHandle chunksHandle);
    4.   dependsOn = JobHandle.CombineDependencies(dependsOn, chunksHandle);
    5.   return new JobChunkSingleRunner<T> {
    6.     RealJob = job,
    7.     Chunks = chunks,
    8.   }.Schedule(dependsOn);
    9. }
    10.  
    11. [BurstCompile]
    12. struct JobChunkSingleRunner<T> : IJob where T : IJobChunk {
    13.   public T RealJob;
    14.   [DeallocateOnJobCompletion]
    15.   public NativeArray<ArchetypeChunk> Chunks;
    16.  
    17.   public void Execute() {
    18.     int firstEntityIndex = 0;
    19.     for (int i = 0; i < Chunks.Length; ++i) {
    20.       RealJob.Execute(Chunks[i], i, firstEntityIndex);
    21.       firstEntityIndex += Chunks[i].Count;
    22.     }
    23.   }
    24. }
     
    BrendonSmuts likes this.
  9. tarahugger

    tarahugger

    Joined:
    Jul 18, 2014
    Posts:
    129
    please add this.