Faiss

- Main Page
- Namespaces
- Classes
- Files

- File List

All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
IndexProxy.h
1 /**
2 * Copyright (c) 2015-present, Facebook, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD+Patents license found in the
6 * LICENSE file in the root directory of this source tree.
7 */
8
9 // Copyright 2004-present Facebook. All Rights Reserved.
10
11 #pragma once
12
13 #include "../Index.h"
14 #include "utils/WorkerThread.h"
15 #include <memory>
16 #include <vector>
17
18 namespace faiss { namespace gpu {
19
20 /// Takes individual faiss::Index instances, and splits queries for
21 /// sending to each Index instance, and joins the results together
22 /// when done.
23 /// Each index is managed by a separate CPU thread.
24 class IndexProxy : public faiss::Index {
25 public:
26 IndexProxy();
27 ~IndexProxy() override;
28
29 /// Adds an index that is managed by ourselves.
30 /// WARNING: once an index is added to this proxy, it becomes unsafe
31 /// to touch it from any other thread than that on which is managing
32 /// it, until we are shut down. Use runOnIndex to perform work on it
33 /// instead.
34 void addIndex(faiss::Index* index);
35
36 /// Remove an index that is managed by ourselves.
37 /// This will flush all pending work on that index, and then shut
38 /// down its managing thread, and will remove the index.
39 void removeIndex(faiss::Index* index);
40
41 /// Run a function on all indices, in the thread that the index is
42 /// managed in.
43 void runOnIndex(std::function<void(faiss::Index*)> f);
44
45 /// faiss::Index API
46 /// All indices receive the same call
47 void reset() override;
48
49 /// faiss::Index API
50 /// All indices receive the same call
51 void train(Index::idx_t n, const float* x) override;
52
53 /// faiss::Index API
54 /// All indices receive the same call
55 void add(Index::idx_t n, const float* x) override;
56
57 /// faiss::Index API
58 /// Query is partitioned into a slice for each sub-index
59 /// split by ceil(n / #indices) for our sub-indices
60 void search(faiss::Index::idx_t n,
61 const float* x,
62 faiss::Index::idx_t k,
63 float* distances,
64 faiss::Index::idx_t* labels) const override;
65
66 /// reconstructs from the first index
67 void reconstruct(idx_t, float *v) const override;
68
69 bool own_fields;
70
71 int count() const {return indices_.size(); }
72
73 faiss::Index* at(int i) {return indices_[i].first; }
74 const faiss::Index* at(int i) const {return indices_[i].first; }
75
76
77 private:
78 /// Collection of Index instances, with their managing worker thread
79 mutable std::vector<std::pair<faiss::Index*,
80 std::unique_ptr<WorkerThread> > > indices_;
81 };
82
83
84
85 /** Clustering on GPU (is here because uses Proxy with ngpu > 1
86 *
87 * @param ngpu nb of GPUs to use
88 * @param d dimension of the data
89 * @param n nb of training vectors
90 * @param k nb of output centroids
91 * @param x training set (size n * d)
92 * @param centroids output centroids (size k * d)
93 * @return final quantization error
94 */
95 float kmeans_clustering_gpu (int ngpu, size_t d, size_t n, size_t k,
96 const float *x,
97 float *centroids,
98 bool useFloat16,
99 bool storeTransposed);
100
101
102
103 } } // namespace
faiss::gpu::IndexProxy::reset
void reset() override
Definition: IndexProxy.cpp:100
faiss::gpu::IndexProxy::removeIndex
void removeIndex(faiss::Index *index)
Definition: IndexProxy.cpp:67
faiss::gpu::IndexProxy::runOnIndex
void runOnIndex(std::function< void(faiss::Index *)> f)
Definition: IndexProxy.cpp:85
faiss::gpu::IndexProxy::train
void train(Index::idx_t n, const float *x) override
Definition: IndexProxy.cpp:106
faiss::gpu::IndexProxy::reconstruct
void reconstruct(idx_t, float *v) const override
reconstructs from the first index
Definition: IndexProxy.cpp:117
faiss::gpu::IndexProxy
Definition: IndexProxy.h:24
faiss::Index::idx_t
long idx_t
all indices are this type
Definition: Index.h:62
faiss::gpu::IndexProxy::addIndex
void addIndex(faiss::Index *index)
Definition: IndexProxy.cpp:32
faiss::gpu::IndexProxy::add
void add(Index::idx_t n, const float *x) override
Definition: IndexProxy.cpp:111
faiss::Index
Definition: Index.h:60
faiss::gpu::IndexProxy::search
void search(faiss::Index::idx_t n, const float *x, faiss::Index::idx_t k, float *distances, faiss::Index::idx_t *labels) const override
Definition: IndexProxy.cpp:124