Skip to main content
 首页 » 编程设计

multithreading之mkl_set_num_threads() 是否受物理内核数量的限制

2024年10月24日32linjiqin

在一台 PC 中,我有一个 Intel i7 4790,它有 4 个内核和 8 个线程。每当我使用 mkl.set_num_threads(n) n > 4在哪里, 当我稍后从 mkl.get_max_threads() 检查它时,它被设置为 4 .这是有道理的,因为 MKL 不受益于超线程。

在另一台 PC 上,我有一个 Intel Xeon E5 2643,它也有 4 个内核和 8 个线程。不过这次可以设置线程数为8,这是为什么呢?

在两台机器中,如果我 mkl.set_num_threads(2) , mkl.get_max_threads()按预期返回 2。

请您参考如下方法:

直接来自:mkl_get_max_threads 的文档:

int mkl_get_max_threads (void); 

This function returns the number of OpenMP threads for Intel MKL to use in internal parallel regions. This number depends on whether dynamic adjustment of the number of threads by Intel MKL is disabled (by an environment setting or in a function call):

If the dynamic adjustment is disabled, the function inspects the environment settings and return values of the function calls below in the order they are listed until it finds a non-zero value:

  1. A call to mkl_set_num_threads_local
  2. The last of the calls to mkl_set_num_threads or mkl_domain_set_num_threads( …, MKL_DOMAIN_ALL)
  3. The MKL_DOMAIN_NUM_THREADS environment variable with the MKL_DOMAIN_ALL tag
  4. The MKL_NUM_THREADS environment variable
  5. A call to omp_set_num_threads
  6. The OMP_NUM_THREADS environment variable

If the dynamic adjustment is enabled, the function returns the number of physical cores on your system. The number of threads returned by this function is a hint, and Intel MKL may actually use a different number.