Why does function become slower when placed in spmd block?

7 visualizaciones (últimos 30 días)
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
delete(obj);
parpool('local', nlab);
end
%% 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
tic;
fun(a,lab);
t1=toc;
t = t + t1; % record time and sum
end
end
fprintf('normal time %.3f\n', t);
%% spmd
t = 0;
spmd
b = sum(a);
for repeat = 1:nrep
tic;
fun(a,labindex);
t1=toc;
t = t + t1;
end
end
ttot = 0;
for ii = 1:nlab
ttot = ttot + t{ii};
end
fprintf('spmd time %.3f\n', ttot);
% below is a time-consuming test function
function y = fun(a, k)
y=0;
for l = 1:length(a)
y = y + k*a(l);
end
end
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.

Respuesta aceptada

Edric Ellis
Edric Ellis el 5 de Dic. de 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.

Más respuestas (0)

Etiquetas

Productos


Versión

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by