繁体   English   中英

将 Cython 中的 numpy 数组传递给需要动态分配数组的 C 函数

[英]Passing numpy arrays in Cython to a C function that requires dynamically allocated arrays

我有一些具有以下声明的 C 代码:

int myfunc(int m, int n, const double **a, double **b, double *c);

所以a是一个常量二维数组, b是一个二维数组, c是一个一维数组,都是动态分配的。 bc在传递给myfunc之前不需要是什么特别的东西,应该理解为输出信息。 出于这个问题的目的,我不允许更改myfunc的声明。

问题 1:如何将给定的 numpy 数组a_np转换为具有此 C 函数所需格式的数组a ,以便我可以在 Cython 中使用a调用此 C 函数?

问题 2:下面bc的声明是否正确,或者它们是否需要采用其他格式以便 C 函数将它们理解为 2D 和 1D 数组(分别)?

我的尝试:

我的文件.pxd

cdef extern from "myfile.h":
    int myfunc(int p, int q, const double **a, double **b, double *c)

mytest.pyx

cimport cython
cimport myfile
import numpy as np
cimport numpy as np

p = 3
q = 4
cdef:
    double** a = np.random.random([p,q])
    double** b
    double* c

myfile.myfunc(p, q, a, b, c)

然后在 iPython 中我运行

import pyximport; pyximport.install()
import mytest

带有a定义的行给了我错误消息Cannot convert Python object to 'double **' 我没有收到关于bc任何错误消息,但由于此时我无法运行 C 函数,我不确定bc的声明是否正确编写(也就是说,以某种方式将使 C 函数分别输出 2D 和 1D 数组)。

其他尝试:我也尝试遵循此处的解决方案,但这不适用于myfunc声明中的双星号类型的数组。 这里的解决方案不适用于我的任务,因为我无法更改myfunc的声明。

在 cython 中创建一个辅助数组

要从 numpy 数组中获取double** ,您可以在 *.pyx 文件中创建一个辅助指针数组。 此外,您必须确保 numpy 数组具有正确的内存布局。 (这可能涉及创建副本)

Fortran命令

如果您的 C 函数需要 fortran 顺序(一个列表中的所有 x 坐标,另一个列表中的所有 y 坐标,第三个列表中的所有 z 坐标,如果您的数组 a 对应于 3D 空间中的点列表)

N,M = a.shape
# Make sure the array a has the correct memory layout (here F-order)
cdef np.ndarray[double, ndim=2, mode="fortran"] a_cython =
                         np.asarray(a, dtype = float, order="F")
#Create our helper array
cdef double** point_to_a = <double **>malloc(M * sizeof(double*))
if not point_to_a: raise MemoryError
try:
    #Fillup the array with pointers
    for i in range(M): 
        point_to_a[i] = &a_cython[0, i]
    # Call the C function that expects a double**
    myfunc(... &point_to_a[0], ...)
finally:
    free(point_to_a)

C-顺序

如果您的 C 函数需要 C 顺序([x1,y1,z1] 是第一个列表,[x2,y2,z2] 是 3D 点列表的第二个列表):

N,M = a.shape
# Make sure the array a has the correct memory layout (here C-order)
cdef np.ndarray[double, ndim=2, mode="c"] a_cython =
                         np.asarray(a, dtype = float, order="C")
#Create our helper array
cdef double** point_to_a = <double **>malloc(N * sizeof(double*))
if not point_to_a: raise MemoryError
try:
    for i in range(N): 
        point_to_a[i] = &a_cython[i, 0]
    # Call the C function that expects a double**
    myfunc(... &point_to_a[0], ...)
finally:
    free(point_to_a)

回复1:可以通过Cython 将NumPy 数组传递给C 使用数组开头的位置(见下面代码)。

回复2:你的声明看起来是对的,但是我没有使用这种显式内存管理的方法。 您可以使用 NumPy 来声明cdef -ed 数组。

cdef double[:,::1] a = np.random.random([p, q])
cdef double[:,::1] b = np.empty([p, q])
cdef double[::1] b = np.empty(q)

然后将&a[0] (数组开头的位置)传递给您的 C 函数。 ::1是为了确保连续性。

对此的一个很好的参考是 Jake Vanderplas 的博客: https ://jakevdp.github.io/blog/2012/08/08/memoryview-benchmarks/

最后,通常在 Cython 中创建函数并在 Python 中调用它们,因此您的 Python 代码将是:

import pyximport; pyximport.install()
import mytest
mytest.mywrappedfunc()

其中mywrappedfunc是在模块中定义的 Python( def而不是cdef )函数,可以执行上面显示的数组声明。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM