Island solving using multi-threading?

It is possible to split up large islands at the cost of some quality. Basically you would use some heuristic to split the island, then solve the two sub-islands. Finally the two sub-islands would be partially synchronized by solving the inter-island contacts (one iteration). You could repeat this in a loop to improve convergence. The sub-island solve is Gauss-Seidel while the interface solve is Jacobi. Jacobi solvers are known to have much slower convergence than Gauss-Seidel solvers.

This would probably not work well for stacking or large articulated structures, but it might be sufficient for destruction.
I made the pyramid test (SlickTestMain) draw 4 pyramids and started 4 worker threads when the app starts which are reused (not sure if this is necessary--maybe thread creation isn't that costly). With threads I'm seeing processor usage on Dual Core jump to 60-70% (which might be expected I guess, since Island solving is not taking up 100% of the CPU time the app gets (more like 30-40%), but I'm barely noticing any change in FPS (when the bodies sleep, it jumps up drastically). Of course, FPS might not be the best measurement, I will run it with a profiler probably this weekend.
That sounds way smart, Erin. But, after a quick amount of wikipediaing, I think you can do better, and have Gauss-Seidel between them too.

As I gather, the only difference is that with the better method, you use the most recent value you have from the solver, not the value from the previous iteration. And that seems doable with multiple threads. Even though you are reading and writing to the same thing with different threads, we just need ensure that the read and writes are atomic. We don't really care whether a particular read and write (within one iteration) come in one way round or another, as we get some convergence either way, so no race conditions. As long as we get a decent proportion of "reads after writes", we'd still see Gauss-Seidel like convergence, I guess. (And you would expect roughly as many as in the single threaded case, i.e. half of all reads come from the value updated in the current iteration instead of the previous, assuming the number of threads is much smaller than the size of island).

Please, stop me if I'm talking crazy. I really don't understand the maths involved here, or the threading principles.

Also, you could try random partitioning instead of a heuristic, changing the partition each iteration.
BorisTheBrave wrote:
\Even though you are reading and writing to the same thing with different threads, we just need ensure that the read and writes are atomic. We don't really care whether a particular read and write (within one iteration) come in one way round or another, as we get some convergence either way, so no race conditions. As long as we get a decent proportion of "reads after writes", we'd still see Gauss-Seidel like convergence, I guess. (And you would expect roughly as many as in the single threaded case, i.e. half of all reads come from the value updated in the current iteration instead of the previous, assuming the number of threads is much smaller than the size of island).

I'm not sure if I totally understand what you're suggesting, so I'll try to rephrase, correct me if I'm wrong. Are you basically saying that since within an iteration the solving order is unimportant (and in fact any regularity may lead to artifacts) we might as well leave that to the whim of the thread scheduler, and instead of locking a whole island, or even a sub-island, we merely lock at the level of the individual contact? Or are you going a step further and suggesting that we shouldn't even care if a particular contact's read-solve-write cycle is interrupted (and most likely screwed up), since in the long run everything is converging towards the same solution anyways? I don't know how expensive synchronization is in other languages, but I know that in Java, it's so bad that it actually might be better to do twice as many iterations to undo the damage done by concurrent accesses if it can avoid locking sections of code.

That's an interesting take, though I do notice that it removes determinism from the system, as the scheduler is now in control of what order contacts get processed in. I wonder how it would work in practice.
I was talking about locking at contact level, I guess. I'd feel less confident arguing for completely ignoring locks. I was jabbering about atomic updates as I thought, that despite the 2 dimensions of velocity and position, as we were effectively just inverting a big matrix, each write is writing only one float at a time, meaning there's no difference between the two things you mentioned.

But yeah, you've already got the nugget of what I was saying. I don't really get the performance issues, which are no doubt fundamentally different for C++ and Java anyway, but I thought as we are doing relatively little writes compared with reads, we didn't need worry too much about how much it hurts, only that it is always correct.

Oh, and screw determinism. Box2D doesn't promise it for most things, and thanks to chaos and rounding errors doesn't provide it at the moment for any practical purpose.
Quote:
Oh, and screw determinism. Box2D doesn't promise it for most things, and thanks to chaos and rounding errors doesn't provide it at the moment for any practical purpose.

I'm still waiting for Adobe to support fixed-point numbers in AS3. But I may be waiting forever. 🙁
I did some more tests with this and I think there isn't much gained on 2-core CPU, even with a lot of contacts. But I'm guessing that there may be noticeable gain with more cores. I don't have such a machine, though. If anyone is interested, I can provide a patch for JBox2D that enables multi-threaded island solving.

Another guess I have is that think a bit more can be gained by using a good algorithm for dynamically deciding when to solve normally and when to solve with multiple threads (which has some additional overhead).

Of course, I may be running the wrong tests, I should create a test with several islands that always has many contacts.
Villane wrote:
I did some more tests with this and I think there isn't much gained on 2-core CPU, even with a lot of contacts. But I'm guessing that there may be noticeable gain with more cores. I don't have such a machine, though. If anyone is interested, I can provide a patch for JBox2D that enables multi-threaded island solving.

I'd definitely be interested in taking a look, if you don't mind sending a copy over (my e-mail is my username here at gmail, or you can post a copy here). I took a very quick stab at this a while back, but I never worked it out well enough to see a significant speed increase because I was losing to thread creation anything I gained from the multiprocessing.

I'm skeptical about any gain on more cores if you're not seeing one on two - in my (limited) experience with multithreading for speed, the easiest gain to achieve is a roughly 2:1 boost when moving from one to two cores, with smaller further gains for each successive doubling. But a lot of that depends exactly what's done in the code, and, as you mentioned, what example you're testing.

Would be real neat to get this working, for sure.

Original thread: https://box2d.org/forum/viewtopic.php?t=1450