Faiss
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
/data/users/hoss/faiss/IndexReplicas.h
1 /**
2  * Copyright (c) Facebook, Inc. and its affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #pragma once
9 
10 #include "Index.h"
11 #include "IndexBinary.h"
12 #include "ThreadedIndex.h"
13 
14 namespace faiss {
15 
16 /// Takes individual faiss::Index instances, and splits queries for
17 /// sending to each Index instance, and joins the results together
18 /// when done.
19 /// Each index is managed by a separate CPU thread.
20 template <typename IndexT>
21 class IndexReplicasTemplate : public ThreadedIndex<IndexT> {
22  public:
23  using idx_t = typename IndexT::idx_t;
24  using component_t = typename IndexT::component_t;
25  using distance_t = typename IndexT::distance_t;
26 
27  /// The dimension that all sub-indices must share will be the dimension of the
28  /// first sub-index added
29  /// @param threaded do we use one thread per sub-index or do queries
30  /// sequentially?
31  explicit IndexReplicasTemplate(bool threaded = true);
32 
33  /// @param d the dimension that all sub-indices must share
34  /// @param threaded do we use one thread per sub index or do queries
35  /// sequentially?
36  explicit IndexReplicasTemplate(idx_t d, bool threaded = true);
37 
38  /// int version due to the implicit bool conversion ambiguity of int as
39  /// dimension
40  explicit IndexReplicasTemplate(int d, bool threaded = true);
41 
42  /// Alias for addIndex()
43  void add_replica(IndexT* index) { this->addIndex(index); }
44 
45  /// Alias for removeIndex()
46  void remove_replica(IndexT* index) { this->removeIndex(index); }
47 
48  /// faiss::Index API
49  /// All indices receive the same call
50  void train(idx_t n, const component_t* x) override;
51 
52  /// faiss::Index API
53  /// All indices receive the same call
54  void add(idx_t n, const component_t* 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
59  void search(idx_t n,
60  const component_t* x,
61  idx_t k,
62  distance_t* distances,
63  idx_t* labels) const override;
64 
65  /// reconstructs from the first index
66  void reconstruct(idx_t, component_t *v) const override;
67 
68  protected:
69  /// Called just after an index is added
70  void onAfterAddIndex(IndexT* index) override;
71 };
72 
73 using IndexReplicas = IndexReplicasTemplate<Index>;
74 using IndexBinaryReplicas = IndexReplicasTemplate<IndexBinary>;
75 
76 } // namespace
void removeIndex(IndexT *index)
void train(idx_t n, const component_t *x) override
IndexReplicasTemplate(bool threaded=true)
void remove_replica(IndexT *index)
Alias for removeIndex()
Definition: IndexReplicas.h:46
void onAfterAddIndex(IndexT *index) override
Called just after an index is added.
void addIndex(IndexT *index)
void search(idx_t n, const component_t *x, idx_t k, distance_t *distances, idx_t *labels) const override
void reconstruct(idx_t, component_t *v) const override
reconstructs from the first index
void add(idx_t n, const component_t *x) override
void add_replica(IndexT *index)
Alias for addIndex()
Definition: IndexReplicas.h:43