User`s guide
2 Parallel for-Loops (parfor)
2-34
Required (static): If the reduction assignment uses * or [,], then in every reduction
assignment for X, X must be consistently specified as the first argument or consistently
specified as the second.
The parfor-loop on the left below is not valid because the order of items in the
concatenation is not consistent throughout the loop. The parfor-loop on the right is
valid:
Invalid Valid
parfor i = 1:n
if testLevel(k)
A = [A, 4+i];
else
A = [r(i), A];
end
% loop body continued
end
parfor i = 1:n
if testLevel(k)
A = [A, 4+i];
else
A = [A, r(i)];
end
% loop body continued
end
Further Considerations with Reduction Variables
This section provides more detail about reduction assignments, associativity,
commutativity, and overloading of reduction functions.
Reduction Assignments. In addition to the specific forms of reduction assignment listed
in the table in “Reduction Variables” on page 2-32, the only other (and more general)
form of a reduction assignment is
X = f(X, expr) X = f(expr, X)
Required (static): f can be a function or a variable. If it is a variable, it must not be
affected by the parfor body (in other words, it is a broadcast variable).
If f is a variable, then for all practical purposes its value at run time is a function
handle. However, this is not strictly required; as long as the right-hand side can be
evaluated, the resulting value is stored in X.
The parfor-loop below on the left will not execute correctly because the statement f =
@times causes f to be classified as a temporary variable and therefore is cleared at the