Search Unity

Feedback Request: Parallel Writes to the same DynamicBuffer

Discussion in 'Entity Component System' started by PublicEnumE, Aug 5, 2020.

  1. PublicEnumE

    PublicEnumE

    Joined:
    Feb 3, 2019
    Posts:
    729
    An ECS limitation I’ve run into:

    I’ve optimized my DOTS code so that multiple jobs can write to the same DynamicBuffer in parallel, just with an index offset. There is no chance of collisions. This has been one of our most impactful optimizations, and has also helped simplify our code.

    The one big drawback: if you call GetBufferFromEntity() for the same buffer type, from inside two different systems, it will automatically make their jobs dependent on one another.

    There’s no way to inform Unity that - no it’s safe - these jobs will definitely write to different indices of these buffers.

    The clumsy workaround has been to have a group of systems run before any of my job scheduling systems, which simply call BufferFromEntity() for a given type, and cache the result. Later systems will refer to that cached data instead of calling BufferFromEntity() themselves, and pass it into their jobs. This tricks Unity’s auto-dependency measures.

    This works, however - I’m willing to bet that the cost of updating these ‘caching’ systems may be greater than the cost of calling GetBufferFromEntity in each of my job systems. It’s also a clunky way to organize the code.

    It would be great if there were a way to inform Unity of this case, and to expect safe parallel writes to a BufferFromEntity from multiple jobs.
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    The right solution is to merge the two systems into one. The only way you can gurantee correctness here is if you can gurantee that no other systems runs inbetween. Which you can't do if they are seperate systems. Moving it into one system gives you full control.
     
  3. PublicEnumE

    PublicEnumE

    Joined:
    Feb 3, 2019
    Posts:
    729
    It’s not 2 systems. I have dozens of systems which spawn jobs which write to these buffers. Because of how the code is structured, new systems are frequently added which might also write to these buffers.

    That might sound strange out of context, but we have good reasons for doing it. It’s worked very well - perf has been very good (thank you DOTS team).

    I’m not sure why this would be true. I keep all of my systems organized in ComponentSystemGroups, and the order of those groups is tightly controlled. I can guarantee that no other systems run inbetween, because of the strict grouping.
     
    Last edited: Aug 6, 2020
  4. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    You can maybe prove that to yourself. But entities can't prove that.