windowsmultithreadingperformancewinapi

How to externally set CPU core affinity for a process on Windows systems with 64+ cores?


I am working on optimizing a multi-threaded application on a Windows system with more than 64 cores. Typically, I would use the SetProcessAffinityMask function to set the CPU core affinity for the entire process, but this approach is limited to systems with up to 64 cores due to its 64-bit mask constraint.

While I know about the SetThreadGroupAffinity function, it requires manual setting of affinity for each thread, including new threads post-initial setting. I am seeking a method to set affinity that can be applied externally to the process, impacting all current and future threads without the need to alter the process's internal code to manage this affinity.

Question: Are there any methods or tools that allow for setting and managing CPU core affinity externally for a Windows process, particularly suitable for systems with more than 64 cores? How can these methods ensure that the affinity settings apply to all threads within the process?


Solution

  • Process Lasso is able to enforce a CPU affinity on a multi-group process by continually reapplying the CPU affinity to all threads in the process using SetThreadGroupAffinity.

    There is unfortunately no one-time API call that can apply a CPU affinity to a multi-group process.

    A multi-group process is one that spans more than 64 logical CPU cores and therefore has threads scheduled to more than one processor group. You are correct that SetProcessAffinityMask and other older Win32 APIs are not designed to handle it. Kernel and newer Win32 APIs supply a processor group variable via GROUP_AFFINITY to indicate which processor group the bitmask pertains to.

    CPU Sets, a weaker form of CPU affinities, are a viable alternative, and also supported by Process Lasso. However, their behavior is less well defined, so they may not achieve your objective.