Convert Nested for-Loops to parfor

A typical use case for nested loops is to step through an array using one loop variable to index through one dimension, and a nested loop variable to index another dimension. The basic form looks like this:

X = zeros(n,m);
for a = 1:n
    for b = 1:m
        X(a,b) = fun(a,b)
    end
end

The following code shows an extremely simple example, with results you can easily view.

M1 = magic(5);
for x = 1:5
    for y = 1:5
        M2(x,y) = x*10 + y + M1(x,y)/10000;
    end
end
M2

Although you can parallelize either of the nested loops, you cannot run both in parallel. This is because the workers in a parallel pool cannot start or access further parallel pools.

If the loop counted by x is converted to a parfor-loop, then each worker in the pool executes the nested loops using the y loop counter, and the y loops themselves cannot run as a parfor on each worker. So if you convert only the outer loop to a parfor, it looks like this:

M1 = magic(5);
parfor a = 1:5
    for b = 1:5
        M2(a,b) = a*10 + b + M1(a,b)/10000;
    end
end
M2

Should you consider converting only the inner loop to a parfor? While it might be programmatically simpler in your case to do this, it might not be advisable. The converted code looks like this:

M1 = magic(5);
for a = 1:5
    parfor b = 1:5
        M2(a,b) = a*10 + b + M1(a,b)/10000;
    end
end
M2

In this case, each iteration of the outer loop in MATLAB, initiates a parfor-loop. That is, this code creates five parfor-loops. There is generally more overhead to a parfor-loop than a for-loop, so you might find that this approach does not perform optimally.

More About

Was this topic helpful?