Skip to content

Commit a4c3dd4

Browse files
Merge pull request #1 from AnyVisionltd/support-multiple-gpus
Support multiple gpus
2 parents 72e5dea + 608c887 commit a4c3dd4

5 files changed

Lines changed: 87 additions & 17 deletions

File tree

go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
6+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
7+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
8+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
9+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
10+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

gpu_bindings.go

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,69 @@
1-
//+build gpu
1+
//go:build gpu
2+
// +build gpu
23

34
package 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
*/
912
import "C"
1013
import (
1114
"errors"
15+
"unsafe"
1216
)
1317

14-
1518
func 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

3169
func 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

4482
func 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

5191
func 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
}

gpu_bindings_cpu.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
package faiss
44

5+
import "C"
56
import "errors"
67

78
func TransferToGpu(index Index) (Index, error) {
@@ -19,3 +20,11 @@ func Free(gpuIndex Index) error {
1920
func CreateGpuIndex() (Index, error) {
2021
return nil, errors.New("Not supported when running in CPU mode..")
2122
}
23+
24+
func TransferToAllGPUs(index Index,gpuIndexes []int) (Index, error) {
25+
return nil, errors.New("Not supported when running in CPU mode..")
26+
}
27+
28+
func TransferToAllGPUsWithOptions(index Index,gpuIndexes []int) (Index, error) {
29+
return nil, errors.New("Not supported when running in CPU mode..")
30+
}

gpu_bindings_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,15 @@ func TestTransferToGpuAndBack(t *testing.T) {
117117

118118
func TestFreeGPUResource(t *testing.T) {
119119
for i := 0; i < 20; i++ {
120+
gpus:= []int{0}
120121
t.Logf("creating index %v", i)
121122
flatIndex, err := NewIndexFlatIP(256)
122123
require.Nil(t, err)
123-
flatIndexGpu, err := TransferToGpu(flatIndex)
124+
flatIndexGpu, err := TransferToAllGPUs(flatIndex, gpus)
124125
require.Nil(t, err)
125126

126127
t.Log("created indexes, freeing..")
127-
err = Free(flatIndexGpu)
128+
Free(flatIndexGpu)
128129
require.Nil(t, err)
129130
t.Log("freed, memory should be freed..")
130131
time.Sleep(1 * time.Second)

index.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,23 @@ type Index interface {
5959

6060
cPtr() *C.FaissIndex
6161

62-
cGpuResource() *C.FaissStandardGpuResources
62+
cGpuResource() []*C.FaissStandardGpuResources
63+
64+
cGpuMultipleClonerOptions() *C.FaissGpuMultipleClonerOptions
6365
}
6466

6567
type faissIndex struct {
6668
idx *C.FaissIndex
67-
resource *C.FaissStandardGpuResources
69+
resources []*C.FaissStandardGpuResources
70+
options *C.FaissGpuMultipleClonerOptions
71+
}
72+
73+
func (idx *faissIndex) cGpuResource() []*C.FaissStandardGpuResources {
74+
return idx.resources
6875
}
6976

70-
func (idx *faissIndex) cGpuResource() *C.FaissStandardGpuResources {
71-
return idx.resource
77+
func (idx *faissIndex) cGpuMultipleClonerOptions() *C.FaissGpuMultipleClonerOptions {
78+
return idx.options
7279
}
7380

7481
func (idx *faissIndex) cPtr() *C.FaissIndex {

0 commit comments

Comments
 (0)