2

Not sure if that is even possible in a managed language, but is it possible to actually make an application perform better in the presence of hyper threading ?

Is there something specific that can be done in the code ? , maybe some functions run better than other in HT environments ?

I know that to benefit from multi-threading, all that is needed is to spawn new threads to perform heavy cpu tasks, but I am not sure what is required for HT ?

sharp12345
  • 4,420
  • 3
  • 22
  • 38
  • that's a very broad question, you'd be better off asking it on programmers.stackexchange.com. – didierc Mar 09 '13 at 19:08
  • The question is a bit confusing; are you asking whether there's a way to optimize .NET programs to deal with a hyperthreaded environment (in which there are "virtual" cpus) vs one in which there are that many "real" CPUs? Or is your question simply "how do I take advantage of multiple cores?" regardless of whether the cores are real or virtual? – Eric Lippert Mar 09 '13 at 19:08
  • edited the answer to remove some of the confusion, hope I removed enough to be answerable. @EricLippert – sharp12345 Mar 09 '13 at 19:12
  • 2
    It's the operating system's job to make the hyperthreaded "virtual" cores look like real cores. Unless you're writing an operating system or other software that depends on details of how the processor works, I wouldn't worry about it. Treat hyperthreaded cores just like you'd treat hardware cores. – Eric Lippert Mar 09 '13 at 19:32

1 Answers1

4

Depends a lot on the actual hardware you have. Read Be aware: To Hyper or not to Hyper for a more in dept analysis. Slava's point in the link is that in HT the virtual cores share the cache and a the virtual core workload can trash the cache of the 'main' core and, even though it can do at best some 20% of workload compared with the main core, becuase it evicts the hot cache of the main core, it cause the main core to stall and overall results in worse performance than w/o HT.

But a lot of things have changed since the article was posted, HT post Nehalemis significantly better.

I know that to benefit from multi-threading, all that is needed is to spawn new threads to perform heavy cpu tasks

That is quite a naive view. You must read Rick Vicik articles on high performance windows programs (they apply to managed apps too) and you must absolutely understand CPU Caches and Why You Care.

Remus Rusanu
  • 288,378
  • 40
  • 442
  • 569
  • So what you mean is that optimizations in the way user threads are started should done when running on HT machines, since the way parallel execution takes place on the physical CPU is different than on a multi-core CPU. But since no dedicated API for this exists, is it worth the effort of doing this? I know that for BizTalk and SQL Server MS recommends disabling HT and warns about sub-optimal performance in that mode, but for apps that just need to do something in a worker thread what do you suggest? – Marcel N. Mar 09 '13 at 19:49
  • 2
    @marceln: My suggestion is: set a performance goal. Measure to see if you met your goal. If you did, take the afternoon off and go to the beach. If you didn't, get out a profiler and optimize the slowest thing. The odds that your worker thread is slow because of subtle issues involving cache locality on hyperthreaded processors is pretty low. Can you describe the kind of work your worker thread is doing? – Eric Lippert Mar 09 '13 at 20:00
  • @EricLippert: I agree and I like to think that this is the course of action every good developer takes in this kind of situations. But, it never hurts to know what happens at lower levels (and for that, thanks Remus). – Marcel N. Mar 09 '13 at 20:05