How to Pin Workers to Specific Cores

조회 수: 3 (최근 30일)
Jonathan
Jonathan 2015년 4월 8일
댓글: Ayad Anwer 2019년 9월 6일
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.
Ayad Anwer
Ayad Anwer 2019년 9월 6일
Excellent...

댓글을 달려면 로그인하십시오.

추가 답변 (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