parfor-Loop Iterations are Independent
If you get an error when you convert
parfor-loops, ensure that your
iterations are independent.
have no guaranteed order, while the iteration
for-loops is sequential.
parfor-loop iterations are performed on different MATLAB® workers
in the parallel pool, so that there is no sharing of information between
iterations. Therefore one
must not depend on the result of a previous iteration. The only exception
to this rule is to accumulate values in a loop using Reduction Variables.
The following example produces equivalent
results, using a
for-loop on the left and a
on the right. Try the example in your MATLAB Command Window:
clear A for i = 1:8 A(i) = i; end A
A = 1 2 3 4 5 6 7 8
clear A parfor i = 1:8 A(i) = i; end A
A = 1 2 3 4 5 6 7 8
Each element of
equal to its index. The
parfor-loop works because
each element is determined by the indexed loop variable only and does
not depend on other variables.
for-loops with independent
tasks are ideal candidates for
parfor automatically starts a
parallel pool of workers, if you have not started one already.
a pool using your default cluster profile, if you have set your parallel
In the example, the array elements are available
in the client workspace after the
exactly as with a
a nonindexed variable inside the loop, or a variable whose indexing
does not depend on the loop variable
i. Try these
examples, and note the values of
clear A d = 0; i = 0; for i = 1:4 d = i*2; A(i) = d; end A d i
A = 2 4 6 8 d = 8 i = 4
clear A d = 0; i = 0; parfor i = 1:4 d = i*2; A(i) = d; end A d i
A = 2 4 6 8 d = 0 i = 0
Although the elements
A are the same in both examples, the value of
not. In the
for-loop, the iterations are executed
sequentially, so afterward
d has the value it held
in the last iteration of the loop. In the
however, the iterations execute in parallel, so it is impossible to
d a defined value at the end of the loop.
This situation also applies to the loop variable
parfor-loop behavior is defined so that
it does not affect the values
the loop. Their values remain the same before and after the loop.
If the variables in your
parfor-loop are not independent,
then you might get different answers from those in the
In summary, a
parfor-loop requires that each iteration
be independent of the other iterations. All code that follows the
should not depend on the loop iteration sequence.
Code Analyzer can help diagnose whether the loop iterations are dependent. The code in the example shows iterations defined in terms of the previous iteration:
parfor k = 2:10 x(k) = x(k-1) + k; end
In other cases, however, Code Analyzer is unable to mark dependencies.
For help with other common
see Nested parfor-Loops and for-Loops.