Jav*_*ack 1 python ctypes numpy
我有一个在 c++ 和 python 进程之间共享的 ctypes 对象。python 进程从该对象获取输入值,通过 Tensorflow 运行它们,然后留下一个 numpy 数组作为输出。由于这些数组非常大,我想知道是否有更好的方法将数据从张量流的输出复制回共享 ctypes 对象,以便 c++ 进程可以对它们进行操作。(速度是问题,是的。)
现在我正在一一复制每个值:
output = np.array([12, 13, 11, 10]) # in reality this is fairly large (the Tensorflow result)
for i, value in enumerate(output):
data.pressure[i] = ctypes.c_double(value)
Run Code Online (Sandbox Code Playgroud)
其中 data 是内存中共享的 ctypes 对象。(在此示例之后构建)
另一方面,将数据从 ctypes 对象复制到 numpy 很容易,我想知道是否有相反的东西(从 numpy 到 ctypes 数组)这是简单的代码:
# Creating a numpy array from the ctypes array
input = np.reshape(data.velocity, (1, 29791))
# Tensorflow returns a numpy array
output = sess.run(final, feed_dict={Input: input})
# Now how do I get the data from output into data.pressure?
Run Code Online (Sandbox Code Playgroud)
编辑:作为参考,这就是 ctypes 的样子(python 端)
class TransferData(ctypes.Structure):
_fields_ = [
('statusReady', ctypes.c_bool),
# ...
('velocity', ctypes.c_double * (31 * 31 * 31)),
('pressure', ctypes.c_double * (31 * 31 * 31))
]
Run Code Online (Sandbox Code Playgroud)
这显示了如何将整个数据块从 numpy 数组复制到 ctypes 数组:
import numpy as np
import ctypes
# Preparing example
class TransferData(ctypes.Structure):
_fields_ = [
('statusReady', ctypes.c_bool),
('velocity', ctypes.c_double * 4),
('pressure', ctypes.c_double * 4)
]
data = TransferData()
output = np.array([12., 13., 11., 10.])
# Actual code
# Both values should be equal but there could be problems with alignment settings
assert ctypes.sizeof(data.pressure) == output.nbytes
ctypes.memmove(ctypes.byref(data.pressure), output.ctypes.data, output.nbytes)
print(list(data.pressure))
Run Code Online (Sandbox Code Playgroud)