python高性能计算:cython使用openmp并行 —— 报错:undefined symbol: omp_get_thread_num

test.pyx文件:

from cython.parallel cimport parallel
from openmp cimport omp_get_thread_num




cpdef void long_running_task1() noexcept nogil:
    while True:
        pass

cpdef void long_running_task2() noexcept nogil:
    while True:
        pass

def do_two_tasks():
    cdef int thread_num
    with nogil, parallel(num_threads=2):
        thread_num = omp_get_thread_num()
        if thread_num == 0:
            long_running_task1()
        elif thread_num == 1:
            long_running_task2()


do_two_tasks()


cython的编译文件:

test_setup.py

from distutils.core import setup, Extension
from Cython.Build import cythonize


ext_modules = [
    Extension(
        "test",
        ["test.pyx"],
        extra_compile_args=['-fopenmp'],
        extra_link_args=['-fopenmp'],
    )
]


setup(
  name = 'test v1',
  ext_modules = cythonize(ext_modules, 
                          #compiler_directives={'language_level' : "3"}
                          # or "2" or "3str"
                          ),
)


编译test.pyx:

python test_setup.py build_ext --inplace

报错:

undefined symbol: omp_get_thread_num



修改代码:添加一行内容:cimport openmp

cimport openmp
from cython.parallel cimport parallel
from openmp cimport omp_get_thread_num




cpdef void long_running_task1() noexcept nogil:
    while True:
        pass

cpdef void long_running_task2() noexcept nogil:
    while True:
        pass

def do_two_tasks():
    cdef int thread_num
    with nogil, parallel(num_threads=2):
        thread_num = omp_get_thread_num()
        if thread_num == 0:
            long_running_task1()
        elif thread_num == 1:
            long_running_task2()


do_two_tasks()


问题解决:

运行效果:

image

成功达到200%的进程CPU使用率。



posted on 2024-07-30 19:37  Angry_Panda  阅读(24)  评论(0编辑  收藏  举报

导航