How many threads is too many for a game?

Soldato
Joined
1 Apr 2014
Posts
18,634
Location
Aberdeen
Should every AI object - spaceship, soldier, battleship, whatever - have its own thread? How many threads is too many?

Desktop CPUs can have up to 32 threads (and HEDT up to 128) at 4 GHz. Why not make use of them? Let's say you're redoing Freespace 2 and have a 64 ship battle, each with its own thread. At 4 GHz on a Ryzen 3950 each ship gets half a full core. (Yes, I'm simplifying) Surely that's more than enough? Even a 100 ship battle gives 1 GHz per ship (at 60 fps that's 16.5 million clock cycles per frame) - more than enough for AI.

But how about 1000 threads? Beef up that battle 10 times. Is 1.6 million clock cycles per frame enough for a decent AI? How about thread management overhead? How does CPU cache impact this?

Paging @Rroff.
 
Man of Honour
Joined
13 Oct 2006
Posts
91,158
Some games do just throw everything on a thread of its own, even when it needs to spend a fair amount of time stalled waiting to be able to communicate with other threads and/or data in use by other threads - it isn't a good approach - generally those games feel quite meh to play on PC at high framerate/high refresh though the nastiness is hidden on console and/or 60 FPS V-Sync, etc.

With games there is a lot of stuff that can only be processed serially regardless in the main logic, etc. so there are limits as to how useful threading stuff is but it makes sense to allocate separate threads generally for things like AI overall but not at a per unit level unless you are simulating a wealth of internal systems for that object.
 
Soldato
Joined
8 Dec 2005
Posts
10,543
PC port is usually an afterthought is mainly why as it cost a lot more to create games which can detect & utilise more than the baseline CPU core number of the consoles!
 
Associate
Joined
9 Jun 2009
Posts
1,397
Location
Suffolk
Can you elaborate?

Lets imagine you have three ships in the combat situation, A, B and C, where A and B are fighting a C is fleeing. If A fires on B then there will need to be time for A and B to resolve their firing (which could be a physics solution so could be quite time consuming). During this time if C is just flying away then that thread will be locked until the 'time period' or tick has caught up causing the slowest calculation to hold the rest of the threads back. So the longer you have to wait between 'ticks' in the game world the slower the simulation will run so the slower the frames per second. The more threads you throw at a situation the more there is the potential for locking up of the threads waiting for their calculations to finish.

I have experience of this when computing massive quantum simulations for my PhD, often I could be waiting on tens of thousands of cores to be waiting for the slowest to complete their calculation for the rest to move on, its why super computers (used to) have high speed optical interconnects to speed up the time that nodes are waiting.

I'd recommend reading into some of the Old EVE dev blogs that cover this, as they found they had to use highly overclocked CPUs at one stage for their nodes due to the fact that most things were running on less threads and it would be problematic to rewrite the base code to use numerous threads.
 
Soldato
OP
Joined
1 Apr 2014
Posts
18,634
Location
Aberdeen
Ah, you're talking deadlocks and blocking. I remember those from my degree days. Aren't there easy ways around them? Especially when in this case it doesn't matter if a thread fails to complete in time. IIRC the core thread just polls the other threads for updates and if it doesn't get one just moves on. But this was the point of my asking if the available number of clock cycles were sufficient
 
Soldato
Joined
14 Aug 2009
Posts
2,783

AMD demonstrated back in the day that you could do a lot of AI stuff on the GPU, enabling basically real live villages, towns, etc., to be had in game, with thousands of NPCs, each one going about their business (and that was on a 1TF card). Of course, if devs would only implement this...

But also in real life, TW3 in Novigrad can push pretty well the CPUs, with old FX CPUs showing quite nice numbers

Then you have Crysis 3, with the grass scenes where it pushes multiple threads and again the FX CPUs are doing quite well, the FX8350 being around the performance of a Sandy Bridge i7 and Ryzen (1st gen) doing very well.


https://gamegpu.com/action-/-fps-/-tps/crysis-3-2013-retro-test-gpu-cpu




Now AMD released FEMFX, which supposed to be optimized for multithreaded/multi core processors, so who knows, maybe with the consoles at the horizon, we should be getting some nice stuff.


Also, with Vulkan/DX12, multiple cores could talk to the GPU at once, instead each at a time like in DX11.
 
Back
Top Bottom