0

Hyperthreading can hurt performance of some applications and it should not be used. From the Microsoft website (https://msdn.microsoft.com/en-us/library/cc615012%28BTS.10%29.aspx):

It is critical hyper-threading be turned off for BizTalk Server computers. Hyper-threading makes the server appear to have more processors/processor cores than it actually does; however hyper-threaded processors typically provide between 20 and 30% of the performance of a physical processor/processor core. When BizTalk Server counts the number of processors to adjust its self-tuning algorithms; the hyper-threaded processors cause these adjustments to be skewed which is detrimental to overall performance.

Process Lasso program allows to disable hyperthreading for some processes:

You can use programs like Process Lasso (free) to set default CPU affinities for critical processes, so that their threads never get allocated to logical cores. We call this feature HyperThreaded Core Avoidance.

I've got some older programs which perform a lot of mathematical computations. It is frustrating to see them use one core if they could use 4. I want to rewrite them to use many threads. They use large continuous memory blocks so number of cache misses is minimal. My questions are following:

  • How to decide whether to use hyperthreading or not in your application ? (general guidance with some technical details if necessary)
  • Does it come down to performing experiments to make final decision ?
  • How to avoid hyperthreading in your application if it is not advantageous ? (examples in c++ and c)
Maciej
  • 9,355
  • 2
  • 15
  • 18
  • 2
    hyperthreading is (somewhat) a CPU-internal concept. You sound more like you're discussing threading in general than specifically tailoring code for hyperthreads since a hyperthread can be thought of as a "fake" core. – Olipro Nov 06 '15 at 20:21
  • Do you mean multithreading? – NathanOliver Nov 06 '15 at 20:21
  • Yes. Hyperthreading is a technical stuff of some CPUs. Multi-threading is a way of programming concurrent execution of code. In multithread context (or multi-process context) it clearly depends on the kind of treatments performed, about which you don't give details. – hexasoft Nov 06 '15 at 20:24
  • I mean hyperthreading. I read somewhere that it is not always good and you can avoid it. But I don't know any more details. – Maciej Nov 06 '15 at 20:24
  • So I don't understand. You can't decide to use or not hyperthreading as it is viewed by OS as standard CPUs. As a sysadmin you can activate or not HT but as a programmer you must rely on the available CPUs (reals or HT ones) to decide (let say) on how many thread you execute your code. – hexasoft Nov 06 '15 at 20:27
  • When 2 threads compete for resources of one core it can degrade performance. I want to know when it happens and how to avoid it. – Maciej Nov 06 '15 at 20:30
  • You *can't* decide this without knowledge about the CPU you are running on. Some codes will work mostly the same on some HT and not on some others. And it will depend on the code, the memory, the cache, the IOs specs… Some old Intel HT were horrible, some recent one are very fine for most codes. And some multi-cores (no HT) will not perform well due to IOs or cache issues. – hexasoft Nov 06 '15 at 20:42
  • Question was updated to make it more clear. – Maciej Nov 07 '15 at 11:47

2 Answers2

2

Basically, it comes down to configuring the number of concurrent threads executing CPU workloads. The OS is aware of hyperthreading, and will assign threads to physical cores until it runs out, and only if there are more threads than physical cores will it start assigning work to logical cores.

To decide whether the optimal number of threads is the number of physical or logical cores, measuring performance of your real tasks is the best approach. Synthetic benchmarks can teach you something about how hyperthreading works, but won't tell you what is best for your particular mix of instructions.

The exact way to control number of threads depends on the multithreading construct you use -- if you create threads yourself, it is obvious, but threadpool and automated parallelism frameworks such as OpenMP provide ways to tune thread count also.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • 1
    In reality if your program runs e.g. 4 threads on a 4C/8T CPU, there will be periods of time when 2 threads are assigned to the same physical core. That's probably because OS deals with few dozen more background threads at all times. Only explicit affinity control prevents that. Good answer nonetheless. – void_ptr Nov 06 '15 at 20:42
  • 1
    see `numactl` on procs that handle this. – hexasoft Nov 06 '15 at 20:44
  • @void_ptr: Sure, but because your threads are CPU bound, they pretty much will sit on separate physical cores, and the background threads will occasionally get scheduled onto logical cores stealing resources from your threads. This will happen even if you do assign affinity on your threads. – Ben Voigt Nov 06 '15 at 20:44
  • 1
    @BenVoigt - Well, no, "my" CPU-heavy threads will occasionally get scheduled on the same physical core. This has measurable performance impact. – void_ptr Nov 06 '15 at 20:45
  • 1
    @void_ptr: "occasionally", yes. It's worth testing with and without affinity assignment, but in most cases I doubt it will be worth the effort. – Ben Voigt Nov 06 '15 at 20:54
  • Question was updated to make it more clear. – Maciej Nov 07 '15 at 11:48
2

I don't know Process Lasso works wrt "disabling HyperThreading". For that particular app, the best you can do is to inject DLL into every process into the system, call SetProcessAffinityMask with something that only amounts to a guess, disable every other core, in the hopes that the OS will avoid scheduling to the hyperthreaded logical cores.

Guesses and hopes, there's nothing in the Windows API that will do this for certain. This answers your third bullet point.

You can disable HyperThreading as the BIOS level (usually).

I can't comment on the Microsoft advice of disabling HT for BizTalk, your linked article, since I can't find a date for this article. The only interesting bit was about "Assigning interrupt affinity to logical processors...", new to me. The only other advice in that article regarding HT is rather weak.

On a larger note: I don't know why you're asking about HyperThreading, when you should be concerned with multithreading in general. If you're concerned about multiple threads contending for the same shared resource... then don't use threads in your app.

A humorous aside: the same company also sells a product called SmartTrim, reminiscent of the RAM-doublers that were popular in the '90's.

Chris O
  • 5,017
  • 3
  • 35
  • 42