Get TBB
TBB's official website in http://threadingbuildingblocks.org/, can be found on its downloads page commercial Aligned release, the latest version is tbb20_ 014,windows on the development need to download tbb20_014oss_src.tar.gz and tbb20_014oss_win.tar.gz. Other supported platforms are Linux and Mac.
After extracting the downloaded compressed package, the tbb20_014oss_win.tar.gz has:
tbb20_20070927oss_win/
em64t/# Intel Extended Memory Technology (Intel EM64T) Processor
Vc7.1/
bin/
lib/
vc8/
ia32/# Intel IA-32 Processor
Vc7.1/
bin/
lib/
vc8/
bin/
lib/
My processor is the IA32 architecture (Intel P4), so use the library file under Ia32.
Tbb20_014oss_src.tar.gz useful in the main is the include/directory, the following is the use of TBB all the header files, of course examples/directory There are many examples of TBB, to learn.
Configuration
In VC2005, "Tools" è "options", set VC + + include files and library files, such as:
Example
Create a new project in VC2005, enter the following code, compile and run.
#include "stdafx.h"
#include "Tbb/task_scheduler_init.h"
#include "tbb/blocked_range.h"
#include "Tbb/parallel_for.h"
Link Tbb_debug.lib
#pragma comment (lib, "Tbb_debug.lib")
using namespace TBB;
Perform this action on each item
void Foo (float value)
{
printf ("%.2f", value);
}
Class Applyfoo
{
float * Const MY_A;
Public
void operator () (const blocked_range<size_t> & R) const
{
float * a = my_a;
for (size_t i = R.begin (); I! = R.end (); + + i)
Foo (A[i]);
}
Applyfoo (float a[]): My_a (a) {}
};
int _tmain (int argc, _tchar* argv[])
{
Create a Task Scheduler
Task_scheduler_init supports a parameter to specify the number of threads to use
Task_scheduler_init Init;
float a[100];
for (int i = 0; i <; i + +)
A[i] = (float) i;
TBB will divide the array into blocks.
Call Applyfoo to block this functor
parallel_for (blocked_range<size_t> (0), Applyfoo (a));
return 0;
}
The example creates an array of size 100, applies a parallel algorithm to the array using the PARALLEL_FOR algorithm provided by TBB, and applies the Foo function to each item. At runtime we see multiple threads executing foo at the same time and running the results more clearly on multicore platforms.
Through this example, we can find that using TBB can be a good place to write parallel programs, through the algorithm provided by TBB can realize the abstraction of the parallel concept.
TBB composition
The contents of TBB can be broadly divided into the following categories:
-Universal Parallel algorithm
o This is the most important part of TBB, such as the parallel_for in the example is an algorithm for this part.
o TBB provides algorithms such as Parallel_for,parallel_while,parallel_reduce, which are used in different parallel algorithm scenarios
-Concurrent containers
O This is the implementation of the thread-safe version of the common container, taking into account the requirements of the performance, provides a fine-grained locking mechanism, TBB2.0 provides a container including a hash map,vector,queue.
-Task Scheduler
O provides encapsulation of the task mechanism
-Synchronization Primitives
O provides the encapsulation of synchronous primitives such as atomic operations, mutexes, lock, etc.
-Memory allocation
O provides more friendly support for the cache mechanism
The relationships between them are as follows:
Getting started with TBB