我正在尝试编写一些在 GoLang 中创建数组的代码,并将其返回给 python 脚本 ctypes(和一些 numpy)。到目前为止,我所得到的不起作用,我不明白为什么......我将不胜感激任何帮助!
我的 Go 代码是这样的:
func Function(physics_stuff... float64, N int ) []float64{
result := make([]float64, N)
for i:= 0; i< N; i++{
result[i] = blah....
}
return result;
}
我目前正在尝试使用以下方法将此功能导入 python:
from ctypes import c_double, cdll, c_int
from numpy.ctypeslib import ndpointer
lib = cdll.LoadLibrary("./go/library.so")
lib.Function.argtypes = [c_double]*6 + [c_int]
def lovely_python_function(stuff..., N):
lib.Function.restype = ndpointer(dtype = c_double, shape = (N,))
return lib.Function(stuff..., N)
这个 python 函数永远不会返回。来自同一个库的其他函数工作得很好,但它们都返回一个 float64(python 中的 c_double)。
最佳答案
在您的代码中,restype
需要_ndtpr
类型,请参阅:
lib.Function.restype = ndpointer(dtype = c_double, shape = (N,))
在 numpy 文档中也可以看到:
def ndpointer(dtype=None, ndim=None, shape=None, flags=None)
[others texts]
Returns
klass : ndpointer type object
A type object, which is an
_ndtpr
instance containing
dtype, ndim, shape and flags information.[其他文本]
这样lib.Function.restype
就是指针类型,在Golang中使用的类型必须是unsafe.Pointer
。
但是你想要一个需要作为指针传递的 slice :
func Function(s0, s1, s2 float64, N int) unsafe.Pointer {
result := make([]float64, N)
for i := 0; i < N; i++ {
result[i] = (s0 + s1 + s2)
}
return unsafe.Pointer(&result)//<-- pointer of result
}
这会导致在 Go 和 C 之间传递指针的规则。
- C code may not keep a copy of a Go pointer after the call returns.
Source: https://github.com/golang/proposal/blob/master/design/12416-cgo-pointers.md
所以你必须将unsafe.Pointer
转换成uintptr
golang类型。
func Function(s0, s1, s2 float64, N int) uintptr {
result := make([]float64, N)
for i := 0; i < N; i++ {
result[i] = (s0 + s1 + s2)
}
return uintptr(unsafe.Pointer(&result[0]))//<-- note: result[0]
}
这样你就可以正常工作了!
注意:C中slice的结构用typedef struct { void *data; 表示进入 len; GoInt上限; } GoSlice;
,但 C 只需要数据,因为这只需要结果 void *data
(第一个字段,或字段 [0])。
概念验证:https://github.com/ag-studies/stackoverflow-pointers-ref-in-golang
关于python - 如何使用 ctypes 将数组从 Go [lang] 返回到 Python?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51845092/