4386427
4386427

Reputation: 44284

How control which numa node executes my program

I have a server with 4 numa nodes each having 8 cores with hyper threading. So I have 64 logical CPUs.

I have some programs producing data and other programs consuming data. These programs are written in c++ (11). I want to start a number of producers and a number of consumers programs.

To maximize performance I like to control where the program instances execute. In other words a consumer-program should be on the same numa node as the corresponding producer.

In the end I need to control which core the program is running on.

I'm using c++ (11).

How can I control the way by programs is distributed?

EDIT: Maybe I should add that the server is running Linux. A solution for Linux would be great but a solution supporting both Linux and Windows would be even better.

Upvotes: 1

Views: 1528

Answers (1)

Soren
Soren

Reputation: 14688

This code should make your thread run on a specific core or subset of cores -- core 17 and 18 in this example;

cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(17, &cpuset);
CPU_SET(18, &cpuset);
if (pthread_setaffinity_np(thread[0], sizeof(cpu_set_t), &cpuset) != 0)
    throw error"Failed to set affinity for thread");  

You can allow your thread to have affinity to multiple number of cores, so you can setup the mask so any core on the same cpu is allowable, or tinker with the allowable set for maximum L2/L3 cache performance.

You can write your own logic around that sniplet for allowing the remaining threads on other cores.

This still works in in case you have multiple processes you want to control, instead of multiple threads, however you would need to either determine the core allocation ahead of time, or create an external service which each program can ask for which set of core would be allowable.

Upvotes: 3

Related Questions