Disable hyperthreading vs. changing ProcessorAffinity?
Asked Answered
H

1

7

I have noticed that several of my multi-threaded calculations run faster if I disable hyper-threading in the BIOS.

I have also learnt that I can programmatically disable the (logical) CPU:s by modifying the processor affinity for the current process, for example like this in C#:

// using System.Diagnostics;
var current = Process.GetCurrentProcess();
var affinity = current.ProcessorAffinity.ToInt32();
current.ProcessorAffinity = new IntPtr(affinity & 0x5555);

At least from a performance point of view, will disabling every second (logical) CPU by changing processor affinity have the same effect as disabling hyperthreading altogether?

Hospital answered 7/6, 2012 at 11:21 Comment(6)
Hmm, key issue is: are you going to ship your dev machine along with your software?Importunate
Another consideration might be operating system processes. If you disable hyperthreading in BIOS and your software tries to use all the cores, then when an operating system (or other background) process tries to run, it will compete with your software for processor access. Best bet may be to just try and benchmark both options and see what differences you find in the process times; the difference may be inconsequential to allow for the ease of not disabling hyperthreading. NOTE: If you don't have hardware control, check for AMD processors that have dedicated cores without hyperthreading.Anguiano
Thanks, Chris, a very helpful answer. I will do some more benchmarking to see whether any of the options will make a true difference in a regular user environment.Hospital
One of the big impacts of HT is the impact on the size of the cache available to each core; by turning off HT you doubled the cache-per-core. But the only way to know if changing affinity has an effect is to : measure it yourself.Lindie
Thanks Marc, good to know about the cache-per-core relationship.Hospital
Did you do benchmark to see difference between disabling ht in bios and disabling every second logical cpu? Im interested to know. Also, how do you know the current system hyper thread is enabled in bios?Ammieammine
I
2

You can try utilizing the NUMA APIs, or manually discover CPU topology with the CPUID instruction... But IMHOthe best solution is doing some sane defaults, and let the end-user tweak threading settings. Unless you have a specific hardware target, there's a fair amou t of possible scenarios to handle - logical vs. physical cores, hyper-threading or not, single- vs. multi-socket systems, cache and memory topology.

Isolation answered 7/6, 2012 at 14:10 Comment(2)
Thanks, snemarch. Interesting to learn about NUMA, this was new to me. You are most certainly right, it is probably best to provide a general solution and leave the performance tweaking up to the end user. From a principal point of view though, would you say that there is no "yes or no" answer to my original question?Hospital
It depends on the nature of your code as well as the hardware it runs on :) - if your code battles for execution units, then not running on HT cores (which doesn't necessarily have to be every 2nd) can help. But there's quite some difference between P4, Core2 and what AMD are doing.Isolation

© 2022 - 2024 — McMap. All rights reserved.