Hello, why does my parallel source code turns out to be slow compared to sequential source code?

this is my sequential source code:
for n = 1:lh hj(2^j*(n-1)+1)=h(n); end
for n = 1:lg
gj(2^j*(n-1)+1)=g(n);
end
a(:,:,1,j+1) = conv2(hj,hj,a(:,:,1,j),'same');
dx(:,:,1,j+1) = conv2(delta,gj,a(:,:,1,j),'same');
dy(:,:,1,j+1) = conv2(gj,delta,a(:,:,1,j),'same');
x = dx(:,:,1,j+1);
y = dy(:,:,1,j+1);
dj(:,:,1,j+1) = sqrt(x.^2+y.^2);
I1 = imadjust(dj(:,:,1,j+1),stretchlim(dj(:,:,1,j+1)),[0 1]);
figure;imshow(I1);
end
and this is my parallel source code:
for j = 1:J+1
lhj = 2^j*(lh-1)+1;
hj=zeros(1, lhj);
hj(1:2^j:end)=h;
a(:,:,1,j+1) = conv2(hj,hj,a(:,:,1,j),'same');
end
parfor j = 1:J+1
lgj = 2^j*(lg-1)+1;
gj=zeros(1, lgj);
gj(1:2^j:end)=g;
atmp=a(:,:,1,j); %excessive optimization perhaps, eliminate one subsref() call
dx(:,:,1,j+1) = conv2(delta,gj,atmp,'same');
dy(:,:,1,j+1) = conv2(gj,delta,atmp,'same');
end
dj = sqrt(dx.^2+dy.^2);
for j = 1:J+1
I1 = imadjust(dj(:,:,1,j+1),stretchlim(dj(:,:,1,j+1)),[0 1]);
figure;imshow(I1);
end

回答(0 个)

类别

帮助中心File Exchange 中查找有关 Parallel for-Loops (parfor) 的更多信息

标签

提问:

2018-5-2

评论:

2018-5-2

Community Treasure Hunt

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

Start Hunting!

Translated by