Parfor slows down computation
조회 수: 4 (최근 30일)
이전 댓글 표시
Hello,
I noticed that some functions are slower in parallel computation (for each loop not for the total time) such fft calculations, and it's due to the fact that fft2 uses optimized functions that use multi-cores.
The following code shows the implemention of fft2 :
function [ F ] =fft_implemented(A)
% equivalent to F=fft2(A)
M=size(A,1);
N=size(A,2);
[ x, y ] = meshgrid( 0 : M - 1, 0 : M - 1 );
a1 = exp( -2 * pi * 1i / M .* x .* y );
[x, y ] = meshgrid( 0 : N - 1, 0 : N - 1 );
a2 = exp( -2 * pi * 1i / N .* x .* y );
F = a1 * A * a2;
end
To test this function, I used this script:
for i=1:5
A{i}=rand(800,1280);
m=A{i};
tic
fft_implemented(m);
toc
end
The result is :
Elapsed time is 0.207859 seconds.
Elapsed time is 0.116945 seconds.
Elapsed time is 0.115507 seconds.
Elapsed time is 0.115516 seconds.
Elapsed time is 0.113433 seconds.
After using the parallel version, i found :
parfor i=1:5
A{i}=rand(800,1280);
m=A{i};
tic
fft_implemented(m);
toc
end
Elapsed time is 0.941441 seconds.
Elapsed time is 0.872370 seconds.
Elapsed time is 0.868988 seconds.
Elapsed time is 0.979503 seconds.
Elapsed time is 1.004280 seconds.
I didn't understand why it's slower in parallel (for each loop, not for the total execution time) that in the serial case. Thank you in advance
댓글 수: 0
답변 (1개)
Prashant Arora
2017년 4월 27일
This is because the overhead added by parfor for parallel computation setup dominates the time needed for calculations. You can refer to the following document for further information on when parfor loop might not be useful.
댓글 수: 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!