How to Pin Workers to Specific Cores

5 visualizaciones (últimos 30 días)
Jonathan
Jonathan el 8 de Abr. de 2015
Comentada: Ayad Anwer el 6 de Sept. de 2019
I would like to know how to pin parpool workers to specific cores. I have a dual socket system, so each socket has faster access to certain resources, like GPUs.
Therefore, say I have 8 workers in my pool and 8 GPUs. I want to pin each worker to a specific core on my dual socket system.

Respuesta aceptada

Edric Ellis
Edric Ellis el 8 de Abr. de 2015
What OS are you using? If Windows, you could adapt the code from this answer on Stack Overflow. One way to pick the affinitization is to use an spmd block after opening the pool and using labindex to choose the CPU.
On Linux, you can use this approach to set affinity. I would adapt that approach and use the undocumented feature('getpid') means of accessing the current process' PID, something like this
spmd
pid = feature('getpid');
system(sprintf('taskset -pc %d %d', labindex-1, pid));
end
  3 comentarios
Steve Lantz
Steve Lantz el 10 de Nov. de 2017
Here's a function that does the trick on Windows 7. It is based on the Stack Overflow link above.
function pin_process_to_core(coreidx,totalcores)
proc = System.Diagnostics.Process.GetCurrentProcess();
procbit = int32(2^(coreidx-1)); % will become mask
fprintf('For coreidx %2d, procbit is %s\n',...
coreidx,dec2bin(procbit,totalcores));
proc.ProcessorAffinity = System.IntPtr(procbit);
end
In typical usage, the argument coreidx corresponds to the labindex for each worker, so the function is meant to be called from (e.g.) within a spmd block.
The above calls to the .NET library actually fail if they are not enclosed in a function. If they appear directly within the spmd, MATLAB can't decide whether it's looking at a variable or a function, and it throws an error.
You can comment out the fprintf if you don't want to look at the masks that are set for each worker.
This trick can improve performance quite a bit if there are many cores and many workers, but only a few tasks. It prevents the few workers/tasks from roaming around from core to core, resulting in continual trashing of the caches.
Ayad Anwer
Ayad Anwer el 6 de Sept. de 2019
Excellent...

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Parallel Computing Fundamentals en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by