gpt4 book ai didi

python - ctypes:如何正确更新指针?

转载 作者:太空宇宙 更新时间:2023-11-03 23:59:14 24 4
gpt4 key购买 nike

我想将这个来自 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 复制值,所以 Woutarray_type_out(*Wout) 将是独立的(修改一个不会影响另一个)。
解决方案是从已被lib.modwtj(array_type_out(*Wout)array_type_out(*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;
}
}

代码.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}[i] = vIn[i] + ${COEFF_ARRAY}[i] + j(其中 ${COEFF_ARRAY} 如果 ${OUT_ARRAY}, ${COEFF_ARRAY} 短于 vIn) 则重复:
      • wOut, ht
      • vOut,gt
  • python :
    • 将变量名称改编为 [Python]: PEP 8 -- Style Guide for Python Code合规
    • 代码很长,但与问题严格相关的行是 modwtj_func 调用之后的第二行(在 test_np 中)
    • 更优雅(和直接)解决方案使用npctypes接口(interface)(将直接在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.])

关于python - ctypes:如何正确更新指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51481025/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com