[LINUX] How to set CPU affinity for process threads

Note that I had to set CPU affinity for threads to do a certain experiment.

What is CPU affinity?

Process threads run on some CPU core depending on the OS. By setting the CPU affinity, the CPU cores to be executed can be arbitrarily restricted and specified.

Set CPU affinity for process

Linux has a function sched_setaffinity for setting CPU affinity.

For the time being, an example


#define _GNU_SOURCE
#include <sched.h>

int main() {
  pid_t pid;
  cpu_set_t cpu_set;
  int result;

  pid = getpid;
  CPU_ZERO(&cpu_set);
  CPU_SET(2, &cpu_set);

  result = sched_setaffinity(pid, sizeof(cpu_set_t), &cpu_set);
  if (result != 0) {
    //error
  }

  //processing

  return 0;
}

The cpu_set_t structure represents a set of CPUs. Initialize the cpu_set_t structure with the CPU_ZERO function, and then specify the CPU core to be executed with the CPU_SET function. For a 4-core CPU, specify a value from 0 to 3 as the first argument. For details, refer to Manual.

sched_setaffinity The argument of the function is the process ID, the data size of cpu_set_t, and the pointer of cpu_set_t, in that order.

The first argument can be the getpid function. The second argument is basically sizeof (cpu_set_t). The third argument specifies the pointer of the cpu_set_t structure variable specified by CPU_SET.

Set CPU affinity for threads

With the above method, it is set in the process itself, so if you want to set affinity for each thread, it is a little different.

Depending on the environment, it is possible with pthread_setaffinity_np. The program example is omitted because it is described in Manual.

If the above function cannot be used, it can be set by passing the thread ID as the first argument of the sched_setaffinity function. If you can use the gettid function, you can just throw the pid_t structure variable obtained by the gettid function.

If the gettid function cannot be used, call the system call directly to get the thread ID.

Example of getting the thread ID using a system call and setting the CPU affinity


#include <sched.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/types.h>


pid_t gettid(void) {
  return syscall(SYS_gettid);
}


void* thread_main(void* args) {
  pid_t pid;
  cpu_set_t cpu_set;
  int result;

  pid = gettid;
  CPU_ZERO(&cpu_set);
  CPU_SET(2, &cpu_set);

  result = sched_setaffinity(pid, sizeof(cpu_set_t), &cpu_set);
  if (result != 0) {
    //error
  }

 //processing

  return NULL;
}


int main() {
  pthread_t p;
  pthread_create(&p, NULL, thread_main, NULL);

  //processing

  return 0;
}

Summary

Please point out any mistakes.

Recommended Posts

How to set CPU affinity for process threads
How to set up Ubuntu for Windows Subsystem for Linux 2 (WSL2)
[For non-programmers] How to walk Kaggle
How to set cron for regular Python scraping on Sakura server.
How to set the output resolution for each keyframe in Blender
How to set proxy, redirect and SSL authentication for Python Requests module
[Blender] How to set shape_key with script
How to set optuna (how to write search space)
How to set up SVM using Optuna
How to set the server time to Japanese time
How to make a process thread run only on a specific CPU core
How to set xg boost using Optuna
[Python] Organizing how to use for statements
How to install Windows Subsystem For Linux
How to use Pylint for PyQt5 apps
How to use "deque" for Python data
How to write this process in Perl?
How to use fingerprint authentication for KDE
[For recording] Keras image system Part 1: How to create your own data set?
How to use MkDocs for the first time
How to make Spigot plugin (for Java beginners)
How to set up Random forest using Optuna
How to use Template Engine for Network Engineer
How to set browser location in Headless Chrome
How to set Django DB to mongodb visual studio 2019
How to use data analysis tools for beginners
Device driver to get CPU information for ARM
How to write a ShellScript Bash for statement
How to set up Random forest using Optuna
How to create a shortcut command for LINUX
How to set up a local development server
[ESXi (vCenter)] How to add NIC for CentOS 7.3
[For beginners] How to study programming Private memo
How to find the correlation for categorical variables
Find a guideline for the number of processes / threads to set in the application server
How to set the development environment for each project with VSCode + Python extension + Miniconda
How to set up public key authentication in ssh
How to check for missing values (Kaggle: House Prices)
How to process camera images with Teams or Zoom
[BigQuery] How to use BigQuery API for Python -Table creation-
[For beginners] How to use say command in python!
How to set up a Python environment using pyenv
[For beginners] How to study Python3 data analysis exam
How to run python in virtual space (for MacOS)
[Go] How to create a custom error for Sentry
How to make unit tests Part.2 Class design for tests
How to set layer on Lambda using AWS SAM
I thought about how to learn programming for free.
How to create a local repository for Linux OS
[Blender] How to dynamically set the selection of EnumProperty
How to build a development environment for TensorFlow (1.0.0) (Mac)
[Introduction to Udemy Python3 + Application] 30. How to use the set
Python # How to check type and type for super beginners
How to set up and compile your Cython environment
Compare how to write processing for lists by language
[Introduction to Python] How to write repetitive statements using for statements
How to set up WSL2 on Windows 10 and create a study environment for Linux commands
How to set variables that can be used throughout the Django app-useful for templates, etc.-