我想将python中的这个c函数与ctypes一起使用,这里是c函数:
#include "mex.h"
#include "math.h"
void modwtj(double *Vin, int N, int j, int L, double *ht, double *gt,
double *Wout, double *Vout)
{
int k, n, t;
for (t = 0; t < N; t++) {
k = t;
Wout[t] = ht[0] * Vin[k];
Vout[t] = gt[0] * Vin[k];
for (n = 1; n < L; n++) {
k -= (int) pow(2.0, (double) j - 1.0);
if (k < 0) {
k += N;
}
Wout[t] += ht[n] * Vin[k];
Vout[t] += gt[n] * Vin[k];
}
}
}
在这里,python代码,我初始化了Wout和Vout的输出,但我注意到modwtj函数没有改变这些,我如何正确分配输出?
import ctypes
import numpy as np
lib = ctypes.CDLL('/import_modwt/libmodwtj.so')
Vin=data[0,:,0]
ht=np.asarray([-0.0075,-0.0233,0.0218]).astype(np.double)
gt=np.asarray([0.1629,0.5055,0.4461]).astype(np.double)
N = int(600)
j = 1 # scale
coeff_length = int(3) # filter length
Wout = np.random.rand(N,1).astype(np.double)
Vout = np.random.rand(N,1).astype(np.double)
array_type_a = ctypes.c_double * N
array_type_ht = ctypes.c_double * coeff_length
array_type_gt = ctypes.c_double * coeff_length
array_type_out = ctypes.c_double * N
lib.modwtj.argtypes = [ctypes.POINTER(ctypes.c_double),
ctypes.c_int,
ctypes.c_int,
ctypes.c_int,
ctypes.POINTER(ctypes.c_double),
ctypes.POINTER(ctypes.c_double),
ctypes.POINTER(ctypes.c_double),
ctypes.POINTER(ctypes.c_double)]
lib.modwtj.restype = None
lib.modwtj(array_type_a(*Vin),
ctypes.c_int(N),
ctypes.c_int(j),
ctypes.c_int(coeff_length),
array_type_ht(*ht),
array_type_gt(*gt),
array_type_out(*Wout),
array_type_out(*Vout))
问题就在这里:array_type_out(*Wout)
.这需要Wout
并创建一个array_type_out
实例,但它从Wout
复制值,因此Wout
和array_type_out(*Wout)
将是独立的(修改一个不会影响另一个)。
解决方案是从被lib.modwtj
修改的数据(array_type_out(*Wout)
和array_type_out(*Vout)
应该保存到变量中)中构造输出np数组(Wout
和Vout
)。
我准备了一个"小"例子。
modwtj.c:
#if defined(_WIN32)
# define DLL_EXPORT __declspec(dllexport)
#else
# define DLL_EXPORT
#endif
DLL_EXPORT void modwtj(const double *vIn, int n, int j, int l, const double *ht, const double *gt, double *wOut, double *vOut) {
int batches = n / l, last = n % l, i = 0;
for (i = 0; i < batches; i++)
for (int k = 0; k < l; k++)
{
wOut[i * l + k] = vIn[i * l + k] + ht[k] + j;
vOut[i * l + k] = vIn[i * l + k] + gt[k] + j;
}
for (int k = 0; k < last; k++) {
wOut[i * l + k] = vIn[i * l + k] + ht[k] + j;
vOut[i * l + k] = vIn[i * l + k] + gt[k] + j;
}
}
code.py:
import sys
import ctypes
import numpy as np
modwtj_lib = ctypes.CDLL("libmodwtj.so")
modwtj_func = modwtj_lib.modwtj
modwtj_func.argtypes = [
ctypes.POINTER(ctypes.c_double), ctypes.c_int, ctypes.c_int, ctypes.c_int,
ctypes.POINTER(ctypes.c_double), ctypes.POINTER(ctypes.c_double),
ctypes.POINTER(ctypes.c_double), ctypes.POINTER(ctypes.c_double)
]
modwtj_func.restype = None
data_array_dim = 10 # Size for v_in, w_out, v_out arrays
coeff_array_dim = 4 # Size for ht, gt arrays
scale = 1
double_data_array = ctypes.c_double * data_array_dim
double_coeff_array = ctypes.c_double * coeff_array_dim
def print_ctypes_array(array, array_name=None):
print("{:s}en: {:d}:n {:s}".format(str(array_name) + " - l" if array_name else "L", len(array), ", ".join(["{:.3f}".format(item) for item in array])))
def print_ctypes_arrays(*arrays_data):
print("nPrinting ctypes arrays...")
for array_data in arrays_data:
array, array_name = array_data
print_ctypes_array(array, name=array_name)
def print_np_array(array, array_name=None):
print("{:s}{:s}".format(str(array_name) + ": " if array_name else "", repr(array)))
"""
def test_no_np():
print("n{:s}".format(test_no_np.__name__))
v_in = double_data_array(*range(1, 1 + data_array_dim))
ht = double_coeff_array(*(1,) * coeff_array_dim)
gt = double_coeff_array(*(2,) * coeff_array_dim)
w_out = double_data_array()
v_out = double_data_array()
print_ctypes_arrays((v_in, "v_in"), (ht, "ht"), (gt, "gt"), (w_out, "w_out"), (v_out, "v_out"))
modwtj_func(v_in, data_array_dim, scale, coeff_array_dim, ht, gt, w_out, v_out)
print_ctypes_arrays((w_out, "w_out"), (v_out, "v_out"))
"""
def test_np():
print("n{:s}".format(test_np.__name__))
v_in_np = np.asarray(range(1, 1 + data_array_dim)).astype(np.double)
ht_np = np.asarray([1] * coeff_array_dim).astype(np.double)
gt_np = np.asarray([2] * coeff_array_dim).astype(np.double)
w_out_np = np.asarray([0] * data_array_dim).astype(np.double)
v_out_np = np.asarray([0] * data_array_dim).astype(np.double)
v_in = double_data_array(*v_in_np)
ht = double_coeff_array(*ht_np)
gt = double_coeff_array(*gt_np)
w_out = double_data_array(*w_out_np)
print("TESTING arrays relation")
print_np_array(v_out_np, array_name="v_out_np")
v_out = double_data_array(*v_out_np) # The data from v_out_np has been copied to v_out, so the 2 arrays are detached now, meaning that modifying one will not impact the other
print_ctypes_array(v_out, array_name="v_out")
v_out_np[1] = 3
v_out[2] = 5
print_np_array(v_out_np, array_name="v_out_np")
print_ctypes_array(v_out, array_name="v_out")
print("DONE testing")
modwtj_func(v_in, data_array_dim, scale, coeff_array_dim, ht, gt, w_out, v_out)
w_out_np = np.asarray(w_out).astype(np.double)
v_out_np = np.asarray(v_out).astype(np.double)
# Create the np arrays from modified data
print_np_array(w_out_np, array_name="w_out_np")
print_np_array(v_out_np, array_name="v_out_np")
def main():
#test_no_np()
test_np()
if __name__ == "__main__":
print("Python {:s} on {:s}n".format(sys.version, sys.platform))
main()
注释:
- 三:
- 我修改了函数,以便输出数组
${OUT_ARRAY}[i] = vIn[i] + ${COEFF_ARRAY}[i] + j
(如果短于vIn
${COEFF_ARRAY}
则重复)${OUT_ARRAY}, ${COEFF_ARRAY}
:wOut, ht
vOut, gt
- 我修改了函数,以便输出数组
- 蟒蛇:
- 将变量名称改编为 [Python]:PEP 8 -- Python 代码兼容的样式指南
- 代码很长,但与问题严格相关的行是
modwtj_func
调用后的 2 行(test_np
) - 一个更优雅(和直接)的解决方案是使用 np的ctypes接口(它将直接在np数组上运行),但我在这方面的经验相当有限
输出:
e:WorkDevStackOverflowq051481025>"c:Installx86MicrosoftVisual Studio Community2015vcvcvarsall.bat" x64 e:WorkDevStackOverflowq051481025>dir /b code.py modwtj.c e:WorkDevStackOverflowq051481025>cl /nologo modwtj.c /link /NOLOGO /DLL /OUT:libmodwtj.so modwtj.c Creating library libmodwtj.lib and object libmodwtj.exp e:WorkDevStackOverflowq051481025>dir /b code.py libmodwtj.exp libmodwtj.lib libmodwtj.so modwtj.c modwtj.obj e:WorkDevStackOverflowq051481025>"e:WorkDevVEnvspy35x64_testScriptspython.exe" code.py Python 3.5.4 (v3.5.4:3f56838, Aug 8 2017, 02:17:05) [MSC v.1900 64 bit (AMD64)] on win32 test_np TESTING arrays relation v_out_np: array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]) v_out - len: 10: 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000 v_out_np: array([0., 3., 0., 0., 0., 0., 0., 0., 0., 0.]) v_out - len: 10: 0.000, 0.000, 5.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000 DONE testing w_out_np: array([ 3., 4., 5., 6., 7., 8., 9., 10., 11., 12.]) v_out_np: array([ 4., 5., 6., 7., 8., 9., 10., 11., 12., 13.])