3

I use the taskset to set a multi-thread process to run on a Linux host as below:

task -c 1,2 ./myprocess

Will a particular thread always run on a particular CPU, for example, thread 1 always run on c1? or it will run on c1 or c2 at different times?

flyisland
  • 397
  • 2
  • 8

1 Answers1

3

No, the filter is applied to the whole process and threads can move between (the restricted list of) cores. If you want threads not to move, then you need set the affinity of each thread separately (eg. using pthread_setaffinity_np for example). Note that you can check the affinity of threads of a given process with the great hwloc tool (hwloc-ps -t).

Note that some libraries/frameworks have ways to do that more easily. This is the case for OpenMP programs where you can use environment variables like OMP_PLACES to set the affinity of each thread.

Jérôme Richard
  • 41,678
  • 6
  • 29
  • 59
  • Thanks, then I will start multiple instances of the process, each running on a core with lesser threads. – flyisland May 22 '22 at 15:01
  • I am using, for example, `taskset -c 7,8 ./executable` but it is taking only one core out of 7 and 8 always. Where 7 and 8 are isolated cores on the centOS machine. Any inputs on how to make sure the executable uses both 7 and 8th cores? – SatKetchum Jul 21 '22 at 14:12
  • @SatKetchum It is not clear what is a "processor" (aka "CPU") in the `taskset` documentation. AFAIK, a "processor" in this context is a processing unit so it can be an hardware thread on microprocessor chip supporting SMT or simple cores. If the 7 and 8 places are two hardware threads mapped on the same core, then it will often be less efficient than if you provide two hardware threads ID of different cores. In practice, this is even a bit more complex: there are logical and physical IDs and you choose the right one even though the doc nearly never tell which one is required... – Jérôme Richard Jul 21 '22 at 17:41
  • @SatKetchum For system commands, physical IDs are generally the right one. Tools like **`hwloc`** make all of this much more clear and avoid (very common) mistakes, especially `lstopo` and `hwloc-calc`. – Jérôme Richard Jul 21 '22 at 17:47