2010-10-31

When shoudln't you use MATLAB Parallel Computing Toolbox



by Forrest Sheng Bao http://fsbao.net

Well, a line of code is more meaningful than a thousand sentences. Here is the beef!

The MATLAB code:

%%%%%%%%%%%%%% begin test 1 %%%%%%%%%%%%%%%%%%
clear all;
clc

scale = 8192*8192;

A=zeros(1,scale);

for i=1:scale
  A(i) = sin(i*2*pi/1024);
end

serial_time = toc

tic

parfor i=1:scale
  A(i) = sin(i*2*pi/1024);
end

parallel_time = toc

%%%%%%%%%%%%%% END of test 1 %%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%% BEGIN of test 2 %%%%%%%%%%%%%%%

clear all;
scale2 = 10;
a= zeros(scale2);

tic
for k = 1:scale2
    a(k) = max(eig(rand(300)));
end
serial_time2 = toc

matlabpool open local 2

tic
parfor k = 1:scale2
    a(k) = max(eig(rand(300)));
end
parallel_time2 = toc

matlabpool close

%%%%%%%%%%%%%% END of test 2 %%%%%%%%%%%%%%%

The timing result:

serial_time =

    0.0048


parallel_time =

    0.2451


serial_time2 =

    3.8324

Starting matlabpool using the 'local' configuration ... connected to 2 labs.

parallel_time2 =

    2.5019

Sending a stop signal to all the labs ... stopped.

Therefore, using parfor does not guarantee speed up all the time.

No comments: