11 #include "IndexProxy.h"
12 #include "../FaissAssert.h"
14 #include "../Clustering.h"
15 #include "GpuIndexFlat.h"
16 #include "StandardGpuResources.h"
19 namespace faiss {
namespace gpu {
21 IndexProxy::IndexProxy():own_fields(false) {
24 IndexProxy::~IndexProxy() {
26 for (
auto& index : indices_)
34 if (!indices_.empty()) {
35 auto& existing = indices_.front().first;
37 if (index->
d != existing->d) {
42 if (index->
ntotal != existing->ntotal) {
61 indices_.emplace_back(
68 for (
auto it = indices_.begin(); it != indices_.end(); ++it) {
69 if (it->first == index) {
73 it->second->waitForThreadExit();
86 std::vector<std::future<bool>> v;
88 for (
auto& index : indices_) {
89 auto indexPtr = index.first;
90 v.emplace_back(index.second->add([indexPtr, f](){ f(indexPtr); }));
94 for (
auto& func : v) {
100 IndexProxy::reset() {
118 FAISS_ASSERT (count() > 0);
119 indices_[0].first->reconstruct (n, x);
129 FAISS_ASSERT(!indices_.empty());
134 auto dim = indices_.front().first->d;
136 std::vector<std::future<bool>> v;
139 auto queriesPerIndex =
141 FAISS_ASSERT(n / queriesPerIndex <= indices_.size());
143 for (
int i = 0; i < indices_.size(); ++i) {
144 auto base = i * queriesPerIndex;
149 auto numForIndex = std::min(queriesPerIndex, n - base);
150 auto queryStart = x + base * dim;
151 auto distancesStart = distances + base * k;
152 auto labelsStart = labels + base * k;
154 auto indexPtr = indices_[i].first;
156 [indexPtr, numForIndex, queryStart, k, distancesStart, labelsStart]() {
157 indexPtr->search(numForIndex, queryStart,
158 k, distancesStart, labelsStart);
161 v.emplace_back(indices_[i].second->add(std::move(fn)));
176 float kmeans_clustering_gpu (
int ngpu,
size_t d,
size_t n,
size_t k,
180 bool storeTransposed)
184 clus.verbose = d * n * k > (1L << 34);
185 FAISS_ASSERT(ngpu >= 1);
187 std::vector<std::unique_ptr<StandardGpuResources> > res;
188 std::vector<std::unique_ptr<GpuIndexFlatL2> > sub_indices;
189 for(
int dev_no = 0; dev_no < ngpu; dev_no++) {
198 sub_indices.emplace_back(
205 index = sub_indices[0].get();
207 for(
int dev_no = 0; dev_no < ngpu; dev_no++) {
208 proxy.addIndex(sub_indices[dev_no].
get());
212 clus.
train (n, x, *index);
214 memcpy(centroids, clus.centroids.data(),
sizeof(*centroids) * d * k);
215 return clus.obj.back();
void removeIndex(faiss::Index *index)
void runOnIndex(std::function< void(faiss::Index *)> f)
virtual void reset()=0
removes all elements from the database.
virtual void train(idx_t, const float *)
bool useFloat16
Whether or not data is stored as float16.
int device
GPU device on which the index is resident.
virtual void add(idx_t n, const float *x)=0
long idx_t
all indices are this type
idx_t ntotal
total nb of indexed vectors
bool verbose
verbosity level
void addIndex(faiss::Index *index)
MetricType metric_type
type of metric this index uses for search
bool is_trained
set if the Index does not require training, or if training is done already