parallel for loop in mex
Show older comments
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
on 4 Aug 2015
0 votes
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
on 4 Aug 2015
James Tursa
on 4 Aug 2015
Edited: James Tursa
on 4 Aug 2015
To run MATLAB parfor loops you need to purchase the Parallel Computing Toolbox. But parfor is not something you would use in a mex routine. To use OpenMP in a mex routine, you need to purchase a compiler with that capability. E.g., one of the Microsoft Professional/Ultimate/Etc editions.
Are you simply looking for speed improvements for some critical part of your code?
htrh5
on 4 Aug 2015
James Tursa
on 4 Aug 2015
OpenMP is a relatively easy way to do it, but it is not the only way. Another way is to write your own threading code. For that I would suggest looking at some examples in the FEX to see what others have done. E.g., here is one example:
htrh5
on 4 Aug 2015
htrh5
on 4 Aug 2015
Categories
Find more on Write C Functions Callable from MATLAB (MEX Files) 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!