I am having problem implementing parfor

I am using parfor to speed up my computation. But instead of getting speed up, it takes significantly longer. I don't know what I am doing wrong and I am new to matlab. Here's the code I have. I implemented the same with GPU and it takes like 20 secs but with two cores of my laptop it take 400s something seconds. And the surprising thing is that, I get answer in 60 secs with single core without parfor.
tic
for j2=1:screenpoints
for i2=1:screenpoints/vertscreen
temp=zeros(specimenpoints,specimenpoints);
for j=1:specimenpoints
parfor i=1:specimenpoints
kernelB=vortexpattern(i,j)*kernel1(i,j)* ...
exp(I*2.0*pi*(((i-specimenpoints/2.0-0.5)* ...
specimensize/specimenpoints- ...
(i2-screenpoints/2.0/vertscreen-0.5)* ...
screensize/screenpoints )^2.0+ ...
((j-specimenpoints/2.0-0.5)*...
specimensize/specimenpoints-...
(j2-screenpoints/2.0-0.5)*...
screensize/screenpoints)^2.0+...
(D-d)^2.0)^(1.0/2.0)/lambda);
temp(i,j)=kernelB;
end
end
kernel2(i2,j2)=sum(sum(temp));
end
end
timefork2=toc

답변 (1개)

Edric Ellis
Edric Ellis 2013년 6월 18일

0 개 추천

Generally, it's best to have the PARFOR loop at the highest level possible because there's an overhead to running each PARFOR loop. In your case, it seems as though you could make the outermost loop be a PARFOR, and revert the innermost loop to a FOR.

댓글 수: 1

Matador
Matador 2013년 6월 19일
Thanks for the response Edric.
I tried that but I still don't get the speed up. Is it because of the fact that the vortexpattern and kernel1 are in the client and it takes longer time transfer those values?(those two are 1000 X 1000 matrices) I also tried to implement the inner loop using arrayfun but I realized the the size input arguments won't be the same in that case. Anyways, thanks for any help in advance

이 질문은 마감되었습니다.

태그

질문:

2013년 6월 17일

마감:

2021년 8월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by