Programmatically testing openmp support using python script setup

I am working on a python project that uses cython and c to speed up time sensitive operations. In several of our cython routines, we use openmp to speed things up if free kernels are available.

This leads to some annoying situation in OS X, since the default compiler for recent OS versions (llvm / clang on 10.7 and 10.8) does not support openmp. Our great solution is to tell people how to install gcc as their compiler when they are created. We really would like to do this programmatically, since clang can do the rest without any problems.

At the moment, compilation will fail:

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 

The relevant part of our installation script is as follows:

 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"]) 

The full setup.py file is here .

Is there a way to programmatically test openmp support from within a script installation?

+8
python openmp distutils
source share
1 answer

I managed to get this working by checking to compile a test program:

 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 
0
source share

All Articles