8

I have Matlab R2012b for Ubuntu 64 bits. I have a Intel Core i3 CPU M 330 @ 2.13GHz × 4.

I want to use parfor to parallelize 4 loops at same time. Because Intel Core i3 has 2 Cores and 4 Threads I use this code:

if matlabpool('size') == 0 % checking to see if my pool is already open
    matlabpool(4)
else
    matlabpool close
    matlabpool(4)
end

And I obtain the following error:

Error:

You requested a minimum of 4 workers, but the cluster "local" has the NumWorkers property set to allow a maximum of 2 workers. To run a communicating job on more workers than this (up to a maximum of 12 for the Local cluster), increase the value of the NumWorkers property for the cluster. The default value of NumWorkers for a Local cluster is the number of cores on the local machine.

Why? The default value of NumWorkers in my machine is 2 but if I can do 4 loops at the same time, how do I get it?

2 Answers2

10

To increase the default NumWorkers, open the Cluster Profile Manager (Parallel->Manage Cluster Profiles). Pick the localprofile, click edit, and increase NumWorkers to the maximum possible value (in your case 4). Now it is possible to create a matlabpool with more workers than physical cores on your machine.

However, note that using more workers than cores might lead to decreased performance as compared to having the same number of workers as cores.

H.Muster
  • 9,297
  • 1
  • 35
  • 46
  • Two questions: why more workers than cores decreased performance? How I can know the default value of NumWorkers for a Local cluster? – Manuel Ignacio López Quintero Feb 20 '13 at 16:28
  • 3
    The default value is the number of physical cores (this is even stated in the error message you quoted in your question). More workers than cores *can* decrease your performance because they do not run on physically separate cores, i.e., they might influence each other. – H.Muster Feb 20 '13 at 16:31
  • Also note that one Matlab worker can use more than one hardware thread or core concurrently, since some of Matlab's function implementations are internally multithreaded. One worker could temporarily saturate all cores, depending on what code it's running. The nworkers = physical cores is just a rough rule of thumb for typical CPU utilization. The more your CPUs are saturated, the more overhead there's going to be in context switching and cache contention, and overall processing throughput can go down. YMMV. – Andrew Janke Nov 13 '14 at 12:07
  • What if I want less? Or If I want to know how many can I have in the current system? – Royi Mar 25 '15 at 14:17
  • This setting does not persist between sessions, and neither does Franck's method below. Is such expected? Do I just have to run Franck's code every time if I really want a different NumWorkers than my number-of-cores? – tsbertalan Feb 13 '16 at 17:35
9

To programmatically change the value of NumWorkers from 2 to 4 of the local cluster profile, you can use:

myCluster = parcluster('local');
myCluster.NumWorkers = 4;  % 'Modified' property now TRUE
saveProfile(myCluster);    % 'local' profile now updated,
                           % 'Modified' property now FALSE    
Franck Dernoncourt
  • 77,520
  • 72
  • 342
  • 501