【问题标题】:ctypes: how to correctly update the pointers?ctypes:如何正确更新指针?
【发布时间】:2018-12-31 00:53:55
【问题描述】:

我想在 ctypes 中使用这个来自 python 的 c 函数,这里是 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))

【问题讨论】:

    标签: python c function pointers ctypes


    【解决方案1】:

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

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

    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}):
        • wOut, ht
        • vOut, gt
    • Python
      • 适应的变量名称符合[Python]: PEP 8 -- Style Guide for Python Code
      • 代码很长,但与问题密切相关的行是modwtj_func 调用之后的 2(在test_np 中)
      • 更优雅(更直接)解决方案使用npctypes接口(可以直接在 np 数组上操作),但我在该领域的经验相当有限

    输出

    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.])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-11
      • 1970-01-01
      • 2019-04-19
      • 1970-01-01
      • 1970-01-01
      • 2012-12-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多