Long, low-priority operation
Games occasionally rely upon computation that needs to run
continuously but not at the expense of other more immediate work. Level
loading, asset decompression, and AI pathfinding are common examples. These
tasks can be set up to run on dedicated threads, but this exposes the problem
of the continuous operation fighting for hardware resources with more immediate
work. Priority APIs can help defend against this, but usually cannot guarantee
that no conflict will happen. Intel TBB can implement this technique without
introducing additional threads and therefore without introducing the
possibility of conflict.
Figure E: Time slice a low-priority operation into the work tree.
Figure E shows parts of two work trees. On the left is some
middle section of a large work tree. On the right is a root that is hosting
some low-priority tasks. The Parent on the left, before spawning its own
children, tests a flag for the need to initiate a low-priority task. If so, it
clears the flag and adds a low-priority task to the root.
The low-priority task is added to a root with a very small
task depth. If the thread that executes the Parent completes all of its other
work, it will run the low-priority task. As other Intel TBB threads exhaust
their own supplies of tasks to run, they will engage in stealing work from each
other. If one of these other threads steals from the original thread, the
low-priority task will be the one stolen due to its minimal depth value. This
ensures that low-priority tasks are run periodically but infrequently even when
the work tree continues to grow.
Sample 7: Adding low-priority tasks to a work tree
tbb::task *execute()
{
...
if(s_tLowPriorityTaskFlag.compare_and_swap(false, true) == true)
{
// allocation
with "placement new" syntax, see TBB reference documents
tbb::task
*pLowPriorityTask = new(
this->allocate_additional_child_of(*s_pLowPriorityRoot)
)
LowPriorityTask();
spawn(*pLowPriorityTask);
}
// add some
children
...
}
Sample 7 shows the test for low-priority operations as part
of a base class's implementation of tbb::task::execute. This hypothetical base
class would be used for all normal-priority tasks in the work tree. An atomic
flag is used to test if a low-priority task should be created. This flag is set
to true again when the low-priority task completes.
Game architectures will continue to use long, low-priority
operations for certain types of tasks. Using this modified time-slicing scheme,
a game architecture based on Intel TBB can accommodate this need without
introducing extra threads and associated competition for hardware resources.
Other Features of Intel TBB Relevant to Game Architectures
Throughout the samples in this article, some core features
of Intel TBB have been used without due explanation. These low-level objects
provide a cross-platform encapsulation of basic synchronization and timing
APIs, which are commonly used in game architectures. The APIs for all of these
objects are described in detail in the Intel TBB reference documentation.
Atomics
As shown in Samples 6(a) and 6(b), Intel TBB's atomics
provide a simple syntax for atomic compare-and-swap operations.
Mutexes
Intel TBB provides mutual exclusion objects with a variety
of contention policies. Spinning, blocking, read-write, and recursive mutexes
are available.
Time intervals
Intel TBB encapsulates each host platform's high-frequency
timer into a simple API.
The Future of Intel TBB
Intel TBB has a bright future ahead in game architectures.
With the variety of multi-core topologies in the major gaming platforms,
developers are faced with the challenge of designing an architecture that is
high performing without requiring extensive per-platform tuning. Because Intel
TBB is an open-source library, it has great flexibility for the gaming
platforms of the future. Wherever Intel TBB is implemented, it will continue to
provide game architectures with high-performance threading through its flexible
API.
Intel TBB is available at no cost from opentbb.org for
Microsoft Windows, Mac OS X, Linux, Solaris, and Microsoft Xbox 360. It
ships with Intel compilers. You can learn more about Intel TBB at opentbb.org
and in the O'Reilly nutshell book Intel
Threading Building Blocks by James Reinders.
|