PARFOR temporary variables warning
64 次查看(过去 30 天)
显示 更早的评论
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
2017-3-22
Do you still get the warnings if you name the variables differently to the class properties?
采纳的回答
Ken
2017-5-12
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 个评论
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 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Parallel for-Loops (parfor) 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!