pthread_attr_setaffinity_np and logical core

0

I am a bit confused about CPU logical cores and physical cores and scheduler affinity. Let's assume there is a 4 physical core CPU where each core supports 2 hyperthreads. Henceforth, according to /cpu/procinfo logical core 0 and logical core 8 are inherently the same physical core.

Now, assume two threads:

Case I: the two threads are launched pthread_attr_setaffinity_np with logical core 0.

Case II: One thread is launched with pthread_attr_setaffinity_np with logical core 0 and another with logical core 8

Would there be any difference in both cases?

Tushar Goyal

Posted 2018-02-10T22:58:09.663

Reputation: 1

Answers

0

Yes, there will be a significant difference. With both threads set to logical core 0, the two threads will never run at the same time and will take turns using logical core 0. With one thread to logical core 0 and the other to logical core 8, the two threads can run at the same time, sharing the resources in that physical core.

Imagine if one thread does almost entirely integer operations and the other does almost entirely floating point operations. If they're both tied to logical core 0, the physical core will alternate having its integer units idle and its floating point units idle. With one set to 0 and one set to 8, one thread can use the integer units while the other thread is using the floating point units of the physical core.

David Schwartz

Posted 2018-02-10T22:58:09.663

Reputation: 58 310

Why would that happen? On the hardware level, the physical core doesn't have the concept of logical cores. It just tries to execute as much instruction from the multiple execution contexts available to it. – Tushar Goyal – 2018-02-11T00:14:03.967

@TusharGoyal On the hardware level, the physical core has two sets of everything that's specific to a logical thread such as registers. Those two sets of execution contexts share execution units. If both threads are using the same logical core, only one execution context is available to the physical core at a time. If each thread is on a different logical core, then both execution contexts are available to the physical core at the same time. – David Schwartz – 2018-02-11T03:44:01.070