why parfor is much slower than for when the overhead is negligible?

1 Ansicht (letzte 30 Tage)
Jiangmin zhang
Jiangmin zhang am 14 Sep. 2011
i have a code shown below. it costs 660 seconds with regular for
however, with parfor, it costs more than half an hour. The point is that the overhead is negligible.
==================
clear all; close all; clc; tic
Nx=100; Ny=100;
plist=0.05:0.05:0.95;
linked_list=zeros(1,length(plist));
num_sample=20000;
matlabpool open 3
parfor sss=1:length(plist)
sss
p=plist(sss);
numlink=0;
for s10=1:num_sample
pattern=(rand(Ny,Nx)<p);
pattern2=zeros(Ny,Nx);
new=zeros(2,10000);
new2=zeros(2,10000);
num_new=0;
num_new2=0;
flag=0;
found=0;
s=0;
while (found==0)&&(s<Nx)
s=s+1;
if (pattern(1,s)==1)&&(pattern2(1,s)==0);
flag=flag+1;
pattern2(1,s)=flag;
num_new=1;
new(1,1)=s;
new(2,1)=1;
num_new2=0;
while num_new>0
num_new2=0;
for s1=1:num_new
x=new(1,s1);
y=new(2,s1);
if (x>1)&&(pattern(y,x-1)==1)&&(pattern2(y,x-1)~=flag)
num_new2=num_new2+1;
new2(1,num_new2)=x-1;
new2(2,num_new2)=y;
pattern2(y,x-1)=flag;
end
if (x<Nx)&&(pattern(y,x+1)==1)&&(pattern2(y,x+1)~=flag)
num_new2=num_new2+1;
new2(1,num_new2)=x+1;
new2(2,num_new2)=y;
pattern2(y,x+1)=flag;
end
if (y>1)&&(pattern(y-1,x)==1)&&(pattern2(y-1,x)~=flag)
num_new2=num_new2+1;
new2(1,num_new2)=x;
new2(2,num_new2)=y-1;
pattern2(y-1,x)=flag;
end
if (y<Ny)&&(pattern(y+1,x)==1)&&(pattern2(y+1,x)~=flag)
num_new2=num_new2+1;
new2(1,num_new2)=x;
new2(2,num_new2)=y+1;
pattern2(y+1,x)=flag;
end
end
num_new=num_new2;
new(:,1:num_new)=new2(:,1:num_new);
if max(new(2,1:num_new))==Ny
found=1;
break;
end
end
end
end
numlink=numlink+found;
end
linked_list(sss)=numlink/num_sample;
end
if matlabpool('size') > 0
matlabpool close
end
plot(plist,linked_list,'*')
toc

Antworten (1)

Walter Roberson
Walter Roberson am 14 Sep. 2011
The overhead for parfor is not negligible. There is a lot of cpu or core-level synchronization that has to happen as the bits of work get dispatched and the result copied back. That interferes a lot compared to a single cpu simply looping through items that are fully in primary cache.
  2 Kommentare
Jiangmin zhang
Jiangmin zhang am 14 Sep. 2011
but for my code, it is negligible
just try it on your computer
Walter Roberson
Walter Roberson am 14 Sep. 2011
How do you measure the overhead?
Which MATLAB version are you using?

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Parallel for-Loops (parfor) finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by