Re: [go-nuts] Re: Passing 2d arrays from Go to C using runtime.Pinner

2024-01-31 Thread peterGo
Michael, Here is a Go solution to the OP's problem. It uses Go memory and Go pointers passed to C in Go memory point to pinned Go memory. https://go.dev/play/p/KLdenIesz1K Peter On Monday, January 29, 2024 at 12:21:18 PM UTC-5 Michael Knyszek wrote: > Thanks for the reproducer. I think I've p

Re: [go-nuts] Re: Passing 2d arrays from Go to C using runtime.Pinner

2024-01-29 Thread 'Michael Knyszek' via golang-nuts
Thanks for the reproducer. I think I've puzzled out what's going wrong and it's pretty subtle. TL;DR: You can work around this by either calling `calloc` or just allocating `inRows` as Go memory and pinning that as well. The latter will be safer and faster overall. It's not totally clear to me at

[go-nuts] Re: Passing 2d arrays from Go to C using runtime.Pinner

2024-01-29 Thread peterGo
Michael, The OP appears to have lost interest in debugging. Here's my minimal, reproducible example that produces the same fatal errror: https://go.dev/play/p/flEmSh1euqR (run locally) If the runtime.GC() statement is uncommented then the fatal error does not occur. The use of this profli

[go-nuts] Re: Passing 2d arrays from Go to C using runtime.Pinner

2024-01-26 Thread 'Michael Knyszek' via golang-nuts
Ignoring more efficient ways to pass memory to C for a moment, superficially I do think your code using Pinner should work. Do you have a full reproducer? It's hard to tell from just looking at your code if your code is the problem, or its just enough to trigger some other cgo issue elsewhere i

[go-nuts] Re: Passing 2d arrays from Go to C using runtime.Pinner

2024-01-26 Thread Tamás Gulácsi
To convert a Go slice to C array is easy with unsafe.Slice. The problem is that the multi-dimensional C array is an array of pointers (*(*float64)) which cannot travel between the C/Go barrier. In your example, you flatten your slice, and recreate the pointers in that one big slice. You could g