TBB的学习
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. 应用场景
- 数据包处理: 如 network router emulator
- 图像处理,和OPENCV,IPP一块使用,如 IPP with TBB
- 任何可并行的地方
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的学习的更多相关文章
- Opencv改变图像亮度和对比度以及优化
https://blog.csdn.net/u013139259/article/details/52145377 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.cs ...
- TBB 学习笔记
#include <tbb/task_scheduler_init.h> #include <tbb/blocked_range.h> #include <tbb/par ...
- 值得学习的C语言开源项目
值得学习的C语言开源项目 - 1. Webbench Webbench是一个在linux下使用的非常简单的网站压测工具.它使用fork()模拟多个客户端同时访问我们设定的URL,测试网站在压力下工 ...
- opencv 3.0 DPM Cascade 检测 (附带TBB和openMP加速)
opencv 3.0 DPM cascade contrib模块 转载请注明出处,楼燚(yì)航的blog,http://www.cnblogs.com/louyihang-loves-baiyan/ ...
- JMeter学习-012-JMeter 配置元件之-HTTP Cookie管理器-实现 Cookie 登录
前文我们讲过了若何获取登录后的 Cookie 信息,不知如何获取登录 Cookie 的朋友,敬请参阅我之前写的博文:Fiddler-005-获取 Cookie 信息.参阅上篇文章,获取到 Cookie ...
- 学习OpenCV——OpenMP
转自:http://www.cnblogs.com/yangyangcv/archive/2012/03/23/2413335.html openMP的一点使用经验 最近在看多核编程.简单来说,由 ...
- TBB入门
获取TBB TBB的官方网站在http://threadingbuildingblocks.org/,可以在它的Downloads页面里找到Commercial Aligned Release,最新版 ...
- 值得学习的C/C++开源框架(转)
值得学习的C语言开源项目 - 1. Webbench Webbench是一个在linux下使用的非常简单的网站压测工具.它使用fork()模拟多个客户端同时访问我们设定的URL,测试网站在压力下工作的 ...
- 《Ray Tracing in One Weekend》、《Ray Tracing from the Ground Up》读后感以及光线追踪学习推荐
<Ray Tracing in One Weekend> 优点: 相对简单易懂 渲染效果相当好 代码简短,只看书上的代码就可以写出完整的程序,而且Github上的代码是将基类与之类写在一起 ...
随机推荐
- springboot添加fluent日志记录
istio默认会进行日志的记录,但是仅仅记录到服务.以及服务之间调用的信息,不记录业务日志. 如: 所以需要添加业务日志记录. 1.引入依赖 <dependency> <gr ...
- linux命令行下执行循环动作
在当前子目录下分别创建x86_64 for dir in `ls `;do (cd $dir;mkdir x86_64);done
- 201621123008 《Java程序设计》第12周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 2. 面向系统综合设计-图书馆管理系统或购物车 使用流与文件改造你的图书馆管理系统或购物车. 2.1 简述如何 ...
- div同时使用两个class
<p class="con hide">...</p> 1:使用空格分割 2:这个段落将同时应用这两个 class 制定的规则 3:如果二者有重叠,后者覆盖 ...
- IOS初级:app的启动图像
1,准备若干张适合不同设备满屏幕的图像 这里补充说明一下图像的命名方法: iphone4屏幕 Default@2x.png iphone5屏幕 Default-568h@2x.png (568*2即 ...
- Hibernate学习笔记:基础模型类
根据业务建模型时,有一些字段基本每个表都是需要的,如下表: package com.company.jelly.model; import javax.persistence.EntityListen ...
- The Django Book(自定义ModelAdmi类)
默认的,管理界面下显示的东西只是 python2:def __unicode__(self): 和 python3:def __str__(self): 中返回的字段内容 想要让它更加的多元化的话 c ...
- bootstrap 中 css 与 javascript 的使用
1.css 1.1.选择器 1.2.子选择器: css 里的子元素用符号'>'表示.如下示例是表示拥有table样式的表盒,其thead元素内的tr元素如果有th的话,则应用该样式. .tabl ...
- flex布局中的主轴和侧轴的确定
1.主轴和侧轴是通过flex-direction确定的 如果flex-direction是row或者row-reverse,那么主轴就是justify-contain 如果flex-direction ...
- Codeforces Round #524 (Div. 2) F. Katya and Segments Sets(主席树)
https://codeforces.com/contest/1080/problem/F 题意 有k个区间,区间的种类有n种,有m个询问(n,m<=1e5,k<=3e5),每次询问a,b ...