TBB and some custom threads
Members - Reputation: 190
Posted 13 May 2014 - 10:41 AM
My engine should be based on jobs which are executed by Intel's TBB. My design is similar to the one Intel uses in Smoke.
The job system itself is working but there are two things I want to do in 2 separate threads. The first one is creating textures etc. (With a shared OpenGL context) and the second one is rendering.
Now to my questions:
1. Are such job systems in general state of the art? I ask because I was out of engine programming for some time.
2. Is it a good idea to make the 2 extra threads? My thoughts behind this idea were that using dedicated threads for those two tasks reduces permant calls to MakeCurrent because in the "normal" job system I would not know in which thread the task is running the next time.
3. Is the performance of TBB's job scheduling affected by my custom threads? The number oft threads TBB creates is based on the number of CPU cores so my two tasks "steal" some processing power. But I think that the job stealing in TBB will handle this, am I right?
Members - Reputation: 914
Posted 13 May 2014 - 02:57 PM
Threads express mandatory parallelism. Tasks express the opportunity for parallelism.
(cnf. Structured Parallel Programming)
For example you could use a networking thread so it can block on incoming connections.
Updating game entities could be expressed using tasks, so it's up to TBB to distribute the work
among available processors for optimal performance.
I wouldn't worry too much about stealing processing power from TBB with your own threads. The OS
does a good job allocating time slices for each threads.
You want to have more threads than cores anyways because tasks block from time to time, waiting for
memory or file io.
You find some good slides from Dice here:
Moderators - Reputation: 25735
Posted 13 May 2014 - 11:09 PM
I'll recommend caution.
1. Is it state of the art? Some games have used it to a limited extent.
2. Is it a good idea? For an individual or a small team, probably not unless you are extremely niche. For a large organization with an 8-figure budget, possibly, with care.
3. Is TBB affected by other threads? Yes, if you have enough work. There are only so much resources to go around. As long as you stay below saturation things will generally be fine. Once it begins to hit saturation on any resource, additional work causes problems. CPU time is only one resource; caches, branch prediction tables, communication buses, and many other resource limits exist and will all cause performance to plummet once their limits are hit.
Now for some hopefully more useful notes.
Do a thought experiment on what you propose. You might be able to assign multiple CPU threads to the task. Now you have <b>two</b> sources fighting to send data across <b>one</b> bus to <b>one</b> graphics card, also fighting over <b>one</b> graphics context with a shared state. All that resource contention is not good for performance if anything comes close to saturation. Even if you do try some naive parallel techniques, generally the drivers will end up de-parallelizing your work back to serial operations, costing even more time. You need to find things that the drivers won't convert back to a serial process, that can be done without resource contention, and that will still work well on low spec machines.
I'm not trying to say threads are bad. They are an added complexity to the engine and a frequent source of bugs to novices, yet threads are useful in many things. All the major engines use threads as a form of damage control; when something dies you kill the thread and restart whatever was running on it. That is a great feature, but it requires some serious engineering efforts to build and debug. All the engines I've used over the past five years have basically run game code with invisibly-threaded systems. The game object code feels like it is single theaded, and is also usually processed sequentially, but it exists within its own thread. It is possible that multiple game object threads are running concurrently, but they are isolated in such a way that they don't stomp on each other. This also take significant engineering skill and effort.
Games need to still run acceptably on minimum spec machines, and typically min-spec still includes single processor machines from the P4 era which ended 2008 and Core 2 era which ended in 2011 where single-processor chips were common. While you can take advantage of certain parallel algorithms on games for non-critical processing if your hardware supports it, it is still too early to rely on them overmuch. Way too many low-spec machines only have one processor, or have their second processor busy working on spewing viruses or other malware or whatever other thousands of programs the user has running at the time.
You can rely on threads for many things, such as containment and organization, as well as non-critical things like crazy-intense particle systems and smoothly-drawn cloth. But it is still too early for mainstream games to put game-critical processing into mandatory multiprocessed tasks. Be very cautious about anything that requires parallel processing, including parallel rendering.
Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.
Also check out my personal website at bryanwagstaff.com, where I write about assorted stuff.