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. Dismiss Notice

Question Is there any Limit to use EntityManager in Bursted Job.

Discussion in 'Burst' started by Lieene-Guo, Oct 6, 2020.

  1. Lieene-Guo

    Lieene-Guo

    Joined:
    Aug 20, 2013
    Posts:
    547
    As EntityManager is strcut now, I am wondering is there some limitations when using it in Bursted job.
    For example, Is it okay to check Entity's existence in job with EntityManager.Exist?
     
  2. brunocoimbra

    brunocoimbra

    Joined:
    Sep 2, 2015
    Posts:
    677
    I think that they were changed to be struct to be compatible with the bursted systems that seems to be coming, EntityManager is still main-thread-only AFAIK
     
  3. Lieene-Guo

    Lieene-Guo

    Joined:
    Aug 20, 2013
    Posts:
    547
    I tried EntityManager.Exist in job. It works fine. I just don't know if it is expected to be used so.
    Structural change commands should only be used in main-thread. that's fine.
    For now EntityManager.Exist seems to be the only way to test Entity existence.
    ComponentDataFromEntity can be used to check entity existence.
    But in my EventSystem, I don't know what component the target entity should have at all. I just need a way to check it without referring to any component type.
     
    Last edited: Oct 6, 2020
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,580
    Did you try with any other Job than IJob? Does Burst compiles and can you actually check for entities existence?

    I am not convinced, this is right tool to to check for entities. But maybe it will become right tool?
     
  5. Lieene-Guo

    Lieene-Guo

    Joined:
    Aug 20, 2013
    Posts:
    547
    Yes, I tried IJobParallelForDefer. And it works. But EntityComponentStore's safety check is a bit of a mass.
    I have not figured out exactly if EntityManager itself has the Safety handle that is needed to check entity existence.
    But EntityManager.Exist can be used in mainthread when IjobChunks are on the fly without any problem.
    So I guess. it is fine to use it as long as your job is not working across a syncpoint.

    pass the jobhandle of the job use EntityManager to the next ECBS should be good enough. assuming all structural change is done in ECB.
     
  6. Lieene-Guo

    Lieene-Guo

    Joined:
    Aug 20, 2013
    Posts:
    547
    FYI, EntityManager from SystemBase needs to be cache to a local copy to be used in Entities.ForEach. as all System field is not allowed to be used in Entities.ForEach.
     
    Antypodish likes this.
  7. Zec_

    Zec_

    Joined:
    Feb 9, 2017
    Posts:
    148
    I also played around with this a few weeks ago to see what's possible and what's not, since the backend in the EntityManager is largely burst-compatible. Just like Lieene-Guo has experienced, I realized that the Job system allows for bursted EntityManager access as long as you don't access SystemBase.EntityManager in the job. You basically need to either store it in a local parameter or pass EntityManager to a job struct. Here's an example:
    BurstedEntityManager.png
    The code in the Job is fully burstable. The main restriction with accessing the EntityManager this way is that the Job systems prevents you from doing any structural changes while in a Job. That's simply not allowed, even if the job is executed on the main thread via Run.

    I therefore started playing around with the thought of whether it was possible to compile the EntityManager access as Bursted code via FunctionPointers to bypass this Job system structural change restriction. This is what I came up with, and it actually seems to work. The job is burstable and performs structural changes. Not sure how safe or actually viable it is, I just found it amusing to play around with :)
    BurstedEntityManagerWithStructuralChanges.png
     
    Last edited: Oct 6, 2020
  8. Lieene-Guo

    Lieene-Guo

    Joined:
    Aug 20, 2013
    Posts:
    547
    If you mark Entities.ForEach by .WithStructualChange(), using EntityManager structural change commands and burst will work. but still, need to call Run() instead of Schedule().
    maybe Schedule() + Complete() is okay.
    But I don't think SchechuleParalle() +Complete() works.
    ECB will do the job anyway.
    Still, I want to know in what condition can I use EntityManager.Exist in parallel bursted job.
     
  9. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,627
    Unless something has changed recently, WithStructualChange automatically turns off burst
     
    unity-freestyle likes this.
  10. burningmime

    burningmime

    Joined:
    Jan 25, 2014
    Posts:
    845
    That sounds incredibly unsafe. Like, base jumping down a nuclear reactor while high on PCP levels of unsafe. But if you always run that job on the main thread and not worker threads, it'll probably live.
     
    unity-freestyle likes this.
  11. unity-freestyle

    unity-freestyle

    Joined:
    Aug 26, 2015
    Posts:
    45
    What a ride hahahahahahah xD
     
  12. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    you can use Begin/End ExclusiveEntityTransaction to get access to an entity manager that can only be used from a job.
    It is definitely not safe to use it in paralell to usage on the main thread or schedulign other normal system jobs, there are safety checks against any such abuse.
     
  13. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Given that full safety checks exist and are default on to tell you exactly when something is or isn't safe, i am not sure this parallel is true... I am also not exactly sure what a better parallel would be? :)
     
    DreamingImLatios likes this.