intel tbb 学习笔记
tbb是**Threading Building Blocks library的缩写,**是一个为开发者提供并行解决方案的库.
先放个文档https://www.threadingbuildingblocks.org/intel-tbb-tutorial
再放一个代码示例:
1/* ***********************************************
2Author :111qqz
3mail: renkuanze@sensetime.com
4Created Time :2018年07月18日 星期三 14时20分54秒
5File Name :parallel_for.cpp
6************************************************ */
7#include <iostream>
8#include "tbb/tbb.h"
9#include <chrono>
using namespace std;
using namespace tbb;
1const int N=1E9+7;
2float a[N+5];
3void Foo(float &x) { x -= 100; }
4void SerialApplyFoo( float a[], size_t n ) {
5for( size_t i=0; i!=n; ++i )
6Foo(a[i]);
7}
1class ApplyFoo
2{
3 float *const my_a;
1 public:
2 void operator()(const blocked_range<size_t> &r) const
3 {
4 float *a = my_a;
5 for (size_t i = r.begin(); i != r.end(); ++i)
6 {
1 Foo(a[i]);
2 //printf("%.4f\n", a[i]);
3 }
4 }
5 ApplyFoo(float a[]) : my_a(a)
6 {
7 }
8};
1void ParallelApplyFoo(float a[], size_t n)
2{
3 parallel_for(blocked_range<size_t>(0, n), ApplyFoo(a));
4}
1void print(float a[])
2{
3 for (int i = 0; i < N; i++)
4 printf("%.4f%c", a[i], i == 9 ? '\n' : ' ');
5}
6int main()
7{
8 for (int i = 0; i < N; i++)
9 a[i] = 1. * i;
auto t1 = std::chrono::high_resolution_clock::now();
SerialApplyFoo(a,N);
1 //ParallelApplyFoo(a, N);
2 auto t2 = std::chrono::high_resolution_clock::now();
3 std::chrono::duration<double, std::milli> fp_ms = t2 - t1;
4 std::cout << "f() took " << fp_ms.count() << " ms, "<<std::endl;
1 //print(a);
2 return 0;
3}
编译选项为: g++ -std=c++11 parallel_for.cpp -L/home/sensetime/workspace/graph/3rdparty/tbb/lib/intel64/gcc4.7 -ltbb -o parallel_for