parallel for loop in mex

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++)'.

回答(1 个)

James Tursa
James Tursa 2015-8-4

0 个投票

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 个评论

Okay, what must I do? matlab itself can run parfor loops. Is this OpenMP something I can simply install? Where would I learn how to make matlab to use that instead of sdk 7.1?
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?
There is no way to write parallel for loops in C++ without paying money for compilers? That doesn't sound right.
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:
maybe I have access to intel c++ compiler, if I installed that and changed matlab's compiler to that one, would OpenMP work then?
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.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Call C++ from MATLAB 的更多信息

产品

提问:

2015-8-4

评论:

2015-8-4

Community Treasure Hunt

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

Start Hunting!

Translated by