ctypes:如何正确更新指针?

时间:2018-07-23 14:19:29

标签: python c function pointers ctypes

我想将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))

1 个答案:

答案 0 :(得分:0)

问题在这里:array_type_out(*Wout)。这需要Wout并创建一个array_type_out实例,但是它从Wout复制值,因此Woutarray_type_out(*Wout)将是独立的(修改一个不会影响)。
解决方案是从Wout({{}}修改的数据中构造输出 np 数组(Voutlib.modwtj)。 {1}}和array_type_out(*Wout)应该已经保存到变量中了。

我准备了一个“小”例子。

modwtj.c

array_type_out(*Vout)

code.py

#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;
    }
}

注释

  • C
    • 我修改了函数,以使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(如果短于${COEFF_ARRAY},则复制vIn):
      • ${OUT_ARRAY}, ${COEFF_ARRAY}
      • wOut, ht
  • Python
    • 适应的变量名称要符合[Python]: PEP 8 -- Style Guide for Python Code
    • 代码很长,但是与问题严格相关的行是vOut, gt调用后的第二行(在modwtj_func中)
    • 更优雅(且简单明了)的解决方案使用 np ctypes 界面(可以直接在 np 数组上运行),但是我在该领域的经验非常有限

输出

test_np