forked from shiwendai/Faiss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIndexProxy.cpp
221 lines (175 loc) · 5.25 KB
/
IndexProxy.cpp
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/**
* 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.
*/
#include "IndexProxy.h"
#include "../FaissAssert.h"
#include "../Clustering.h"
#include "GpuIndexFlat.h"
#include "StandardGpuResources.h"
#include <cstring>
namespace faiss { namespace gpu {
IndexProxy::IndexProxy():own_fields(false) {
}
IndexProxy::~IndexProxy() {
if (own_fields) {
for (auto& index : indices_)
delete index.first;
}
}
void
IndexProxy::addIndex(faiss::Index* index) {
// Make sure that the parameters are the same for all prior indices
if (!indices_.empty()) {
auto& existing = indices_.front().first;
if (index->d != existing->d) {
FAISS_ASSERT(false);
return;
}
if (index->ntotal != existing->ntotal) {
FAISS_ASSERT(false);
return;
}
if (index->metric_type != existing->metric_type) {
FAISS_ASSERT(false);
return;
}
} else {
// Set our parameters
// FIXME: this is a little bit weird
this->d = index->d;
this->ntotal = index->ntotal;
this->verbose = index->verbose;
this->is_trained = index->is_trained;
this->metric_type = index->metric_type;
}
indices_.emplace_back(
std::make_pair(index,
std::unique_ptr<WorkerThread>(new WorkerThread)));
}
void
IndexProxy::removeIndex(faiss::Index* index) {
for (auto it = indices_.begin(); it != indices_.end(); ++it) {
if (it->first == index) {
// This is our index; stop the worker thread before removing it,
// to ensure that it has finished before function exit
it->second->stop();
it->second->waitForThreadExit();
indices_.erase(it);
return;
}
}
// index not found
FAISS_ASSERT(false);
}
void
IndexProxy::runOnIndex(std::function<void(faiss::Index*)> f) {
std::vector<std::future<bool>> v;
for (auto& index : indices_) {
auto indexPtr = index.first;
v.emplace_back(index.second->add([indexPtr, f](){ f(indexPtr); }));
}
// Blocking wait for completion
for (auto& func : v) {
func.get();
}
}
void
IndexProxy::reset() {
runOnIndex([](faiss::Index* index){ index->reset(); });
ntotal = 0;
}
void
IndexProxy::train(Index::idx_t n, const float* x) {
runOnIndex([n, x](faiss::Index* index){ index->train(n, x); });
}
void
IndexProxy::add(Index::idx_t n, const float* x) {
runOnIndex([n, x](faiss::Index* index){ index->add(n, x); });
ntotal += n;
}
void
IndexProxy::reconstruct(Index::idx_t n, float* x) const {
FAISS_ASSERT (count() > 0);
indices_[0].first->reconstruct (n, x);
}
void
IndexProxy::search(faiss::Index::idx_t n,
const float* x,
faiss::Index::idx_t k,
float* distances,
faiss::Index::idx_t* labels) const {
FAISS_ASSERT(!indices_.empty());
if (n == 0) {
return;
}
auto dim = indices_.front().first->d;
std::vector<std::future<bool>> v;
// Partition the query by the number of indices we have
auto queriesPerIndex =
(faiss::Index::idx_t) (n + indices_.size() - 1) / indices_.size();
FAISS_ASSERT(n / queriesPerIndex <= indices_.size());
for (int i = 0; i < indices_.size(); ++i) {
auto base = i * queriesPerIndex;
if (base >= n) {
break;
}
auto numForIndex = std::min(queriesPerIndex, n - base);
auto queryStart = x + base * dim;
auto distancesStart = distances + base * k;
auto labelsStart = labels + base * k;
auto indexPtr = indices_[i].first;
auto fn =
[indexPtr, numForIndex, queryStart, k, distancesStart, labelsStart]() {
indexPtr->search(numForIndex, queryStart,
k, distancesStart, labelsStart);
};
v.emplace_back(indices_[i].second->add(std::move(fn)));
}
// Blocking wait for completion
for (auto& f : v) {
f.get();
}
}
//
// GPU clustering implementation
//
float kmeans_clustering_gpu (int ngpu, size_t d, size_t n, size_t k,
const float *x,
float *centroids,
bool useFloat16,
bool storeTransposed)
{
Clustering clus (d, k);
// display logs if > 16Gflop per iteration
clus.verbose = d * n * k > (1L << 34);
FAISS_ASSERT(ngpu >= 1);
std::vector<std::unique_ptr<StandardGpuResources> > res;
std::vector<std::unique_ptr<GpuIndexFlatL2> > sub_indices;
for(int dev_no = 0; dev_no < ngpu; dev_no++) {
res.emplace_back(new StandardGpuResources());
GpuIndexFlatConfig config;
config.device = dev_no;
config.useFloat16 = useFloat16;
config.storeTransposed = storeTransposed;
sub_indices.emplace_back(
new GpuIndexFlatL2(res.back().get(), d, config));
}
IndexProxy proxy;
Index *index;
if (ngpu == 1) {
index = sub_indices[0].get();
} else {
for(int dev_no = 0; dev_no < ngpu; dev_no++) {
proxy.addIndex(sub_indices[dev_no].get());
}
index = &proxy;
}
clus.train (n, x, *index);
memcpy(centroids, clus.centroids.data(), sizeof(*centroids) * d * k);
return clus.obj.back();
}
} } // namespace