Matlabpool -- can't make it work
2 views (last 30 days)
Show older comments
Hello everyone!
I have a problem using matlabpool . Let's say I have a function that evaluates for 10 sec (using a single core) because of a large data set, and let's say that I have 10 cores available. Roughly said, if I divide this data set in 10 data subsets (1 for each core), overall duration would be 1 sec. However, in my case I don't get any speed-up at all!
Here is an example code:
% Number of cores
np = 4;
% Matlabpool for parallel computing
try matlabpool(np)
catch err
end
% Large data set
A = rand(2287500,7);
B = rand(2287500,7);
% Run function on a single core
% Duration: 0.45 sec
tic;
C = A.*sqrt(B.^3);
toc;
% Memory preallocation
x = cell(np,1);
y = cell(np,1);
z = cell(np,1);
% Divide data set in 4 sub-sets
for ii=1:np
lo = (ii-1)*(2287500/np)+1;
x{ii} = A(lo:lo+2287500/np-1,:);
y{ii} = B(lo:lo+2287500/np-1,:);
end
% Run function on a single core
% but with a single sub-set
% Duration: 0.15 sec
tic;
x{1}.*sqrt(y{1}.^3);
toc;
% Run function on a multiple cores
% with whole data set
% Duration: 6.25 sec
tic;
parfor ii=1:np
z{ii} = x{ii}.*sqrt(y{ii}.^3);
end
toc;
z = cell2mat(z);
% Validate results
e = abs(C-z);
max(max(e))
Please tell me what to do, it's driving me nuts. :(
0 Comments
Answers (2)
Edric Ellis
on 2 Dec 2013
Thanks for the detailed reproduction. In this case, MATLAB has already been able to run your code in a multithreaded manner. This is always more efficient than using PARFOR (at least on a single machine) since the overheads are lower. The only way you could speed this up would be to have multiple machines - and even then, you'd need to be sure that you weren't losing the advantage by transferring too much data. In this case, you're losing out twice because the amount of computation is relatively small for each element of data - because PARFOR has to transfer data to a remote process, you need to ensure that much more computation is performed per element of data transferred.
0 Comments
Marko Gulin
on 3 Dec 2013
1 Comment
Edric Ellis
on 6 Dec 2013
Yes, many built-in methods in MATLAB are already parallelized using multi-threading. If your code is able to take advantage of those methods, then using PARFOR locally will get you no benefit.
See Also
Categories
Find more on Parallel Computing Fundamentals 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!