从python安装脚本以编程方式测试openmp支持

时间:2013-05-14 18:05:52

标签: python openmp distutils

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

由于最新操作系统版本的默认编译器(10.7和10.8上的llvm / clang)不支持openmp,因此在OS X上会出现一些烦人的情况。我们的权宜之计解决方案是告诉人们在构建时将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支持?

1 个答案:

答案 0 :(得分:1)

我能够通过检查测试程序是否编译来实现这个目的:

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
相关问题