将 numpy 与 Cython 一起使用

神机

我想从 python 创建 .so 文件并在 C 中执行 .so 文件。为此,我使用 cython 将 .pyx 转换为 .so

## print_me.pyx
cimport numpy as cnp
import  numpy as np
cimport cython
cpdef public char* print_me(f):
    # I know this numpy line does nothing
    cdef cnp.ndarray[cnp.complex128_t, ndim=3] a = np.zeros((3,3,3), dtype=np.complex128)
    return f

然后我使用 setup.py 将 .pyx 实际转换为 .so

## setup.py
from distutils.core import setup
from Cython.Build import cythonize
import numpy as np

setup(
    ext_modules=cythonize("print_me.pyx"),
    include_dirs=[np.get_include()]
)

通过运行以下命令行,我能够创建 .so 文件

python setup.py build_ext --inplace

当我尝试使用以下 C 代码运行 so 文件时,出现分段错误。

/* toloadso.c */
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <time.h>
#include <python2.7/Python.h>

int main(void)
{
    // define function
    void *handle;
    char* (*print_me)(PyObject*);
    char *error;

    PyObject* filename = PyString_FromString("hello");

    // load so file
    handle = dlopen("./print_me.so", RTLD_LAZY);
    if (!handle) {
        fprintf(stderr, "%s\n", dlerror());
        exit(EXIT_FAILURE);
    }
    dlerror();

    // get function handler from so file
    print_me = (char* (*)(PyObject*))dlsym(handle, "print_me");

    // check if handler got error
    error = dlerror();
    if (error != NULL) {
        fprintf(stderr, "%s\n", error);
        exit(EXIT_FAILURE);
    }

    // execute loaded function
    printf("%s\n", (char*)(*print_me)(filename));
    dlclose(handle);
    exit(EXIT_SUCCESS);
}

我使用以下命令编译了这个 .c 文件:

gcc -fPIC -I/usr/include/ -o toloadso toloadso.c -lpython2.7 -ldl
(It compiled without error or warning)

当我尝试运行此代码时,出现分段错误

[root@localhost ~]# ./toloadso
Segmentation fault

如果我在 print_me.pyx 中注释掉以下行

cdef cnp.ndarray[cnp.complex128_t, ndim=3] a = np.zeros((3,3,3), dtype=np.complex128)

我的 C 代码运行没有错误,但是一旦我取消注释这一行,它就不起作用。我认为尝试在 cython 中使用 numpy 会以某种方式产生错误。

我该怎么修??我非常感谢你的回复

沃伦·韦克瑟

您必须通过调用import_array()初始化 numpy C API

将此行添加到您的 cython 文件中:

cnp.import_array()

正如在评论中指出@ user4815162342和@DavidW,你必须调用Py_Initialize()Py_Finalize()main()

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

将 contextlib 与 cython 一起使用

来自分类Dev

将NumPy与JyNI一起使用

来自分类Dev

将NumPy与JyNI一起使用

来自分类Dev

将numpy数组与scipy odeint一起使用

来自分类Dev

将numpy.testing函数与unittest一起使用

来自分类Dev

将辅助函数与numpy.genfromtxt()一起使用

来自分类Dev

如何将两个数组加在一起,使Cython比Python(没有Numpy)快得多?

来自分类Dev

如何将包含erf函数的SymPy表达式与NumPy一起使用

来自分类Dev

将numpy mgrid与可变数量的索引一起使用

来自分类Dev

将pypy2与numpy.testing一起使用时ImportError

来自分类Dev

如何将Numba“ @vectorize” ufunc与结构化的Numpy数组一起使用?

来自分类Dev

无法将numpy模块导入python文件,但可与终端一起使用

来自分类Dev

将pypy2与numpy.testing一起使用时的ImportError

来自分类Dev

用numpy将数组拼接在一起

来自分类Dev

将Cython模块与Python软件包捆绑在一起

来自分类Dev

我收到“ DLL加载失败:找不到指定的模块”。将Cython与Numpy程序配合使用时

来自分类Dev

将cython与gsl结合使用

来自分类Dev

将cython与gsl结合使用

来自分类Dev

使用 cython 使用 numpy 数组加速类

来自分类Dev

使Cython与WinPython 3.3 64位一起使用

来自分类Dev

如何让cython和gensim与pyspark一起使用

来自分类Dev

在cython中使用numpy.array

来自分类Dev

Python numpy:无法将datetime64 [ns]转换为datetime64 [D](与Numba一起使用)

来自分类Dev

Cython:将内存视图转换为NumPy数组

来自分类Dev

Cython:使用将Cython嵌入C的API的分段错误

来自分类Dev

将“ -Filter”与变量一起使用

来自分类Dev

将PowerMock与黄瓜一起使用

来自分类Dev

将XhtmlTextWriter与XmlTextReader一起使用

来自分类Dev

将Scrapyd与参数一起使用

Related 相关文章

  1. 1

    将 contextlib 与 cython 一起使用

  2. 2

    将NumPy与JyNI一起使用

  3. 3

    将NumPy与JyNI一起使用

  4. 4

    将numpy数组与scipy odeint一起使用

  5. 5

    将numpy.testing函数与unittest一起使用

  6. 6

    将辅助函数与numpy.genfromtxt()一起使用

  7. 7

    如何将两个数组加在一起,使Cython比Python(没有Numpy)快得多?

  8. 8

    如何将包含erf函数的SymPy表达式与NumPy一起使用

  9. 9

    将numpy mgrid与可变数量的索引一起使用

  10. 10

    将pypy2与numpy.testing一起使用时ImportError

  11. 11

    如何将Numba“ @vectorize” ufunc与结构化的Numpy数组一起使用?

  12. 12

    无法将numpy模块导入python文件,但可与终端一起使用

  13. 13

    将pypy2与numpy.testing一起使用时的ImportError

  14. 14

    用numpy将数组拼接在一起

  15. 15

    将Cython模块与Python软件包捆绑在一起

  16. 16

    我收到“ DLL加载失败:找不到指定的模块”。将Cython与Numpy程序配合使用时

  17. 17

    将cython与gsl结合使用

  18. 18

    将cython与gsl结合使用

  19. 19

    使用 cython 使用 numpy 数组加速类

  20. 20

    使Cython与WinPython 3.3 64位一起使用

  21. 21

    如何让cython和gensim与pyspark一起使用

  22. 22

    在cython中使用numpy.array

  23. 23

    Python numpy:无法将datetime64 [ns]转换为datetime64 [D](与Numba一起使用)

  24. 24

    Cython:将内存视图转换为NumPy数组

  25. 25

    Cython:使用将Cython嵌入C的API的分段错误

  26. 26

    将“ -Filter”与变量一起使用

  27. 27

    将PowerMock与黄瓜一起使用

  28. 28

    将XhtmlTextWriter与XmlTextReader一起使用

  29. 29

    将Scrapyd与参数一起使用

热门标签

归档