How to Pin Workers to Specific Cores
10 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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.
0 Commenti
Risposta accettata
Edric Ellis
il 8 Apr 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 Commenti
Steve Lantz
il 10 Nov 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.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Parallel Computing Fundamentals in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!