1. TBB简介

TBB ( Thread Building Blocks, 线程构建模块) 是Intel公司开发的并行编程开发的工具。它支持Windows,OS X, Linux平台,支持的编译器有Visual C++ (version 8.0 or higher, on Windows only), Intel C++ Compiler (version 11.1 or higher) or the GNU Compiler Collection (gcc).

TBB is a collection of components for parallel programming:

  • Basic algorithms: parallel_for, parallel_reduce, parallel_scan
  • Advanced algorithms: parallel_while, parallel_do, parallel_pipeline, parallel_sort
  • Containers: concurrent_queue, concurrent_priority_queue, concurrent_vector, concurrent_hash_map
  • Scalable memory allocation: scalable_malloc, scalable_free, scalable_realloc, scalable_calloc, scalable_allocator, cache_aligned_allocator
  • Mutual exclusion: mutex, spin_mutex, queuing_mutex, spin_rw_mutex, queuing_rw_mutex, recursive_mutex
  • Atomic operations: fetch_and_add, fetch_and_increment, fetch_and_decrement, compare_and_swap, fetch_and_store
  • Timing: portable fine grained global time stamp
  • Task Scheduler: direct access to control the creation and activation of tasks

2. 应用场景

  1. 数据包处理: 如 network router emulator
  2. 图像处理,和OPENCV,IPP一块使用,如 IPP with TBB
  3. 任何可并行的地方

3. 配置

1. 下载地址:https://www.threadingbuildingblocks.org/download

下载:windows release版tbb43_20150424oss_win.zip
- 配置环境变量PATH,保证运行时找到dll
- 在TBB应用工程中添加tbb包含目录,即tbb相关头文件
- 在TBB应用工程中添加tbb库目录,即tbb的lib文件,注意32位和64位有分别的目录

4. 实例

问题:查找一个范围内的所有素数,子问题是判断一个数是不是素数,而且每个数字的判断互不影响,所以可以用并行算法

  find_prime.cc

/*
** find_prime.cc
** g++ find_prime.cc -ltbb -lrt -o find_prime
** -ltbb for tbb library
** -lrt for tbb::tick_count::now() using clock_gettime()
*/
#include<iostream>
#include<tbb/tbb.h>
using namespace std;
using namespace tbb;
int is_prime(int x)
{
int i;
if (x <= ) { /*1不是質數,且不考慮負整數與0,故輸入x<=1時輸出為假 */
return ;
}
for (i = ; i * i <= x; ++i) {
if (x % i == ) { /*若整除時輸出為假,否則輸出為真 */
return ;
}
}
return ;
}
class FindPrime {
public:
void operator() (const blocked_range < size_t > &r)const {
for (size_t i = r.begin(); i != r.end(); ++i) {
if (is_prime(i)) {
cout << i << endl;
}
}
}
};
int main(int argc, char *argv[])
{
size_t end = ;
if (argc > && atoi(argv[]) > ) {
end = atoi(argv[]);
}
parallel_for(blocked_range < size_t > (, end), FindPrime());
return ;
}

编译

  g++ find_prime.cc -ltbb -lrt -o find_prime

5. C++相关特性

0. operator overloading

parallel_for第二参数是一个类A的实例对象,该类A要重载操作符 () .

对于

	class Test{
public:
void operator()(const int &i) const{
cout<<"operation with i" <<endl;
}
};
第一个const保证i不被改变,第二个const保证调用对象不被改变。
使用方法
Test t;
t(110);

1. lambda表达式

匿名函数在C++11中引用,语法是

    [capture子句](参数列表)mutable throw() -> int{函数体}

最简单的一个例子,没有参数,没有异常处理,没有返回类型(自动推断)

    []{cout<<"hello world from lambda!"<<endl;}();

最后的 () 是对匿名函数的调用,分开来看是这样的,首先定义一个函数指针

    typedef void (*func)();
func f = []{cout<<"hello world from lambda!"<<endl;};
f();

capture子句主要有两个符号 = , &= 表示以传值的方式传递参数, & 表示以引用的方式(传地址)传递参数,这两个符号可以单独使用,也可以组合使用, & 后面还可以跟一个变量,只修饰它。

	[]  // 没有定义任何变数。使用未定义变数会导致错误。
[x, &y] // x以传值方式传入(预设),y以传地址方式传入。
[&] // 任何被使用到的外部变数皆隐式地以地址方式加以引用。
[=] // 任何被使用到的外部变数皆隐式地以传值方式加以引用。
[&, x] // x显示地以传值方式加以引用。其馀变数以地址方式加以引用。
[=, &z] // z显示地以地址方式加以引用。其馀变数以传值方式加以引用。

2. placement new

所谓placement new就是在用户指定的内存位置上构建新的对象,这个构建过程不需要额外分配内存,只需要调用对象的构造函数即可。

Task Scheduler 库会用到这一特性

  #include <iostream>
#include "tbb/tbb.h"
using namespace tbb;
using namespace std; class first_task:public task {
public:
task * execute() {
cout << "Hello World!\n";
return NULL;
}
}; int main()
{
task_scheduler_init init(task_scheduler_init::automatic);
first_task & f1 = *new(tbb::task::allocate_root())first_task();
tbb::task::spawn_root_and_wait(f1);
return 0;
}

endl;

参考博客:http://www.tuicool.com/articles/R3uu22

TBB的学习的更多相关文章

  1. Opencv改变图像亮度和对比度以及优化

    https://blog.csdn.net/u013139259/article/details/52145377 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.cs ...

  2. TBB 学习笔记

    #include <tbb/task_scheduler_init.h> #include <tbb/blocked_range.h> #include <tbb/par ...

  3. 值得学习的C语言开源项目

    值得学习的C语言开源项目   - 1. Webbench Webbench是一个在linux下使用的非常简单的网站压测工具.它使用fork()模拟多个客户端同时访问我们设定的URL,测试网站在压力下工 ...

  4. opencv 3.0 DPM Cascade 检测 (附带TBB和openMP加速)

    opencv 3.0 DPM cascade contrib模块 转载请注明出处,楼燚(yì)航的blog,http://www.cnblogs.com/louyihang-loves-baiyan/ ...

  5. JMeter学习-012-JMeter 配置元件之-HTTP Cookie管理器-实现 Cookie 登录

    前文我们讲过了若何获取登录后的 Cookie 信息,不知如何获取登录 Cookie 的朋友,敬请参阅我之前写的博文:Fiddler-005-获取 Cookie 信息.参阅上篇文章,获取到 Cookie ...

  6. 学习OpenCV——OpenMP

    转自:http://www.cnblogs.com/yangyangcv/archive/2012/03/23/2413335.html openMP的一点使用经验   最近在看多核编程.简单来说,由 ...

  7. TBB入门

    获取TBB TBB的官方网站在http://threadingbuildingblocks.org/,可以在它的Downloads页面里找到Commercial Aligned Release,最新版 ...

  8. 值得学习的C/C++开源框架(转)

    值得学习的C语言开源项目 - 1. Webbench Webbench是一个在linux下使用的非常简单的网站压测工具.它使用fork()模拟多个客户端同时访问我们设定的URL,测试网站在压力下工作的 ...

  9. 《Ray Tracing in One Weekend》、《Ray Tracing from the Ground Up》读后感以及光线追踪学习推荐

    <Ray Tracing in One Weekend> 优点: 相对简单易懂 渲染效果相当好 代码简短,只看书上的代码就可以写出完整的程序,而且Github上的代码是将基类与之类写在一起 ...

随机推荐

  1. 2D情况下,复数的意义代表旋转

    4 x i x i = - 4 就是"4"在数轴上旋转了180度. 那么4 x i = 4i 就旋转了90度. 复数的意义就表示旋转 乘以-1,表示x正半轴的数,围绕原点,逆时针偏 ...

  2. 案例情景--在一次Oracle 数据库导出时 EXP-00008;ORA-00904:EXP-00000: oracle不同版本导入导出规则

    案例情景--在一次Oracle 数据库导出时: C:\Documents and Settings\Administrator>exp lsxy/lsxy@lsxy_db file=E:\lsx ...

  3. RPG难题

    /* 人称“AC女之杀手”的超级偶像LELE最近忽然玩起了深沉,这可急坏了众多“Cole”(LELE的粉丝,即"可乐"),经过多方打探,某资深Cole终于知道了原因,原来,LELE ...

  4. 十年百度工作心得(月薪75k)

    百度,是多少从事IT事业的程序员梦寐以求的地方,能进入这样大厂的程序员可以说都是数一数二的人才. 最近有不少朋友问,成为百度,腾讯,阿里Java架构师需要系统学习哪些Java技术. 下面分享互联网Ja ...

  5. apicloud模块开发知识点

    1. 没有加模块的时候dex里面的包 \android\support\annotation \android\support\v4 \com\uzmap\pkg \compile 2. 不能混淆的类 ...

  6. callable与runable区别?switch char ?sql只查是否存在,sql复制表 ?反射 ? spring mvc 和spring 上下文区别?

    中化技术部  2018.4.16 1. callable 和 thread 区别 实现Callable接口的线程能返回执行结果,而Runable 不可以 . Callable 的call方法允许抛出异 ...

  7. EditText输入小数

    edtValue.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);

  8. IPutils

    package com.mmall.util; import lombok.extern.slf4j.Slf4j;import org.apache.commons.lang3.StringUtils ...

  9. PHP字符串大小写转换函数

    string strtolower(string $str) 将字符串转换为小写 string strtoupper(string $str) 将字符串转换为大写 $str1 = 'html'; $s ...

  10. python文件打包

    python文件打包 先安装 pip3 install --upgrade pyinstaller 进入到文件的当前目录 ,在命令行中输入: pyinstaller -F -w[文件名].py 其中- ...