编辑:< Matlab限制TBB但不限制OpenMP >我的问题与上面的问题不同,虽然使用相同的示例代码进行说明,但它并没有重复.在我的情况下,我在tbb初始化中指定了多个线程,而不是使用"deferred".另外我在谈论cx中TBB与TBM中TBB之间的奇怪行为.该问题的答案仅演示了在C++中运行TBB时的线程初始化,而不是在MEX中.
我正在尝试提升Matlab mex文件以提高性能.在mex中使用TBB时遇到的奇怪之处是TBB初始化不能按预期工作.
这个C++程序执行100%的cpu使用,并且在单独执行时有15个TBB线程:
main.cpp中
#include "tbb/parallel_for_each.h"
#include "tbb/task_scheduler_init.h"
#include <iostream>
#include <vector>
#include "mex.h"
struct mytask {
mytask(size_t n)
:_n(n)
{}
void operator()() {
for (long i=0;i<10000000000L;++i) {} // Deliberately run slow
std::cerr << "[" << _n << "]";
}
size_t _n;
};
template <typename T> struct invoker {
void operator()(T& it) const {it();}
};
void mexFunction(/* int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[] */) {
tbb::task_scheduler_init init(15); // 15 threads …Run Code Online (Sandbox Code Playgroud)