Is there a way to set thread affinity to a processor core with the boost thread library?
Asked Answered
S

4

4

And would it be a good idea to do so if I needed the processor cores running on 100% for a dozen seconds or would I get better performance if I let the system decide how to handle the threads?

What I need is fast execution and I'm worried the system might spend a few seconds before using all the cores, but I haven't found any way of doing this with boost thread.

Sergo answered 11/11, 2012 at 21:29 Comment(0)
S
4

You'll first need to call the get_native_handle member function then pass the obtained handle to a platform specific function to set the thread's CPU affinity (i.e. pthread_setaffinity_np/SetThreadAffinityMask/...).

As to if that's a good idea: profile & find out...

Sheers answered 12/11, 2012 at 20:13 Comment(0)
C
6

Your operating system's scheduler should take care of this for you on the order of milliseconds, not seconds. The details depend on which OS you're running on, but one common approach is that when the scheduler runs on an idle CPU, it will check to see if it can steal a thread from another CPU that has too many. That quickly balances the threads across all CPUs.

As a general rule, if you need to set CPU affinity, either you have a very specific and unusual use case, or your operating system has a bug.

Colubrine answered 11/11, 2012 at 21:35 Comment(1)
Well, I had to find this out for myself in the end. But you were right, it's best to let the system decide. I had slightly better performance, overall, and less code to deal with.Sergo
E
6

In very high performance systems, performance improves if you assign low memory footprint tasks that are sensitive to cache to a particular core and the core does pretty much only this task. The effect is to improve cache hit rates which can make a huge difference. If this is not the case, the leave things to the OS's scheduler. It is (as noted above) a rather specialised situation, but when they arise, processor affinity is worth looking into.

Ectype answered 12/11, 2012 at 19:46 Comment(2)
And to add on - in cases like this - you will almost always need to isolate a CPU core as well. Setting thread affinity to a CPU core without isolating that CPU core will imply that your thread will end up competing with other threads for that particular CPU core. And if another thread gets to run on that core - your cache will be polluted. So - if you are doing thread affinity - you should absolutely isolate that CPU core as well. If you cannot isolate that CPU core - you probably should not be doing thread affinity.Fiddle
This is a critical point. You really don't want another process clobbering your cache.Ectype
S
4

You'll first need to call the get_native_handle member function then pass the obtained handle to a platform specific function to set the thread's CPU affinity (i.e. pthread_setaffinity_np/SetThreadAffinityMask/...).

As to if that's a good idea: profile & find out...

Sheers answered 12/11, 2012 at 20:13 Comment(0)
P
0

There is one more reason for affinity set. For some modern systems (e.g. ARM based SOCs) power saving technology significantly affects scheduler. The system run as minimum cpu cores as possible. If all current tasks can live on one core only the other cores are sleeping. If the load suddenly rises (running new heavy task) there is one or more cores need to be waked up. This process is relatively slow, so it can give your task going lack of cpu while scheduler is balancing load for more cores. With manual affinity set to a few threads/processes you can keep all cores running (and always be ready for sudden load rise). It looks like to be good for pseudo real-time tasks.

Purgatorial answered 16/9, 2015 at 8:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.