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 日

0 投票

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 件のコメント

Himanshi Rani
Himanshi Rani 2017 年 11 月 16 日
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.
Matt J
Matt J 2017 年 11 月 16 日
It doesn't seem like you actually have a parallelizable loop. The update
phi(m+1)=(V-Ve)+Vg;
will affect later loop iterations.
Himanshi Rani
Himanshi Rani 2017 年 11 月 17 日
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.
Walter Roberson
Walter Roberson 2017 年 11 月 17 日
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.
Himanshi Rani
Himanshi Rani 2017 年 11 月 17 日
I overlooked that. It is actually round ((1/0.1) *t)
Walter Roberson
Walter Roberson 2017 年 11 月 17 日
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
Himanshi Rani
Himanshi Rani 2017 年 11 月 17 日
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?
Walter Roberson
Walter Roberson 2017 年 11 月 18 日
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.
Himanshi Rani
Himanshi Rani 2017 年 11 月 18 日
So can parfor be used if I preallocate all of phi?
Walter Roberson
Walter Roberson 2017 年 11 月 18 日
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.
Himanshi Rani
Himanshi Rani 2017 年 11 月 18 日
Okay thankyou. I will try that

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLoops 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