Introduction and use of TBB

Source: Internet
Author: User

1.TBB What to do

TBB (Thread Building Blocks), obtained 17 session Jolt Productivity Awards, is a set of C + + template libraries, and the raw Thread ratio that leverages the OS API writer directly, providing the appropriate abstraction in parallel programming, when It also includes additional content, such as the task concept, the mature implementation of common algorithms, the automatic load Balancing feature, and the flexible scalability of the number of unbound CPUs.

TBB provides parallel_for, Parallel_while, Parallel_reduce, and so on, (these are the interfaces that TBB gives to C + + programmers), which is certainly multi-threading, so the data competition problem is tricky, So TBB provides concurrent containers;
These interfaces provided by TBB have not yet been able to solve performance problems, so we can study the use of mutexes, Atomic, task, etc. in more depth.

From bottom to top, Task_scheduler--------concurrent_container--------parallel_for---Pipeline
To put it simply, TBB helps us to dispatch a task (more efficient than the OS scheduling) and achieve efficient parallel algorithms.

2. Download and install

    • Website
    • GitHub Address

Installation:
There is an TBB plugin on the official website, with the VS version, which can be downloaded and installed as per instructions.

The security of Add -ins/macros--environment------for tools ----with VS, add your directory to vs version.

In the VS right-click Project, select the TBB->TBB version , which allows the tool to automatically add the TBB inclusion directory and library directory to the project.

3. Use

1), parallel_for applicable occasions: Multiple data or requests are not dependent on each other, the operation is the same (typical SPMD)

template<typename Range , typename Body >void    parallel_for (const Range &range, const Body &body)

Range, used to define the series, here is the template, you can use the STL container

R::R( const R& ); R::~R(); bool R::is_divisible() const; //True if range can be partitioned into two subrangesbool R::empty() const;// True if range is emptyR::R( R& r, split ); //Split range r into two subranges.

Body, the definition class used to perform the operation, user-defined, the following is the template

Example:

#include <iostream>#include <vector>#include <tbb/tbb.h>#include <tbb/blocked_range.h>#include <tbb/parallel_for.h>using namespace std;using namespace tbb;typedef vector<int>::iterator IntVecIt;//定义操作struct body{    void operator()(const blocked_range<IntVecIt>&r)const    {        for(auto i = r.begin(); i!=r.end(); i++)        {            cout<<*i<<‘ ‘;        }    }};int _tmain(int argc, _TCHAR* argv[]){    vector<int> vec;    for(int i=0; i<10; i++)    {        vec.push_back(i);    }    //执行操作    while (1)    {        parallel_for(blocked_range< IntVecIt>(vec.begin(), vec.end()), body());        cout<<"sleep...\n";        Sleep(100);    }    return 0;}

2), parallel_reduce suitable for the need to summarize the situation, that the results of each data need to be aggregated back

There are two kinds of forms:
One is Lamda form, one is body form

Body definition

Body::Body( Body&, split ); //Splitting constructor.//Must be able to run concurrently with operator() and method joinBody::~Body(); void Body::operator()( Range& r ); void Body::join( Body& b ); //Join results.

One of the lambda:

template<typename Range , typename Value , typename RealBody , typename Reduction >Value tbb::parallel_reduce  (    const Range &   range,    const Value &   identity,    const RealBody &    real_body,    const Reduction &   reduction )   

Examples of Lambda:

int result = parallel_reduce(    blocked_range<vector<int>::iterator>(vec.begin(), vec.end()),    0,    [](const blocked_range<vector<int>::iterator>& r, int init)->int{        for(auto a = r.begin(); a!=r.end(); a++)            init+=*a;        return init;    },    [](int x, int y)->int{        return x+y;    });cout<<"result:"<<result<<endl;

3), parallel_while sometimes do not know when the loop ends, that is, use for the end is unknown, in this case can use Parallel_while

4), Concurrent containers:concurrent_hash_map\concurrent_vector\concurrent_queue

Reference:

    1. My coding.net
    2. Intel Thread Building Blocks (TBB) Introductory article
    3. TBB (Intel threading Building Blocks) Learning notes
    4. A detailed description of lambda expressions in C + +

Introduction and use of TBB

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.