1- //+build gpu
1+ //go:build gpu
2+ // +build gpu
23
34package faiss
45
56/*
7+
8+ #include <stddef.h>
69#include <faiss/c_api/gpu/StandardGpuResources_c.h>
710#include <faiss/c_api/gpu/GpuAutoTune_c.h>
811*/
912import "C"
1013import (
1114 "errors"
15+ "unsafe"
1216)
1317
14-
1518func TransferToGpu (index Index ) (Index , error ) {
16- var gpuResources * C.FaissStandardGpuResources
19+ var gpuResource * C.FaissStandardGpuResources
1720 var gpuIndex * C.FaissGpuIndex
18- c := C .faiss_StandardGpuResources_new (& gpuResources )
21+ c := C .faiss_StandardGpuResources_new (& gpuResource )
1922 if c != 0 {
2023 return nil , errors .New ("error on init gpu %v" )
2124 }
2225
23- exitCode := C .faiss_index_cpu_to_gpu (gpuResources , 0 , index .cPtr (), & gpuIndex )
26+ exitCode := C .faiss_index_cpu_to_gpu (gpuResource , 0 , index .cPtr (), & gpuIndex )
27+
28+ if exitCode != 0 {
29+ return nil , errors .New ("error transferring to gpu" )
30+ }
31+
32+ var gpuResources []* C.FaissStandardGpuResources
33+ gpuResources = append (gpuResources , gpuResource )
34+
35+ return & faissIndex {idx : gpuIndex , resources : gpuResources }, nil
36+ }
37+
38+ func TransferToAllGPUs (index Index , gpuIndexes []int ) (Index , error ) {
39+ amountOfGPUs := len (gpuIndexes )
40+ gpuResources := make ([]* C.FaissStandardGpuResources , len (gpuIndexes ))
41+ for i := 0 ; i < len (gpuIndexes ); i ++ {
42+ var resourceIndex * C.FaissStandardGpuResources
43+ gpuResources [i ] = resourceIndex
44+
45+ }
46+
47+ var gpuIndex * C.FaissGpuIndex
48+ for i := 0 ; i < amountOfGPUs ; i ++ {
49+ c := C .faiss_StandardGpuResources_new (& gpuResources [i ])
50+ if c != 0 {
51+ return nil , errors .New ("error on init gpu %v" )
52+ }
53+ }
54+
55+ exitCode := C .faiss_index_cpu_to_gpu_multiple (
56+ (* * C .FaissStandardGpuResources )(unsafe .Pointer (& gpuResources [0 ])),
57+ (* C .int )(unsafe .Pointer (& gpuIndexes [0 ])),
58+ C .size_t (len (gpuIndexes )),
59+ index .cPtr (),
60+ & gpuIndex )
61+
2462 if exitCode != 0 {
2563 return nil , errors .New ("error transferring to gpu" )
2664 }
2765
28- return & faissIndex {idx : gpuIndex , resource : gpuResources }, nil
66+ return & faissIndex {idx : gpuIndex , resources : gpuResources }, nil
2967}
3068
3169func TransferToCpu (gpuIndex Index ) (Index , error ) {
@@ -37,15 +75,17 @@ func TransferToCpu(gpuIndex Index) (Index, error) {
3775 }
3876
3977 Free (gpuIndex )
40-
78+
4179 return & faissIndex {idx : cpuIndex }, nil
4280}
4381
4482func Free (index Index ) {
45- var gpuResource * C.FaissStandardGpuResources
46- gpuResource = index .cGpuResource ()
47- C .faiss_StandardGpuResources_free (gpuResource )
83+ gpuResources := index .cGpuResource ()
84+ for _ , gpuResource := range gpuResources {
85+ C .faiss_StandardGpuResources_free (gpuResource )
86+ }
4887 index .Delete ()
88+
4989}
5090
5191func CreateGpuIndex () (Index , error ) {
@@ -56,5 +96,8 @@ func CreateGpuIndex() (Index, error) {
5696 return nil , errors .New ("error on init gpu %v" )
5797 }
5898
59- return & faissIndex {idx : gpuIndex , resource : gpuResource }, nil
99+ var gpuResources []* C.FaissStandardGpuResources
100+ gpuResources = append (gpuResources , gpuResource )
101+
102+ return & faissIndex {idx : gpuIndex , resources : gpuResources }, nil
60103}
0 commit comments