10 #include "IndexProxy.h"
11 #include "../FaissAssert.h"
13 #include "../Clustering.h"
14 #include "GpuIndexFlat.h"
15 #include "StandardGpuResources.h"
18 namespace faiss {
namespace gpu {
20 IndexProxy::IndexProxy():own_fields(false) {
23 IndexProxy::~IndexProxy() {
25 for (
auto& index : indices_)
33 if (!indices_.empty()) {
34 auto& existing = indices_.front().first;
36 if (index->
d != existing->d) {
41 if (index->
ntotal != existing->ntotal) {
60 indices_.emplace_back(
67 for (
auto it = indices_.begin(); it != indices_.end(); ++it) {
68 if (it->first == index) {
72 it->second->waitForThreadExit();
85 std::vector<std::future<bool>> v;
87 for (
auto& index : indices_) {
88 auto indexPtr = index.first;
89 v.emplace_back(index.second->add([indexPtr, f](){ f(indexPtr); }));
93 for (
auto& func : v) {
117 FAISS_ASSERT (count() > 0);
118 indices_[0].first->reconstruct (n, x);
128 FAISS_ASSERT(!indices_.empty());
133 auto dim = indices_.front().first->d;
135 std::vector<std::future<bool>> v;
138 auto queriesPerIndex =
140 FAISS_ASSERT(n / queriesPerIndex <= indices_.size());
142 for (
int i = 0; i < indices_.size(); ++i) {
143 auto base = i * queriesPerIndex;
148 auto numForIndex = std::min(queriesPerIndex, n - base);
149 auto queryStart = x + base * dim;
150 auto distancesStart = distances + base * k;
151 auto labelsStart = labels + base * k;
153 auto indexPtr = indices_[i].first;
155 [indexPtr, numForIndex, queryStart, k, distancesStart, labelsStart]() {
156 indexPtr->search(numForIndex, queryStart,
157 k, distancesStart, labelsStart);
160 v.emplace_back(indices_[i].second->add(std::move(fn)));
175 float kmeans_clustering_gpu (
int ngpu,
size_t d,
size_t n,
size_t k,
179 bool storeTransposed)
183 clus.verbose = d * n * k > (1L << 34);
184 FAISS_ASSERT(ngpu >= 1);
186 std::vector<std::unique_ptr<StandardGpuResources> > res;
187 std::vector<std::unique_ptr<GpuIndexFlatL2> > sub_indices;
188 for(
int dev_no = 0; dev_no < ngpu; dev_no++) {
197 sub_indices.emplace_back(
204 index = sub_indices[0].get();
206 for(
int dev_no = 0; dev_no < ngpu; dev_no++) {
207 proxy.addIndex(sub_indices[dev_no].
get());
211 clus.
train (n, x, *index);
213 memcpy(centroids, clus.centroids.data(),
sizeof(*centroids) * d * k);
214 return clus.obj.back();
void removeIndex(faiss::Index *index)
void runOnIndex(std::function< void(faiss::Index *)> f)
virtual void reset()=0
removes all elements from the database.
virtual void train(idx_t n, const float *x)
bool useFloat16
Whether or not data is stored as float16.
int device
GPU device on which the index is resident.
virtual void add(idx_t n, const float *x)=0
long idx_t
all indices are this type
idx_t ntotal
total nb of indexed vectors
bool verbose
verbosity level
void addIndex(faiss::Index *index)
MetricType metric_type
type of metric this index uses for search
bool is_trained
set if the Index does not require training, or if training is done already