forked from shiwendai/Faiss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGpuIndex.h
99 lines (78 loc) · 2.74 KB
/
GpuIndex.h
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD+Patents license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include "../Index.h"
#include "utils/MemorySpace.h"
namespace faiss { namespace gpu {
class GpuResources;
struct GpuIndexConfig {
inline GpuIndexConfig()
: device(0),
memorySpace(MemorySpace::Device) {
}
/// GPU device on which the index is resident
int device;
/// What memory space to use for primary storage.
/// On Pascal and above (CC 6+) architectures, allows GPUs to use
/// more memory than is available on the GPU.
MemorySpace memorySpace;
};
class GpuIndex : public faiss::Index {
public:
GpuIndex(GpuResources* resources,
int dims,
faiss::MetricType metric,
GpuIndexConfig config);
int getDevice() const {
return device_;
}
GpuResources* getResources() {
return resources_;
}
/// `x` can be resident on the CPU or any GPU; copies are performed
/// as needed
/// Handles paged adds if the add set is too large; calls addInternal_
void add(faiss::Index::idx_t, const float* x) override;
/// `x` and `ids` can be resident on the CPU or any GPU; copies are
/// performed as needed
/// Handles paged adds if the add set is too large; calls addInternal_
void add_with_ids(Index::idx_t n, const float* x, const Index::idx_t* ids)
override;
/// `x`, `distances` and `labels` can be resident on the CPU or any
/// GPU; copies are performed as needed
void search(
faiss::Index::idx_t n,
const float* x,
faiss::Index::idx_t k,
float* distances,
faiss::Index::idx_t* labels) const override;
protected:
/// Handles paged adds if the add set is too large, passes to
/// addImpl_ to actually perform the add for the current page
void addInternal_(Index::idx_t n,
const float* x,
const Index::idx_t* ids);
/// Overridden to actually perform the add
virtual void addImpl_(Index::idx_t n,
const float* x,
const Index::idx_t* ids) = 0;
/// Overridden to actually perform the search
virtual void searchImpl_(faiss::Index::idx_t n,
const float* x,
faiss::Index::idx_t k,
float* distances,
faiss::Index::idx_t* labels) const = 0;
protected:
/// Manages streans, cuBLAS handles and scratch memory for devices
GpuResources* resources_;
/// The GPU device we are resident on
const int device_;
/// The memory space of our primary storage on the GPU
const MemorySpace memorySpace_;
};
} } // namespace