필터 지우기
필터 지우기

Parfor seems slower than for loop

조회 수: 1 (최근 30일)
federico nutarelli
federico nutarelli 2022년 11월 18일
댓글: federico nutarelli 2022년 11월 21일
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
Stephen23
Stephen23 2022년 11월 21일
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 2022년 11월 21일
@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.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Parallel Computing Fundamentals에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by