Intel threading building block is a multi threading library from Intel, for C++ language. This library makes multi threaded application development easier, by abstracting low level threading details for better multi core performance. Parallel tasks in a programs no longer require manual creation of threads and overhead of thread synchronizations issues. TBB (Intel Threading building blocks) got program constructs which is easier to use to perform parallel tasks in a efficient way. Internal implementation of TBB is better than traditional parallel programming models and very much optimized for future many core processors. More over, for programmers it is easy to use and code.
Let me explain it with simple example I wrote a year ago to understand TBB (this is nothing to do with my work at Intel). Here is my problem, “I need to create 10 different files with different set of data. Each files data manipulation and IO write is independent of each other. Each file creation is independent task and can be carried out with out impacting other file creation tasks”.
Let us discuss different option available in front of us to achieve this tasks.
1. Sequential way (no parallel programming).
Write code in sequence manner where you create one file at a time and then go and take next file creation task. Probably little code can be reused here, for example pseudo code of this approach may be
BOOL CreateMyFile(Some Data)
{
//File creation code, data manipulation, and IO operations
}
for(int i = 0; i < 10;i++)
{
CreateMyFile(Some Data)
}
Here, each task is carried out one after other, This approach is not optimal if your machine is multi core and multiprocessor machine, lot of processing slots will be wasted and processor power is not utilized to its full potential. Also this application will take more time to finish all the tasks when compare to two other ways explained below.
2. Traditional Multi threaded code
In this case, each file creation task can be done by a thread. So for each task create a thread assign the tasks and wait for the threads to complete. Ideally if thread creation tasks are quick enough, all 10 tasks will be done in parallel. This way of programming will look like this way
//Thread function for each thread to execute to create a file
UINT FileCreationThereadFn(LPVOID someData)
{
//File creation code, data manipulation, and IO operations
}
//Thread creation function, as we need 10 threads, using for loop to start 10 different threads. For each iteration //new thread will be created.
for(int i = 0; i < 10; i++)
{
BeginThread(FileCreationThereadFn, LPVOID(SomeData));
}
This way of programming is better than first one, as because at any given point of time (after all the threads are started), all 10 tasks will be executed by 10 different threads in parallel. Thread scheduler will take care of identifying idle processor slots and utilize it all the cores and processors at same time.
3. Using Intel Threading building blocks
In option 2, we used ‘for loop’ to start different threads to execute the tasks. What if ‘for’ loop itself got option to execute each iteration in parallel, also in optimized way for current and future multi core processors? That is what Intel threading blocks doing, it got ‘parallel_for’, and each iteration of parallel_for will be treated as parallel task and executed independently. Just by using ‘parallel_for’, your program is optimized for parallel execution of the tasks for current and future multi core machines. The pseudo code may look like this way
BOOL CreateMyFile(Some Data)
{
//File creation code, data manipulation, and IO operations
}
parallel_for(range,CreateMyFile); // Range here is iterative values.
The simple above code will do the magic of parallel execution of the tasks. This is not ends here, Intel threading building blocks got many other algorithms and programming structs which solves many of multi threading programming and more importantly very easy way to optimize it for many core processors of the future. If your application demands huge parallel tasks and need to optimized it for future processors, then TBB is the way to program your app.
Above sample application written using C++ and MFC with visual cue for how tasks are executed can be found here.
Supporting presentation with animation explaining above three options can be found here
References:
Intel threading building blocks are open source now under GPL.
http://www.threadingbuildingblocks.org/




Recent Comments