Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I programmatically pick and choose which core of a multi-core CPU my thread should run on?

Tags:

linux

Or is it controlled by the operating system? I hear the new Go language from Google has built-in features for a programmer to go that granular, or have I understood it wrong?

like image 615
Murali Avatar asked Dec 06 '09 08:12

Murali


2 Answers

For linux os, sched_setaffinity is your answer. It's supported since linux kernel 2.5.8.

Name

sched_setaffinity, sched_getaffinity — set and get a process's CPU affinity mask

#define _GNU_SOURCE
#include <sched.h>

int sched_setaffinity(  pid_t pid,
    size_t cpusetsize,
    cpu_set_t *mask);

int sched_getaffinity(  pid_t pid,
    size_t cpusetsize,
    cpu_set_t *mask);

The affinity mask is actually a per-thread attribute that can be adjusted independently for each of the threads in a thread group. The value returned from a call to gettid(2) can be passed in the argument pid. Specifying pid as 0 will set the attribute for the calling thread, and passing the value returned from a call to getpid(2) will set the attribute for the main thread of the thread group. (If you are using the POSIX threads API, then use pthread_setaffinity_np(3) instead of sched_setaffinity().)

like image 72
Sam Liao Avatar answered Oct 01 '22 01:10

Sam Liao


It is determined by the operating system.

You can set hints for it with pthread_attr_setaffinity_np().

But the operating system can override you. The call above is only a suggestion your program makes to the OS.

As for Go, I haven't worked with it yet or even looked at it too deeply, but my understanding of Go is that a lot of the parallelism is rather implicit. You have co-routines (they say "go-routines", very punny) and communication between them. It seems like CPU affinity and the concept of threading itself is separate from that. That is, the language runtime could even decide to do it all on 1 CPU if it decides that's best... But again, I caution that I haven't looked at it too deeply, so I could be wrong. :-)

like image 45
asveikau Avatar answered Oct 01 '22 00:10

asveikau