目标:使用 Cython 从 2D C++ 字符数组中获取 Memoryview。
一点背景:
我有一个本地 C++ 库,它生成一些数据并通过 a 返回char**
到 Cython 世界。数组在库中初始化和操作如下:
struct Result_buffer{
char** data_pointer;
int length = 0;
Result_buffer( int row_capacity) {
data_pointer; = new char*[row_capacity];
return arr;
}
// the actual data is appended row by row
void append_row(char* row_data) {
data_pointer[length] = row_data;
length++;
}
}
所以我们基本上得到了一个嵌套子数组的数组。
旁注:
- 每行具有相同的列数
- 行可以共享内存,即指向相同的 row_data
目标是将此数组与 memoryview 一起使用,最好不要进行昂贵的内存复制。
第一种方法(不工作):
使用 Cython 数组和内存视图:
这是应该使用生成的数据的 .pyx 文件
from cython cimport view
cimport numpy as np
import numpy as np
[...]
def raw_data_to_numpy(self):
# Dimensions of the source array
cdef int ROWS = self._row_count
cdef int COLS = self._col_count
# This is the array from the C++ library and is created by 'create_buffer()'
cdef char** raw_data_pointer = self._raw_data
# It only works with a pointer to the first nested array
cdef char* pointer_to_0 = raw_data_pointer[0]
# Now create a 2D Cython array
cdef view.array cy_array = <char[:ROWS, :COLS]> pointer_to_0
# With this we can finally create our NumPy array:
return np.asarray(cy_array)
这实际上编译得很好并且运行没有崩溃,但结果并不完全符合我的预期。如果我打印出 NumPy 数组的值,我会得到:
000: [1, 2, 3, 4, 5, 6, 7, 8, 9]
001: [1, 0, 0, 0, 0, 0, 0, 113, 6]
002: [32, 32, 32, 32, 96, 96, 91, 91, 97]
[...]
事实证明,第一行被正确映射,但其他行看起来更像是未初始化的内存。因此,可能与char**
2D 内存视图的内存布局和默认模式不匹配。
编辑#1:我从另一个问题中学到的是内置的 cython 数组不支持间接内存布局,所以我必须为它创建一个 cython-wrapper 来unsigned char**
公开缓冲区协议