Increasing number of workers - loss of performance

Viewed 254

if I type lscpu in the command-line:

CPU(s):              4
Thread(s) per core:  2
Core(s) per socket:  2
Socket(s):           1

so I have 2 physical cores.

I have no background on parallel computing, but I need it for my purposes. So, since I am a MatLab user, I'm interested in parfor loops, but I need to understand what is really going on.

I've red from MatLab documentation, that default number of workers is one per physical CPU core using a single computational thread, and also that this choice optimize performance. What I want to understand is how the number of workers affects the performance:

To see this, I tried to run (inspired by this) the following standard piece of code, changing the number of workers in the parpool line.

m = 500;
A = randn(m);
N  = 200;
parpool(1); 
tic
x = zeros(1,N);
parfor i=1:N
    x(i) = max(abs(eig(A)));
end
toc

and I measure with tic-toc the time taken.

With 1 worker: % Elapsed time is 26.534430 seconds.

With 2 workers: % Elapsed time is 14.528462 seconds.

With 3 workers: % Elapsed time is 14.403359 seconds.

With 4 workers: % Elapsed time is 17.946775 seconds.

If I go on with the workers, it takes more time.

I have two questions:

  1. I would expect to have the best performance with 2 workers: why with 3 workers I have still good results?

  2. Why more workers implies more time?

1 Answers

The gain in speed is not linear (double the number of workers, so half the computational time require is wrong). This due to an (almost) constant overhead, in which an underlying scheduler needs to break up the problem in chuncks and organize the distribution -- and eventually join the results again. So you will experience a margin benefit. Have a look at Gustafson's law on wikipedia.

It becomes worse if the problems are not independent so that the individual workers need to communicate with each other. The slowest worker slowdown the other workers eating up your overall speed-up.

Nevertheless, there is (almost) always an improvement if your switch to parpool -- the question is if it is worth it...

BTW, there is no benefit in hyperthreading as MATLAB simply does not use it. If it occupies a CPU with calculations, there is no advantage in registering a second thread that also want to perform calculations on this CPU...

Now, in your particular case: you have a maximum of four workers. However, if you use all four, your system may freeze. Plus there is no way that the background tasks of the system with higher priority can be bypass your calculations on a fee core, so the calculations will get interrupted. Therefore, it is unlikely that this is your optimal setting for your parpool -- I like to recommend to always use all-1 core as maximum.

This is all independent from the question whether this is a reasonable number of test to back-up your results statistically.

Related