2017-02-23 75 views
0

我用C++編程並使用OpenMP進行並行化。該機器具有2個CPU插槽和每個插槽8個內核。OpenMP僅檢測多插槽/多核系統上的單個內核

自從我與英特爾的編譯器編譯,我設置以下的環境變量

export KMP_AFFINITY=verbose,scatter 

隨着詳細選項,運行二進制文件時,我可以看到下面的消息。

[0] OMP: Info #204: KMP_AFFINITY: decoding x2APIC ids. 
[0] OMP: Info #202: KMP_AFFINITY: Affinity capable, using global cpuid leaf 11 info 
[0] OMP: Info #154: KMP_AFFINITY: Initial OS proc set respected: {0} 
[0] OMP: Info #156: KMP_AFFINITY: 1 available OS procs 
[0] OMP: Info #157: KMP_AFFINITY: Uniform topology 
[0] OMP: Info #159: KMP_AFFINITY: 1 packages x 1 cores/pkg x 1 threads/core (1 total cores) 
[0] OMP: Info #206: KMP_AFFINITY: OS proc to physical thread map: 
[0] OMP: Info #171: KMP_AFFINITY: OS proc 0 maps to package 0 
[0] OMP: Info #242: KMP_AFFINITY: pid 12759 thread 0 bound to OS proc set {0} 
[0] OMP: Info #242: KMP_AFFINITY: pid 12759 thread 14 bound to OS proc set {0} 
[0] OMP: Info #242: KMP_AFFINITY: pid 12759 thread 15 bound to OS proc set {0} 
[0] OMP: Info #242: KMP_AFFINITY: pid 12759 thread 11 bound to OS proc set {0} 
[0] OMP: Info #242: KMP_AFFINITY: pid 12759 thread 6 bound to OS proc set {0} 
[0] OMP: Info #242: KMP_AFFINITY: pid 12759 thread 7 bound to OS proc set {0} 
[0] OMP: Info #242: KMP_AFFINITY: pid 12759 thread 8 bound to OS proc set {0} 
[0] OMP: Info #242: KMP_AFFINITY: pid 12759 thread 9 bound to OS proc set {0} 
[0] OMP: Info #242: KMP_AFFINITY: pid 12759 thread 10 bound to OS proc set {0} 
[0] OMP: Info #242: KMP_AFFINITY: pid 12759 thread 13 bound to OS proc set {0} 
[0] OMP: Info #242: KMP_AFFINITY: pid 12759 thread 12 bound to OS proc set {0} 

正如你所看到的,OMP無法檢測每包套餐(插座)和內核的正確的號碼。結果,所有的線程被固定到一個核心。

我該如何解決這個問題?我應該從哪裏開始?

回答

0

我回答我自己的問題。

我的程序設置主線程的CPU親和力如下:

...

CPU_ZERO(&cpuset); 
CPU_SET(0, &cpuset); 
pid_t tid = (pid_t) syscall(SYS_gettid); 
sched_setaffinity(tid, sizeof(cpu_set_t), &cpuset); 

unsigned long mask = -1; 
int rc = sched_getaffinity(tid, sizeof(unsigned long), (cpu_set_t*) &mask); 
if (rc != 0) { 
    std::cout << "ERROR calling pthread_setaffinity_np; " << rc << std::endl; 
    abort(); 
} 

...

OpenMP編譯線程後setaffinitiy系統調用都必然會催生與主線程綁定的相同內核。