17
$\begingroup$

I do not know much about computer architecture, but some Intel processors have hyper-threading technology, which can apparently improve parallelization for applications that take advantage of it. In a processor with hyper-threading technology, the number of threads is usually twice the number of cores -- whereas in a processor without hyper-threading, the number of threads is equal to the number of cores.

Does Mathematica take advantage of hyper-threading?

My old (2007) desktop computer has an old Intel Core2 processor with 2 cores that does not support hyper-threading. So the processor has 2 threads and 2 cores. If I execute $ProcessorCount in Mathematica, it gives the output 2.

However, some new processors (i.e., Intel i7 series) support hyper-threading. For example, Intel's website says that the i7-4790 has 8 threads and 4 cores. If I were to execute $ProcessorCount on such a system, would the output be 4 or 8?

$\endgroup$
  • 3
    $\begingroup$ I have an Intel® Core™ i7-4790K CPU @ 4.00GHz × 8 and $ProcessorCount gives 4! :( $\endgroup$ – Mahdi May 25 '15 at 22:04
  • 3
    $\begingroup$ Of course. MMA does not "know" what's happening under the covers, the O/S scheduler handles that... $\endgroup$ – ciao May 25 '15 at 22:09
  • $\begingroup$ @ciao, well, it can get the information about how many physical cores are there if it needs to (at least under Linux-based OSes ... ) $\endgroup$ – Bichoy May 25 '15 at 23:26
  • $\begingroup$ Great question. Like @Mahdi, my Intel® Core™ i7-3820QM CPU has 2x hyper-threading, but $ProcessorCount gives 4, the number of physical processors. $\endgroup$ – bbgodfrey May 26 '15 at 1:10
  • 2
    $\begingroup$ Probable dupe of this. Simply put by Yes, default on v9 but only if you force it to on v10, and its impact is variable dependent on the calculation. So the simple answer is try it and see if it works for your use case. $\endgroup$ – Gordon Coale May 26 '15 at 8:44
21
$\begingroup$

In Mathematica 10 or newer, as pointed out by Szabolcs, $ProcessorCount will return the number of physical CPU cores, not the number of logical cores. Thus, on a system with 4 cores and 8 threads,

In[1]:= $ProcessorCount        

Out[1]= 4

Functionality which uses multiple kernels in parallel, for example ParallelEvaluate and friends, will then launch only 4 subkernels by default. This can be overriden by specifying an explicit number of kernels to start, say LaunchKernels[8].

Mathematica also uses other kinds of parallelism, where multiple threads are running in parallel within the same kernel or library. The number of kernel threads, used e.g. by Compile and many image processing functions can also be changed and is controlled by

In[3]:= SystemOptions["ParallelOptions" -> "ParallelThreadNumber"]

Out[3]= {ParallelOptions -> {ParallelThreadNumber -> 4}}

Similarly, the maximum number of threads used by the Intel MKL library (for instance, when performing machine precision linear algebra computations) is

 In[4]:= SystemOptions["ParallelOptions" -> "MKLThreadNumber"]

 Out[4]= {ParallelOptions -> {MKLThreadNumber -> 4}}

Note that Intel does not recommend using more threads in MKL than the number of physical cores.

Some functions may use all 8 hyperthreads in cases when performance will be increased (an example is Det and other linear algebra operations on exact numeric matrices).

$\endgroup$
  • $\begingroup$ Even in the old days of Pentium 4 with single CPUs with hyperthreading it has been useful to call LaunchKernels[2] to speed up Parallel* functions. However, until now there is no simple way to find out the number of logical processors. If an OpenMP environment variable like OMP_NUM_THREADS exists on the respective system, one could read it using GetEnvironment[]. $\endgroup$ – UDB Nov 25 '15 at 19:45
  • $\begingroup$ I noticed that this has gone unaddressed in many of the hyperthreading related posts, but may be a common remark within recent times, so I request clarification instead of asking a new question on the following: please clarify, when you have 6 cores, how do you tell mathematics to use 4, hyperthreaded to 8? Generally, with Some smaller number of cores, but hyperthreaded (larger than the total number, for example, but it should not be exclusive to this requirement) without using the other remaining cores, is this even possible? $\endgroup$ – CA Trevillian May 14 at 23:02
  • 1
    $\begingroup$ @CATrevillian Not from within the product, but the standard ways of configuring such things still apply, e.g. setting KMP_AFFINITY for MKL/OpenMP threads, or using tools like tasksel and numactl for pthreads. $\endgroup$ – ilian May 15 at 14:41
  • $\begingroup$ @ilian ah okay, many thanks for this response. I’ll have to gain a new skill it seems! Quite incompetent when it comes to “real” computing, that is, outside of my limited knowledge of Mathematica and the WL. This is my first venture with a modern intel processor with more than 2 cores, hence the framing of my ask for clarification. You rock! $\endgroup$ – CA Trevillian May 15 at 15:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.