PARFOR is 10X Slower than FOR
Show older comments
I'm trying to understand why my use of the parfor is so much slower than using a for loop.
Both parfor and for implementations require some IO, namely the reading of a png file that is 20 Kb (buildingDesign.png attached).
The code called (getSpacing_HELP.m) takes this png file, converts it into a BW matrix and does several image processing operations on the matrix. The purpose is to calculate the average spacing between objects in an image.
The code shown here is a pared-down, simplified version that only includes the meat of the code. I'm using 36 workers on a Linux machine. Images below show the histogram of times for 1000 calls to getSpacing_HELP.m.
Why would the parfor implementation be so much slower? Any suggestions on ways to speed it up? In actual use, the spacing routine could be called a ~10^8 times, so this time difference is important.
ntot = 1000; % How many runs to test
% Create a directory to place copies of the file in. This is just to
% simulate the actual use of this code for troubleshooting.
mkdir tmp1
for jj = 1:ntot
theName = ['./tmp1/',num2str(jj),'_clipped.png'];
copyfile('./Files/buildingDesign.png',theName);
end
%
resultsMat = zeros([ntot 1]);
timeMat = zeros([ntot 1]);
tic
%parfor k = 1:ntot
for k = 1:ntot
fileName = ['./tmp1/',num2str(k),'_clipped.png'];
[singleResult,theTime] = getSpace_HELP(fileName);
resultsMat(k) = singleResult;
timeMat(k) = theTime;
end
histogram(timeMat), xlabel('Time (s)')
timeAll = toc;
disp(['Time to do all runs: ',num2str(timeAll)])


6 Comments
Carson Purnell
on 16 Jun 2022
You're re-using every variable within the loop, so this is not a valid test. Clear everything at the end of the loop or use unique variables at every loop iteration to fairly compare. for has optimization that parfor cannot make use of for cases like this.
Stephen23
on 16 Jun 2022
"You're re-using every variable within the loop"
Which is the normal, recommended, best way to write MATLAB code. This allows the JIT-engine to work efficiently.
"Clear everything at the end of the loop or use unique variables at every loop iteration .."
DO NOT do that unless you want to pointlessly slow down all of your code for no benefit. Ignore this bad advice.
"Why would the parfor implementation be so much slower?"
Why do you think that parfor should be faster than for for your problem? It is not the case that parfor is always faster. Have you read the MATLAB documentation on this?:
The only time it is worth using parfor is when the time saved running the operations inside the loop is greater than that of the extra time for the parfor overhead. This is computing, and nothing comes for free (something that beginners seem to forget when they expect infinitely fast computers with infinite memory, infinite-precision numerics, and overhead operations that take no time at all. Sadly reality does not meet these expectations).
Running a parfor is a much more complicated thing than running a simple for loop because the data needs to be partitioned and the results grouped again afterwards, so it is entirely possible that a parfor might be slower. This is clearly explained in the documentation: "There is overhead in calling parfor instead of for. If function evaluations are fast, this overhead could become appreciable. In particular, solving a problem in parallel can be slower than solving the problem serially."
So far you have not provided any actual justifcation, why you think parfor should be faster.
Carson Purnell
on 16 Jun 2022
My comment is specifically to point out why parfor is slower in this case. The efficient implementation of a standard for loop lacking the overhead of the parfor loop is what OP is missing, and this will show at least part of it. It will demonstrate how the parfor loop is slowing down the code for no benefit.
Paul Safier
on 16 Jun 2022
Edited: Paul Safier
on 16 Jun 2022
Edric Ellis
on 20 Jun 2022
Aha. I suspect that bwlookup can take advantage of MATLAB's intrinsic multi-threading. If that is the case, then your multithreaded desktop MATLAB process is already taking full advantage of all the cores on your system. The workers in a parallel pool run in single-threaded mode (by default). You can confirm this by either monitoring the processor utilisation of desktop MATLAB using top (or similar); or, you can force your desktop MATLAB into single-threaded mode for comparison purposes by using maxNumCompThreads(1).
Basically, any time your original for-loop code is dominated by stuff that is already multithreaded by MATLAB itself, there is no advantage to using a local parallel pool. You're already fully utilising your machine. In cases like this, you may see benefit from using parfor with multiple remote workers.
Paul Safier
on 21 Jun 2022
Accepted Answer
More Answers (1)
Steven Lord
on 16 Jun 2022
0 votes
Have you tried using the parallel profiler to determine what percentage of the time taken by the parfor code is spent on the actual computations and how much on overhead? You could try comparing comparing those parallel profiling results with the results of running the for loop version of the code in the MATLAB Profiler.
In order for your code to gain time when run in parallel, the amount of time you spend in the parallel setup and other overhead must be less than the amount of time that you save by running the iterations in parallel. If your overhead is high and/or the amount of time you save is small, your parfor loop could very well take more time to run than your for loop.
Think of grocery shopping with kids. If sending them over to the cereal aisle saves you two minutes but you have to spend five minutes searching the store to find them afterwards (eventually finding them in the candy or snacks aisle) you would be better off just going to the cereal aisle yourself.
3 Comments
Paul Safier
on 16 Jun 2022
Torsten
on 16 Jun 2022
Just out of curiosity:
What if you remove the settings
resultsMat(k) = singleResult;
timeMat(k) = theTime;
in the parfor loop ?
Paul Safier
on 16 Jun 2022
Categories
Find more on Parallel for-Loops (parfor) in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!






