MATLAB uses multicores automatically on Unix?

by Forrest Sheng Bao http://fsbao.net

Tested on Ubuntu Linux 9.10 and Mac OS X 10.6.


I am studying MATLAB Parallel Computing Toolbox these days. Then I notice an interesting thing. It seems MATLAB can use two cores automatically without the need for me to turn on parallel features, e.g., parfor.

Here is a simple code

clear all;
scale2 = 120;
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

The first part uses regular for loop while the second part uses the parallel for loop (parfor). I have only two cores. So I only open two local labs.

But the information from "top" is funny. When the regular for loop runs, both cores are working, like this:

top - 17:06:30 up 17 days, 21:06,  5 users,  load average: 2.27, 2.48, 2.47
Tasks: 225 total,   1 running, 223 sleeping,   0 stopped,   1 zombie
Cpu(s): 99.0%us,  0.7%sy,  0.0%ni,  0.0%id,  0.0%wa,  0.3%hi,  0.0%si,  0.0%st
Mem:   2024568k total,  1296560k used,   728008k free,    36792k buffers
Swap:  4200988k total,   350956k used,  3850032k free,   303616k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND            
 8042 forrest   20   0 1118m 335m  31m S  198 17.0  14:05.05 MATLAB  

On GNOME System Monitor, two cores are both running at 100%.

When parallel loops are running, there are two jobs named MATLAB, each of which takes one core.

top - 17:08:20 up 17 days, 21:08,  5 users,  load average: 2.83, 2.58, 2.50
Tasks: 229 total,   2 running, 226 sleeping,   0 stopped,   1 zombie
Cpu(s):  2.5%us,  1.0%sy,  0.0%ni, 95.9%id,  0.5%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   2024568k total,  1477524k used,   547044k free,    36900k buffers
Swap:  4200988k total,   350956k used,  3850032k free,   305192k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND            
16049 forrest   20   0  897m 137m  53m S   99  6.9   0:10.43 MATLAB             
16055 forrest   20   0  873m 134m  54m S   97  6.8   0:10.19 MATLAB 

Here is the timing result:

serial_time2 =

   31.9760

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

parallel_time2 =

   18.2054

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

So, MATLAB seems to try to use two cores even when parallel codings are not used on UNIX machines. But it does not "fully" use two cores though two cores are both working 100% in regular for loops. I guess this has to do with the communication overhead.

1 comment:

Michael Croucher said...

Some functions in MATLAB automatically use multiple cores without the need to have the parallel toolbox installed and eig is one of them. To find out others try the following link

http://www.walkingrandomly.com/?p=1894

Cheers,
Mike