Temporary Variables - MATLAB & Simulink (original) (raw)
Main Content
A temporary variable is any variable that is the target of a direct, nonindexed assignment, but is not a reduction variable. In the followingparfor
-loop, a
and d
are temporary variables:
a = 0; z = 0; r = rand(1,10); parfor i = 1:10 a = i; % Variable a is temporary z = z + i; if i <= 5 d = 2*a; % Variable d is temporary end end
In contrast to the behavior of a for-loop, MATLAB® clears any temporary variables before each iteration of aparfor
-loop. To help ensure the independence of iterations, the values of temporary variables cannot be passed from one iteration of the loop to another. Therefore, temporary variables must be set inside the body of aparfor
-loop, so that their values are defined separately for each iteration.
MATLAB does not send temporary variables back to the client. A temporary variable in a parfor
-loop has no effect on a variable with the same name that exists outside the loop. This behavior is different from ordinaryfor
-loops.
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. In this case, MATLAB issues a static error rather than a run-time error. There is little point in allowing execution to proceed if a run-time error is guaranteed to occur. This kind of error often arises because of confusion between for
and parfor
, especially regarding the rules of classification of variables. For example:
b = true; parfor i = 1:n if b && some_condition(i) do_something(i); b = false; end ... end
This loop is acceptable as an ordinary for
-loop. However, as aparfor
-loop, b
is a temporary variable because it occurs directly as the target of an assignment inside the loop. Therefore it is cleared at the start of each iteration, so its use in the condition of theif
is guaranteed to be uninitialized. If you changeparfor
to for
, the value ofb
assumes sequential execution of the loop. In that case,do_something(i)
is executed only for the lower values ofi
until b
is setfalse
.
Temporary Variables Intended as Reduction Variables
Another common cause of uninitialized temporaries can arise when you have a variable that you intended to be a reduction variable. However, if you use it elsewhere in the loop, then it is classified as a temporary variable. For example:
s = 0; parfor i = 1:n s = s + f(i); ... if (s > whatever) ... end end
If the only occurrences of s
are the two in the first statement of the body, s
would be classified as a reduction variable. But in this example, s
is not a reduction variable because it has a use outside of reduction assignments in the line s > whatever
. Because s
is the target of an assignment (in the first statement), it is a temporary. Therefore MATLAB issues an error, but points out the possible connection with reduction.
If you change parfor
to for
, the use ofs
outside the reduction assignment relies on the iterations being performed in a particular order. In a parfor
-loop, it matters that the loop “does not care” about the value of a reduction variable as it goes along. It is only after the loop that the reduction value becomes usable.
ans
Variable
Inside the body of a parfor
-loop, the ans
variable is classified as a temporary variable. All considerations and restrictions for temporary variables apply to ans
. For example, assignments toans
inside a parfor
-loop have no effect onans
outside the loop.