Why does function become slower when placed in spmd block?

8 次查看(过去 30 天)
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.

采纳的回答

Edric Ellis
Edric Ellis 2019-12-5
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Distributed Arrays 的更多信息

标签

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by