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

Respuestas (1)

Edric Ellis
Edric Ellis el 18 de Jun. de 2013

0 votos

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 comentario

Matador
Matador el 19 de Jun. de 2013
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

La pregunta está cerrada.

Etiquetas

Preguntada:

el 17 de Jun. de 2013

Cerrada:

el 20 de Ag. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by