TBB

本文深入探讨了TBB(Threading Building Blocks)库,它是一种用于C++的并行编程工具,旨在简化多核处理器上的并发任务。通过TBB,开发者可以高效地利用系统资源,实现高性能的并行算法。文章详细介绍了TBB的主要组件,如任务调度器、并行算法和数据结构,并提供了实例来展示其用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<iostream>
#include<tbb/tbb.h>
#include<sys/time.h>
const size_t end = 1000000;
int is_prime(int x)
{
    if(x<=1)
        return 0;
    else
    {
        for(int i=2;i<=x/2;i++)
        {
            if(x%i==0)
                return 0;
        }
        return 1;
    }
}

class FindPrime{
public:
    FindPrime(){
        //std::cout<<"start find prime..."<<std::endl;
    }
    void operator() (const tbb::blocked_range<size_t> &r)const{
            //std::cout<<"begin:"<<r.begin()<<" "<<"end:"<<r.end()<<std::endl;
            for(size_t i= r.begin();i!=r.end();i++)
            {
                if(is_prime(i))
                {
                    std::cout<<i<<std::endl;
                }
            }

    }

};
void findPrime()
{
    for(int i=2;i<end;i++)
    {
        if(is_prime(i))
        {
            std::cout<<i<<std::endl;
        }
    }

}
int main()
{
    timeval start_time_p,end_time_p,start_time,end_time;


    gettimeofday(&start_time_p,0);
    tbb::parallel_for(tbb::blocked_range<size_t>(0,end),FindPrime());
    gettimeofday(&end_time_p,0);

    gettimeofday(&start_time,0);
    findPrime();
    gettimeofday(&end_time,0);

    long time_use = 1000000*(end_time.tv_sec-start_time.tv_sec)+end_time.tv_usec-start_time.tv_usec;
    time_use = time_use;
    std::cout<<"no parallel use time:"<<time_use<<std::endl;
    long time_use_p = 1000000*(end_time_p.tv_sec-start_time_p.tv_sec)+end_time_p.tv_usec-start_time_p.tv_usec;
    time_use_p = time_use_p;
    std::cout<<"parallel use time:"<<time_use_p<<std::endl;

    return 0;

}
### Intel Threading Building Blocks Overview Intel Threading Building Blocks (TBB) is a widely used C++ template library that simplifies the process of adding parallelism to applications. TBB provides developers with powerful abstractions and tools for creating scalable, high-performance multithreaded applications without delving into low-level thread management. #### Key Features - **Scalable Parallel Algorithms**: TBB offers a rich set of algorithms designed specifically for parallel execution on multi-core processors. - **Task-based Programming Model**: Instead of managing threads directly, programmers define tasks which are automatically scheduled by TBB’s runtime system[^1]. - **Efficient Resource Management**: The library includes sophisticated mechanisms for load balancing and resource allocation ensuring optimal performance across different hardware configurations. #### Integration and Compatibility TBB integrates seamlessly with other development environments and libraries such as OpenMP or MPI. This makes it easier for developers who wish to combine multiple concurrency models within their projects[^2]. #### Performance Optimization Techniques To achieve maximum efficiency, TBB employs advanced techniques like work stealing schedulers where idle cores can steal pending tasks from busy ones thus maintaining an even distribution of workload throughout all available processing units[^3]. ```cpp #include <tbb/task_group.h> using namespace tbb; void perform_computation() { task_group g; g.run([]{ /* Task 1 */ }); g.run([]{ /* Task 2 */ }); g.wait(); // Wait until both tasks complete } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值