Faiss
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
IndexProxy.h
1 
2 /**
3  * Copyright (c) 2015-present, Facebook, Inc.
4  * All rights reserved.
5  *
6  * This source code is licensed under the CC-by-NC license found in the
7  * LICENSE file in the root directory of this source tree.
8  */
9 
10 // Copyright 2004-present Facebook. All Rights Reserved.
11 
12 #pragma once
13 
14 #include "../Index.h"
15 #include "utils/WorkerThread.h"
16 #include <memory>
17 #include <vector>
18 
19 namespace faiss { namespace gpu {
20 
21 /// Takes individual faiss::Index instances, and splits queries for
22 /// sending to each Index instance, and joins the results together
23 /// when done.
24 /// Each index is managed by a separate CPU thread.
25 class IndexProxy : public faiss::Index {
26  public:
27  IndexProxy();
28  ~IndexProxy() override;
29 
30  /// Adds an index that is managed by ourselves.
31  /// WARNING: once an index is added to this proxy, it becomes unsafe
32  /// to touch it from any other thread than that on which is managing
33  /// it, until we are shut down. Use runOnIndex to perform work on it
34  /// instead.
35  void addIndex(faiss::Index* index);
36 
37  /// Remove an index that is managed by ourselves.
38  /// This will flush all pending work on that index, and then shut
39  /// down its managing thread, and will remove the index.
40  void removeIndex(faiss::Index* index);
41 
42  /// Run a function on all indices, in the thread that the index is
43  /// managed in.
44  void runOnIndex(std::function<void(faiss::Index*)> f);
45 
46  /// faiss::Index API
47  /// All indices receive the same call
48  void reset() override;
49 
50  /// faiss::Index API
51  /// All indices receive the same call
52  void train(Index::idx_t n, const float* x) override;
53 
54  /// faiss::Index API
55  /// All indices receive the same call
56  void add(Index::idx_t n, const float* x) override;
57 
58  /// faiss::Index API
59  /// Query is partitioned into a slice for each sub-index
60  /// split by ceil(n / #indices) for our sub-indices
62  const float* x,
64  float* distances,
65  faiss::Index::idx_t* labels) const override;
66 
67  /// reconstructs from the first index
68  void reconstruct(idx_t, float *v) const override;
69 
70  void set_typename() override;
71 
72  bool own_fields;
73 
74  int count() const {return indices_.size(); }
75 
76  faiss::Index* at(int i) {return indices_[i].first; }
77  const faiss::Index* at(int i) const {return indices_[i].first; }
78 
79 
80  private:
81  /// Collection of Index instances, with their managing worker thread
82  mutable std::vector<std::pair<faiss::Index*,
83  std::unique_ptr<WorkerThread>>> indices_;
84 };
85 
86 
87 
88 /** Clustering on GPU (is here because uses Proxy with ngpu > 1
89  *
90  * @param ngpu nb of GPUs to use
91  * @param d dimension of the data
92  * @param n nb of training vectors
93  * @param k nb of output centroids
94  * @param x training set (size n * d)
95  * @param centroids output centroids (size k * d)
96  * @return final quantization error
97  */
98 float kmeans_clustering_gpu (int ngpu, size_t d, size_t n, size_t k,
99  const float *x,
100  float *centroids,
101  bool useFloat16);
102 
103 
104 
105 } } // namespace
void reset() override
Definition: IndexProxy.cpp:101
void removeIndex(faiss::Index *index)
Definition: IndexProxy.cpp:68
void runOnIndex(std::function< void(faiss::Index *)> f)
Definition: IndexProxy.cpp:86
void train(Index::idx_t n, const float *x) override
Definition: IndexProxy.cpp:107
void reconstruct(idx_t, float *v) const override
reconstructs from the first index
Definition: IndexProxy.cpp:118
long idx_t
all indices are this type
Definition: Index.h:64
void addIndex(faiss::Index *index)
Definition: IndexProxy.cpp:33
void add(Index::idx_t n, const float *x) override
Definition: IndexProxy.cpp:112
void search(faiss::Index::idx_t n, const float *x, faiss::Index::idx_t k, float *distances, faiss::Index::idx_t *labels) const override
Definition: IndexProxy.cpp:125