Faiss

- Main Page
- Namespaces
- Classes
- Files

- File List

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 IndexIDMap () {own_fields=false; index=nullptr; }
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
109 struct IndexSplitVectors: Index {
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
faiss::IndexShards::train
virtual void train(idx_t n, const float *x) override
Definition: MetaIndexes.cpp:310
faiss::IndexShards::IndexShards
IndexShards(idx_t d, bool threaded=false, bool successive_ids=true)
Definition: MetaIndexes.cpp:280
faiss::IndexIDMap::search
virtual void search(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels) const override
Definition: MetaIndexes.cpp:69
faiss::IndexShards::add_with_ids
virtual void add_with_ids(idx_t n, const float *x, const long *xids) override
Definition: MetaIndexes.cpp:350
faiss::IndexShards::search
virtual void search(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels) const override
Definition: MetaIndexes.cpp:408
faiss::IndexIDMap
Definition: MetaIndexes.h:26
faiss::IndexSplitVectors::reset
virtual void reset() override
removes all elements from the database.
Definition: MetaIndexes.cpp:640
faiss::IndexIDMap::add
virtual void add(idx_t n, const float *x) override
this will fail. Use add_with_ids
Definition: MetaIndexes.cpp:40
faiss::IndexSplitVectors
Definition: MetaIndexes.h:109
faiss::Index::d
int d
vector dimension
Definition: Index.h:66
faiss::IndexIDMap::id_map
std::vector< long > id_map
! whether pointers are deleted in destructo
Definition: MetaIndexes.h:29
faiss::IndexSplitVectors::train
virtual void train(idx_t n, const float *x) override
Definition: MetaIndexes.cpp:635
faiss::IndexShards::add
virtual void add(idx_t n, const float *x) override
supported only for sub-indices that implement add_with_ids
Definition: MetaIndexes.cpp:333
faiss::Index::idx_t
long idx_t
all indices are this type
Definition: Index.h:64
faiss::IndexSplitVectors::add
virtual void add(idx_t n, const float *x) override
Definition: MetaIndexes.cpp:535
faiss::IndexShards::threaded
bool threaded
should the sub-indexes be deleted along with this?
Definition: MetaIndexes.h:62
faiss::IndexShards::reset
virtual void reset() override
removes all elements from the database.
Definition: MetaIndexes.cpp:400
faiss::IndexIDMap::reset
virtual void reset() override
removes all elements from the database.
Definition: MetaIndexes.cpp:53
faiss::IndexSplitVectors::search
virtual void search(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels) const override
Definition: MetaIndexes.cpp:580
faiss::IndexIDMap::train
virtual void train(idx_t n, const float *x) override
Definition: MetaIndexes.cpp:47
faiss::Index
Definition: Index.h:61
faiss::IndexShards
Definition: MetaIndexes.h:58
faiss::IndexIDMap::add_with_ids
virtual void add_with_ids(idx_t n, const float *x, const long *xids) override
Definition: MetaIndexes.cpp:60
faiss::IndexSplitVectors::IndexSplitVectors
IndexSplitVectors(idx_t d, bool threaded=false)
sum of dimensions seen so far
Definition: MetaIndexes.cpp:505
faiss::IndexIDMap::own_fields
bool own_fields
! the sub-index
Definition: MetaIndexes.h:28