【OneTBB】MacOS&Linux环境下配置OneTBB库
参考博客
MacOS
Linux
代码仓库
安装展示
MacOS
命令行输入
brew install tbb
截图展示
检查是否成功安装好
ls /usr/local/include/tbb # 如果成功下载后,就会在 /usr/local/include下出现 tbb文件夹
Linux
1、获取其代码仓库(外网)
方式1:git clone
git clone https://github.com/oneapi-src/oneTBB
方式2:release 直接点击下载
2、TBBConfig.cmake 配置 复制到系统中cmake
#1.先去对应的系统中library中创建/cmake/TBB cd /usr/local/lib mkdir cmake && cd cmake mkdir TBB #2.再去 oneapi-tbb-2021.11.0/lib/cmake/中 cd oneapi-tbb-2021.11.0/lib/cmake #3.修改文档 vim TBBConfig.make #里面有一个 TBB_FOUND FALSE -> TRUE #4.复制到 /usr/local/lib/cmake/TBB cp TBBConfig*.cmake /usr/local/lib/cmake/TBB/. #5.检查 ls /usr/local/lib/cmake/TBB/
3、TBBConfig.cmake 配置 复制到系统中cmake
3.1 查看自己是系统是32位还是64位
3.2 复制到/usr/local/lib/*.so
# 1. 到相应的目录下 cd /oneapi-tbb-2021.11.0/lib # 2. 查看自己系统是多少位系统 uname -m #x86_64 = 64位 | ( i686 | i386 ) = 32位 # 3. 去相应的文件夹 64位去intel64 ; 32位就去ia32 cd intel64/gcc4.8 # 4. 复制到/usr/local/lib cp *.so* /usr/local/lib # 5. 检查是否 成功复制了? ls /usr/local/lib/*tbb* | wc -l
4、复制 头文件*.h 到 include下
# 1. 进入相应的目录下 cd oneapi-tbb-2021.11.0/include/oneapi # 2. 复制目录下的东西至/usr/local/include即可 cp -rf ./* /usr/local/include # 3. 检查一下是否 成功复制到 ls /usr/local/include/tbb
测试
CMakeList.txt
cmake_minimum_required(VERSION 3.12) project(tbbDemo) set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH}) SET(CMAKE_C_COMPILER "/usr/bin/gcc") SET(CMAKE_CXX_COMPILER "/usr/bin/g++") set(CMAKE_CXX_STANDARD 17) find_package(OpenMP) find_package(TBB REQUIRED) add_compile_options(-O0 -Wall) add_executable(example_mt example/example_multithread.cpp) target_link_libraries(example_mt PUBLIC OpenMP::OpenMP_CXX TBB::tbb)
example_multithread.cpp
#include "tbb/blocked_range.h" #include "tbb/parallel_for.h" #include "tbb/task_scheduler_observer.h" #include <iostream> #include <vector> using namespace std ; struct mytask { mytask(size_t n) :_n(n) {} void operator()() { for (int i=0;i<1000000;++i) {} // Deliberately run slow std::cerr << "[" << _n << "]"; } size_t _n; }; struct executor { executor(std::vector<mytask>& t) :_tasks(t) {} executor(executor& e,tbb::split) :_tasks(e._tasks) {} void operator()(const tbb::blocked_range<size_t>& r) const { for (size_t i=r.begin();i!=r.end();++i) _tasks[i](); } std::vector<mytask>& _tasks; }; int main(int,char**) { tbb::task_scheduler_observer init; // Automatic number of threads // tbb::task_scheduler_init init(2); // Explicit number of threads std::vector<mytask> tasks; for (int i=0;i<1000;++i) tasks.push_back(mytask(i)); executor exec(tasks); tbb::parallel_for(tbb::blocked_range<size_t>(0,tasks.size()),exec); std::cerr << std::endl; cout<< "TEST:: Successful !!!"<<endl; return 0; }
成功运行结果
推荐书目