Faiss
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
GpuIndexFlat.h
1 
2 /**
3  * Copyright (c) 2015-present, Facebook, Inc.
4  * All rights reserved.
5  *
6  * This source code is licensed under the CC-by-NC license found in the
7  * LICENSE file in the root directory of this source tree.
8  */
9 
10 // Copyright 2004-present Facebook. All Rights Reserved.
11 
12 #pragma once
13 
14 #include "GpuIndex.h"
15 
16 namespace faiss {
17 
18 struct IndexFlat;
19 struct IndexFlatL2;
20 struct IndexFlatIP;
21 
22 }
23 
24 namespace faiss { namespace gpu {
25 
26 struct FlatIndex;
27 
29  inline GpuIndexFlatConfig()
30  : device(0),
31  useFloat16(false),
32  storeTransposed(false) {
33  }
34 
35  int device;
36  bool useFloat16;
37  bool storeTransposed;
38 };
39 
40 /// Wrapper around the GPU implementation that looks like
41 /// faiss::IndexFlat; copies over centroid data from a given
42 /// faiss::IndexFlat
43 class GpuIndexFlat : public GpuIndex {
44  public:
45  /// Construct from a pre-existing faiss::IndexFlat instance, copying
46  /// data over to the given GPU
47  GpuIndexFlat(GpuResources* resources,
48  const faiss::IndexFlat* index,
50 
51  /// Construct an empty instance that can be added to
52  GpuIndexFlat(GpuResources* resources,
53  int dims,
54  faiss::MetricType metric,
56 
57  ~GpuIndexFlat() override;
58 
59  /// Set the minimum data size for searches (in MiB) for which we use
60  /// CPU -> GPU paging
61  void setMinPagingSize(size_t size);
62 
63  /// Returns the current minimum data size for paged searches
64  size_t getMinPagingSize() const;
65 
66  /// Do we store vectors and perform math in float16?
67  bool getUseFloat16() const;
68 
69  /// Initialize ourselves from the given CPU index; will overwrite
70  /// all data in ourselves
71  void copyFrom(const faiss::IndexFlat* index);
72 
73  /// Copy ourselves to the given CPU index; will overwrite all data
74  /// in the index instance
75  void copyTo(faiss::IndexFlat* index) const;
76 
77  /// Returns the number of vectors we contain
78  size_t getNumVecs() const;
79 
80  /// Clears all vectors from this index
81  void reset() override;
82 
83  /// This index is not trained, so this does nothing
84  void train(Index::idx_t n, const float* x) override;
85 
86  /// `x` can be resident on the CPU or any GPU; the proper copies are
87  /// performed
88  void add(Index::idx_t n, const float* x) override;
89 
90  /// `x`, `distances` and `labels` can be resident on the CPU or any
91  /// GPU; copies are performed as needed
93  const float* x,
95  float* distances,
96  faiss::Index::idx_t* labels) const override;
97 
98  /// Reconstruction methods; prefer the batch reconstruct as it will
99  /// be more efficient
100  void reconstruct(faiss::Index::idx_t key, float* out) const override;
101 
102  /// Batch reconstruction method
105  float* out) const override;
106 
107  void set_typename() override;
108 
109  /// For internal access
110  inline FlatIndex* getGpuData() { return data_; }
111 
112  protected:
113  /// Called from search when the input data is on the CPU;
114  /// potentially allows for pinned memory usage
115  void searchFromCpuPaged_(int n,
116  const float* x,
117  int k,
118  float* outDistancesData,
119  int* outIndicesData) const;
120 
121  void searchNonPaged_(int n,
122  const float* x,
123  int k,
124  float* outDistancesData,
125  int* outIndicesData) const;
126 
127  protected:
128  /// Size above which we page copies from the CPU to GPU
130 
131  const GpuIndexFlatConfig config_;
132 
133  /// Holds our GPU data containing the list of vectors
135 };
136 
137 /// Wrapper around the GPU implementation that looks like
138 /// faiss::IndexFlatL2; copies over centroid data from a given
139 /// faiss::IndexFlat
140 class GpuIndexFlatL2 : public GpuIndexFlat {
141  public:
142  /// Construct from a pre-existing faiss::IndexFlatL2 instance, copying
143  /// data over to the given GPU
144  GpuIndexFlatL2(GpuResources* resources,
145  faiss::IndexFlatL2* index,
147 
148  /// Construct an empty instance that can be added to
149  GpuIndexFlatL2(GpuResources* resources,
150  int dims,
152 
153  /// Initialize ourselves from the given CPU index; will overwrite
154  /// all data in ourselves
155  void copyFrom(faiss::IndexFlatL2* index);
156 
157  /// Copy ourselves to the given CPU index; will overwrite all data
158  /// in the index instance
159  void copyTo(faiss::IndexFlatL2* index);
160 };
161 
162 /// Wrapper around the GPU implementation that looks like
163 /// faiss::IndexFlatIP; copies over centroid data from a given
164 /// faiss::IndexFlat
165 class GpuIndexFlatIP : public GpuIndexFlat {
166  public:
167  /// Construct from a pre-existing faiss::IndexFlatIP instance, copying
168  /// data over to the given GPU
169  GpuIndexFlatIP(GpuResources* resources,
170  faiss::IndexFlatIP* index,
172 
173  /// Construct an empty instance that can be added to
174  GpuIndexFlatIP(GpuResources* resources,
175  int dims,
177 
178  /// Initialize ourselves from the given CPU index; will overwrite
179  /// all data in ourselves
180  void copyFrom(faiss::IndexFlatIP* index);
181 
182  /// Copy ourselves to the given CPU index; will overwrite all data
183  /// in the index instance
184  void copyTo(faiss::IndexFlatIP* index);
185 };
186 
187 } } // namespace
void copyFrom(faiss::IndexFlatL2 *index)
void copyTo(faiss::IndexFlat *index) const
void reconstruct_n(faiss::Index::idx_t i0, faiss::Index::idx_t num, float *out) const override
Batch reconstruction method.
size_t getMinPagingSize() const
Returns the current minimum data size for paged searches.
Definition: GpuIndexFlat.cu:70
Holder of GPU resources for a particular flat index.
Definition: FlatIndex.cuh:23
FlatIndex * getGpuData()
For internal access.
Definition: GpuIndexFlat.h:110
void copyTo(faiss::IndexFlatL2 *index)
void searchFromCpuPaged_(int n, const float *x, int k, float *outDistancesData, int *outIndicesData) const
size_t getNumVecs() const
Returns the number of vectors we contain.
GpuIndexFlat(GpuResources *resources, const faiss::IndexFlat *index, GpuIndexFlatConfig config=GpuIndexFlatConfig())
Definition: GpuIndexFlat.cu:33
void setMinPagingSize(size_t size)
Definition: GpuIndexFlat.cu:65
GpuIndexFlatL2(GpuResources *resources, faiss::IndexFlatL2 *index, GpuIndexFlatConfig config=GpuIndexFlatConfig())
bool getUseFloat16() const
Do we store vectors and perform math in float16?
Definition: GpuIndexFlat.cu:75
void reconstruct(faiss::Index::idx_t key, float *out) const override
void copyTo(faiss::IndexFlatIP *index)
GpuIndexFlatIP(GpuResources *resources, faiss::IndexFlatIP *index, GpuIndexFlatConfig config=GpuIndexFlatConfig())
long idx_t
all indices are this type
Definition: Index.h:64
void copyFrom(const faiss::IndexFlat *index)
Definition: GpuIndexFlat.cu:80
void add(Index::idx_t n, const float *x) override
size_t minPagedSize_
Size above which we page copies from the CPU to GPU.
Definition: GpuIndexFlat.h:129
void reset() override
Clears all vectors from this index.
void copyFrom(faiss::IndexFlatIP *index)
void search(faiss::Index::idx_t n, const float *x, faiss::Index::idx_t k, float *distances, faiss::Index::idx_t *labels) const override
void train(Index::idx_t n, const float *x) override
This index is not trained, so this does nothing.
MetricType
Some algorithms support both an inner product vetsion and a L2 search version.
Definition: Index.h:44
FlatIndex * data_
Holds our GPU data containing the list of vectors.
Definition: GpuIndexFlat.h:134