我是OpenMP的新手。我有以下代码,使用配置了MSVS2010的Matlab mex进行编译。计算机有8个处理器(我也使用matlabpool检查过)。
#include "mex.h"
#include <omp.h>
typedef unsigned char uchar;
typedef unsigned int uint;
//Takes a uint8 input array and uint32 index array and preallocated uint8 array the same
//size as the first one and copies the data over using the indexed mapping
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray*prhs[] )
{
uint N = mxGetN(prhs[0]);
mexPrintf("n=%i\n", N); mexEvalString("drawnow");
uchar *input = (uchar*)mxGetData(prhs[0]);
uint *index = (uint*)mxGetData(prhs[1]);
uchar *output = (uchar*)mxGetData(prhs[2]);
uint nThreads, tid;
#pragma omp parallel private(tid) shared(input, index, output, N, nThreads) num_threads(8)
{
tid = omp_get_thread_num();
if (tid==0) {
nThreads = omp_get_num_threads();
}
for (int i=tid*N/nThreads;i<tid*N/nThreads+N/nThreads;i++){
output[i]=input[index[i]];
}
}
mexPrintf("nThreads = %i\n",nThreads);mexEvalString("drawnow");
}
我得到的输出是
n=600000000
nThreads = 1
为什么只有一个线程被创建,尽管我请求8?
答案 0 :(得分:10)
叹息。典型的,花费数小时尝试和失败,然后在发布到SO后5分钟找到答案。
该文件需要使用openmp支持
mex mexIndexedCopy.cpp COMPFLAGS="/openmp $COMPFLAGS"