Multithreaded slower (despite Hyper-Threading)!

Started by
26 comments, last by CGameProgrammer 19 years, 4 months ago
A little improvement now. Framerate is 18 ST and 22 MT when the 3 groups fight. 60 when all's idle (vsync *is* disabled).

Y.
Advertisement
Quote:Original post by Ysaneya
A little improvement now. Framerate is 18 ST and 22 MT when the 3 groups fight. 60 when all's idle (vsync *is* disabled).

Y.

That's a smaller change than I would have expected, though it's possible it's "correct". For me, the AI is about 60% of the workload (50% collision, 10% sim), so if it takes 10 seconds (bear with me) to render a frame, with 6 seconds for the AI, and that changed to 3 seconds, it would be 7 seconds per frame, or 10/7 = 42% improvement. That would make 18 FPS change to 25. Maybe the AI is a smaller portion of the workload for you.
~CGameProgrammer( );Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
What you say would be true if you had two threads, having the same workload, working in paralell. I'm suspecting it's not the case.

What probably happens is that your first thread has a lot more work to do than your second one, because the first thread does a lot more things than A.I.: rendering, collisions, logic, input, etc.. while your second thread only does A.I. The second thread is probably sleeping a bit while the first one orders it to start the A.I. work.

Y.
No no no, you misunderstand how the threads work. The main process spawns two threads and then sleeps until those threads have finished. The child threads do AI and nothing more.

The way the workload was split between the two threads has changed a number of times, but with the last build I posted, the way I do it is I have 32 ships evaluated each frame, but the threads do not necessarily do 16 ships each. In pseudocode, the thread function is this:
while(1){    EnterCriticalSection      Index = SharedData->Counter      SharedData->Counter ++    LeaveCriticalSection    Ship[Index].DoAI( );    if( Index == SharedData->LastShip )        break;}

Counter could initially be 0, for example, and LastShip could be 31. So each thread evaluates ships until a specified number of them have been evaluated. This code ensures the threads do roughly the same amount of work, even if that means one thread does 24 ships and the other does 8.
~CGameProgrammer( );Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
But it doesn't matter if you have a parent thread and then two threads for AI, the problem remains.

Your two AI threads (now balanced) are working in parallel. But they're not doing that continuously, they are interrupted when the parent is doing its own stuff. So you have the two processors busy for a short amount of time (when doing the AI), but the rest of the time only one CPU is really working. So you cannot expect your framerate to double. Maybe you can check exactly how much time the A.I. threads are taking, and how much the parent thread is using.

Y.
Quote:Original post by Ysaneya
Your two AI threads (now balanced) are working in parallel. But they're not doing that continuously, they are interrupted when the parent is doing its own stuff. So you have the two processors busy for a short amount of time (when doing the AI), but the rest of the time only one CPU is really working. So you cannot expect your framerate to double. Maybe you can check exactly how much time the A.I. threads are taking, and how much the parent thread is using.

That's true, and I don't expect the framerate to double. As I calculated in my previous post, if the AI takes 60% of the workload single-threaded, a 42% increase in framerate can be expected when it's parallelized (if done perfectly), not a 100% increase -- that was never my goal.

There is merit in the idea of doing rendering and other things while the AI for the next frame is calculated, but it's not worth the trouble. On my system, AI is 60%, rendering is 10%, and the rest is 30%. Parallelizing the AI is more important than anything else, and it's also easier than doing it for the rest.
~CGameProgrammer( );Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
My FPS is 45 but its doesnt seem like it, its really jumpy more like the input is 3 fps. Not to surpriseing, i have a p3 1.13@ ~1.5, and a 6800 Ultra. But to me this shows that somehow your being limited by the Card in measuring the FPS. I dont recive a performance hit when switching to the MT version.
The framerate counter is correct, but it is the average over the last several frames. Because the AI for 32 ships is run each frame (which is just a small portion of the total number of ships), the frame that handles the more expensive ships will take much longer than subsequent frames that do the faster ships, so the game appears jerky. The workload figures are not averages, and you may notice they suddenly change drastically for one frame, then change back.
~CGameProgrammer( );Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.

This topic is closed to new replies.

Advertisement