↓ 跳过正文
  1. Posts/

intel tbb 学习笔记

·375 字·1 分钟

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>
10
11using namespace std;
12using namespace tbb;
13
14const int N=1E9+7;
15float a[N+5];
16void Foo(float &x) { x -= 100; }
17void SerialApplyFoo( float a[], size_t n ) {
18for( size_t i=0; i!=n; ++i )
19Foo(a[i]);
20}
21
22class ApplyFoo
23{
24    float *const my_a;
25
26  public:
27    void operator()(const blocked_range<size_t> &r) const
28    {
29        float *a = my_a;
30        for (size_t i = r.begin(); i != r.end(); ++i)
31        {
32
33            Foo(a[i]);
34            //printf("%.4f\n", a[i]);
35        }
36    }
37    ApplyFoo(float a[]) : my_a(a)
38    {
39    }
40};
41
42void ParallelApplyFoo(float a[], size_t n)
43{
44    parallel_for(blocked_range<size_t>(0, n), ApplyFoo(a));
45}
46
47void print(float a[])
48{
49    for (int i = 0; i < N; i++)
50        printf("%.4f%c", a[i], i == 9 ? '\n' : ' ');
51}
52int main()
53{
54    for (int i = 0; i < N; i++)
55        a[i] = 1. * i;
56
57    auto t1 = std::chrono::high_resolution_clock::now();
58    SerialApplyFoo(a,N);
59
60    //ParallelApplyFoo(a, N);
61    auto t2 = std::chrono::high_resolution_clock::now();
62    std::chrono::duration<double, std::milli> fp_ms = t2 - t1;
63     std::cout << "f() took " << fp_ms.count() << " ms, "<<std::endl;
64
65    //print(a);
66    return 0;
67}

编译选项为: g++ -std=c++11 parallel_for.cpp -L/home/sensetime/workspace/graph/3rdparty/tbb/lib/intel64/gcc4.7 -ltbb -o parallel_for

相关文章

C++ 记录代码运行时间

·169 字·1 分钟
以前用的办法太老土啦 看到一个since C++11的方法,我觉得比较优雅 1 #include <iostream> 2 #include <chrono> 3 //#include <ratio> 4 #include <thread> 5 6 void f() 7 { 8 std::this_thread::sleep_for(std::chrono::seconds(1)); 9 } 10 11 int main() 12 { 13 auto t1 = std::chrono::high_resolution_clock::now(); 14 f(); 15 auto t2 = std::chrono::high_resolution_clock::now(); 16 17 // floating-point duration: no duration_cast needed 18 std::chrono::duration<double, std::milli> fp_ms = t2 - t1; 19 20 // integral duration: requires duration_cast 21 auto int_ms = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1); 22 23 // converting integral duration to integral duration of shorter divisible time unit: 24 // no duration_cast needed 25 std::chrono::duration<long, std::micro> int_usec = int_ms; 26 27 std::cout << "f() took " << fp_ms.count() << " ms, " 28 << "or " << int_ms.count() << " whole milliseconds " 29 << "(which is " << int_usec.count() << " whole microseconds)" << std::endl; 30 }

gdb学习笔记

·235 字·1 分钟
用gdb调试c++的时候,需要添加-g编译选项add_compile_options(-g),并且关掉各种编译优化 如果是多线程程序,可以用info threads 查看每个线程的信息

linux 下C++ 连接mysql 数据库

·243 字·1 分钟
资料推荐这个:MySQL C API programming tutorial 环境为ubuntu 14.04 lts 需要安装mysql 和mysql 开发包 sudo apt-get install libmysqlclient15-dev mysql-server mysql-client 先在mysql 中建立test数据库和test表格 mysql>create database test; mysql>use test; //切换到test数据库中 mysql> create table test(name varchar(255),num int(10) ); //创建一个叫test的表 mysql>show create table test; //显示刚才创建的表信息 mysql> select * from test; //查询test表中数据 mysql>quit 然后用如下cpp代码连接

使用haproxy中转酸酸流量

·200 字·1 分钟
一个国内vps,一个国外vps. 前提是国外vps已经配置好。 接下来,我们在国内vps上安装haproxy yum -y install haproxy 或者 apt-get install haproxy 然后修改配置文件,位置在/etc/haproxy/haproxy.cfg