8 #include "FaissAssert.h"
14 template <
typename IndexT>
15 ThreadedIndex<IndexT>::ThreadedIndex(
bool threaded)
17 : ThreadedIndex(0, threaded) {
20 template <
typename IndexT>
21 ThreadedIndex<IndexT>::ThreadedIndex(
int d,
bool threaded)
24 isThreaded_(threaded) {
27 template <
typename IndexT>
28 ThreadedIndex<IndexT>::~ThreadedIndex() {
29 for (
auto& p : indices_) {
32 FAISS_ASSERT((
bool) p.second);
36 p.second->waitForThreadExit();
39 FAISS_ASSERT(!(
bool) p.second);
48 template <
typename IndexT>
52 if (indices_.empty() && this->
d == 0) {
57 FAISS_THROW_IF_NOT_FMT(this->
d == index->d,
58 "addIndex: dimension mismatch for "
59 "newly added index; expecting dim %d, "
60 "new index has dim %d",
63 if (!indices_.empty()) {
64 auto& existing = indices_.front().first;
66 FAISS_THROW_IF_NOT_MSG(index->metric_type == existing->metric_type,
67 "addIndex: newly added index is "
68 "of different metric type than old index");
71 for (
auto& p : indices_) {
72 FAISS_THROW_IF_NOT_MSG(p.first != index,
73 "addIndex: attempting to add index "
74 "that is already in the collection");
78 indices_.emplace_back(
81 std::unique_ptr<WorkerThread>(isThreaded_ ?
84 onAfterAddIndex(index);
87 template <
typename IndexT>
89 for (
auto it = indices_.begin(); it != indices_.end(); ++it) {
90 if (it->first == index) {
95 FAISS_ASSERT((
bool) it->second);
97 it->second->waitForThreadExit();
100 FAISS_ASSERT(!(
bool) it->second);
104 onAfterRemoveIndex(index);
115 FAISS_THROW_MSG(
"IndexReplicas::removeIndex: index not found");
118 template <
typename IndexT>
121 std::vector<std::future<bool>> v;
123 for (
int i = 0; i < this->indices_.size(); ++i) {
124 auto& p = this->indices_[i];
125 auto indexPtr = p.first;
126 v.emplace_back(p.second->add([f, i, indexPtr](){ f(i, indexPtr); }));
129 waitAndHandleFutures(v);
133 std::vector<std::pair<int, std::exception_ptr>> exceptions;
135 for (
int i = 0; i < this->indices_.size(); ++i) {
136 auto& p = this->indices_[i];
140 exceptions.emplace_back(std::make_pair(i, std::current_exception()));
148 template <
typename IndexT>
150 std::function<
void(
int,
const IndexT*)> f)
const {
151 const_cast<ThreadedIndex<IndexT>*
>(
this)->runOnIndex(
152 [f](
int i, IndexT* idx){ f(i, idx); });
155 template <
typename IndexT>
157 runOnIndex([](
int, IndexT* index){ index->reset(); });
159 this->is_trained =
false;
162 template <
typename IndexT>
167 template <
typename IndexT>
172 template <
typename IndexT>
177 std::vector<std::pair<int, std::exception_ptr>> exceptions;
179 for (
int i = 0; i < v.size(); ++i) {
185 exceptions.emplace_back(std::make_pair(i, std::current_exception()));
void removeIndex(IndexT *index)
void handleExceptions(std::vector< std::pair< int, std::exception_ptr >> &exceptions)
void addIndex(IndexT *index)
void runOnIndex(std::function< void(int, IndexT *)> f)
virtual void onAfterRemoveIndex(IndexT *index)
Called just after an index is removed.
virtual void onAfterAddIndex(IndexT *index)
Called just after an index is added.
size_t d
size of the input vectors