【发布时间】:2017-01-12 15:09:25
【问题描述】:
我想从 Python 调用一些 OpenMP 程序,更改线程数 (OMP_NUM_THREADS) 及其绑定 (OMP_PLACES, OMP_PROC_BIND)。我写了这个程序:
import os
from ctypes import cdll
lib = cdll.LoadLibrary("/home/fayard/Desktop/libf.so")
nb_socket = 2
nb_core_per_socket = 14
nb_thread_per_core = 2
n = nb_socket * nb_core_per_socket * nb_thread_per_core
for nb_thread in range(1, n + 1):
os.environ['OMP_NUM_THREADS'] = str(nb_thread)
print("nb_thread: {}, omp_get_num_threads: {}".format(
nb_thread, lib.num_threads()))
OpenMP 库如下:
#include <omp.h>
extern "C" {
int num_threads() {
int ans;
#pragma omp parallel
{
#pragma omp single
ans = omp_get_num_threads();
}
return ans;
}
}
并编译为:
g++ -c -fPIC -fopenmp f.cpp -o f.o
g++ -shared -fopenmp -Wl,soname,libf.so -o libf.so f.o
当我运行python program.py 时,我得到:
nb_thread: 1, omp_get_num_threads: 56
...
nb_thread: 56, omp_get_num_threads: 56
这不是我想要的!我还意识到,当使用具有完全相同参数的英特尔编译器进行编译时,我得到:
nb_thread: 1, omp_get_num_threads: 1
...
nb_thread: 56, omp_get_num_threads: 1
有什么想法吗?
【问题讨论】: