Thursday, August 14, 2014

OpenMP from Matlab

To get OpenMP support for a Matlab mex function you need to use the following compiler flags. Suppose testp.cpp is your cpp file with the mex function, you need to compile as follows:
mex testp.cpp CFLAGS="\$CFLAGS -fopenmp" LDFLAGS="\$LDFLAGS -fopenmp" CXXOPTIMFLAGS="\$CXXOPTIMFLAGS -fopenmp".
Here is a simple mex function to test this.
//testp.cpp
#include
#include "mex.h"

void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray*prhs[] )
{
unsigned int threadid, nthreads;
#pragma omp parallel private(threadid) shared(nthreads) num_threads(8)
{
threadid = omp_get_thread_num();

if (threadid==0) {
nthreads = omp_get_num_threads();

}
}
mexPrintf("num_threads = %d\n",nthreads);
}

1 comment:

Anonymous said...

Nice blog..! I really loved reading through this article. Thanks for sharing such
a amazing post with us and keep blogging...Well written article Thank You for Sharing with Us pmp training class in chennai | pmp training fee | project management training certification | project management training in chennai