Error with parfor loop

2 views (last 30 days)
tilfani oussama
tilfani oussama on 22 Oct 2018
Commented: Walter Roberson on 23 Oct 2018
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 Comments
Kevin Chng
Kevin Chng on 23 Oct 2018
[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

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 23 Oct 2018
Edited: Walter Roberson on 23 Oct 2018
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 Comments
Walter Roberson
Walter Roberson on 23 Oct 2018
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
Walter Roberson
Walter Roberson on 23 Oct 2018
Your DCCA_vec() appears to be from https://www.mathworks.com/matlabcentral/answers/423283-reduce-execution-time-in-matlab#answer_340928 . I do not know the code for your heavy()

Sign in to comment.

Categories

Find more on Entering Commands in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!