Help with PARFOR function

조회 수: 8 (최근 30일)
DENNER Aurelia
DENNER Aurelia 2020년 7월 16일
댓글: DENNER Aurelia 2020년 7월 21일
Hello,
I have a matlab code which call a for loop with 150970 iterations.
It takes more than 4 days for my computer to calculate it.
I saw that it is possible to use PARFOR function instead of FOR to make calculs in parallel and then to save time.
This is a part of my code (A_pp is an 150970 rows x 7 colums) :
[m,n] = size(A_pp);
parfor i=1:m;
for j= 2:4;
if A_pp(i,j) == 0;
Xval = Xval;
Xcal = Xcal;
Xfb120 = Xfb120;
Xpred = Xpred;
elseif A_pp(i,j) == 1;
[Xcal, ~, ~] = snv(Xcal);
[Xval, ~, ~] = snv(Xval);
[Xfb120, ~, ~] = snv(Xfb120);
[Xpred, ~, ~] = snv(Xpred);
elseif A_pp(i,j) == 2;
width = A_pp(i,7);
order = A_pp(i,5);
deriv = A_pp(i,6);
[Xcal, ~] = savgol(Xcal,width,order,deriv);
[Xval, ~] = savgol(Xval,width,order,deriv);
[Xfb120, ~] = savgol(Xfb120,width,order,deriv);
[Xpred, ~] = savgol(Xpred,width,order,deriv);
elseif A_pp(i,j) == 3;
[Xcal,~,~,~] = mscorr(Xcal);
[Xval,~,~,~] = mscorr(Xval);
[Xfb120,~,~,~] = mscorr(Xfb120);
[Xpred,~,~,~] = mscorr(Xpred);
end
if j == 4;
LV = A_pp(i,1);
modl = pls(Xcal, Ycal, LV, options);
% pred = pls(Xval,modl,options);
valid = pls(Xval,Yval,modl,options);
pred120 = pls(Xfb120,modl,options);
Ypred120 = pred120.yhat;
valid120 = pls(Xfb120,Yfb120,modl,options);
erreur(i,1) = valid.rmsec(LV); %rmsec
erreur(i,2) = valid.rmsep(LV); %rmsev
erreur(i,3) = valid120.rmsep(LV); %rmsep
erreur(i,4) = valid.r2c(LV); %r2c
erreur(i,5) = valid.r2p(LV); %r2v
erreur(i,6) = valid120.r2p(LV); %r2p
erreur(i,7) = (max(Yval)-min(Yval))/erreur(i,2); %RER
Xcal = X_ini;
Xval = X_ini1;
Xpred = X_ini2;
Xfb120 = X_ini3;
end
end
end
And then I have : "Error: The variable erreur in a parfor cannot be classified."
Could you help me please ?
Have a nice day !
Aurelia

채택된 답변

Matt J
Matt J 2020년 7월 16일
편집: Matt J 2020년 7월 16일
I saw that it is possible to use PARFOR function instead of FOR to make calculs in parallel and then to save time.
That is only possible if the iterations of your for-loop iterations are independent of each other, i.e., the order in which the iterations are executed doesn't affect the result. This doesn't appear to be the case in your code, however, because at the beginning of each iteration, the values of Xval , Xcal, Xfb120, Xpred depend on the outcomes of previous loop iterations.
And then I have : "Error: The variable erreur in a parfor cannot be classified."
Assuming you can solve the above problem, a PARFOR loop must be structured so that all variables referenced in the loop obey these rules:
You can reorganize your calculations of erreur to meet these rules by introducing a temporary variable, tmp.
erreur=nan(m,7); %PREALLOCATE
parfor i=1:m;
...
tmp(1) = valid.rmsec(LV); %rmsec
tmp(2) = valid.rmsep(LV); %rmsev
tmp(3) = valid120.rmsep(LV); %rmsep
tmp(4) = valid.r2c(LV); %r2c
tmp(5) = valid.r2p(LV); %r2v
tmp(6) = valid120.r2p(LV); %r2p
tmp(7) = (max(Yval)-min(Yval))/tmp(2);
erreur(i,:) = tmp;
....
end
end
end
  댓글 수: 3
Matt J
Matt J 2020년 7월 17일
I would need to see how you rewrote the loop. Remember that none of this matters unless you've also rewritten the loop iterations to be independent of one another (see my first comment).
DENNER Aurelia
DENNER Aurelia 2020년 7월 21일
Hi Matt !
I resolve the problem : it was necessary to create the vector tmp with this form :
tmp = [valid.rmsec(LV), valid.rmsep(LV), valid120.rmsep(LV), valid.r2c(LV), valid.r2p(LV), valid120.r2p(LV), (max(Yval)-min(Yval))/valid.rmsep(LV)] ;
erreur(i,:) = tmp ;
Thank you very much !
Aurelia

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by