A temporary variable is any variable that
is the target of a direct, nonindexed assignment, but is not a reduction
variable. In the following parfor
-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® effectively
clears any temporary variables before each iteration of a parfor
-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 a parfor
-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 the context of the parfor
statement
has no effect on a variable with the same name that exists outside
the loop, again in contrast to ordinary for
-loops.
Because temporary variables are cleared at the beginning of
every iteration, MATLAB can detect certain cases in which any
iteration through the loop uses the temporary variable before it is
set in that iteration. In this case, MATLAB issues a static
error rather than a run-time error, because 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, suppose you write
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,
but as a parfor
-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 the if
is
guaranteed to be uninitialized. (If you change parfor
to for
,
the value of b
assumes sequential execution of
the loop, so that do_something(i)
is executed for
only the lower values of i
until b
is
set false
.)
Another common cause of uninitialized temporaries can arise when you have a variable that you intended to be a reduction variable, but you use it elsewhere in the loop, causing it technically to be 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
were the two
in the first statement of the body, it 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,
so MATLAB issues an error about this fact, but points out the
possible connection with reduction.
Note that if you change parfor
to for
,
the use of s
outside the reduction assignment relies
on the iterations being performed in a particular order. The point
here is that 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.