为什么在mex文件中的OpenMP只产生1个线程?

twe*_*ter 8 parallel-processing matlab openmp mex

我是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");
}
Run Code Online (Sandbox Code Playgroud)

我得到的输出是

n=600000000
nThreads = 1
Run Code Online (Sandbox Code Playgroud)

尽管我要求8,但为什么只创建了一个线程?

twe*_*ter 10

叹.典型的,花费数小时尝试和失败,然后在发布到SO后5分钟找到答案.

该文件需要使用openmp支持

mex mexIndexedCopy.cpp COMPFLAGS="/openmp $COMPFLAGS"
Run Code Online (Sandbox Code Playgroud)

  • @linello是的.我实际上只是浪费了几个小时,因为我没有正确地传递`-fopenmp`.您需要将它传递给编译器和链接器.`mex CXXFLAGS ="\ $ CXXFLAGS -fopenmp"LDFLAGS ="\ $ LDFLAGS -fopenmp"[其他选项] <file.cpp>`for C++.(对于C,使用`CFLAGS`而不是`CXXFLAGS`;对于C和C++,使用两者.) (2认同)