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()
问题解决:
运行效果:
成功达到200%的进程CPU使用率。
本博客是博主个人学习时的一些记录,不保证是为原创,个别文章加入了转载的源地址,还有个别文章是汇总网上多份资料所成,在这之中也必有疏漏未加标注处,如有侵权请与博主联系。
如果未特殊标注则为原创,遵循 CC 4.0 BY-SA 版权协议。
posted on 2024-07-30 19:37 Angry_Panda 阅读(24) 评论(0) 编辑 收藏 举报