why is mex parfor slower them mex for?

5 views (last 30 days)
I am starting to work with the Parallel Computing Toolbox, and just constructed an FIR filter example to compare for and parfor
coefs = [-0.00393617608745112 -5.95945405003999e-05...] length 1x10498
values = [30.3750000000000 30.3760000000000...] length 1x131000
tic;
outVal = FIRMP(coefs,values);
%outVal = FIRMP_mex(coefs,values);
time = toc;
with function FIRMP
function [result] = FIRMP(coefs, values)
coefLen = length(coefs);
valLen = length(values);
result = zeros(size(values));
(par)for I = 1 : valLen - coefLen;
suma = 0;
for J = 1 : coefLen
suma = suma + coefs(J)*values(I + J);
end
result(I) = suma;
end
end
I used 4 threads and got this results
for : time= 13.5s
parfor: time = 5.5s
It is OK, but if I create C++ mex (matlab CODER) and run again, the result has changed
for : time = 3.1s
parfor: time = 4.3s
why is the 'parfor' in C++ mex slower than 'for'?

Accepted Answer

Ryan Livingston
Ryan Livingston on 29 Aug 2018
Edited: Ryan Livingston on 29 Aug 2018
When I try your example on Linux (Debian 9) using GCC I see a good speedup with parfor in generated MEX:
for : time = 1.3s
parfor : time = 0.4s
On Windows 10 using Microsoft Visual Studio 2017, I see a much more modest speedup:
for : time = 1.3s
parfor : time = 1.0s
What compiler and OS are you using?
One thing that may be happening for certain compilers is that each of the parfor loop iterations are very fast. When this is the case, the overhead of managing threads can dominate the loop execution time. This can ruin any possible parallelism gains.
The Coder documentation covers this in some detail:
as does the MATLAB parfor documentation:
  6 Comments
Ryan Livingston
Ryan Livingston on 3 Sep 2018
You're welcome Josef. Glad to hear you found a solution.

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB Code Analysis 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!