12 #include "../FaissAssert.h"
13 #include "GpuResources.h"
14 #include "utils/DeviceUtils.h"
17 namespace faiss {
namespace gpu {
20 constexpr
size_t kAddPageSize = (size_t) 256 * 1024 * 1024;
21 constexpr
size_t kSearchPageSize = (size_t) 256 * 1024 * 1024;
24 constexpr
size_t kAddVecSize = (size_t) 512 * 1024;
29 constexpr
size_t kSearchVecSize = (size_t) 32 * 1024;
31 GpuIndex::GpuIndex(GpuResources* resources,
34 GpuIndexConfig config) :
36 resources_(resources),
37 device_(config.device),
38 memorySpace_(config.memorySpace) {
39 FAISS_THROW_IF_NOT_FMT(device_ < getNumDevices(),
40 "Invalid GPU device %d", device_);
42 FAISS_THROW_IF_NOT_MSG(dims > 0,
"Invalid number of dimensions");
44 #ifdef FAISS_UNIFIED_MEM
45 FAISS_THROW_IF_NOT_FMT(
46 memorySpace_ == MemorySpace::Device ||
47 (memorySpace_ == MemorySpace::Unified &&
48 getFullUnifiedMemSupport(device_)),
49 "Device %d does not support full CUDA 8 Unified Memory (CC 6.0+)",
52 FAISS_THROW_IF_NOT_MSG(memorySpace_ == MemorySpace::Device,
53 "Must compile with CUDA 8+ for Unified Memory support");
56 FAISS_ASSERT(resources_);
57 resources_->initializeForDevice(device_);
78 FAISS_THROW_IF_NOT_MSG(this->
is_trained,
"Index not trained");
81 size_t totalSize = n * (size_t) this->
d *
sizeof(
float);
83 if (totalSize > kAddPageSize || n > kAddVecSize) {
85 size_t maxNumVecsForPageSize =
86 kAddPageSize / ((size_t) this->
d *
sizeof(
float));
89 maxNumVecsForPageSize = std::max(maxNumVecsForPageSize, (
size_t) 1);
91 size_t tileSize = std::min((
size_t) n, maxNumVecsForPageSize);
92 tileSize = std::min(tileSize, kSearchVecSize);
94 for (
size_t i = 0; i < n; i += tileSize) {
95 size_t curNum = std::min(tileSize, n - i);
98 x + i * (
size_t) this->
d,
99 ids ? ids + i :
nullptr);
115 FAISS_THROW_IF_NOT_MSG(this->
is_trained,
"Index not trained");
118 size_t totalSize = n * (size_t) this->
d *
sizeof(
float);
120 if ((totalSize > kSearchPageSize) || (n > kSearchVecSize)) {
124 size_t maxNumVecsForPageSize =
125 kSearchPageSize / ((size_t) this->
d *
sizeof(
float));
128 maxNumVecsForPageSize = std::max(maxNumVecsForPageSize, (
size_t) 1);
130 size_t tileSize = std::min((
size_t) n, maxNumVecsForPageSize);
131 tileSize = std::min(tileSize, kSearchVecSize);
133 for (
size_t i = 0; i < n; i += tileSize) {
134 size_t curNum = std::min(tileSize, n - i);
137 x + i * (
size_t) this->
d,
void search(faiss::Index::idx_t n, const float *x, faiss::Index::idx_t k, float *distances, faiss::Index::idx_t *labels) const override
virtual void searchImpl_(faiss::Index::idx_t n, const float *x, faiss::Index::idx_t k, float *distances, faiss::Index::idx_t *labels) const =0
Overridden to actually perform the search.
void addInternal_(Index::idx_t n, const float *x, const Index::idx_t *ids)
void add_with_ids(Index::idx_t n, const float *x, const Index::idx_t *ids) override
const int device_
The GPU device we are resident on.
long idx_t
all indices are this type
virtual void addImpl_(Index::idx_t n, const float *x, const Index::idx_t *ids)=0
Overridden to actually perform the add.
void add(faiss::Index::idx_t, const float *x) override
bool is_trained
set if the Index does not require training, or if training is done already
MetricType
Some algorithms support both an inner product vetsion and a L2 search version.