In this Intel-sponsored feature, part of Gamasutra's Visual Computing section
, game programming veteran Orion Granatir presents a practical look
at how to use multi-core CPUs to thread game elements, in this case artificial intelligence (AI) for your game.
Whether discussing complex pathfinding systems or simple scripting engines, AI drives gameplay -- and to maximize AI's potential, it needs threading in order to utilize the entire CPU. As Granatir explains in the technical article's introduction:
"The concepts described in this article were used in the creation of the multithreaded AI of Intel's Smoke demo. This demo showcases functional and data decomposition with multiple game technologies, including physics, audio, and AI. The source code is free to download at Whatif.intel.com
Granatir offers illustrated examples of a sample AI, and then defines challenges of threading its code:
"For our simple AI, we need the collision and the move system to be thread safe. We could put locks around any access to these systems, but we'll see lower performance because threads will have to block and wait for each other.
The solution for collision and pathfinding are similar, so let's focus on collision. For the collision system we will have multiple requests in parallel. For multiple readers, the collision data is just fine. This is no different from the serial case because the data is not changing. When someone starts writing data, then we have work to do. Data access to be protected (with locks) or the data has to be double buffered. If the data is double buffered, the readers can just access the data from the last frame and not worry about the writes until the next frame.
We can run collision independent of AI three ways: issue early and use late, deferred requests, and task stealing. Each is worth a bit of investigation."
The full Gamasutra sponsored feature
goes into greater depth on the multithreaded AI concepts used in Intel's Smoke demo.