parallel processing time problem

4 次查看(过去 30 天)
Anas A.Salam
Anas A.Salam 2013-5-11
hello i have a question about the time when i use single core and when i use 4 cores at the first time i had time equal 15 second also this is the code
-----------------------
a=imread('something.jpg');
b(1:size(a,1),1:size(a,2))=0;
c(1:size(a,1),1:size(a,2))=0;
for i=1:size(a,1)
for j=1:size(a,2)
for k=1:size(a,3)
b(i,j)=b(i,j)+a(i,j,k);
c(i,j)=b(i,j)/3;
end
end
end
---------------------
when i try to use matlabpool i had time larger than i had it with single core so what is the problem ?? by the way i used this code with multi core
----------------------
a=imread('something.jpg');
b(1:size(a,1),1:size(a,2))=0;
c(1:size(a,1),1:size(a,2))=0;
parfor i=1:size(a,1)
parfor j=1:size(a,2)
for k=1:size(a,3)
b(i,j)=b(i,j)+a(i,j,k);
c(i,j)=b(i,j)/3;
end
end
end
-------------------------- thank you

回答(1 个)

Edric Ellis
Edric Ellis 2013-5-13
Firstly, you should note that only the outermost PARFOR has any effect. All the available parallelism is used there, so the inner PARFOR makes no difference.
In this case, the amount of computation you're doing inside the loop compared to the amount of data you're accessing is much too low. In this case, I think vectorization should give you much better results. I think in this case can't you simply do:
a = imread('something.jpg');
b = sum(a, 3);
c = b ./ 3;

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by