Parfor getting slower than a normal for loop

조회 수: 1 (최근 30일)
federico nutarelli
federico nutarelli 2022년 11월 19일
댓글: federico nutarelli 2022년 11월 21일
Hi all,
I am trying to parallelize the execution of a funtion in matlab.
Specifically, I am performing the following code:
clear
%reduce the size of A if necessary.
A=rand(1000,60);
B=int8(rand(1000,60)>0.5);
lambda_tol=0.000000000000004;
N=10;
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=4;
tol = 1e-9;
tic
CompletedMat_nopar={};
%using normal for loop:
for k = 1:size(lambda_tol_vector,1)
fprintf('Completion using nuclear norm regularization... \n');
[CompletedMat,objective,flag] = matrix_completion_nuclear_GG_alt(A.*double(B),double(B),N,lambda_tol_vector(k),tol);
if flag==1
CompletedMat_nopar{k}=zeros(size(A));
end
CompletedMat_nopar{k}=CompletedMat;
end
toc
%1000x60 --> Elapsed time is 55.271974 seconds
%using a parfor loop:
tic
CompletedMat_par={};
parfor (k = 1:size(lambda_tol_vector,1),M)
fprintf('Completion using nuclear norm regularization... \n');
[CompletedMatpar,objective_par,flag_par] = matrix_completion_nuclear_GG_alt(A.*double(B),double(B),N,lambda_tol_vector(k),tol);
if flag_par==1
CompletedMat_par{k}=zeros(size(A));
end
CompletedMat_par{k}=CompletedMatpar;
end
toc
%1000x60 --> Elapsed time is 95.671825 seconds
You can see the function matrix_completion_nuclear_GG_alt attached.
I aam not able to understand the reason why the parffor runs that slower than the normal for loop. Does it depend on the matrix_completion_nuclear_GG_alt function or on the syntax of the parfor?
EDIT: if it might be of any help here is my ticByttes and tocBytes:
BytesSentToWorkers BytesReceivedFromWorkers
__________________ ________________________
1 84744 2.4034e+07
2 87328 2.6919e+07
3 84712 2.2112e+07
4 84752 2.4516e+07
5 84744 2.4034e+07
6 82192 2.3072e+07
Total 5.0847e+05 1.4469e+08
  댓글 수: 7
Stephen23
Stephen23 2022년 11월 21일
편집: Stephen23 2022년 11월 21일
"However, in my case daaa cannot be split since the algorithm I aam performing needs the information about the entire matrix. What I can do is to split the "for k=..." loop. In this case, is there an efficient way to perform such a split? I meaan, if I understood correctly I should:"
No, that is the complete opposite of what Walter Roberson was telling you. The point is that many MATLAB operations, e.g. SUM, are already multi-threaded. This is automatic and occurs without the user needing to do anything special at all.
This is one reason for the advice that Ayush gave you to vectorize, given in your other thread.
Your attempts to speed things up are most likely going to fail because most of what you are attempting is fighting the actual ways that MATLAB code can be written to be fast and to benefit from the inherent speed and multi-threading of basic MATLAB operators. Learning how to write efficient MATLAB code is the best start to this task. Only once you have shown that reasonable best-practice really is a bottleneck should you start to investigate alternatives.
federico nutarelli
federico nutarelli 2022년 11월 21일
@Stephen23 thank you I will dig more on efficient MATLAB coding.

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

답변 (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