Python and OpenMP C Extensions
Question: I have a C extension in which I'd like to use OpenMP. When I import my module,
though, I get an import error:

    
    
    ImportError: /home/.../_entropysplit.so: undefined symbol: GOMP_parallel_end
    

I've compiled the module with -fopenmp and -lgomp. Is this because my Python
installation wasn't compiled with the -fopenmp flag? Will I have to build
Python from source? Or is there some other possibility? This is the only time
I actually use openmp in my module:

    
    
    unsigned int feature_index;
    #pragma omp parallel for
    for (feature_index = 0; feature_index < num_features; feature_index++) {
    

I'd like to stick with openmp if it's possible, just because it's so easy and
the parallelization in this case suits it well.

EDIT: I bit the bullet and recompiled Python with OpenMP support. My module
works perfectly now, but this isn't really a great solution. I can't really
distribute this if it requires a complete recompile of Python. So does anybody
know some way around this? Would ctypes work, maybe?

SOLVED! It was a simple linking issue. (I rebuilt Python for that?!) OpenMP
wasn't being properly linked during the compilation of the module. So it _IS_
possible to load a C Python extension that uses OpenMP.


Answer: Just to make it clearer, here is what your setup.py should look like:

    
    
    ext = Extension(
          'milk.unsupervised._som',
          sources = ['milk/unsupervised/_som.cpp'],
          extra_compile_args=['-fopenmp'],
          extra_link_args=['-lgomp'])
    
    
    ...
    setup(..., ext_modules = [ext])
    

