Help with PARFOR function

1 次查看(过去 30 天)
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