8

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?

twerdster
  • 4,977
  • 3
  • 40
  • 70

1 Answers1

11

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"
twerdster
  • 4,977
  • 3
  • 40
  • 70
  • I know that feel, bro. – CptSupermrkt Dec 18 '13 at 14:51
  • what is the equivalent option under linux with gcc as compiler? `-fopenmp` ? – linello Dec 20 '13 at 09:23
  • 2
    @linello Yes. I actually just wasted a couple hours though because I wasn't passing `-fopenmp` correctly. You need to pass it to both the compiler and the linker. `mex CXXFLAGS="\$CXXFLAGS -fopenmp" LDFLAGS="\$LDFLAGS -fopenmp" [other options] ` for C++. (For C, use `CFLAGS` instead of `CXXFLAGS`; for both C and C++, use both.) – Nicu Stiurca Jan 21 '14 at 21:54
  • 1
    Indeed @SchighSchagh. I a linux machine I had to make sure `-fopenmp` was in every step of the compilation so what worked for me was `mex CFLAGS='$CFLAGS -fopenmp' LDFLAGS='$LDFLAGS -fopenmp' COPTIMFLAGS='$COPTIMFLAGS -fopenmp -O2' LDOPTIMFLAGS='$LDOPTIMFLAGS -fopenmp -O2' DEFINES='$DEFINES -fopenmp' -v functionName.F` where `DEFINES` is only used to make sure `-fopenmp` made it to the first compilation line. – user1008139 Jan 10 '16 at 07:30