Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is OpenMP in a mex file only producing 1 thread?

I am new to OpenMP. I have the following code which compiles fine using Matlab mex configured with MSVS2010. The computer has 8 processors available (which I checked also by using 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");
}

The output I get is

n=600000000
nThreads = 1

Why is only one thread being created despite me requesting 8?

like image 395
twerdster Avatar asked Nov 07 '11 00:11

twerdster


1 Answers

Sigh. Typical, spend hours trying and failing and then find the answer 5 minutes after posting to SO.

The file needs to be mexed with openmp support

mex mexIndexedCopy.cpp COMPFLAGS="/openmp $COMPFLAGS"
like image 132
twerdster Avatar answered Oct 15 '22 06:10

twerdster