Error with parfor loop

1 次查看(过去 30 天)
tilfani oussama
tilfani oussama 2018-10-22
I have the following code
ft=zeros(1000,1);
gt=zeros(1000,1);
FT=zeros(1000,1) ;
Gt=zeros(1000,1) ;
CovXY=zeros(length(SP)-999,1) ;
VarX=zeros(length(SP)-999,1) ;
VarY=zeros(length(SP)-999,1) ;
rho=zeros(1,length(SP)-999) ;
x=zeros(1000,1) ;
y=zeros(1000,1);
for j=1:length(SP)-999
x=SP(j :j+999);
y=CAC40(j :j+999) ;
[parameters, ll, Ft, VCV, scores] = heavy(x,1,1) ;
[parameters, ll, Gt, VCV, scores] = heavy(y,1,1) ;
ft = x ./ sqrt(Ft);
gt = y ./sqrt(Gt);
[CovXY(j, :),VarX(j, :),VarY(j, :)]=DCCA_vec(ft,gt,n);
rho(j)=CovXY(j)/(VarX(j)*VarY(j)) ;
end
I replaced for by a "parfor" then i get a message
Error: The variable CovXY in a parfor cannot be classified.
See Parallel for Loops in MATLAB, "Overview".
If someone can help me.
Thank you
  2 个评论
Kevin Chng
Kevin Chng 2018-10-23
[CovXY(j, :),VarX(j, :),VarY(j, :)]=DCCA_vec(ft,gt,n);
Your error is here? Do you mind attach your script including all the variables required? So that I can help to try out

请先登录,再进行评论。

回答(1 个)

Walter Roberson
Walter Roberson 2018-10-23
编辑:Walter Roberson 2018-10-23
On the line
[CovXY(j, :),VarX(j, :),VarY(j, :)]=DCCA_vec(ft,gt,n);
you use CovXY as if it is a 2D array in which you are writing a new row each time.
In the next line
rho(j)=CovXY(j)/(VarX(j)*VarY(j)) ;
you are using CovXY as if it is a vector.
When you use parfor, you need to be consistent in how you access your variables.
Also, the fact that you pre-initialized gt and ft will be taken as evidence that they are to be output from the loop, but because you write over them every time, you would get the last iteration with for, and parfor would refuse to run.
The initialization you are doing of CovXY suggests that you are expecting scalar outputs from DCCA_vec. If so then
[CovXY(j, 1),VarX(j, 1),VarY(j, 1)]=DCCA_vec(ft,gt,n);
rho(j,1)=CovXY(j,1)./(VarX(j,1)*VarY(j,1)) ;
  3 个评论
Walter Roberson
Walter Roberson 2018-10-23
The general outline for parfor is:
  • If you have variables that are being used for temporary storage and are not needed after the loop, then do not initialize them before the loop. If they are completely written over unconditionally, do not initialize them (wastes time and can confuse the parser.) If they are written to in pieces, initialize them to full size inside the parfor
  • For output variables needed after the loop, it is still preferred to initialize them before the loop, so that they will definitely have proper size. parfor deliberately runs the last iteration first in case you failed to pre-initialize some variables
  • It is permitted to initialize an array before the loop, read from it, and write to it. However, when you do that, all of the subscripts you use in reading should be the same as the subscripts used in writing
  • inside parfor, for any one parallel variable, all reads and all writes must use the same subscript expressions
  • inside parfor, only have one write to any one parallel variable
  • because of the restrictions on reading and writing, you should generally pull out a complete row or column at the beginning of the loop, assigning to a temporary variable, reading and writing to temporary variables, and then at the end, update that one row or column from the temporary variable
For example:
parfor J = 1 : 10
output(J,1) = 7;
output(J,2) = 9; %NO
end
and instead
parfor J = 1 : 10
t = zeros(1,2);
t(1) = 7;
t(2) = 9;
output(J,:) = t;
end

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Entering Commands 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by