Faiss
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
/data/users/matthijs/github_faiss/faiss/MetaIndexes.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 // -*- c++ -*-
12 
13 #ifndef META_INDEXES_H
14 #define META_INDEXES_H
15 
16 
17 #include <vector>
18 
19 
20 #include "Index.h"
21 
22 
23 namespace faiss {
24 
25 /** Index that translates search results to ids */
26 struct IndexIDMap : Index {
27  Index * index; ///! the sub-index
28  bool own_fields; ///! whether pointers are deleted in destructo
29  std::vector<long> id_map;
30 
31  explicit IndexIDMap (Index *index);
32 
33  /// Same as add_core, but stores xids instead of sequential ids
34  /// @param xids if non-null, ids to store for the vectors (size n)
35  virtual void add_with_ids (idx_t n, const float * x, const long *xids)
36  override;
37 
38  /// this will fail. Use add_with_ids
39  virtual void add (idx_t n, const float *x) override;
40 
41  virtual void search (
42  idx_t n, const float *x, idx_t k,
43  float *distances, idx_t *labels) const override;
44 
45  virtual void train (idx_t n, const float *x) override;
46 
47  virtual void reset () override;
48 
49  virtual void set_typename () override;
50 
51  virtual ~IndexIDMap ();
52 
53 };
54 
55 /** Index that concatenates the results from several sub-indexes
56  *
57  */
58 struct IndexShards : Index {
59 
60  std::vector<Index*> shard_indexes;
61  bool own_fields; /// should the sub-indexes be deleted along with this?
62  bool threaded;
63  bool successive_ids;
64 
65  /**
66  * @param threaded do we use one thread per sub_index or do
67  * queries sequentially?
68  * @param successive_ids should we shift the returned ids by
69  * the size of each sub-index or return them
70  * as they are?
71  */
72  explicit IndexShards (idx_t d, bool threaded = false,
73  bool successive_ids = true);
74 
75  void add_shard (Index *);
76 
77  // update metric_type and ntotal. Call if you changes something in
78  // the shard indexes.
79  void sync_with_shard_indexes ();
80 
81  Index *at(int i) {return shard_indexes[i]; }
82 
83  /// supported only for sub-indices that implement add_with_ids
84  virtual void add (idx_t n, const float *x) override;
85 
86  virtual void add_with_ids (idx_t n, const float * x, const long *xids)
87  override;
88 
89 
90 
91  virtual void search (
92  idx_t n, const float *x, idx_t k,
93  float *distances, idx_t *labels) const override;
94 
95  virtual void train (idx_t n, const float *x) override;
96 
97  virtual void reset () override;
98 
99  virtual void set_typename () override;
100 
101  virtual ~IndexShards ();
102 
103 };
104 
105 /** splits input vectors in segments and assigns each segment to a sub-index
106  * used to distribute a MultiIndexQuantizer
107  */
108 
110  bool own_fields;
111  bool threaded;
112  std::vector<Index*> sub_indexes;
113  idx_t sum_d; /// sum of dimensions seen so far
114 
115  explicit IndexSplitVectors (idx_t d, bool threaded = false);
116 
117  void add_sub_index (Index *);
118  void sync_with_sub_indexes ();
119 
120  virtual void add (idx_t n, const float *x) override;
121 
122 
123  virtual void search (
124  idx_t n, const float *x, idx_t k,
125  float *distances, idx_t *labels) const override;
126 
127  virtual void train (idx_t n, const float *x) override;
128 
129  virtual void reset () override;
130 
131  virtual void set_typename () override;
132 
133  virtual ~IndexSplitVectors ();
134 
135 };
136 
137 
138 
139 }
140 
141 
142 #endif
virtual void train(idx_t n, const float *x) override
IndexShards(idx_t d, bool threaded=false, bool successive_ids=true)
virtual void search(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels) const override
Definition: MetaIndexes.cpp:69
virtual void add_with_ids(idx_t n, const float *x, const long *xids) override
virtual void search(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels) const override
virtual void reset() override
removes all elements from the database.
virtual void add(idx_t n, const float *x) override
this will fail. Use add_with_ids
Definition: MetaIndexes.cpp:40
int d
vector dimension
Definition: Index.h:66
std::vector< long > id_map
! whether pointers are deleted in destructo
Definition: MetaIndexes.h:29
virtual void train(idx_t n, const float *x) override
virtual void add(idx_t n, const float *x) override
supported only for sub-indices that implement add_with_ids
long idx_t
all indices are this type
Definition: Index.h:64
virtual void add(idx_t n, const float *x) override
bool threaded
should the sub-indexes be deleted along with this?
Definition: MetaIndexes.h:62
virtual void reset() override
removes all elements from the database.
virtual void reset() override
removes all elements from the database.
Definition: MetaIndexes.cpp:53
virtual void search(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels) const override
virtual void train(idx_t n, const float *x) override
Definition: MetaIndexes.cpp:47
virtual void add_with_ids(idx_t n, const float *x, const long *xids) override
Definition: MetaIndexes.cpp:60
IndexSplitVectors(idx_t d, bool threaded=false)
sum of dimensions seen so far
bool own_fields
! the sub-index
Definition: MetaIndexes.h:28