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

htrh5
htrh5 2015년 8월 4일
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?
James Tursa
James Tursa 2015년 8월 4일
편집: James Tursa 2015년 8월 4일
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
htrh5 2015년 8월 4일
There is no way to write parallel for loops in C++ without paying money for compilers? That doesn't sound right.
James Tursa
James Tursa 2015년 8월 4일
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
htrh5 2015년 8월 4일
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 2015년 8월 4일
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