How should I divide tasks in parallelization?

Hello!

Every Heartbeat I am managing the movement of a bunch of projectiles, that require shapecasting to check for collisions and what not. In high numbers, the frames start lagging a little. After looking at the microprofiler, I see that I should use parallelization within this.

What I do not yet comprehend is how should I divide these objects between actors in the most optimal way possible. I am looking for insights on what criteria I could use to have some better performance. Thank you in advance.

2 Likes

This is kind of a difficult thing to give good advice for, but generally you need to look at how long your Projectile calculations take in relation to the rest of your frame. If it happens right at the end, you’ll know you’re going to need multiple Actors to see any benefit, but in other cases, even 1 is enough.

For example, in a case like this, the parallelize-able task happens in the middle of the frame,

, and here, there only needs to be one Actor to completely remove it’s impact:

I would recommend avoiding having, for instance, 1 Actor per Projectile, because each Projectile is probably less expensive to compute than the time it takes for the Actor to receive it’s data to perform it’s calculations on and send it back, because that on it’s own has overhead.

Going back to my previous example, more than 1 Actor only becomes necessary when Projectiles goes beyond the blue bar, so the most efficient distribution would be one that has the least amount of Actors to prevent that.

Usually what I do, is just have each Actor perform some number of tasks (in your case each Actor would be in charge of a certain number of Projectiles), and more Actors are used when there are too many tasks per Actor with the previous number of Actors.

For example, if I had 400 Projectiles with 100 Projectiles per Actor, then:

  • 1 Actor: Too few, this one would have to handle 400 Projectiles
  • 2 Actors: Same as above, each would have to handle 200
  • 3 Actors: Same as above, 133 - 134
  • 4 Actors: Each Actor only has to handle 100

Obviously, this involves some guesswork, although the best possible distribution could be calculated by measuring the total Actor time VS the total frame time, taking into account when the Actors were kicked-off.

Hello, first and foremost I really appreciate the answer, this is a topic I have seen little exploration within the forum.

In my case I usually end up with frames like this after a while, which is not convenient…

I was wondering how I would be able to measure this.

1 Like

I spent an hour trying to write my post, I accidentally cut it off. It’s just a hypothetical, and more work than just making an educated guess, I really shouldn’t even have written it.

My idea was measuring how long 1 Actor took, and figuring out how far that pushes the frame time up past your “target” (i.e. 1/60 or 16ms), and then continuously adding 1 more Actor each frame until the target frame time was reached or a limit was hit. I’ve never tried to do it, it’s just something I thought of.

Oh, and, if you haven’t already, I think there are some Parallel Luau modules on this Forum that people have made that will probably make your life easier.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.