python 将结构和指针传递给 c 函数以进行输入和输出
python pass struct and pointer to c function for input and output
我按如下操作,报错
使用ctypes有什么问题吗,或者我传递的参数类型有问题?
我觉得可能有三点需要思考:
在python.
中生成传递给c函数的正确参数
将结构类型的指针传递给c函数。
正确实例化结构取决于给定的数组,如果在python代码中需要结构。
我是 c 的新手,尤其是指针。
c code:the 结构,ansfer.h
typedef unsigned char boolean_T;
struct emxArray_real_T
{
double *data;
int *size;
int allocatedSize;
int numDimensions;
boolean_T canFreeData;
};
python、ansfer.c
调用的函数
void ansferA(const emxArray_real_T *dataArray, double H, emxArray_real_T *TE,
emxArray_real_T *Lag)
{
...
P2 += dataArray->data[a + dataArray->size[0] * b];
...
TE->data[a + TE->size[0] * b] = te;
Lag->data[a + Lag->size[0] * b] = h;
...
}
然后,gcc -o libansfer.so -shared -fPIC *.c
libansfer.so生成。
我根据建议重写了test.py。添加结构如下。
python代码:test.py
import numpy as np
import ctypes
c_double_p = ctypes.POINTER(ctypes.c_double)
c_int_p = ctypes.POINTER(ctypes.c_int)
class emxArray_real_T(ctypes.Structure):
_fields_ = [
("data", c_double_p),
("size", c_int_p),
("allocatedSize", ctypes.c_int),
("numDimensions", ctypes.c_int),
("canFreeData", ctypes.c_bool)
]
indata = np.array([[1.1,1.1,1.1,1.1,1.1,1.1,1.1,1.1,1.1,1.1,1.1,1.1,1.1,1.1,1.1,1.1],[2.2,2.2,2.2,2.2,2.2,2.2,2.2,2.2,2.2,2.2,2.2,2.2,2.2,2.2,2.2,2.2],[3.3,3.3,3.3,3.3,3.3,3.3,3.3,3.3,3.3,3.3,3.3,3.3,3.3,2.2,2.2,2.2]])
LL, CC = indata.shape
TE = np.zeros((LL,LL), dtype=np.double)
Lag = np.zeros((LL,LL), dtype=np.double)
SS = np.array([LL, CC])
instruct = emxArray_real_T()
instruct.data = c_double_p(ctypes.c_double(indata.ctypes.data))
instruct.size = c_int_p(ctypes.c_int(SS.ctypes.data))
instruct.allocatedSize = ctypes.c_int(LL*CC)
instruct.numDimensions = ctypes.c_int(2)
instruct.canFreeData = ctypes.c_bool(0)
ss = np.array([LL, LL])
outstruct1 = emxArray_real_T()
outstruct1.data = c_double_p(ctypes.c_double(TE.ctypes.data))
outstruct1.size = c_int_p(ctypes.c_int(ss.ctypes.data))
outstruct1.allocatedSize = ctypes.c_int(LL*LL)
outstruct1.numDimensions = ctypes.c_int(2)
outstruct1.canFreeData = ctypes.c_bool(0)
outstruct2 = emxArray_real_T()
outstruct2.data = c_double_p(ctypes.c_double(Lag.ctypes.data))
outstruct2.size = c_int_p(ctypes.c_int(ss.ctypes.data))
outstruct2.allocatedSize = ctypes.c_int(LL*LL)
outstruct2.numDimensions = ctypes.c_int(2)
outstruct2.canFreeData = ctypes.c_bool(0)
lib = ctypes.cdll.LoadLibrary('./libansfer.so')
lib.ansferA(instruct,ctypes.c_double(3), outstruct1, outstruct2)
但类似的错误
Traceback (most recent call last):
File "C:\Python27\testfile\test.py", line 106, in <module>
lib.ansferA(instruct,ctypes.c_double(3), outstruct1, outstruct2)
WindowsError: exception: access violation writing 0x0000000000000000
这不是答案,但格式规则不允许我将其作为评论(并且仍然可读)。
该结构看起来像这样:
c_double_p = ctypes.POINTER(ctypes.c_double)
c_int_p = ctypes.POINTER(ctypes.c_int)
class emxArray_real_T(ctypes.Structure):
_fields_ = [
("data", c_double_p),
("size", c_int_p),
("allocatedSize", ctypes.c_int),
("numDimensions", ctypes.c_int),
("canFreeData", ctypes.c_bool)
]
我不得不猜测 canFreeData
的类型,boolean_T
不是标准的 C 类型。
然后用类似的东西初始化结构:
emxArray_struct = emxArray_real_T( insert values here as parameters )
我没有使用过 numpy,所以我不知道 if/how 它的类型映射到 ctypes 或常规 python 变量类型。
我按如下操作,报错
使用ctypes有什么问题吗,或者我传递的参数类型有问题?
我觉得可能有三点需要思考:
在python.
中生成传递给c函数的正确参数
将结构类型的指针传递给c函数。
正确实例化结构取决于给定的数组,如果在python代码中需要结构。
我是 c 的新手,尤其是指针。
c code:the 结构,ansfer.h
typedef unsigned char boolean_T;
struct emxArray_real_T
{
double *data;
int *size;
int allocatedSize;
int numDimensions;
boolean_T canFreeData;
};
python、ansfer.c
调用的函数void ansferA(const emxArray_real_T *dataArray, double H, emxArray_real_T *TE,
emxArray_real_T *Lag)
{
...
P2 += dataArray->data[a + dataArray->size[0] * b];
...
TE->data[a + TE->size[0] * b] = te;
Lag->data[a + Lag->size[0] * b] = h;
...
}
然后,gcc -o libansfer.so -shared -fPIC *.c
libansfer.so生成。
我根据建议重写了test.py。添加结构如下。
python代码:test.py
import numpy as np
import ctypes
c_double_p = ctypes.POINTER(ctypes.c_double)
c_int_p = ctypes.POINTER(ctypes.c_int)
class emxArray_real_T(ctypes.Structure):
_fields_ = [
("data", c_double_p),
("size", c_int_p),
("allocatedSize", ctypes.c_int),
("numDimensions", ctypes.c_int),
("canFreeData", ctypes.c_bool)
]
indata = np.array([[1.1,1.1,1.1,1.1,1.1,1.1,1.1,1.1,1.1,1.1,1.1,1.1,1.1,1.1,1.1,1.1],[2.2,2.2,2.2,2.2,2.2,2.2,2.2,2.2,2.2,2.2,2.2,2.2,2.2,2.2,2.2,2.2],[3.3,3.3,3.3,3.3,3.3,3.3,3.3,3.3,3.3,3.3,3.3,3.3,3.3,2.2,2.2,2.2]])
LL, CC = indata.shape
TE = np.zeros((LL,LL), dtype=np.double)
Lag = np.zeros((LL,LL), dtype=np.double)
SS = np.array([LL, CC])
instruct = emxArray_real_T()
instruct.data = c_double_p(ctypes.c_double(indata.ctypes.data))
instruct.size = c_int_p(ctypes.c_int(SS.ctypes.data))
instruct.allocatedSize = ctypes.c_int(LL*CC)
instruct.numDimensions = ctypes.c_int(2)
instruct.canFreeData = ctypes.c_bool(0)
ss = np.array([LL, LL])
outstruct1 = emxArray_real_T()
outstruct1.data = c_double_p(ctypes.c_double(TE.ctypes.data))
outstruct1.size = c_int_p(ctypes.c_int(ss.ctypes.data))
outstruct1.allocatedSize = ctypes.c_int(LL*LL)
outstruct1.numDimensions = ctypes.c_int(2)
outstruct1.canFreeData = ctypes.c_bool(0)
outstruct2 = emxArray_real_T()
outstruct2.data = c_double_p(ctypes.c_double(Lag.ctypes.data))
outstruct2.size = c_int_p(ctypes.c_int(ss.ctypes.data))
outstruct2.allocatedSize = ctypes.c_int(LL*LL)
outstruct2.numDimensions = ctypes.c_int(2)
outstruct2.canFreeData = ctypes.c_bool(0)
lib = ctypes.cdll.LoadLibrary('./libansfer.so')
lib.ansferA(instruct,ctypes.c_double(3), outstruct1, outstruct2)
但类似的错误
Traceback (most recent call last):
File "C:\Python27\testfile\test.py", line 106, in <module>
lib.ansferA(instruct,ctypes.c_double(3), outstruct1, outstruct2)
WindowsError: exception: access violation writing 0x0000000000000000
这不是答案,但格式规则不允许我将其作为评论(并且仍然可读)。
该结构看起来像这样:
c_double_p = ctypes.POINTER(ctypes.c_double)
c_int_p = ctypes.POINTER(ctypes.c_int)
class emxArray_real_T(ctypes.Structure):
_fields_ = [
("data", c_double_p),
("size", c_int_p),
("allocatedSize", ctypes.c_int),
("numDimensions", ctypes.c_int),
("canFreeData", ctypes.c_bool)
]
我不得不猜测 canFreeData
的类型,boolean_T
不是标准的 C 类型。
然后用类似的东西初始化结构:
emxArray_struct = emxArray_real_T( insert values here as parameters )
我没有使用过 numpy,所以我不知道 if/how 它的类型映射到 ctypes 或常规 python 变量类型。