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

Difference between Schedule() and ScheduleParallel()?

Discussion in 'Entity Component System' started by Mr-Mechanical, Mar 30, 2020.

  1. Mr-Mechanical

    Mr-Mechanical

    Joined:
    May 31, 2015
    Posts:
    507
    I know to understand this thoroughly as I optimize my code. What is the difference in how the workload is distributed amongst CPU cores?
     
  2. Sarkahn

    Sarkahn

    Joined:
    Jan 9, 2013
    Posts:
    440
    In SystemBase Entities.ForEach.Schedule will force the job to run on a single thread. ScheduleParallel will run the job on parallel threads, with each thread processing one or more chunks from the query. That's my understanding at least.
     
  3. BitPax

    BitPax

    Joined:
    Oct 4, 2019
    Posts:
    89
    What's the default when you don't designate either?

    EDIT: Apparently in the more recent versions you have to designate Run(), Schedule(), or ScheduleParallel().
     
    Last edited: Dec 11, 2020
  4. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,695
    My understanding is run forces it to run in the main thread .. which is different from schedule since schedule is single job thread .. someone please correct me if I’m wrong.
     
  5. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,993
    Before codegen, and still currently in GameObjectConversionSystem, not designating anything (actually you can't designate anything in these contexts) is equivalent to .WithStructuralChanges().Run().
     
  6. AndrewSol

    AndrewSol

    Joined:
    Oct 10, 2012
    Posts:
    5
    i saw that both worked same.
    ScheduleParallel & Schedule.
    only "run()" worked at mainthread

    Debug.Log($"threadID {Thread.CurrentThread.ManagedThreadId}");
     
  7. WAYNGames

    WAYNGames

    Joined:
    Mar 16, 2019
    Posts:
    944
    Run = single thread on main thread
    Schedule = single thread on worker thread (not on main thread)
    ScheduleParallel = multi threaded on worker threads

    Note that synch points (job handle.complete(), other job dependancy and entity command buffers) can force the execution on main thread to finish the work.
     
    Hecocos, DevViktoria and Lapsapnow like this.
  8. DreamersINC

    DreamersINC

    Joined:
    Mar 4, 2015
    Posts:
    130
    This doesn't sound 100% or is subject to change 0.50 update as there is schedule, ScheduleSingle and Schedule ScheduleParallel. According to documentation, schedule and schedule parallel can be used interchangeably.

    https://docs.unity3d.com/Packages/com.unity.entities@0.17/api/Unity.Entities.JobChunkExtensions.html
     
  9. Lapsapnow

    Lapsapnow

    Joined:
    Jul 4, 2019
    Posts:
    51
    He is 100% correct. I use them.

    Run = single thread on main thread
    Schedule = single thread on worker thread
    ScheduleParallel = multi threaded on worker threads


    ScheduleSingle is the same as Schedule, it's legacy stuff.
     
  10. DreamersINC

    DreamersINC

    Joined:
    Mar 4, 2015
    Posts:
    130
    It's in contrast withe documentation linked above.
    Schedule<T>(T, EntityQuery, JobHandle)
    Adds an IJobChunk instance to the Job scheduler queue for parallel execution. Note: This method is being replaced with use of ScheduleParallel to make non-sequential execution explicit.
     
  11. WAYNGames

    WAYNGames

    Joined:
    Mar 16, 2019
    Posts:
    944
    Not the same subject. You are right for IJobChunk but above talks were about entities.foreach which state in the documentation
     
    lclemens likes this.