Page 1 of 1

Threading the AI - an idea

Posted: 31 May 2012, 06:51
by Lord Apocalypse
This is based on a question asked here
NoQ wrote:
Maybe it would be possible to put that routine into a seperate thread, so the CPU can work on it simoutaniously?
No.
The reason is that the scripts work with copies of game objects (and there is a good reason for that too). These copies may get outdated if used in a different game frame. So even if the computation done in the script can be parallelized, the script can't run in background of the game itself. Each event needs to get handled before the game can advance further.
Which got me to thinking... If we are to attempt to thread the AI then why not do so for EACH ai per frame instead of all AIs running in order. For advanced AIs or a true learning AI this would spread the load around. I know in python this could be done each frame as threads are built into python. Not sure about the WZ script/js. The only option would be to have a copy of each script running in a separate thread.

By this I am talking about having the AI (when its the AIs turn to process information) through each AI player into a thread pool and have them process that thread independently rather than in turn. Overall, AI only has one thread but each player AI gets to do its chores on its own rather than collectively. This may not work well for combat units but should work out ok for trucks and overall combat strategies, perhaps research choices and anything that doesn't require advanced concepts (locks etc.).

Re: Threading the AI - an idea

Posted: 31 May 2012, 06:55
by NoQ
No, that won't work either, because only few of the frames are critical for every AI, and they are already different for different AI instances.

Re: Threading the AI - an idea

Posted: 31 May 2012, 08:20
by Per
I have considered the idea. However, unless there is anything blocking on IO, or an extra CPU core is just sitting there, then threading will not actually yield any performance benefit over sequential execution. We are already utilizing an extra core for path-finding, and I do not think more than dual core is very common yet.

Re: Threading the AI - an idea

Posted: 31 May 2012, 10:50
by Duha
Every thread about premature optimization should contain info about PC and time of execution of code.

IMHO js does not work with IO. js does not make a lot math. It just uses C++ API.

Re: Threading the AI - an idea

Posted: 31 May 2012, 21:32
by crass
what parts of the game each take up a cpu core? I only see 1 out of 8 cores active