Search Unity

Any way to use slices to enable simultaneous job writes to different areas of an array?

Discussion in 'Entity Component System' started by Peeling, Jun 15, 2019.

  1. Peeling

    Peeling

    Joined:
    Nov 10, 2013
    Posts:
    443
    I have a process that I'm trying to split across multiple jobs, and falling foul of Unity's job-nanny.

    The process involves operations on a single large array (1M+ elements). Each element is generated independently; there are no possible race conditions between the jobs.

    I can't split the array into chunks because a later pass requires them to all be part of a single contiguous array, and copying them around in memory would render the optimisation pointless (as well as consuming more memory which I want to avoid).

    Even with safety checks turned off, Unity is aborting the jobs as soon as I try to start the second one. Is there any way I can work around this? It doesn't seem like such an unreasonable thing to want to do. Thanks.
     
  2. LennartJohansen

    LennartJohansen

    Joined:
    Dec 1, 2014
    Posts:
    2,394
    In general to use all cores while processing large datasets it is good to use the IJobParallellFor job.

    If you have multiple jobs that will process this data pass the jobHandle from the first as a parmeter to the next etc.

    Then schedule the jobs and complete them when you need.
     
  3. Peeling

    Peeling

    Joined:
    Nov 10, 2013
    Posts:
    443
    Thanks - that's done pretty much what I wanted :) There's a little extra overhead for working back from array index to other values, but it seems to be worth it.
     
  4. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    And in this case they’ll be in chain and not parallel. If you have multiple jobs (not same job doing same stuff like IJobParallelFor, but with different logic) and want them in parallel write in to same array, but you can guarantee safety (different, not overlapping indices), better will be use NativeDisableContainerSafetyRestrictions and use JobHandle.CombineDependencies.
     
    wmpunk likes this.
  5. Peeling

    Peeling

    Joined:
    Nov 10, 2013
    Posts:
    443
    "NativeDisableContainerSafetyRestrictions"

    Oh..... I had no idea about that. I just tried turning it off in the 'Burst' menu. In this case IJobParallelFor is suiting me just fine, but it's nice to know there's a fallback. Thanks!