Parfor seems slower than for loop
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi all,
I am trying to parallelize an operation in MATLAB. The MWE goes like this:
clear
A=rand(5000,3000);
B=int8(rand(5000,3000)>0.5);
lambda_tol=0.00000000000000004;
N=10;
%A='/Users/federiconutarelli/Desktop/Paper_Samuel/cd_2004.csv';
G=15;
lambda_tol_vector= zeros(G,1);
conto = 1;
for h=-G:0.1:G
lambda_tol_vector(conto)=2^(h);
conto = conto+1;
end
M=1;
tic
tol = 1e-9;
parfor (k = 1:size(lambda_tol_vector,1),M)
lambda_tol = lambda_tol_vector(k);
fprintf('Completion using nuclear norm regularization... \n');
[CompletedMat,objective,flag] = matrix_completion_nuclear_GG_alt(A.*double(B),double(B),N,lambda_tol,tol);
if flag==1
CompletedMat=zeros(size(A));
end
end
toc
Now, what I would expect is that when M>1 the time taken is lower than when M=1. However the decrease in time is not very sensitive. Why is like that?
Furthermore, I have noticed that, after a while that I do not launch the parfor, the latter takes on a while displaying the ollowing message before starting the loop:
Starting parallel pool (parpool) using the 'local' profile ...
Preserving jobs with IDs: 1 because they contain crash dump files.
You can use 'delete(myCluster.Jobs)' to remove all jobs created with profile local. To create 'myCluster' use 'myCluster = parcluster('local')'.
Connected to the parallel pool (number of workers: 6).
Is there a way to avoid this message to appear and go directly to the parfor loop?
Thank you
8 Kommentare
Stephen23
am 21 Nov. 2022
Expanding on Ayush's advice to vectorize, you will find more advice on how to write faster MATLAB code here:
From the short look I took now, you will find a number of those recommendations apply to your code.
As Walter Roberson already commented in your other thread on this topic, many MATLAB operations are inherently multi-threaded, so simply jumping on the parallel-bandwagon is not a "universal speed up" that many beginners image it to be. In contrast, understanding how arrays are stored, careful testing, and following MATLAB best-practice are much more likely to have a positive impact on code performance.
Antworten (0)
Siehe auch
Kategorien
Mehr zu Parallel Computing Fundamentals finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!