Parfor seems slower than for loop

3 visualizzazioni (ultimi 30 giorni)
federico nutarelli
federico nutarelli il 18 Nov 2022
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 Commenti
Stephen23
Stephen23 il 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.
federico nutarelli
federico nutarelli il 21 Nov 2022
@Stephen23 thank you a lot for improving on the questionn. I will surely pay more attention on carefully applying MATLAB bes-practice addvice and get deeper on the link about code improvement.

Accedi per commentare.

Risposte (0)

Categorie

Scopri di più su Parallel Computing Fundamentals in Help Center e File Exchange

Prodotti


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by