11 #include "../FaissAssert.h"
12 #include "GpuResources.h"
13 #include "utils/DeviceUtils.h"
16 namespace faiss {
namespace gpu {
19 constexpr
size_t kAddPageSize = (size_t) 256 * 1024 * 1024;
20 constexpr
size_t kSearchPageSize = (size_t) 256 * 1024 * 1024;
23 constexpr
size_t kAddVecSize = (size_t) 512 * 1024;
28 constexpr
size_t kSearchVecSize = (size_t) 32 * 1024;
30 GpuIndex::GpuIndex(GpuResources* resources,
33 GpuIndexConfig config) :
35 resources_(resources),
36 device_(config.device),
37 memorySpace_(config.memorySpace) {
38 FAISS_THROW_IF_NOT_FMT(device_ < getNumDevices(),
39 "Invalid GPU device %d", device_);
41 FAISS_THROW_IF_NOT_MSG(dims > 0,
"Invalid number of dimensions");
43 #ifdef FAISS_UNIFIED_MEM
44 FAISS_THROW_IF_NOT_FMT(
45 memorySpace_ == MemorySpace::Device ||
46 (memorySpace_ == MemorySpace::Unified &&
47 getFullUnifiedMemSupport(device_)),
48 "Device %d does not support full CUDA 8 Unified Memory (CC 6.0+)",
51 FAISS_THROW_IF_NOT_MSG(memorySpace_ == MemorySpace::Device,
52 "Must compile with CUDA 8+ for Unified Memory support");
55 FAISS_ASSERT(resources_);
56 resources_->initializeForDevice(device_);
77 FAISS_THROW_IF_NOT_MSG(this->
is_trained,
"Index not trained");
80 size_t totalSize = n * (size_t) this->
d *
sizeof(
float);
82 if (totalSize > kAddPageSize || n > kAddVecSize) {
84 size_t maxNumVecsForPageSize =
85 kAddPageSize / ((size_t) this->
d *
sizeof(
float));
88 maxNumVecsForPageSize = std::max(maxNumVecsForPageSize, (
size_t) 1);
90 size_t tileSize = std::min((
size_t) n, maxNumVecsForPageSize);
91 tileSize = std::min(tileSize, kSearchVecSize);
93 for (
size_t i = 0; i < n; i += tileSize) {
94 size_t curNum = std::min(tileSize, n - i);
97 x + i * (
size_t) this->
d,
98 ids ? ids + i :
nullptr);
114 FAISS_THROW_IF_NOT_MSG(this->
is_trained,
"Index not trained");
117 size_t totalSize = n * (size_t) this->
d *
sizeof(
float);
119 if ((totalSize > kSearchPageSize) || (n > kSearchVecSize)) {
123 size_t maxNumVecsForPageSize =
124 kSearchPageSize / ((size_t) this->
d *
sizeof(
float));
127 maxNumVecsForPageSize = std::max(maxNumVecsForPageSize, (
size_t) 1);
129 size_t tileSize = std::min((
size_t) n, maxNumVecsForPageSize);
130 tileSize = std::min(tileSize, kSearchVecSize);
132 for (
size_t i = 0; i < n; i += tileSize) {
133 size_t curNum = std::min(tileSize, n - i);
136 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 version and a L2 search version.