我安装了英特尔线程构建模块。我无法为 XCode 设置环境变量(lib 和包含路径(。
首先,我想编写一个简单的parallel_for程序,我甚至无法在我的程序中添加命名空间tbb
。
谁能帮忙?
在此处输入图像描述
这很简单: 安装它的最佳方法:
brew install tbb
需要自制软件,强烈建议任何想要使用各种开源工具的 Mac 用户使用。
调整 3 个项目设置
之后,brew info tbb
查看安装目录,就我而言
/usr/local/Cellar/tbb/2017_U7
结果为
/usr/local/Cellar/tbb/2017_U7/include/
/usr/local/Cellar/tbb/2017_U7/lib/
对于相应的项目设置,Header Search Paths
和Library Search Paths
。
在Other Linker Flags
输入-ltbb
,你就在那里。
测试代码示例
我已经使用Xcode 8.3中的上述设置验证了此示例
#include "tbb/parallel_for.h"
#include "tbb/task_scheduler_init.h"
#include <iostream>
#include <vector>
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;
};
int main(int,char**) {
//tbb::task_scheduler_init init; // Automatic number of threads
tbb::task_scheduler_init init(tbb::task_scheduler_init::default_num_threads()); // Explicit number of threads
std::vector<mytask> tasks;
for (int i=0;i<1000;++i)
tasks.push_back(mytask(i));
tbb::parallel_for(
tbb::blocked_range<size_t>(0,tasks.size()),
[&tasks](const tbb::blocked_range<size_t>& r) {
for (size_t i=r.begin();i<r.end();++i) tasks[i]();
}
);
std::cerr << std::endl;
return 0;
}