Faiss
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
GpuIndexIVFFlat.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 
9 #pragma once
10 
11 #include "GpuIndexIVF.h"
12 
13 namespace faiss { struct IndexIVFFlat; }
14 
15 namespace faiss { namespace gpu {
16 
17 class IVFFlat;
18 class GpuIndexFlat;
19 
21  inline GpuIndexIVFFlatConfig()
22  : useFloat16IVFStorage(false) {
23  }
24 
25  /// Whether or not IVFFlat inverted list storage is in float16;
26  /// supported on all architectures
28 };
29 
30 /// Wrapper around the GPU implementation that looks like
31 /// faiss::IndexIVFFlat
32 class GpuIndexIVFFlat : public GpuIndexIVF {
33  public:
34  /// Construct from a pre-existing faiss::IndexIVFFlat instance, copying
35  /// data over to the given GPU, if the input index is trained.
36  GpuIndexIVFFlat(GpuResources* resources,
37  const faiss::IndexIVFFlat* index,
39 
40  /// Constructs a new instance with an empty flat quantizer; the user
41  /// provides the number of lists desired.
42  GpuIndexIVFFlat(GpuResources* resources,
43  int dims,
44  int nlist,
45  faiss::MetricType metric,
47 
48  ~GpuIndexIVFFlat() override;
49 
50  /// Reserve GPU memory in our inverted lists for this number of vectors
51  void reserveMemory(size_t numVecs);
52 
53  /// Initialize ourselves from the given CPU index; will overwrite
54  /// all data in ourselves
55  void copyFrom(const faiss::IndexIVFFlat* index);
56 
57  /// Copy ourselves to the given CPU index; will overwrite all data
58  /// in the index instance
59  void copyTo(faiss::IndexIVFFlat* index) const;
60 
61  /// After adding vectors, one can call this to reclaim device memory
62  /// to exactly the amount needed. Returns space reclaimed in bytes
63  size_t reclaimMemory();
64 
65  void reset() override;
66 
67  void train(Index::idx_t n, const float* x) override;
68 
69  protected:
70  /// Called from GpuIndex for add/add_with_ids
71  void addImpl_(int n,
72  const float* x,
73  const Index::idx_t* ids) override;
74 
75  /// Called from GpuIndex for search
76  void searchImpl_(int n,
77  const float* x,
78  int k,
79  float* distances,
80  Index::idx_t* labels) const override;
81 
82  private:
83  GpuIndexIVFFlatConfig ivfFlatConfig_;
84 
85  /// Desired inverted list memory reservation
86  size_t reserveMemoryVecs_;
87 
88  /// Instance that we own; contains the inverted list
89  IVFFlat* index_;
90 };
91 
92 } } // namespace
GpuIndexIVFFlat(GpuResources *resources, const faiss::IndexIVFFlat *index, GpuIndexIVFFlatConfig config=GpuIndexIVFFlatConfig())
void addImpl_(int n, const float *x, const Index::idx_t *ids) override
Called from GpuIndex for add/add_with_ids.
void copyFrom(const faiss::IndexIVFFlat *index)
void train(Index::idx_t n, const float *x) override
long idx_t
all indices are this type
Definition: Index.h:62
void copyTo(faiss::IndexIVFFlat *index) const
void reserveMemory(size_t numVecs)
Reserve GPU memory in our inverted lists for this number of vectors.
void searchImpl_(int n, const float *x, int k, float *distances, Index::idx_t *labels) const override
Called from GpuIndex for search.
void reset() override
removes all elements from the database.
MetricType
Some algorithms support both an inner product version and a L2 search version.
Definition: Index.h:44