All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
GpuIndex.h
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include "../Index.h"
#include "utils/MemorySpace.h"
namespace faiss { namespace gpu {
class GpuResources;
struct GpuIndexConfig {
inline GpuIndexConfig()
: device(0),
memorySpace(MemorySpace::Device) {
}
/// GPU device on which the index is resident
int device;
/// What memory space to use for primary storage.
/// On Pascal and above (CC 6+) architectures, allows GPUs to use
/// more memory than is available on the GPU.
MemorySpace memorySpace;
};
class GpuIndex : public faiss::Index {
public:
GpuIndex(GpuResources* resources,
int dims,
faiss::MetricType metric,
GpuIndexConfig config);
inline int getDevice() const {
return device_;
}
inline GpuResources* getResources() {
return resources_;
}
/// Set the minimum data size for searches (in MiB) for which we use
/// CPU -> GPU paging
void setMinPagingSize(size_t size);
/// Returns the current minimum data size for paged searches
size_t getMinPagingSize() const;
/// `x` can be resident on the CPU or any GPU; copies are performed
/// as needed
/// Handles paged adds if the add set is too large; calls addInternal_
void add(faiss::Index::idx_t, const float* x) override;
/// `x` and `ids` can be resident on the CPU or any GPU; copies are
/// performed as needed
/// Handles paged adds if the add set is too large; calls addInternal_
void add_with_ids(Index::idx_t n,
const float* x,
const Index::idx_t* ids) override;
/// `x`, `distances` and `labels` can be resident on the CPU or any
/// GPU; copies are performed as needed
void search(Index::idx_t n,
const float* x,
Index::idx_t k,
float* distances,
Index::idx_t* labels) const override;
protected:
/// Does addImpl_ require IDs? If so, and no IDs are provided, we will
/// generate them sequentially based on the order in which the IDs are added
virtual bool addImplRequiresIDs_() const = 0;
/// Overridden to actually perform the add
/// All data is guaranteed to be resident on our device
virtual void addImpl_(int n,
const float* x,
const Index::idx_t* ids) = 0;
/// Overridden to actually perform the search
/// All data is guaranteed to be resident on our device
virtual void searchImpl_(int n,
const float* x,
int k,
float* distances,
Index::idx_t* labels) const = 0;
private:
/// Handles paged adds if the add set is too large, passes to
/// addImpl_ to actually perform the add for the current page
void addPaged_(int n,
const float* x,
const Index::idx_t* ids);
/// Calls addImpl_ for a single page of GPU-resident data
void addPage_(int n,
const float* x,
const Index::idx_t* ids);
/// Calls searchImpl_ for a single page of GPU-resident data
void searchNonPaged_(int n,
const float* x,
int k,
float* outDistancesData,
Index::idx_t* outIndicesData) const;
/// Calls searchImpl_ for a single page of GPU-resident data,
/// handling paging of the data and copies from the CPU
void searchFromCpuPaged_(int n,
const float* x,
int k,
float* outDistancesData,
Index::idx_t* outIndicesData) const;
protected:
/// Manages streams, cuBLAS handles and scratch memory for devices
GpuResources* resources_;
/// The GPU device we are resident on
const int device_;
/// The memory space of our primary storage on the GPU
const MemorySpace memorySpace_;
/// Size above which we page copies from the CPU to GPU
size_t minPagedSize_;
};
} } // namespace
faiss::gpu::GpuIndex::getMinPagingSize
size_t getMinPagingSize() const
Returns the current minimum data size for paged searches.
Definition: GpuIndex.cu:74
faiss::gpu::GpuIndex::searchImpl_
virtual void searchImpl_(int n, const float *x, int k, float *distances, Index::idx_t *labels) const =0
faiss::gpu::GpuResources
Definition: GpuResources.h:21
faiss::gpu::GpuIndex::addImplRequiresIDs_
virtual bool addImplRequiresIDs_() const =0
faiss::gpu::GpuIndexConfig::device
int device
GPU device on which the index is resident.
Definition: GpuIndex.h:25
faiss::gpu::GpuIndex
Definition: GpuIndex.h:33
faiss::gpu::GpuIndex::add_with_ids
void add_with_ids(Index::idx_t n, const float *x, const Index::idx_t *ids) override
Definition: GpuIndex.cu:85
faiss::Index::idx_t
long idx_t
all indices are this type
Definition: Index.h:62
faiss::gpu::GpuIndexConfig::memorySpace
MemorySpace memorySpace
Definition: GpuIndex.h:30
faiss::gpu::GpuIndex::device_
const int device_
The GPU device we are resident on.
Definition: GpuIndex.h:126
faiss::gpu::GpuIndex::resources_
GpuResources * resources_
Manages streams, cuBLAS handles and scratch memory for devices.
Definition: GpuIndex.h:123
faiss::gpu::GpuIndex::add
void add(faiss::Index::idx_t, const float *x) override
Definition: GpuIndex.cu:79
faiss::gpu::GpuIndex::memorySpace_
const MemorySpace memorySpace_
The memory space of our primary storage on the GPU.
Definition: GpuIndex.h:129
faiss::gpu::GpuIndexConfig
Definition: GpuIndex.h:18
faiss::Index
Definition: Index.h:61
faiss::gpu::GpuIndex::minPagedSize_
size_t minPagedSize_
Size above which we page copies from the CPU to GPU.
Definition: GpuIndex.h:132
faiss::gpu::GpuIndex::setMinPagingSize
void setMinPagingSize(size_t size)
Definition: GpuIndex.cu:69
faiss::gpu::GpuIndex::addImpl_
virtual void addImpl_(int n, const float *x, const Index::idx_t *ids)=0
faiss::gpu::GpuIndex::search
void search(Index::idx_t n, const float *x, Index::idx_t k, float *distances, Index::idx_t *labels) const override
Definition: GpuIndex.cu:176
faiss::MetricType
MetricType
Some algorithms support both an inner product version and a L2 search version.
Definition: Index.h:44