-
Notifications
You must be signed in to change notification settings - Fork 6
MB-67052: Add GPU bindings #46
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
base: master
Are you sure you want to change the base?
Changes from all commits
ddad139
9ff4c94
03a9151
7f7445e
e7c7650
684e60c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,101 @@ | ||||||
| package faiss | ||||||
|
|
||||||
| /* | ||||||
| #include <stddef.h> | ||||||
| #include <faiss/c_api/gpu/StandardGpuResources_c.h> | ||||||
| #include <faiss/c_api/gpu/GpuAutoTune_c.h> | ||||||
| #include <faiss/c_api/gpu/DeviceUtils_c.h> | ||||||
| */ | ||||||
| import "C" | ||||||
| import ( | ||||||
| "errors" | ||||||
| "fmt" | ||||||
| "unsafe" | ||||||
| ) | ||||||
|
|
||||||
| // NumGPUs returns the number of available GPU devices. | ||||||
| func NumGPUs() (int, error) { | ||||||
| var rv C.int | ||||||
| c := C.faiss_get_num_gpus(&rv) | ||||||
| if c != 0 { | ||||||
| return 0, errors.New("error getting number of GPUs") | ||||||
| } | ||||||
| return int(rv), nil | ||||||
| } | ||||||
|
|
||||||
| func FreeMemory(device int) (uint64, error) { | ||||||
| var freeBytes C.size_t | ||||||
| c := C.faiss_get_free_memory(C.int(device), &freeBytes) | ||||||
| if c != 0 { | ||||||
| return 0, fmt.Errorf("error getting free memory for device %d", device) | ||||||
|
||||||
| } | ||||||
| return uint64(freeBytes), nil | ||||||
| } | ||||||
|
|
||||||
| type GPUIndexImpl struct { | ||||||
| Index | ||||||
| gpuResource *C.FaissStandardGpuResources | ||||||
| } | ||||||
|
|
||||||
| func (g *GPUIndexImpl) Close() { | ||||||
| if g == nil { | ||||||
| return | ||||||
| } | ||||||
| if g.Index != nil { | ||||||
| g.Index.Close() | ||||||
| g.Index = nil | ||||||
| } | ||||||
| if g.gpuResource != nil { | ||||||
| C.faiss_StandardGpuResources_free(g.gpuResource) | ||||||
| g.gpuResource = nil | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| // TransferToGPU transfers a CPU index to the specified GPU device. | ||||||
| func TransferToGPU(index *IndexImpl, device int) (*GPUIndexImpl, error) { | ||||||
| if index == nil { | ||||||
| return nil, errors.New("index cannot be nil") | ||||||
| } | ||||||
| var gpuResource *C.FaissStandardGpuResources | ||||||
| if code := C.faiss_StandardGpuResources_new(&gpuResource); code != 0 { | ||||||
| return nil, fmt.Errorf("failed to initialize GPU resources: error code %d", code) | ||||||
|
||||||
| return nil, fmt.Errorf("failed to initialize GPU resources: error code %d", code) | |
| return nil, fmt.Errorf("failed to initialize GPU resources: error code %d, details: %s", code, C.GoString(C.faiss_get_last_error())) |
Copilot
AI
Nov 13, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider using getLastError() for more detailed error information instead of only including the error code. This pattern is used consistently throughout the codebase and provides better debugging context.
Copilot
AI
Nov 13, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider using getLastError() for more detailed error information instead of only including the error code. This pattern is used consistently throughout the codebase and provides better debugging context.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider using
getLastError()instead of a generic error message for consistency with the rest of the codebase. The underlying C API likely sets a detailed error message that can be retrieved viagetLastError(), which would provide more helpful debugging information.