Why does function become slower when placed in spmd block?
Show older comments
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.
Accepted Answer
More Answers (0)
Categories
Find more on Introduction to Installation and Licensing 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!