Why is Parallel Computing Slower than Single-Processor Computing?
8 次查看(过去 30 天)
显示 更早的评论
I wrote a program which is required to manipulate large matricies in relatively short periods of time. I vectorized my algorithms, whenever possible, but have found that the program still runs rather slowly. So now I'm trying parallel techniques by using the Parallel Computing Toolbox. To my surprise, a parallelized version of a short program is much slower than the single-processor version. Here's the single-processor version of my test program:
multp = 4943;
length = 162000000;
numofworkers = 12;
block = logical(dec2bin(0:2^15-1,15)-48);
A = [repmat(block,multp,1); logical(zeros(length-multp*2^15,15))];
pattern = reshape(repmat(1:numofworkers,length/numofworkers,1),length,1);
B = [repmat(pattern,1,10) zeros(length,5)];
C = repmat(pattern-1,1,15)*5+3;
tic;
D = ~A&B~=0;
B(D) = B(D)+C(D);
toc;
When running this program, the tic/toc section takes about 46 seconds.
Here's my parallelized version of the program:
multp = 4943;
length = 162000000;
numofworkers = 12;
block = logical(dec2bin(0:2^15-1,15)-48);
A = [repmat(block,multp,1); logical(zeros(length-multp*2^15,15))];
pattern = reshape(repmat(1:numofworkers,length/numofworkers,1),length,1);
B = [repmat(pattern-1,1,10) zeros(length,5)];
A = distributed(A');
B = distributed(B');
tic;
spmd(12)
D = ~A&B~=0;
B(D) = B(D)+(labindex-1)*5+3;
end
B = gather(B)';
toc;
When running this parallelized version, the tic/toc section takes about 389 seconds. Does anyone know why the parallelized version is slower? As you can see, I started the tic/toc timing after 'A' and 'B' were distributed, and therefore after the pool was created. However, it's still slower than the non-parallelized version. Why is that?
Thanks for your time, and for any suggestions that anyone may have.
1 个评论
OCDER
2017-9-12
Parallel can be slower than serial processing due to overhead caused by communication between workers. Here's a good resource for that.
I also notice a lot of repmat usages - if you can use bsxfun instead, things may (or may not) go faster. Here's a good discussion on that:
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!