MATLAB: Which loop to run in parallel if I have three of them

parallel computingParallel Computing Toolbox

Assuming I have a code like this:
for ii=1:51
do something
for jj=1:100
do something
for kk=1:200
do something
end
end
end
I have parallel programming toolbox and want to run the code on all processors available. Which loop should I change to parfor? Or maybe all three?
Please, give a detailed explanation.

Best Answer

It's going to depend.
Specifically, where is the heavy lifting done? If you have code that runs fast on a single CPU, it may be anti-productive to wrap that in a parfor loop as you'll have to distribute and re-gather the data every time.
I would suggest coming up with a test case and using tic and toc to time how long it takes for parfor in each of the cases.
tic;
parfor ii=1:51
do something
for jj=1:100
do something
for kk=1:200
do something
end
end
end
a = toc;
tic;
for ii=1:51
do something
parfor jj=1:100
do something
for kk=1:200
do something
end
end
end
b = toc;
tic;
for ii=1:51
do something
for jj=1:100
do something
parfor kk=1:200
do something
end
end
end
c = toc;
fprintf('parfor in 1st loop: %ds\nparfor in 2nd loop: %ds\nparfor in 3rd loop: %ds\n',a,b,c);
Also have a look at the matlab profiler to see which functions take longer to run, and where you should be spending your time optimizing your code.