forked from DataIntelligenceCrew/go-faiss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpu_bindings.go
60 lines (47 loc) · 1.36 KB
/
gpu_bindings.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//+build gpu
package faiss
/*
#include <faiss/c_api/gpu/StandardGpuResources_c.h>
#include <faiss/c_api/gpu/GpuAutoTune_c.h>
*/
import "C"
import (
"errors"
)
func TransferToGpu(index Index) (Index, error) {
var gpuResources *C.FaissStandardGpuResources
var gpuIndex *C.FaissGpuIndex
c := C.faiss_StandardGpuResources_new(&gpuResources)
if c != 0 {
return nil, errors.New("error on init gpu %v")
}
exitCode := C.faiss_index_cpu_to_gpu(gpuResources, 0, index.cPtr(), &gpuIndex)
if exitCode != 0 {
return nil, errors.New("error transferring to gpu")
}
return &faissIndex{idx: gpuIndex, resource: gpuResources}, nil
}
func TransferToCpu(gpuIndex Index) (Index, error) {
var cpuIndex *C.FaissIndex
exitCode := C.faiss_index_gpu_to_cpu(gpuIndex.cPtr(), &cpuIndex)
if exitCode != 0 {
return nil, errors.New("error transferring to gpu")
}
Free(gpuIndex)
return &faissIndex{idx: cpuIndex}, nil
}
func Free(index Index) {
var gpuResource *C.FaissStandardGpuResources
gpuResource = index.cGpuResource()
C.faiss_StandardGpuResources_free(gpuResource)
index.Delete()
}
func CreateGpuIndex() (Index, error) {
var gpuResource *C.FaissStandardGpuResources
var gpuIndex *C.FaissGpuIndex
c := C.faiss_StandardGpuResources_new(&gpuResource)
if c != 0 {
return nil, errors.New("error on init gpu %v")
}
return &faissIndex{idx: gpuIndex, resource: gpuResource}, nil
}