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

Lambda version of IJobParallelFor?

Discussion in 'Entity Component System' started by JakHussain, Mar 10, 2020.

  1. JakHussain

    JakHussain

    Joined:
    Oct 20, 2016
    Posts:
    318
    I pretty much never had a usecase for IJob since all the logic I ever needed to optimise were loops.

    Will a coming version of entities support a lambda API for this? Or am I expected to design all my loop logic around entities?

    I just want to be able to crunch numbers that aren't necessarily tied to any entity.
     
  2. psuong

    psuong

    Joined:
    Jun 11, 2014
    Posts:
    118
    https://docs.unity3d.com/Packages/com.unity.entities@0.7/manual/ecs_job_withcode.html

    I think you're looking for Job.WithCode lambda expression.
     
    Nyanpas likes this.
  3. JakHussain

    JakHussain

    Joined:
    Oct 20, 2016
    Posts:
    318
    Ah see there's a note in the manual saying that Job.WithCode is equivalent to an IJob and at the bottom of the example in the link that you sent me it even says that the only way to run a scheduled job currently is still to directly implement IJobParallelFor and schedule that.

    So I'm aware what we can and can't do I'm more asking about whether Unity are basically hinting that they only want jobs to work on arrays of entities that satisfy an entity query or if purely mathematical parallel jobs that have no interaction with entity data will eventually come in a later release.
     
  4. JakHussain

    JakHussain

    Joined:
    Oct 20, 2016
    Posts:
    318
    So in short, should I keep my math code in Job.WithCode in anticipation of some eventual Job.WithParallelCode method or refactor my data to be kept in entities as well so I can use Entities.ForEach?
     
  5. Curlyone

    Curlyone

    Joined:
    Mar 15, 2018
    Posts:
    41
    You can still do IJobParallelFor inside your system, but you have to manually handle dependency. so Dependency=new MyParallelJob.Schedule(x,x,Dependency) should work.
     
  6. Nyanpas

    Nyanpas

    Joined:
    Dec 29, 2016
    Posts:
    406
    psuong likes this.
  7. psuong

    psuong

    Joined:
    Jun 11, 2014
    Posts:
    118
    Ah I see - honestly not sure how about how Unity intends us to go into
    Hmm honestly not quite sure on the Unity side, but if they introduce something similar to IJobParallelFor struct interface for their fluent API it would be quite nice to work with on a high level.

    So as for refactoring the data - I was in a similar situation where I just end up storing pieces of data on various entities to enable updates on all of them in parallel (that ended up fitting my problem space much better).
     
    JakHussain likes this.