User`s guide

2 Parallel for-Loops (parfor)
2-36
parfor statement might produce values of X with different round-off errors. This is an
unavoidable cost of parallelism.
For example, the statement on the left yields 1, while the statement on the right returns
1 + eps:
(1 + eps/2) + eps/2 1 + (eps/2 + eps/2)
With the exception of the minus operator (-), all the special cases listed in the table in
“Reduction Variables” on page 2-32 have a corresponding (perhaps approximately)
associative function. MATLAB calculates the assignment X = X - expr by using X =
X + (-expr). (So, technically, the function for calculating this reduction assignment is
plus, not minus.) However, the assignment X = expr - X cannot be written using an
associative function, which explains its exclusion from the table.
Commutativity in Reduction Assignments. Some associative functions, including +, .*,
min, and max, intersect, and union, are also commutative. That is, they satisfy the
following for all a and b:
f(a,b) = f(b,a)
Examples of noncommutative functions are * (because matrix multiplication is not
commutative for matrices in which both dimensions have size greater than one), [,],
[;], {,}, and {;}. Noncommutativity is the reason that consistency in the order
of arguments to these functions is required. As a practical matter, a more efficient
algorithm is possible when a function is commutative as well as associative, and parfor
is optimized to exploit commutativity.
Recommended: Except in the cases of *, [,], [;], {,}, and {;}, the function f of
a reduction assignment should be commutative. If f is not commutative, different
executions of the loop might result in different answers.
Violating the restriction on commutativity in a function used for reduction, could result
in unexpected behavior, even if it does not generate an error.
Unless f is a known noncommutative built-in, it is assumed to be commutative. There is
currently no way to specify a user-defined, noncommutative function in parfor.
Overloading in Reduction Assignments. Most associative functions f have an identity
element e, so that for any a, the following holds true: