I am trying to write some code that creates an array in GoLang, and returns it to a python script ctypes (and some numpy). What I have got so far doesn't work, and I cannot figure out why... I would appreciate any help!
My Go code goes something like this:
func Function(physics_stuff... float64, N int ) []float64{
result := make([]float64, N)
for i:= 0; i< N; i++{
result[i] = blah....
}
return result;
}
and I am currently trying to import this functionality to python using:
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)
This python function never returns. Other functions from the same library work just fine, but they all return a single float64 (c_double in python).
In your code restype
is expecting _ndtpr
type, see:
lib.Function.restype = ndpointer(dtype = c_double, shape = (N,))
See too in numpy document:
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.[others texts]
In this way lib.Function.restype
is pointer type which appropriated type in Golang must be unsafe.Pointer
.
However you want of an slice that need be passed as pointer:
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
}
This cause a problem in Rules for passing pointers between Go and 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
So you must convert unsafe.Pointer
to uintptr
golang type.
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]
}
In this way you will work fine!
Note: The structure of slice in C is represented by typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;
, but C expect only data, for this is need result only void *data
(first field, or field[0]).
PoC: https://github.com/ag-studies/stackoverflow-pointers-ref-in-golang