Parfor slows down computation

1 view (last 30 days)
Elkhanssaa Marsali
Elkhanssaa Marsali on 25 Apr 2017
Answered: Prashant Arora on 27 Apr 2017
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

Answers (1)

Prashant Arora
Prashant Arora on 27 Apr 2017
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.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!