pythoncgcccythongcc5

ImportError: Cython and gcc-5


I want to use some C-functions in Python by using Cython. Here I noticed that if I use GCC-5 for compiling the C-code (in order to use Cilk) nm lists a lot less entrances in the resulting *.so-function:

0000000000201030 B __bss_start
0000000000201030 b completed.6973
                 w __cxa_finalize@@GLIBC_2.2.5
00000000000005c0 t deregister_tm_clones
0000000000000630 t __do_global_dtors_aux
0000000000200df8 t __do_global_dtors_aux_fini_array_entry
0000000000201028 d __dso_handle
0000000000200e08 d _DYNAMIC
0000000000201030 D _edata
0000000000201038 B _end
00000000000006a8 T _fini
0000000000000670 t frame_dummy
0000000000200df0 t __frame_dummy_init_array_entry
00000000000006b8 r __FRAME_END__
0000000000201000 d _GLOBAL_OFFSET_TABLE_
                 w __gmon_start__
0000000000201031 B __gnu_lto_slim
0000000000201032 B __gnu_lto_v1
0000000000000570 T _init
                 w _ITM_deregisterTMCloneTable
                 w _ITM_registerTMCloneTable
0000000000200e00 d __JCR_END__
0000000000200e00 d __JCR_LIST__
                 w _Jv_RegisterClasses
00000000000005f0 t register_tm_clones
0000000000201030 d __TMC_END__

and all my self-writen functions are missing. Furthermore I get the error

ImportError: dynamic module does not define init function (initcython_wrapper)

How can I fix that, and why is that?

The content of the pyx-file:

from libcpp cimport bool as bool_t
cdef extern from "complex.h":
    pass

cimport numpy as np

# if you want to use the Numpy-C-API from Cython
# (not strictly necessary for this example, but good practice)
np.import_array()

# cdefine the signature of our c function
cdef extern from "GNLSE_RHS.h":
    void compute(int size, double *a, double *b)
    void speedcompute(int size, double *a, double *b)
    void test_fft(int size, double complex *a, double complex *b)

# create the wrapper code, with numpy type annotations
def compute_func(np.ndarray[double, ndim=1, mode="c"] in_array not None,
                     np.ndarray[double, ndim=1, mode="c"] out_array not None):
    compute(in_array.shape[0], <double*> np.PyArray_DATA(in_array),
                <double*> np.PyArray_DATA(out_array))

def speedcompute_func(np.ndarray[double, ndim=1, mode="c"] in_array not None,
                    np.ndarray[double, ndim=1, mode="c"] out_array not None):
    speedcompute(in_array.shape[0], <double*> np.PyArray_DATA(in_array),
                <double*> np.PyArray_DATA(out_array))   

def fft_func(np.ndarray[complex, ndim=1, mode="c"] in_array not None,
                np.ndarray[complex, ndim=1, mode="c"] out_array not None):
    test_fft(in_array.shape[0], <complex*> np.PyArray_DATA(in_array),
            <complex*> np.PyArray_DATA(out_array))   

and the command line options in the setup.py-file:

os.environ["CC"] = "gcc-5" 
os.environ["CXX"] = "g++-5"
# The configuration object that hold information on all the files
# to be built.
config = Configuration('', parent_package, top_path)
config.add_extension(name='cython_wrapper',
                     sources=['cython_wrapper.pyx', 'GNLSE_RHS.c'],
                     # libraries=['m'],
                     include_dirs=[numpy.get_include(),  "/opt/intel/compilers_and_libraries_2016.0.109/linux/mkl/include"],
                     #libraries=["m", "pthread", "gsl", "gslcblas"],
                     depends=['GNLSE_RHS.c'],
                     extra_compile_args=["-DMKL_ILP64 -mavx -msse4.2 -msse3 -msse2 -m64 -Ofast -flto -march=native -funroll-loops -std=gnu99"],
                     extra_link_args=["-Wl,--start-group /opt/intel/compilers_and_libraries_2016.0.109/linux/mkl/lib/intel64/libmkl_intel_ilp64.a /opt/intel/compilers_and_libraries_2016.0.109/linux/mkl/lib/intel64/libmkl_core.a /opt/intel/compilers_and_libraries_2016.0.109/linux/mkl/lib/intel64/libmkl_sequential.a -Wl,--end-group -lpthread -lm -ldl -lfftw3 -lfftw3_threads"])

Solution

  • I could solve my problem: The reason behind was not the compiler, but the option -flto which generated strange code when using it with gcc-5. Removing this option generated usable code.