Multithreaded physics

Discuss any questions about BEPUphysics or problems encountered.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Multithreaded physics

Post by Norbo »

Here's a little sample game using physics on a separate thread and the Stopwatch.GetTimestamp() method.

There's two main parts, the initialization section:

Code: Select all

space.simulationSettings.useInternalTimeStepping = true; //This will let the space step forward varying amounts of time per update.
//Set up the physics thread and start it.
Thread thread = new Thread(physicsLoop);
thread.IsBackground = true;
thread.Start();
and the update section:

Code: Select all

void physicsLoop()
{
   double dt;
   double time;
   double previousTime = Stopwatch.GetTimestamp() / Stopwatch.Frequency;
            
   while (true)
   {
             
       time = (double)Stopwatch.GetTimestamp() / Stopwatch.Frequency;
       dt = time - previousTime;
       previousTime = time;
                
       space.update((float)dt);

       Thread.Sleep(0); //Give other threads a chance to execute
   }

}
Attachments
PhysicsThread.zip
(182.64 KiB) Downloaded 287 times
nindim
Posts: 10
Joined: Thu Nov 06, 2008 1:40 pm

Re: Multithreaded physics

Post by nindim »

Thanks very much for posting the example code, I have been really busy so I haven't had a chance to try it untill now!

I have ran your example threading game on my 360 and I still seem to be experiencing the stuttering. I printed out the delta time values being used each time the thread loops and it seems EXTREMELY high for the scene:

Delta Time: 0.769995107769319
Delta Time: 0.724994265663554
Delta Time: 0.72699651127823
Delta Time: 0.731993964912363
Delta Time: 0.762996511278288
Delta Time: 0.779994406015248
Delta Time: 0.763994325814565
Delta Time: 0.789995047618504
Delta Time: 0.783994345864812
Delta Time: 0.78070221553935
Delta Time: 0.765287278195501
Delta Time: 0.752994927317559
Delta Time: 0.743994626566746
Delta Time: 0.721040962405823
Delta Time: 0.729949734336515
Delta Time: 0.720039819548219
Delta Time: 0.705951378446116

So almost three quarters of a second have passed between each space.update. The time it actually takes to complete the update is closer to 0.07 (which still seems high for that scene). Anyway, it seems to work OK on my PC, so something is happening with the threading when running on 360. Even an empty thread just calcualting the delta time only executes about 40-45 times per second....
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Multithreaded physics

Post by Norbo »

Are those .7 outputs from near the shutdown of the program? I noticed some very large numbers near the end during my testing that didn't line up with how the program was running normally.

I recommend also setting your thread's processor affinity if you haven't yet. That may help quite a bit. I've attached an updated version of the project which sets the thread up on the 6th hardware thread.

The scene itself is actually pretty complicated for the Xbox 360. The only effective way right now to get great performance with that many boxes piling on top of each other on the Xbox is internal multithreading (multiple threads for the physics alone), which is in the works. There could also be additional timing issues on the Xbox, so I'll do some more investigating.
Attachments
PhysicsThread.zip
(331.29 KiB) Downloaded 265 times
nindim
Posts: 10
Joined: Thu Nov 06, 2008 1:40 pm

Re: Multithreaded physics

Post by nindim »

As far as I remember they weren't from the shutdown, they were from the point in the demo where all the boxes were on top of each other. I can imagine that the update time would be increased in this instance as you have many objects colliding at once.

The thing I find most strange though is that the space.update() call itself is only taking 0.07 seconds, yet the next call to space.update() is 0.7 seconds later. This means that the thread is off somewhere else for 0.63 seconds or there abouts. What is it doing and why doesnt it just loop again. I'm sure this is the root of the problem and not the speed at which space.update() is operating at. Somehow the thread is delaying the next iteration of the loop AFTER the space has been updated.

Regarding the processor affinity I tried that previously on my own version and whilst it does seem to help (it seems to reduce the delat time by a little, we're not talking much here though) the overall effect is still stuttering really badly.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Multithreaded physics

Post by Norbo »

Does the latest PhysicsThread project I posted behave the same way when you deploy it with no modifications? Currently, all I observe is a pretty regular slowdown during intense interaction (when the box count is around 100) when deployed. Hopefully we can establish a control group of some kind.
diadem
Posts: 10
Joined: Sat Jan 17, 2009 4:19 am

Re: Multithreaded physics

Post by diadem »

Norbo wrote: The scene itself is actually pretty complicated for the Xbox 360. The only effective way right now to get great performance with that many boxes piling on top of each other on the Xbox is internal multithreading (multiple threads for the physics alone), which is in the works. There could also be additional timing issues on the Xbox, so I'll do some more investigating.
Any idea when the internal multithreading for the 360 will be ready?
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Multithreaded physics

Post by Norbo »

Right now, it's looking like it will be in March somewhere. A lot can change in between now and then, though.
Post Reply