Repository navigation
Replies: 8 comments 12 replies
|
There has been some discussion of using continuations. I find the current threading model easy for me to debug and reason about. I find task graphs disconnected from the program flow. Basically a bunch of callbacks. That said, I am interested in alternative threading models because they can lead to threads staying busier. The b3WorldDef worker count, is not for parallel_for.c partitioning. That uses cooperative claiming. The worker count is used to allocate task contexts. That would likely need some re-work if the number of workers becomes variable. |
|
One thing I need to figure out is that while Box2D/3D do a lot of fork-and-join parallel-for style work, they also have some longer running tasks that span across multiple sync points, such as the broad-phase rebuild and island splitting. |
|
I'm curious with this model where there is less control over when tasks are running. Is there an expectation to interact with the Box3D world while it is simulating? Because that is something I'm not planning on. You seem to be hinting at the threading model in PhysX. I'm a little concerned because PhysX doesn't scale that well and also has a very high floor when nothing is happening. There is also an idea that tasks shouldn't spin on siblings which also exists in PhysX: And indeed PhysX wants to know the worker count, same as Box3D. Which can be adjusted using b3World_SetWorkerCount. This is used for context capacity. It does not expect a certain number of threads to execute the tasks. The current model in Box2D/3D uses minimal locks in normal operation. Except for some pool allocations. So I would want to avoid adding any. |
|
Good to know. I'm interested in doing this work, but just want to pin down the scope and expectations. I've already done work in Box2D/3D to ensure any task can make progress if no other tasks show up. But there can be coordination between tasks when they do show up. This is in the constraint solver which is uses "micro tasks" and optimized atomic synchronization. This is only a problem if a task shows up to help but then gets swapped out. I've never seen this in practice. The Box2D/3D schedule is pretty simple. I have a drawing of it here: The square boxes are parallel-for. Rounded boxes are single threaded work (but can be off the critical path). The solve constraints box is where the micro tasks occur and that is the most complex part of the parallel coordination. Externally it looks like N tasks running in parallel with no external coordination. There is where the worker count does drive behavior (it is N). So optimal performance means all N tasks execute in parallel. However, it isn't required. One of those tasks can solve the block on its own. The first one in claims a leadership role for coordination. That setup might need to change a bit. |
|
Some thoughts about a potential multithreading API for box2d/3d: I just introduced a job system into my game and want to make box2d use it natively in the future. So far, I am mostly single threaded and have only used the built-in task system in box2d. That one has been working very well, but it would of course be nice to see box2ds work items on my existing, named worker threads and have it scale more dynamically/cooperatively with the rest of the game. The job system is very simple at the moment, not allowing for task stealing and only allowing to submit work from the main thread. For the threaded work that there is, I currently create jobs on the main thread, then kick them off. Then I go do some single-threaded work or create other jobs which are kicked off too. There is a polling and a blocking API to wait on a job group, which check semaphores that are signaled by the jobs after completion. I'm just going to outline an API for box2d multithreading that would work well with the system I currently have. For simplicity, I'm going to pretend there are only 2 phases of work here, broadphase and contacts:
With this API, I would call This would also get rid of callbacks. I hate callbacks. As I said, the current multithreading is working fine for me, and I haven't gone deep into multithreading yet, so take all of this with a grain of salt. |
|
Thanks for your input. I have prototyped a pull model in Box2D. I'll add you both for review. Once approved, I'll port it to Box3D. |
|
BTW, @All8Up I'm looking for shipped games that use wavefront. I was on a project that tried this but it did not go well. Many small tasks and no global view. |


Uh oh!
There was an error while loading. Please reload this page.
The current approach to 3rd party threading integration is reasonable for simple threading scenarios but is causing a number of issues for integration into a fully asynchronous data parallel approach. The primary issue is that b3World_Step assumes that it can steal a thread and block it while the eventual work tasks are resolved. In async cooperative threading solutions that unfortunately is a problematic assumption. I have worked around the problem but I've seen threading solutions which would not have easy answers to the current behavior.
The primary issues with the current model from the perspective of fully async execution systems:
In some of the past physics engines I've used, I generally had the option to do the following (very pseudo code outline):
Something along those lines such that there is no thread 'stuck' in code holding a stack open that must remain pinned during execution. Basically, given a cooperative model, the desire is to have a series of steps produce lists of work which can be distributed without locking up a thread, the current thread might swap out to a worker on the tasks and then back to calling 'next_step', or a completely different thread might call next, you never really know in such a system.
This is significantly different than current b3World_Step of course which is why I figured I'd start with this outline of the problematic area for the less common threading models.
All reactions