gpt4 book ai didi

python - 以编程方式测试来自 python 安装脚本的 openmp 支持

转载 作者:太空狗 更新时间:2023-10-29 21:11:15 26 4
gpt4 key购买 nike

我正在开发一个使用 cython 和 c 来加速时间敏感操作的 python 项目。在我们的一些 cython 例程中,如果空闲内核可用,我们会使用 openmp 进一步加速操作。

这会在 OS X 上导致一些烦人的情况,因为最近操作系统版本(10.7 和 10.8 上的 llvm/clang)的默认编译器不支持 openmp。我们的权宜之计是告诉人们在构建时将 gcc 设置为他们的编译器。我们真的很想以编程方式执行此操作,因为 clang 可以毫无问题地构建其他所有内容。

现在,编译将失败并出现以下错误:

clang: error: linker command failed with exit code 1 (use -v to see invocation)
error: Command "cc -bundle -undefined dynamic_lookup -L/usr/local/lib -L/usr/local/opt/sqlite/lib build/temp.macosx-10.8-x86_64-2.7/yt/utilities/lib/geometry_utils.o -lm -o yt/utilities/lib/geometry_utils.so -fopenmp" failed with exit status 1

我们设置脚本的相关部分如下所示:

config.add_extension("geometry_utils", 
["yt/utilities/lib/geometry_utils.pyx"],
extra_compile_args=['-fopenmp'],
extra_link_args=['-fopenmp'],
libraries=["m"], depends=["yt/utilities/lib/fp_utils.pxd"])

完整的 setup.py 文件是 here .

有没有一种方法可以从安装脚本内部以编程方式测试 openmp 支持?

最佳答案

我能够通过检查测试程序是否编译来使它正常工作:

import os, tempfile, subprocess, shutil    

# see http://openmp.org/wp/openmp-compilers/
omp_test = \
r"""
#include <omp.h>
#include <stdio.h>
int main() {
#pragma omp parallel
printf("Hello from thread %d, nthreads %d\n", omp_get_thread_num(), omp_get_num_threads());
}
"""

def check_for_openmp():
tmpdir = tempfile.mkdtemp()
curdir = os.getcwd()
os.chdir(tmpdir)

filename = r'test.c'
with open(filename, 'w', 0) as file:
file.write(omp_test)
with open(os.devnull, 'w') as fnull:
result = subprocess.call(['cc', '-fopenmp', filename],
stdout=fnull, stderr=fnull)

os.chdir(curdir)
#clean up
shutil.rmtree(tmpdir)

return result

关于python - 以编程方式测试来自 python 安装脚本的 openmp 支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16549893/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com