Search Unity

Threading BoxcastCommand

Discussion in 'Entity Component System' started by Incode, Aug 29, 2020.

  1. Incode

    Incode

    Joined:
    Apr 5, 2015
    Posts:
    78
    I'm trying to do thousands of BoxcastCommands in a jobified way in a custom inspector, but I still seem to be running everything on the main thread. The code I have so far looks like this:

    Code (CSharp):
    1.  
    2. NativeArray<RaycastHit> results =
    3. new NativeArray<RaycastHit>(nodesProperty.arraySize, Allocator.TempJob);
    4.  
    5. NativeArray<BoxcastCommand> commands =
    6. new NativeArray<BoxcastCommand>(nodesProperty.arraySize, Allocator.TempJob);
    7.  
    8. for (int i = 0; i < nodesProperty.arraySize; ++i)
    9. {
    10.     Vector3 nodePosition = editorNodes[i].position;
    11.  
    12.     commands[i] = new BoxcastCommand(nodePosition + Vector3.up, Vector3.one * diameterProperty.floatValue * 0.5f, Quaternion.identity, Vector3.down, layerMask: LayerMask.GetMask("Ground", "Environment"));
    13.  
    14. }
    15. JobHandle handle = BoxcastCommand.ScheduleBatch(commands, results, 1, default(JobHandle));
    16. handle.Complete();
    Obviously this can be chunked, but how do I do this across threads in the editor?
     
  2. burningmime

    burningmime

    Joined:
    Jan 25, 2014
    Posts:
    845
    Try putting a
    JobHandle.ScheduleBatchedJobs()
    between the last two lines. I don't know how well it works in edit mode, but it's worth a try. Also you should probably bump that
    1
    up to like
    200
    or so.
     
  3. Incode

    Incode

    Joined:
    Apr 5, 2015
    Posts:
    78
    Hmm, that didn't seem to do it.

    Edit* - I've even converted my creation of BoxcastCommands to an IJobParallelFor struct, but am still using only about 5% of my CPU resources (16 core, 32 thread cpu).
     
    Last edited: Aug 30, 2020
  4. RecursiveEclipse

    RecursiveEclipse

    Joined:
    Sep 6, 2018
    Posts:
    298
    From the documentation on .Complete()(https://docs.unity3d.com/ScriptReference/Unity.Jobs.JobHandle.Complete.html)

    If possible, feed the handle into another job, or at least delay calling Complete until you need to.

    Increasing the number can decrease the number of threads running if you don't have enough items, on the other hand, lower numbers are not always faster overall due to scheduling costs.
     
    Last edited: Aug 31, 2020
    nyanpath likes this.