I would like to call some OpenMP program from Python, changing the number of threads (OMP_NUM_THREADS
) and their bindings (OMP_PLACES
, OMP_PROC_BIND
). I wrote this program:
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()))
The OpenMP library is the following:
#include <omp.h>
extern "C" {
int num_threads() {
int ans;
#pragma omp parallel
{
#pragma omp single
ans = omp_get_num_threads();
}
return ans;
}
}
and is compiled with:
g++ -c -fPIC -fopenmp f.cpp -o f.o
g++ -shared -fopenmp -Wl,soname,libf.so -o libf.so f.o
When I run python program.py
, I get:
nb_thread: 1, omp_get_num_threads: 56
...
nb_thread: 56, omp_get_num_threads: 56
which is not what I want! I also realized, that when compiled with the Intel compilers with exactly the same arguments, I get:
nb_thread: 1, omp_get_num_threads: 1
...
nb_thread: 56, omp_get_num_threads: 1
Any thoughts on what's going wrong?