Faiss

- Main Page
- Namespaces
- Classes
- Files

- File List

All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
/data/users/hoss/faiss/ThreadedIndex.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 "WorkerThread.h"
13 #include <memory>
14 #include <vector>
15
16 namespace faiss {
17
18 /// A holder of indices in a collection of threads
19 /// The interface to this class itself is not thread safe
20 template <typename IndexT>
21 class ThreadedIndex : public IndexT {
22 public:
23 explicit ThreadedIndex(bool threaded);
24 explicit ThreadedIndex(int d, bool threaded);
25
26 ~ThreadedIndex() override;
27
28 /// override an index that is managed by ourselves.
29 /// WARNING: once an index is added, it becomes unsafe to touch it from any
30 /// other thread than that on which is managing it, until we are shut
31 /// down. Use runOnIndex to perform work on it instead.
32 void addIndex(IndexT* index);
33
34 /// Remove an index that is managed by ourselves.
35 /// This will flush all pending work on that index, and then shut
36 /// down its managing thread, and will remove the index.
37 void removeIndex(IndexT* index);
38
39 /// Run a function on all indices, in the thread that the index is
40 /// managed in.
41 /// Function arguments are (index in collection, index pointer)
42 void runOnIndex(std::function<void(int, IndexT*)> f);
43 void runOnIndex(std::function<void(int, const IndexT*)> f) const;
44
45 /// faiss::Index API
46 /// All indices receive the same call
47 void reset() override;
48
49 /// Returns the number of sub-indices
50 int count() const { return indices_.size(); }
51
52 /// Returns the i-th sub-index
53 IndexT* at(int i) { return indices_[i].first; }
54
55 /// Returns the i-th sub-index (const version)
56 const IndexT* at(int i) const { return indices_[i].first; }
57
58 /// Whether or not we are responsible for deleting our contained indices
59 bool own_fields;
60
61 protected:
62 /// Called just after an index is added
63 virtual void onAfterAddIndex(IndexT* index);
64
65 /// Called just after an index is removed
66 virtual void onAfterRemoveIndex(IndexT* index);
67
68 protected:
69 static void waitAndHandleFutures(std::vector<std::future<bool>>& v);
70
71 /// Collection of Index instances, with their managing worker thread if any
72 std::vector<std::pair<IndexT*, std::unique_ptr<WorkerThread>>> indices_;
73
74 /// Is this index multi-threaded?
75 bool isThreaded_;
76 };
77
78 } // namespace
79
80 #include "ThreadedIndex-inl.h"
faiss::ThreadedIndex::removeIndex
void removeIndex(IndexT *index)
Definition: ThreadedIndex-inl.h:88
faiss::ThreadedIndex::isThreaded_
bool isThreaded_
Is this index multi-threaded?
Definition: ThreadedIndex.h:75
faiss::ThreadedIndex::at
const IndexT * at(int i) const
Returns the i-th sub-index (const version)
Definition: ThreadedIndex.h:56
faiss::ThreadedIndex
Definition: ThreadedIndex.h:21
faiss::ThreadedIndex::own_fields
bool own_fields
Whether or not we are responsible for deleting our contained indices.
Definition: ThreadedIndex.h:59
faiss::ThreadedIndex::addIndex
void addIndex(IndexT *index)
Definition: ThreadedIndex-inl.h:49
faiss::ThreadedIndex::indices_
std::vector< std::pair< IndexT *, std::unique_ptr< WorkerThread > > > indices_
Collection of Index instances, with their managing worker thread if any.
Definition: ThreadedIndex.h:72
faiss::ThreadedIndex::count
int count() const
Returns the number of sub-indices.
Definition: ThreadedIndex.h:50
faiss::ThreadedIndex::runOnIndex
void runOnIndex(std::function< void(int, IndexT *)> f)
Definition: ThreadedIndex-inl.h:119
faiss::ThreadedIndex::onAfterRemoveIndex
virtual void onAfterRemoveIndex(IndexT *index)
Called just after an index is removed.
Definition: ThreadedIndex-inl.h:169
faiss::ThreadedIndex::at
IndexT * at(int i)
Returns the i-th sub-index.
Definition: ThreadedIndex.h:53
faiss::ThreadedIndex::onAfterAddIndex
virtual void onAfterAddIndex(IndexT *index)
Called just after an index is added.
Definition: ThreadedIndex-inl.h:164
faiss::ThreadedIndex::reset
void reset() override
Definition: ThreadedIndex-inl.h:156