multithreading whithin farseer.

Topics: Developer Forum
Jul 21, 2008 at 1:51 PM
Hi all.

I currently have my physics engine (farseer) running on it's own thread, and this has helped massively with getting decent performance out of the 360.

I just wanted to ask if anyone could give me any advice on breaking farseer out into more than one thread?
(I have a feeling the advice is going to be "don't try it")

I am just trying to squeeze a bit more performance out, and want to see where to put the effort.

Cheers

J.

ps.. another huge win I found was only updating the physics sim every other update (which means the physics thread has twice as long to complete an update in)
Coordinator
Jul 21, 2008 at 9:37 PM
You are touching one of the biggest problems in the programming world today: Parallel computing
I don't think there is any way for you to just split a part of farseer into another thread. This would cause big problems with synchronization and would be hard to maintain/debug.

One thing you could do, is using a parallelism library such as Task Parallel Library from Microsoft. I don't think it will work on Xbox, but thats a something for you to find out.

What Task Parallel Library does is that it has a replacement for "for-loops" (amongst other things) that detects the number of cores you have, and split out the for-loop on those cores. This could very well speed up the physics engine a lot.

A word of caution: Task Parallel Library is not just something you plug in and then it works. You still have to synchronize and make sure that you don't run into deadlocks. 
Jul 22, 2008 at 4:44 PM
Just having the physics on another thread is a pretty big win. I don't think it would be worth it to try and split Farseer across threads, you'd spend more time synchronizing than you would save. The only option I see is if you have multiple type of objects that never interact with each other, you could have two or more PhysicsSimulators running on separate threads. Again, this would only work for objects that would never affect each other in your game. But even then, physics would really have to be bogging you down to go that route.
Jul 22, 2008 at 10:13 PM
Edited Jul 22, 2008 at 10:20 PM
Thanks for the feedback, in a way I am being a bit greedy as I think the performance I am getting at the moment should be nearly sufficient. (just need a bit more poke so I can be confident the physics won't be the bottleneck)

Next time I have some time available I may try using SonOfDaedelus's circle-circle specific code, as my units are mainly circles.

Cheers all.

J