Error: The variable in a parfor cannot be classified.

parfor t=0.1:0.1:v
m=round(0.1*t);
J= (phi(m))^2*exp(-1/phi(m));
k=J*sigma/q;
Vg=v-t;
Ve= V*(1-exp(-k*t))/2;
phi(m1+1)=(Vplus-Ve)+Vg;
end
In the above code, Parfor cannot classify the variable phi. I think this might be because the variable is used as a sliced variable as well as a reduction variable. Could someone please clarify why the error might occur and how can it be resolved? Thanks

답변 (1개)

Matt J
Matt J 2017년 11월 16일
편집: Matt J 2017년 11월 16일
The intent of your code is not clear, though it is likely that your problems lie in the line
phi(m1+1)=(Vplus-Ve)+Vg;
The variables m1 and Vplus are never defined. Also, you say the loop is supposed to be doing some sort of reduction, but there is no reduction operation anywhere to be seen. A reduction variable would appear on both sides of an update, as in,
X=X+1;
However, here is my best guess as to what you might be trying to do:
mvals=round(0.1*(0.1:0.1:v));
parfor i=1:length(mvals);
m=mvals(i);
J= (phi(m))^2*exp(-1/phi(m));
k=J*sigma/q;
Vg=v-t;
Ve= V*(1-exp(-k*t))/2;
subs(i)=m+1;
vals(i)=(Vplus-Ve)+Vg;
end
result=accumarray(subs,vals)

댓글 수: 12

Ohh, I overlooked the variables. Here is the new code:
parfor t=0.1:0.1:v
m=round(0.1*t);
J= (phi(m))^2*exp(-1/phi(m));
k=J*sigma/q;
Vg=v-t;
Ve= V*(1-exp(-k*t))/2;
phi(m+1)=(V-Ve)+Vg;
end
phi depends on Ve which is calculated using J and J depends on phi again. So I called it a reduction variable. I might not be clear with the definition.
It doesn't seem like you actually have a parallelizable loop. The update
phi(m+1)=(V-Ve)+Vg;
will affect later loop iterations.
So how can I reduce the execution time? Because I have to run the same code for t=0.0001:0.0001:v and Matlab takes a lot of time.
You have
parfor t=0.1:0.1:v
m=round(0.1*t);
J= (phi(m))^2*exp(-1/phi(m));
The minimum t is 0.1. In the second line you multiply that by 0.1, getting 0.01 . You round() that, which is going to give 0. You then use phi(0), which is going to be an error if phi is a variable instead of a function.
I overlooked that. It is actually round ((1/0.1) *t)
To confirm, then, you would like to do:
phi(1) = something;
parfor m = 1 : 10*v
J = (phi(m))^2 * exp(-1/phi(m));
k = J * sigma / q;
t = t_idx/10;
Vg = v - t;
Ve = V * (1-exp(-k*t))/2;
phi(m+1) = (V-Ve)+Vg;
end
Could you confirm that v (lower case, used in creating Vq) is a different variable than V (upper case, used in creating Ve and phi(m+1)) ?
Do you pre-allocate phi?
max_m = 10*v;
phi = zeros(1, max_m);
phi(1) = something;
for m = 1 : max_m
J = (phi(m))^2 * exp(-1/phi(m));
k = J * sigma / q;
t = t_idx/10;
Vg = v - t;
Ve = V * (1-exp(-k*t))/2;
phi(m+1) = (V-Ve)+Vg;
end
Yes v and V are different variables. I am preallocating phi(1) to some initial value.
Matt J
Matt J 2017년 11월 18일
Is phi(m) supposed to be converging to something as m-->Infinity? If so, why don't you break out of the loop once it's sufficiently converged?
Pre-allocating phi(1) is not enough: you need to pre-allocate all of phi to avoid the performance problem of extending phi every iteration.
So can parfor be used if I preallocate all of phi?
No. parfor can only be used when the results of any one iteration do not depend upon the results o a previous iteration. Your results for m = 2 depend upon your results for m = 1.
The point is that you were complaining that the calculation took a long time, and one of the big slowdowns in MATLAB is if you do not pre-allocate your arrays. Try again without parfor but with pre-allocation.
Okay thankyou. I will try that

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

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

2017년 11월 16일

댓글:

2017년 11월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by