【问题标题】:return 2D array created from a C function into Python using Cython使用 Cython 从 C 函数创建的二维数组返回到 Python
【发布时间】:2018-03-28 15:54:32
【问题描述】:

我想在python 中使用由c 函数创建的二维数组。我在today 之前询问了如何做到这一点,@Abhijit Pritam 建议的一种方法是使用结构。我实现了它并且它确实有效。

c 代码:

typedef struct {
  int arr[3][5];
} Array;

Array make_array_struct() {
  Array my_array;
  int count = 0;
  for (int i = 0; i < 3; i++)
    for (int j = 0; j  < 5; j++)
      my_array.arr[i][j] = ++count;
  return my_array;
}

在 python 中我有这个:

cdef extern from "numpy_fun.h":
    ctypedef struct Array:
        int[3][5] arr
    cdef Array make_array_struct()

def make_array():
    cdef Array arr = make_array_struct()
    return arr

my_arr = make_array()
my_arr['arr']
[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]]

但是有人建议这不是解决问题的最佳方法,因为可以让 python 控制数据。我正在尝试实现这一点,但到目前为止我还没有做到这一点。这就是我所拥有的。

c 代码:

int **make_array_ptr() {
  int **my_array = (int **)malloc(3 * sizeof(int *));
  my_array[0] = calloc(3 * 5, sizeof(int));
  for (int i = 1; i < 3; i++)
    my_array[i] = my_array[0] + i * 5;
  int count = 0;
  for (int i = 0; i < 3; i++)
    for (int j = 0; j < 5; j++)
      my_array[i][j] = ++count;
  return my_array;
}

蟒蛇:

import numpy as np
cimport numpy as np

np.import_array()

ctypedef np.int32_t DTYPE_t

cdef extern from "numpy/arrayobject.h":
    void PyArray_ENABLEFLAGS(np.ndarray arr, int flags)

cdef extern from "numpy_fun.h":
    cdef int **make_array_ptr()

def make_array():
    cdef int[::1] dims = np.array([3, 5], dtype=np.int32)
    cdef DTYPE_t **data = <DTYPE_t **>make_array_ptr()
    cdef np.ndarray[DTYPE_t, ndim=2] my_array = np.PyArray_SimpleNewFromData(2, &dims[0], np.NPY_INT32, data)
    PyArray_ENABLEFLAGS(my_array, np.NPY_OWNDATA)
    return my_array

我正在关注Force NumPy ndarray to take ownership of its memory in Cython ,这似乎是我需要做的。在我的情况下它是不同的,因为我需要二维数组,所以我可能不得不做一些不同的事情,因为例如函数期望data 是一个指向 int 的指针,我给了它一个指向 int 指针的指针。 我需要做什么才能使用这种方法?

【问题讨论】:

    标签: python c cython


    【解决方案1】:

    我对@9​​87654321@ 方法的问题是:

    1. 只要您想要除固定大小的数组之外的任何内容,它就会中断,并且没有真正的修复方法。

    2. 它依赖于 Cython 从结构到字典的隐式转换。 Cython 将数据复制到 Python 列表中,这不是非常有效。这对于您这里的小数组来说不是问题,但对于较大的数组就很愚蠢。


    我也不真正推荐二维数组作为指针。 numpy(和大多数其他明智的数组库)实现二维数组的方式是存储一维数组和二维数组的形状,然后使用形状来计算要访问的索引。这往往更有效(更快的查找、更快的分配)并且更易于使用(更少的分配/取消分配以跟踪)。

    为此,请将 C 代码更改为:

    int32_t *make_array_ptr() {
      int32_t *my_array = calloc(3 * 5, sizeof(int32_t));
      int count = 0;
      for (int i = 0; i < 3; i++)
        for (int j = 0; j < 5; j++)
          my_array[j+i*5] = ++count;
      return my_array;
    }
    

    我已经删除了您立即覆盖的第一个循环。我还更改了 int32_t 的类型,因为您稍后似乎在 Cython 代码中依赖它。

    Cython 代码与您使用的代码非常接近:

    def make_array():
        cdef np.intp_t dims[2] 
        dims[0]=3; dims[1] = 5
        cdef np.int32_t *data = make_array_ptr()
        cdef np.ndarray[np.int32_t, ndim=2] my_array = np.PyArray_SimpleNewFromData(2, &dims[0], np.NPY_INT32, data)
        PyArray_ENABLEFLAGS(my_array, np.NPY_OWNDATA)
        return my_array
    

    主要的变化是我删除了一些演员表,并且只是将 dims 分配为静态数组(这似乎比 memoryviews 更简单)


    我认为让 numpy 处理指针到指针数组并不容易。通过实现 Python 缓冲区接口可能是可能的,但这似乎需要大量工作并且可能并不容易。

    【讨论】:

      猜你喜欢
      • 2021-03-29
      • 2021-12-04
      • 2021-05-01
      • 2014-12-19
      • 1970-01-01
      • 2012-01-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多