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?
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With