Matlab parallel computing time effiency

3 visualizzazioni (ultimi 30 giorni)
Kerem Ciftci
Kerem Ciftci il 3 Feb 2017
Modificato: Matt J il 12 Apr 2017
Currently I've started to work with matlab's parallel computing toolbox. I wanted to test it with a code for face recognition. My first try was with the Eigenfaces function using 10.000 images:
Script:
function [m, A, Eigenfaces] = EigenfaceCore(T)
tic
Train_Number = size(T,2);
A = [];
for i = 1 : Train_Number
temp = double(T(:,i)) - m;
A = [A temp,]; % Merging all centered images
end
L = A'*A;
[V D] = eig(L); 
x = zeros(1,size(D,1));
for i = 1 : size(D,1)
x (i) = D(i,i);
end
L_eig_vec = [];
for i = 1 : size(V,2)
if( x(i) > 1)
L_eig_vec = [L_eig_vec V(:,i)];
end
end
Eigenfaces = A * L_eig_vec; % A: centered image vectors
toc
For this code I need something that takes about 112 seconds.
If I open a POOL with 4 kernels, and use `parfor` instead of `for`, it takes 421 seconds. Can somebody help me? I thought by increasing the number of images (first it was something about 20 Images and parfor needing more time) the parallel computing version will pass the normal version.

Risposte (2)

Walter Roberson
Walter Roberson il 3 Feb 2017
eig() is multi-threaded and will use all of the available CPUs. Each worker only gets one CPU in current versions of MATLAB. Therefore each eig() call will individually take longer. However, since you would be doing more of them at the same time, the total time should be roughly the same.
If your pool of 4 workers is based upon 2 cores with hyperthreading then you would expect to roughly double your execution time when you do numeric calculations. hyperthreading is only efficient when one thread is waiting for interrupts or I/O, then the other thread can steal the CPU.
This could account for a doubling of execution time, but beyond doubling the reasons typically depend upon a lot of data transfer to the workers, which is extra work not needed in a plain for loop.
I notice you have
A = [];
for i = 1 : Train_Number
temp = double(T(:,i)) - m;
A = [A temp,]; % Merging all centered images
end
You should really re-write that to pre-allocate A and assign to the appropriate row or column of it.
  1 Commento
Kerem Ciftci
Kerem Ciftci il 3 Feb 2017
Hey Walter,
okay so this Code is not well suited for parallel computing? Hmm sadly, I just wanted to try to make it faster with parallel Toolbox.

Accedi per commentare.


Matt J
Matt J il 12 Apr 2017
Modificato: Matt J il 12 Apr 2017
You have a lot of unnecessary and expensive loops. Here's a loop-free version that should run much faster,
function [m, A, Eigenfaces] = EigenfaceCore(T)
A=double(T)-m;
L = A'*A;
[V D] = eig(L);
x=diag(D);
L_eig_vec = V(:,x>1);
Eigenfaces = A * L_eig_vec; % A: centered image vectors

Categorie

Scopri di più su Language Support in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by