Matlabpool -- can't make it work

1 view (last 30 days)
Marko Gulin
Marko Gulin on 29 Nov 2013
Commented: Edric Ellis on 6 Dec 2013
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. :(

Answers (2)

Edric Ellis
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.

Marko Gulin
Marko Gulin on 3 Dec 2013
Did I get this right -- MATLAB is already "paralelized" for matrix algebra? In above example I used random function - my function is a bit complicated (implementation of neural network, but with neural network output integration since inputs are in 1min resolution, and targets are in 1h resolution). I managed to implement this function in matrix algebra only (without for loops) using 3D arrays and built-in functions like bsx (times, plus, divide) function. Since input data set is quite large (18000*61 inputs, 18000 targets), evaluation of the neural network implementation on whole data set (sent at once) takes about 4 sec. But: (i) if I use parfoor with 4 cores such that whole data set is divided in 4 equal data sets (18000*61/4 inputs for each core) it takes about 4-5 secs (ii) if I use parfoor with 4 cores such that whole data set is divided in 4000 equal data sets (18000*61/4000 inputs for each core) it takes about 1.5 secs
Is there an answer why is this so, or the reason is just the implementation of neural network?
  1 Comment
Edric Ellis
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.

Sign in to comment.

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!