parallel for loop in mex

4 views (last 30 days)
htrh5
htrh5 on 4 Aug 2015
Commented: htrh5 on 4 Aug 2015
The outer loop in this code should be parallelizable
#include "mex.h"
#include "matrix.h"
#include <math.h>
//[reX imX]=cfrft(rectpuls(t),0.5,linspace(-10,10,2^12),-5:0.01:5);
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[]) {
nlhs=2;
const double pi = 3.1415926535897;
double *f= mxGetPr(prhs[0]);
size_t flen=mxGetNumberOfElements(prhs[0]);
double *ap= mxGetPr(prhs[1]);
double p = (*ap)*pi/2; double cscp = 2/sin(p); double cotp = 1/tan(p);
double *t= mxGetPr(prhs[2]);
// size_t tlen=mxGetNumberOfElements(prhs[2]);
double *w= mxGetPr(prhs[3]);
size_t wlen=mxGetNumberOfElements(prhs[3]);
plhs[0]=mxCreateDoubleMatrix(1, static_cast<int>(wlen), mxREAL);
plhs[1]=mxCreateDoubleMatrix(1, static_cast<int>(wlen), mxREAL);
double *reF = mxGetPr(plhs[0]);
double *imF = mxGetPr(plhs[1]);
double dt=t[1]-t[0];
for (int i = 0;i<wlen;i++){ //<<<<< THIS LOOP
double re=0;
double im=0;
double wc=w[i];
double wc2=wc*wc;
for(int j=0;j<flen;j++){
re += cos(pi*(cotp*wc2-cscp*t[j]*wc+cotp*t[j]*t[j]))*f[j];
im += sin(pi*(cotp*wc2-cscp*t[j]*wc+cotp*t[j]*t[j]))*f[j];
}
reF[i]=re*dt;
imF[i]=im*dt;
}
}
How do I use parallel loops in mex? My compiler is Building with 'Microsoft Windows SDK 7.1 (C++)'.

Answers (1)

James Tursa
James Tursa on 4 Aug 2015
Some Microsoft compilers support OpenMP, but I don't think the vanilla SDK compiler is one of them. Did you mean using OpenMP, or something else?
  6 Comments
htrh5
htrh5 on 4 Aug 2015
maybe I have access to intel c++ compiler, if I installed that and changed matlab's compiler to that one, would OpenMP work then?
htrh5
htrh5 on 4 Aug 2015
I did try writing that code in matlab (and I vectorialized the inner loop using sum()). The function runs about 30% faster when it uses 4 cores. That much difference is not worth keeping 4 extra matlab processes running and waiting about a minute for the first function call to me.

Sign in to comment.

Categories

Find more on MATLAB Compiler in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!