Threading the AI - an idea

For code related discussions and questions
Lord Apocalypse
Regular
Regular
Posts: 678
Joined: 29 Jul 2009, 18:01

Threading the AI - an idea

Post 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.).
User avatar
NoQ
Special
Special
Posts: 6226
Joined: 24 Dec 2009, 11:35
Location: /var/zone

Re: Threading the AI - an idea

Post 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.
Per
Warzone 2100 Team Member
Warzone 2100 Team Member
Posts: 3780
Joined: 03 Aug 2006, 19:39

Re: Threading the AI - an idea

Post 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.
User avatar
Duha
Trained
Trained
Posts: 287
Joined: 25 Mar 2012, 20:05
Location: SPb, Russia

Re: Threading the AI - an idea

Post 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.
http://addons.wz2100.net/ developer
crass
Trained
Trained
Posts: 113
Joined: 21 Mar 2012, 02:26

Re: Threading the AI - an idea

Post by crass »

what parts of the game each take up a cpu core? I only see 1 out of 8 cores active