title: Why is OpenMP in a mex file only producing 1 thread?
body: <p>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).</p>
  
  <pre><code>#include "mex.h"
  #include &lt;omp.h&gt;
  
  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&lt;tid*N/nThreads+N/nThreads;i++){
              output[i]=input[index[i]];
          }
      }
      mexPrintf("nThreads = %i\n",nThreads);mexEvalString("drawnow");
  }
  </code></pre>
  
  <p>The output I get is </p>
  
  <pre><code>n=600000000
  nThreads = 1
  </code></pre>
  
  <p>Why is only one thread being created despite me requesting 8?</p>