Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Impl to_gpu for new index types #39

Merged
merged 1 commit into from
Dec 18, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 137 additions & 0 deletions src/index/gpu.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! GPU Index implementation

use super::flat::FlatIndexImpl;
use super::ivf_flat::IVFFlatIndexImpl;
use super::scalar_quantizer::IVFScalarQuantizerIndexImpl;
use super::{
AssignSearchResult, CpuIndex, FromInnerPtr, Idx, Index, IndexImpl, NativeIndex,
RangeSearchResult, SearchResult,
Expand Down Expand Up @@ -424,6 +426,141 @@ impl FlatIndexImpl {
}
}

impl IVFFlatIndexImpl {
/// Build a GPU in from the given CPU native index, yielding two
/// independent indices. The operation fails if the index does
/// not provide GPU support.
pub fn to_gpu<'gpu, G>(
&self,
gpu_res: &'gpu G,
device: i32,
) -> Result<GpuIndexImpl<'gpu, IVFFlatIndexImpl>>
where
G: GpuResourcesProvider,
{
GpuIndexImpl::from_cpu(self, gpu_res, device)
}

/// Build a GPU index from the given CPU native index, discarding the
/// CPU-backed index. The operation fails if the index does not
/// provide GPU support.
pub fn into_gpu<'gpu, G>(
self,
gpu_res: &'gpu G,
device: i32,
) -> Result<GpuIndexImpl<'gpu, IVFFlatIndexImpl>>
where
G: GpuResourcesProvider,
{
self.to_gpu(gpu_res, device)
}

/// Build a GPU index from the given CPU native index.
///
/// # Errors
///
/// The operation fails if the number of GPU resources and number of
/// devices do not match, or the index does not provide GPU support.
pub fn to_gpu_multiple<'gpu, G: 'gpu>(
&self,
gpu_res: &'gpu [G],
devices: &[i32],
) -> Result<GpuIndexImpl<'gpu, IVFFlatIndexImpl>>
where
G: GpuResourcesProvider,
{
GpuIndexImpl::from_cpu_multiple(&self, gpu_res, devices)
}

/// Build a GPU index from the given CPU native index. The index residing
/// in CPU memory is discarded in the process.
///
/// # Errors
///
/// The operation fails if the number of GPU resources and number of
/// devices do not match, or the index does not provide GPU support.
pub fn into_gpu_multiple<'gpu, G: 'gpu>(
self,
gpu_res: &'gpu [G],
devices: &[i32],
) -> Result<GpuIndexImpl<'gpu, IVFFlatIndexImpl>>
where
G: GpuResourcesProvider,
{
self.to_gpu_multiple(gpu_res, devices)
// let the CPU index drop naturally
}
}

impl<Q> IVFScalarQuantizerIndexImpl<Q>
where
Q: NativeIndex + CpuIndex,
{
/// Build a GPU in from the given CPU native index, yielding two
/// independent indices. The operation fails if the index does
/// not provide GPU support.
pub fn to_gpu<'gpu, G>(
&self,
gpu_res: &'gpu G,
device: i32,
) -> Result<GpuIndexImpl<'gpu, IVFScalarQuantizerIndexImpl<Q>>>
where
G: GpuResourcesProvider,
{
GpuIndexImpl::from_cpu(self, gpu_res, device)
}

/// Build a GPU index from the given CPU native index, discarding the
/// CPU-backed index. The operation fails if the index does not
/// provide GPU support.
pub fn into_gpu<'gpu, G>(
self,
gpu_res: &'gpu G,
device: i32,
) -> Result<GpuIndexImpl<'gpu, IVFScalarQuantizerIndexImpl<Q>>>
where
G: GpuResourcesProvider,
{
self.to_gpu(gpu_res, device)
}

/// Build a GPU index from the given CPU native index.
///
/// # Errors
///
/// The operation fails if the number of GPU resources and number of
/// devices do not match, or the index does not provide GPU support.
pub fn to_gpu_multiple<'gpu, G: 'gpu>(
&self,
gpu_res: &'gpu [G],
devices: &[i32],
) -> Result<GpuIndexImpl<'gpu, IVFScalarQuantizerIndexImpl<Q>>>
where
G: GpuResourcesProvider,
{
GpuIndexImpl::from_cpu_multiple(&self, gpu_res, devices)
}

/// Build a GPU index from the given CPU native index. The index residing
/// in CPU memory is discarded in the process.
///
/// # Errors
///
/// The operation fails if the number of GPU resources and number of
/// devices do not match, or the index does not provide GPU support.
pub fn into_gpu_multiple<'gpu, G: 'gpu>(
self,
gpu_res: &'gpu [G],
devices: &[i32],
) -> Result<GpuIndexImpl<'gpu, IVFScalarQuantizerIndexImpl<Q>>>
where
G: GpuResourcesProvider,
{
self.to_gpu_multiple(gpu_res, devices)
// let the CPU index drop naturally
}
}

#[cfg(test)]
mod tests {
use super::super::{index_factory, CpuIndex, Idx, Index};
Expand Down