ImportError: Cython and gcc-5
ImportError: Cython and gcc-5
我想通过 Cython 在 Python 中使用一些 C 函数。在这里我注意到,如果我使用 GCC-5 编译 C 代码(为了使用 Cilk),nm
在生成的 *.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__
而且我所有的自写函数都丢失了。此外我得到错误
ImportError: dynamic module does not define init function (initcython_wrapper)
我该如何解决这个问题,为什么会这样?
pyx
-文件的内容:
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))
和 setup.py 文件中的命令行选项:
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"])
我可以解决我的问题:背后的原因不是编译器,而是选项 -flto
在与 gcc-5
一起使用时生成了奇怪的代码。删除此选项生成可用代码。
我想通过 Cython 在 Python 中使用一些 C 函数。在这里我注意到,如果我使用 GCC-5 编译 C 代码(为了使用 Cilk),nm
在生成的 *.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__
而且我所有的自写函数都丢失了。此外我得到错误
ImportError: dynamic module does not define init function (initcython_wrapper)
我该如何解决这个问题,为什么会这样?
pyx
-文件的内容:
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))
和 setup.py 文件中的命令行选项:
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"])
我可以解决我的问题:背后的原因不是编译器,而是选项 -flto
在与 gcc-5
一起使用时生成了奇怪的代码。删除此选项生成可用代码。