How to Pin Workers to Specific Cores

4 次查看(过去 30 天)
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.

采纳的回答

Edric Ellis
Edric Ellis 2015-4-8
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 个评论
Steve Lantz
Steve Lantz 2017-11-10
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Parallel Computing Fundamentals 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by