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