Affinity mask
{{more citations needed|date=April 2023}}
An affinity mask is a bit mask indicating what processor(s) a thread or process should be run on by the scheduler of an operating system.{{Cite web |date=January 27, 2022 |title=SetThreadAffinityMask function (winbase.h) - Win32 apps |url=https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setthreadaffinitymask |access-date=April 7, 2023 |website=learn.microsoft.com |language=en-us}} Setting the affinity mask for certain processes running under Windows can be useful as there are several system processes (especially on domain controllers) that are restricted to the first CPU / Core. So, excluding the first CPU might lead to better application performance.
Windows API
Thread affinity in Microsoft Windows can be specified with the SetThreadAffinityMask
function. Forcing of each OpenMP thread to distinctive cores in Windows can be accomplished by means of the following C code:
#include
#include
// Set OpenMP thread affinity
void set_thread_affinity () {
#pragma omp parallel default(shared)
{
DWORD_PTR mask = (DWORD_PTR )1 << omp_get_thread_num();
SetThreadAffinityMask(GetCurrentThread(), mask);
}
}
See also
References
External links
- [http://msdn.microsoft.com/en-us/library/ms686247(VS.85).aspx MSDN article on SetThreadAffinityMask function]
- [https://web.archive.org/web/20041114215425/http://linuxcommand.org/man_pages/taskset1.html Taskset, a tool to set the affinity mask on Linux]
- [http://cpubalancer.brels.net/ CPU Balancer, a free, open-source utility that distributes Windows XP processes uniformly over logical processors using the affinity mask]
{{operating-system-stub}}