Determine what CPU my thread is on
Is there a library/system call that will return to me what CPU core a thread of execution is running on? I've looked for a bit on the net already and also in /usr/include and couldn't find one.
getcpu() and sched_getcpu() are two that I found, but when I include the appropriate header files (linux/getcpu.h, and sys/sched.h respectively), gcc says getcpu.h doesn't exist and the linker complains it cannot find the implementation in sched.h. I'm sure I'm doing something really stupid or overthinking... What I am doing is running an OpenMP application and specifying a list of cpu cores to run on with GOMP_CPU_AFFINITY. I want to make sure that each core is getting the same number of threads. **UPDATE** It seems that this code works, but only with glibc >= 2.6, and my machine has 2.5: Code:
#define _GNU_SOURCE It seems that getcpu() exists, but there is no glibc wrapper for it. The manpage for getcpu states that I must directly invoke it with syscall() and only with kernels 2.6.19+ (I have 2.6.27). Here's more of what I tried: Code:
$ grep -rin SYS_getcpu /usr/include -AM |
Would sched_getaffinity() help?
Cheers, Steve |
Use containers (without CPU affinity) - let the scheduler worry about the details. Go find some (other) real work to do - for yourself I mean.
This is micro-management - if your manager did it to you you would (or should) complain like hell. Let algorithms handle that crap, not carbon based life forms (i.e. you). Look up cgroups. |
@stevexyz
I thought about that, but haven't given it a try yet. The benchmark I run spawns 24 threads using OpenMP and I use an environment variable to tell it to put each thread on its own core. I'm getting weird results and want to verify that it is doing what I am telling it to. Thinking about it now, that may be what it is using, sched_setaffinity(). @syg00 I am doing high-performance computing research and need to know for a fact that thread N is on CPU core N. So no, I cannot be ignorant about what the scheduler is doing. Pinning is required for me. I will take a look at cgroups, thanks for the suggestion. |
All times are GMT -5. The time now is 03:01 AM. |