MATLAB Answers

Why does function become slower when placed in spmd block?

8 views (last 30 days)
Hi all,
I am using spmd to evaluate a function. However, I find that when I put the function into a spmd block, it becomes slower. I use the code below to test this
nlab = 4; % number of cores in use
nrep = 1e4; % repeatition time
%% configure parallel pool
obj = gcp('nocreate');
if isempty(obj)
parpool('local', nlab);
elseif obj.NumWorkers ~= nlab
parpool('local', nlab);
%% normal
a = rand(1e5, 1); % random data
t = 0;
for lab = 1:nlab
% this is to simulate spmd in a non-parallel manner
for repeat = 1:nrep
t = t + t1; % record time and sum
fprintf('normal time %.3f\n', t);
%% spmd
t = 0;
b = sum(a);
for repeat = 1:nrep
t = t + t1;
ttot = 0;
for ii = 1:nlab
ttot = ttot + t{ii};
fprintf('spmd time %.3f\n', ttot);
% below is a time-consuming test function
function y = fun(a, k)
for l = 1:length(a)
y = y + k*a(l);
The result shows that, for nlab = 8 (my computer has 8 cores), normal time is 8.9 seconds while spmd time is 24.4 seconds; for nlab = 4, normal time is 4.4 seconds while spmd time is 6.3 seconds; and for nlab = 1, both of them are 1.1 seconds.
It turns out that the more cores I use, the slower the function is. My guess is that when evaluating a function, MATLAB itself is actually using multiple cores evenif not in parallel mode. When I occupy all cores by using spmd, the MATLAB has only one core to use when evaluating a function so that it becomes slow.
Does anyone know the reason and possible solutions?
I was hoping to increase my program by nearly nlab times, however now it can only speed up for approximately twice. I would appreciate any ideas.


Sign in to comment.

Accepted Answer

Edric Ellis
Edric Ellis on 5 Dec 2019
Your desktop MATLAB client can use "built-in multithreading" for certain operations. By default, workers in a parallel pool use a single computational thread - because the expectation is that you'll run as many workers as you have physical cores in your system.
Therefore, if your program is able to take advantage of MATLAB's built-in multithreading already, then there is no possible benefit to using local workers - you're already at the limits of what your hardware can achieve. To get even better performance, you need additional resources, perhaps in the form of a MATLAB Parallel Server installation.

More Answers (0)

Sign in to answer this question.