Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

document package maturity #149

Closed
JackMordaunt opened this issue Aug 10, 2023 · 7 comments · Fixed by #168
Closed

document package maturity #149

JackMordaunt opened this issue Aug 10, 2023 · 7 comments · Fixed by #168
Labels
documentation Improvements or additions to documentation

Comments

@JackMordaunt
Copy link

This project seems very cool!

However it's hard to assess the relative maturity of this package; which features it has or doesn't have, what are the largest caveats or roadblocks that one might encounter.

It would be super great to have some high-level documentation about what consumers of this package can expect so that people can assess how suitable/viable this package is.

Thanks! :)

@TotallyGamerJet
Copy link
Collaborator

Well the project hasn't reached a stable 1.0 release so the API may change. However, it continues to be updated for fixes and features so will continue to work into the future.

@JupiterRider
Copy link
Contributor

@TotallyGamerJet purego is cool stuff! Great work you've done so far.

Do you think this can ever make it into golang.org/x/sys ?

@TotallyGamerJet
Copy link
Collaborator

That's really up to the Go team. golang#18296 kind of brings up the idea but no one is currently pushing for it.

@biorisk
Copy link

biorisk commented Sep 5, 2023

Purego is amazing. It let me create Golightly, a purego wrapper of LightGBM, a very nice ML library. Golightly has basic ML functionality working, including training models and applying them.

I'm running into problems with passing more complicated data back and forth (e.g., a 2-dimensional array of float32 or an array of strings).

The current documentation for RegisterFunc covers relationships between simple types. Would it be in the scope of the purego project to document more complicated data structure? Or, does the CGO ecosystem provide documentation already that can be applied in a purego context (and perhaps linked from here)?

@TotallyGamerJet
Copy link
Collaborator

So the more complex types are made up of the simpler ones. An array of strings would be **byte. You can get this type by using a []*byte which you'd then pass to the function using unsafe.SliceData(slice)

As for the two dimensional float array I believe it would just be a single slice with the size of width and height make([]float32, width*height). Unless the function requires the memory be in C in which case you'd use malloc.

@biorisk
Copy link

biorisk commented Sep 8, 2023

@TotallyGamerJet, thank you. I was able to create an array of floats using your advice pretty easily, but had a much harder time to figure out what you meant about the array of strings.

Through trial and error, I came up with this working solution:

myStrings := []string{  "first", "second" }
myBytes := make([]*byte, len(myStrings))
for i, str := range myStrings {
	byteName := append([]byte(str), byte(0))
	myBytes[i] = unsafe.SliceData(byteName)
}
variableForCFunction := unsafe.SliceData(myBytes)

In retrospect, it is exactly what you said, but it didn't click at first. Hopefully, this concrete example code will help out someone else.

@TotallyGamerJet
Copy link
Collaborator

I'm glad you figured it out. Just one note. You want to make sure that myStrings doesn't get garbage collected before the string pointers are no longer needed. You can do that by placing a runtime.KeepAlive(myStrings) call after the last call to the C function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants