问题在这里: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()
注意事项:
-
C:
- 我修改了函数,使
${OUT_ARRAY}, ${COEFF_ARRAY} 的输出数组${OUT_ARRAY}[i] = vIn[i] + ${COEFF_ARRAY}[i] + j(如果短于vIn,则复制${COEFF_ARRAY}):
-
Python:
输出:
e:\Work\Dev\StackOverflow\q051481025>"c:\Install\x86\Microsoft\Visual Studio Community\2015\vc\vcvarsall.bat" x64
e:\Work\Dev\StackOverflow\q051481025>dir /b
code.py
modwtj.c
e:\Work\Dev\StackOverflow\q051481025>cl /nologo modwtj.c /link /NOLOGO /DLL /OUT:libmodwtj.so
modwtj.c
Creating library libmodwtj.lib and object libmodwtj.exp
e:\Work\Dev\StackOverflow\q051481025>dir /b
code.py
libmodwtj.exp
libmodwtj.lib
libmodwtj.so
modwtj.c
modwtj.obj
e:\Work\Dev\StackOverflow\q051481025>"e:\Work\Dev\VEnvs\py35x64_test\Scripts\python.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.])