3
votes

My CPU is 8x2600 MHz (Intel Xeon CPU E5-2670 0 @ 2.60GHz).
I have a data processing algorithm which can run in parallel written in Java. This function determines the number of concurrent threads during runtime with Runtime.getRuntime().availableProcessors()which returns 8.

This algorithm is 100% non-blocking. The CPU supports hyper threading with 2 threads per core.

Now should I run the algorithm with 8 threads because Java sees only 8 cores, or should I use 16 Java threads taking in to consideration the hyper threading provided by the CPU?

1
For more than 8 best to do some performance tests, including edge cases.didiz
The benefit is not only based on the number of cores. If we speak for one core only. The OS will give the hand to one thread for an small amount of time, your app is not the only one to ask for time, so if you have more than 1 thread, you will have the hand more often since your application will have more probability to have the hand. It is not exactly why, the logic behing this is complex but you will get the idea. (In french, we call that the "ordonnanceur", but I can't find a translation....)AxelH
Do you really have to know? Couldn't you just create a CachedThreadPool and leave the rest to its implementation?maraca
@AxelH a scheduler, roughly.Rogue
I think HT is disabled in your BIOS, or if enabled, unsupported by your OS. Runtime.getRuntime().availableProcessors() will return 16 if HT is enabled on an 8core CPU.alzee

1 Answers

1
votes

The ideal number of threads depend on the task itself. Context switching in modern CPU's can be somewhat expensive due to the fact that data used in the computation is cached heavily. Consider the situation where there are no IO related activities and the CPU cycles need not be wasted waiting. Then the maximum throughput would be achieved by having n_threads = n_cores. Context switching might be expensive even when hyper threading is available. But if there are IO activities, increasing the thread count beyond the number of cores can benefit.