Island solving using multi-threading?

I have been thinking, mainly with Java (and my not yet released Scala port) in mind -- would it be conceivable to do Island solving using several threads? I know Box2D was not made with multi-threading in mind, but it seems to me that forking the Island.solve method calls into different threads for each island and waiting until they are all done might be doable and give a good performance boost on multi-core processors (in cases when there are lots of bodies forming several islands).
I haven't looked deeply into this yet, but I think the only thing stopping this currently is that after solving the island, static bodies need to get their island flag removed.
Actually much of Box2D was written with multi-threading in mind. There are two easy ways to speed up Box2D via threading:
- Evaluate contact manifolds in parallel.
- Solve islands in parallel.

There is no harm clearing the island flags because none of the island solves look at that.
Oh, didn't realize you had that in mind already. Good to know.

I already gave it a shot in both Java and Scala just now, but did run into some concurrent modification problems so I guess it wasn't exactly as simple as I thought. Will look into it again later.
[edit] the concurrent modification was because I started solving an island in a new thread immediately after it was built, then proceeded with building other islands. The exceptions go away when I first build all the islands and then solve them. But the behavior of some testbed apps is very strange now (hard to describe), so something is still wrong. Will report back when I've got something working.
Hmm, you should be able to immediately solve an island. Maybe I'm forgetting something.
Hmm? A CME is usually thrown in Java when you're modifying a Java collection while iterating over it (whether from another thread or not), but at least in JBox2D the use of Java collections is extremely minimal (they're kind of slow), and I don't think any of those collections should be modified during solving (though I could be wrong); any chance you could post the stack trace you get?

BTW, I'm excited to hear that you're trying this out, it's a feature I'd love to add to JBox2d.
Sorry, the Java version actually had ArrayIndexOutOfBounds exceptions, Scala had CME-s (and I might have caused some bugs there myself).

Here's the stack trace (the source I have is from the 05-08-2008 zip archive)

Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
at org.jbox2d.dynamics.Island.report(Island.java:332)
at org.jbox2d.dynamics.Island.solve(Island.java:226)
at org.jbox2d.dynamics.World$1.call(World.java:653)
at org.jbox2d.dynamics.World$1.call(World.java:1)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.lang.Thread.run(Thread.java:619)

Seems that the constraints array passed to Island.report is empty.

[EDIT] Oh, one thought I have is that might this possibly have something to do with Island.m_positionIterationCount being static? The fact that it was static didn't seem logical to me, but maybe I missed some purpose behind why it was. I changed that to non-static in Scala.
Sorry guys, it turns out I am really stupid and I was reusing the same Island instance. 😳 I didn't notice that it was not created in the loop, but once in the World.solve method and then cleared in the loop 🙂

So, it works now (and was actually quite easy to get working), but I'm not seeing much improvement in speed on a Dual Core Opteron and am seeing around 50% processor usage for the process. Perhaps Java threads don't automatically (or at all?) make use of multiple cores -- will have to do some research and optimizing (reusing threads etc.).
Quote:
So, it works now (and was actually quite easy to get working), but I'm not seeing much improvement in speed on a Dual Core Opteron and am seeing around 50% processor usage for the process. Perhaps Java threads don't automatically (or at all?) make use of multiple cores -- will have to do some research and optimizing (reusing threads etc.).

I'm not an expert on multi-threading, but I'm pretty sure Java always tries to maximize processor usage if there are enough threads available to fill the processors - I just did a very simple test, and on my machine (Core 2 Duo) I'm getting both processors at ~95% with two active threads calculating. The first things I would suspect are:

1) The island solving is not, in fact, a significant bottleneck in your test, and even if it is getting done both processors one of them is still sitting idle most of the time during one of the sequential parts of the program (contact solving, maybe).
2) The threads are not actually running in parallel due to overly aggressive synchronization. I would check using some println statements in the island solver to make sure that the island processing is actually being interleaved. Nothing in the engine itself is thread safe (unless you used thread safe collections when you ported to Scala), so the engine should not be doing any locking itself.

That's good to hear that you at least got it working, though - keep us posted!
Threading won't speed up the island solver if there is only one island. I would run the test with two separate pyramids of boxes.
Erin Catto wrote:
Threading won't speed up the island solver if there is only one island. I would run the test with two separate pyramids of boxes.

Sorry to bring this off topic, but do you know if there any reasonable ways even in theory to parallelize island solving and see a speed gain? For instance, can you break up an island into sub-islands, solve those independently, and then sync up and solve the sub-island junctions, or will that require too many iterations to converge to make it worthwhile?

In realistic game situations these days you'll almost always have enough islands to keep the two to eight (if you're real hard core) processors you have busy, so it's not a huge issue now, but when we start seeing 16+ processors on home machines and consoles, which is looking more and more imminent as time goes by, it will become far more relevant.

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