参考博客
MacOS
https://macappstore.org/tbb/Linux
https://blog.csdn.net/Twilightzr/article/details/126533557
代码仓库
https://github.com/oneapi-src/oneTBB
安装展示
MacOS
命令行输入
1 brew install tbbView Code
截图展示
检查是否成功安装好
1 ls /usr/local/include/tbb 2 # 如果成功下载后,就会在 /usr/local/include下出现 tbb文件夹View Code
Linux
1、获取其代码仓库(外网)
方式1:git clone
1 git clone https://github.com/oneapi-src/oneTBBView Code
方式2:release 直接点击下载
2、TBBConfig.cmake 配置 复制到系统中cmake
1 #1.先去对应的系统中library中创建/cmake/TBB 2 cd /usr/local/lib 3 mkdir cmake && cd cmake 4 mkdir TBB 5 6 #2.再去 oneapi-tbb-2021.11.0/lib/cmake/中 7 cd oneapi-tbb-2021.11.0/lib/cmake 8 9 #3.修改文档 10 vim TBBConfig.make 11 #里面有一个 TBB_FOUND FALSE -> TRUE 12 13 #4.复制到 /usr/local/lib/cmake/TBB 14 cp TBBConfig*.cmake /usr/local/lib/cmake/TBB/. 15 16 #5.检查 17 ls /usr/local/lib/cmake/TBB/View Code
3、TBBConfig.cmake 配置 复制到系统中cmake
3.1 查看自己是系统是32位还是64位
3.2 复制到/usr/local/lib/*.so
1 # 1. 到相应的目录下 2 cd /oneapi-tbb-2021.11.0/lib 3 # 2. 查看自己系统是多少位系统 4 uname -m #x86_64 = 64位 | ( i686 | i386 ) = 32位 5 # 3. 去相应的文件夹 64位去intel64 ; 32位就去ia32 6 cd intel64/gcc4.8 7 # 4. 复制到/usr/local/lib 8 cp *.so* /usr/local/lib 9 # 5. 检查是否 成功复制了? 10 ls /usr/local/lib/*tbb* | wc -lView Code
4、复制 头文件*.h 到 include下
1 # 1. 进入相应的目录下 2 cd oneapi-tbb-2021.11.0/include/oneapi 3 # 2. 复制目录下的东西至/usr/local/include即可 4 cp -rf ./* /usr/local/include 5 # 3. 检查一下是否 成功复制到 6 ls /usr/local/include/tbbView Code
测试
CMakeList.txt
1 cmake_minimum_required(VERSION 3.12) 2 project(tbbDemo) 3 4 set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH}) 5 SET(CMAKE_C_COMPILER "/usr/bin/gcc") 6 SET(CMAKE_CXX_COMPILER "/usr/bin/g++") 7 8 set(CMAKE_CXX_STANDARD 17) 9 10 find_package(OpenMP) 11 find_package(TBB REQUIRED) 12 13 add_compile_options(-O0 -Wall) 14 15 16 add_executable(example_mt example/example_multithread.cpp) 17 target_link_libraries(example_mt PUBLIC OpenMP::OpenMP_CXX TBB::tbb)View Code
example_multithread.cpp
1 #include "tbb/blocked_range.h" 2 #include "tbb/parallel_for.h" 3 #include "tbb/task_scheduler_observer.h" 4 5 #include <iostream> 6 #include <vector> 7 8 using namespace std ; 9 10 struct mytask { 11 mytask(size_t n) 12 :_n(n) 13 {} 14 void operator()() { 15 for (int i=0;i<1000000;++i) {} // Deliberately run slow 16 std::cerr << "[" << _n << "]"; 17 } 18 size_t _n; 19 }; 20 21 struct executor 22 { 23 executor(std::vector<mytask>& t) 24 :_tasks(t) 25 {} 26 executor(executor& e,tbb::split) 27 :_tasks(e._tasks) 28 {} 29 30 void operator()(const tbb::blocked_range<size_t>& r) const { 31 for (size_t i=r.begin();i!=r.end();++i) 32 _tasks[i](); 33 } 34 35 std::vector<mytask>& _tasks; 36 }; 37 38 int main(int,char**) { 39 40 tbb::task_scheduler_observer init; // Automatic number of threads 41 // tbb::task_scheduler_init init(2); // Explicit number of threads 42 43 std::vector<mytask> tasks; 44 for (int i=0;i<1000;++i) 45 tasks.push_back(mytask(i)); 46 47 executor exec(tasks); 48 tbb::parallel_for(tbb::blocked_range<size_t>(0,tasks.size()),exec); 49 std::cerr << std::endl; 50 cout<< "TEST:: Successful !!!"<<endl; 51 return 0; 52 }View Code
成功运行结果
推荐书目
标签:MacOS,include,cmake,lib,OneTBB,usr,Linux,tbb,local From: https://www.cnblogs.com/Osea/p/17854750.html