I am seeing a slew of warnings below which seem to be arising from the use of temporary variables in one of my parfor loops. The warnings would make sense to me if I were using variables of the same name prior to entering the loop - parfor would clear those variables - but I am not doing so. Also, the warnings seem to be triggered by some, but not all of my temporary variables. Does anyone have any idea what else could cause this?
One possible clue: the variables named in the warnings are also the names of class properties in a class I have defined. I do have an object of this class prior to the loop and access its properties as obj.A, obj.B, etc... but I don't see why that would be a cause for the warnings.
Warning: File: LineSegParsing.m Line: 120 Column: 33
The temporary variable coords will be cleared at the beginning
of each iteration of the parfor loop.
Any value assigned to it before the loop will be lost.
If coords is used before it is assigned in the parfor
loop, a runtime error will occur.
See Parallel for Loops in MATLAB, "Temporary Variables".
Warning: File: LineSegParsing.m Line: 116 Column: 38
The temporary variable A will be cleared at the beginning
of each iteration of the parfor loop.
Any value assigned to it before the loop will be lost.
If A is used before it is assigned in the parfor
loop, a runtime error will occur.
See Parallel for Loops in MATLAB, "Temporary Variables".
Warning: File: LineSegParsing.m Line: 152 Column: 25
The temporary variable B will be cleared at the beginning of
each iteration of the parfor loop.
Any value assigned to it before the loop will be lost.
If B is used before it is assigned in the parfor
loop, a runtime error will occur.

댓글 수: 2

Adam
Adam 2017년 3월 22일
Do you still get the warnings if you name the variables differently to the class properties?
Matt J
Matt J 2017년 3월 22일
That would be a good test, but unfortunately, I'm finding that the warnings, even without renaming the variables, are not reproducible on subsequent runs. I'm not sure what it takes yet to reproduce them.

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

 채택된 답변

Ken
Ken 2017년 5월 12일

0 개 추천

Uninitialized Temporaries Temporary variables in a parfor-loop are cleared at the beginning of every iteration. MATLAB can sometimes detect cases in which loop iterations use a temporary variable before it is set in that iteration.
Examples include conditionally setting a variable before any use of the variable.
function myFunction
parfor i = 1:10
if rand < 0.5
a = MyClass;
end
disp(a.SomeValue);
end
end
Likewise, not setting a variable on all possible code paths.
function myFunction
parfor i = 1:10
if rand < 0.5
a = MyClass;
else
% a is not set
end
disp(a.SomeValue);
end
end

댓글 수: 6

Thanks, Ken. You might be on to something. I do set my variables inside a switch statement, similar to the example below, but I cannot get this example to throw the warning, so I'm not sure what's different about my actual code
classdef myclass
properties
p=1;
end
methods
function obj=myclass(n,j)
if ~nargin
return;
end
obj(n).p=[];
parfor i=1:n
switch j
case 1
p=1;
obj(i).p=p;
case 2
p=2;
obj(i).p=p;
otherwise
error 'Unrecognized'
end
end
end
end
end
Yeah, I'm getting this warning in a parfor loop where the temporary variable is set in a while loop. I understand that this counts as conditional (in the case where the boolean activating the while loop is false from the get-go) but it would be nice if MATLAB recognized that said boolean is initialized to true at each parfor loop iteration, thus guaranteeing at least one while loop iteration and the proper setting of the aforementioned temporary variable. In fact, the looping condition literally can't be false without setting the temporary variable first in my case. Basically:
parfor ii=1:N
cond = true;
while cond
tempVar = someThing;
if someBool(tempVar)
cond = false;
end
end
someThingElse = someFnctn(tempVar);
end
Surely this should be perfectly valid, no?
David Lovell
David Lovell 2019년 4월 12일
I'm willing to bet Matlab does NOT notice that you set cond = true before using it as the condition for the while loop. Instead, being more myopic, Matlab can easily notice that tempVar is only used inside a while loop, which in general are never guaranteed to execute, and therefore it throws this warning. Your code will work fine. If you want to get rid of the warning, just put a useless statement like tempVar = someThing; on the line before the while loop.
David Lovell
David Lovell 2019년 4월 12일
And to Matt J, in the example you posted, the only variables whose values can change inside the switch..case statement are p and obj(i), both of which existed in the workspace before the parfor command. Thus, they are not temporary variables. In your code, something else must be the case.
Matt J
Matt J 2019년 4월 12일
편집: Matt J 2019년 4월 12일
@David,
No, p did not exist in the workspace before parfor. More importantly, though, it is the target of a non-indexed assignment inside the parfor loop, so it should be classified as temporary, as far as I can see.
Walter Roberson
Walter Roberson 2019년 4월 13일
Does it make a difference that p is an object property as well as the name of the temporary ? e.g., if you had written the code in terms of new_p for the temporary, would it have the same problem?

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Parallel for-Loops (parfor)에 대해 자세히 알아보기

질문:

2017년 3월 22일

댓글:

2019년 4월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by