MATLAB: Parfor or parfeval, what is better

parallel computingParallel Computing Toolboxparfor

if I have some code that could be writed in same way with parfeval o parfor, what i have to use? What is better? Which is more advantageous, faster… e.g:
parfor i = 1:n
result = function(data);
end
or
for i = 1:n
F(i) = parfeval(@function, 1, data);
end
for i = 1:n
[index, value] = fetchNext(F);
end

Best Answer

At least some of the trade-offs are:
  • parfor is generally easier to use, and the code probably looks much more like your serial code
  • parfor loops can be used by people who don't have Parallel Computing Toolbox
  • parfor loops perform a degree of load-balancing to try and minimise overheads
  • parfeval gives you complete control over how the work is chunked up for the workers
  • parfeval is asynchronous, and lets the MATLAB client get on with other stuff while the workers are busy (e.g. updating plots or other visualisations)
Related Question