parfor loop

Parfor loops behave similarly to regular for loops except that they attempt to use multiple processors or threads to divide the loop space into chunks. The chunks are equally-sized (or as equal as possible).

The number of chunks is determined by the value set by the parcluster command. Your computer configuration will determine the number of chunks that can be processed in parallel. For example, if your machine only has two cores, setting parcluster(64) will cause the loop to be executed in 64 chunks, but only two of those will occur simultaneously.

Display of variable values is disabled during a parfor loop.

Special types of variables are inside a parfor loop:

Loop Variable

parfor j=1:64 

end 
Where j must be continuous.

Broadcast Variable

b = 5; 
parfor j=1:64 
     b = j; 
end 
b 
Where b will be printed in the command window as 5, because broadcast variables are readable within the parfor loop, but any modifications to them are discarded once the loop ends.

Local Variable


parfor j=1:64
     a = j; 
end 
a 
In this case, a will cause an error as an undefined variable, because local variables only exist within the parfor loop and are not accessible after the loop completes itself.

Sliced Variable

a = zeros(1,64);
parfor j=1:64
     a(j) = j; 
end 
a 
Where a will be printed with each of its elements having the value that has been assigned to it within the parfor loop. Sliced variables are indexed by the loop variable and each loop then processes a different part of the sliced variable.

For speed and safety, preallocate sliced variables using the zeros function or a similar method.

Reduced Variable

sum = 0;
parfor j=1:64
     sum = sum + j; 
end
sum
Where sum will be printed in the command window as 2080. Reduced variables are computed via calculations using the loop variable (directly or indirectly). Only + and * are supported operations for reduced variables (as they are commutative).
sum = 0;
parfor j=1:64
     sumsq = sumsq + times(j, j);  
end
sumsq
Where sumsq will be printed in the command window as 89440. The reduced operation(s) can be functions of the loop variable.

Limitations

The behavior of global variables in a parfor loop is not well-defined since multiple loops may be modifying the variable concurrently.

Certain functions are not thread-safe and will cause issues in a parfor loop. Currently there is not an exhaustive list of such functions, but one example is readmultvectors and any other function that accesses a shared resource (for example, xlswrite) also has potential to be problematic.