how to set c++11 thread affinity to NUMA node on Windows?

Viewed 258

On Windows, how can I:

  • query how many NUMA nodes the system has
  • set affinity of an std::thread to the CPU cores of a specific NUMA node?
1 Answers

To get the index of the highest node:

auto highestnode = 0UL;
GetNumaHighestNodeNumber(&highestnode);

To get the affinity of all logical cores on a node for up to 64 total logical cores:

auto mask = 0ULL;
GetNumaNodeProcessorMask(nodeindex, &mask);

To set thread affinity:

SetThreadAffinityMask(GetCurrentThread(), mask);

Or for greater than 64 total logical cores:

auto node = static_cast<PGROUP_AFFINITY>(malloc(sizeof(PGROUP_AFFINITY)));;
GetNumaNodeProcessorMaskEx(nodeindex, node);

Where the mask for the node is nodes->Mask.

Large nodes with more than 64 logical cores will be split up into groups specified by nodes->Group.

Related