Commit 27a4ba0d authored by Eduardo Pinho's avatar Eduardo Pinho Committed by Matthijs Douze

[C API] Multi-GPU functions (#628)

* [c_api] minor tweak on Index_c header imports

* [c_api] Multi GPU constructs

- Add constructor & destructor for `FaissGpuMultipleClonerOptions`
- Add function `faiss_index_cpu_to_gpu_multiple`
- Add function `faiss_index_cpu_to_gpu_multiple_with_options`

* [c_api] fix doc comments in GpuClonerOptions

* [c_api] Improve cpu_to_gpu_multiple prototype

* [c_api] Improve cpu_to_gpu_multiple prototype

* [c_api] Add dtor definition for GpuMultipleClonerOptions
parent ac7005b6
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
#ifndef FAISS_INDEX_C_H #ifndef FAISS_INDEX_C_H
#define FAISS_INDEX_C_H #define FAISS_INDEX_C_H
#include <stdio.h> #include <stddef.h>
#include "faiss_c.h" #include "faiss_c.h"
#ifdef __cplusplus #ifdef __cplusplus
......
...@@ -10,14 +10,17 @@ ...@@ -10,14 +10,17 @@
// -*- c++ -*- // -*- c++ -*-
#include "GpuAutoTune_c.h" #include "GpuAutoTune_c.h"
#include "GpuClonerOptions_c.h"
#include "macros_impl.h" #include "macros_impl.h"
#include "Index.h" #include "Index.h"
#include "gpu/GpuAutoTune.h" #include "gpu/GpuAutoTune.h"
#include "gpu/GpuClonerOptions.h" #include "gpu/GpuClonerOptions.h"
#include <vector>
using faiss::Index; using faiss::Index;
using faiss::gpu::GpuResources; using faiss::gpu::GpuResources;
using faiss::gpu::GpuClonerOptions; using faiss::gpu::GpuClonerOptions;
using faiss::gpu::GpuMultipleClonerOptions;
int faiss_index_gpu_to_cpu(const FaissIndex* gpu_index, FaissIndex** p_out) { int faiss_index_gpu_to_cpu(const FaissIndex* gpu_index, FaissIndex** p_out) {
try { try {
...@@ -52,3 +55,43 @@ int faiss_index_cpu_to_gpu_with_options( ...@@ -52,3 +55,43 @@ int faiss_index_cpu_to_gpu_with_options(
*p_out = reinterpret_cast<FaissGpuIndex*>(gpu_index); *p_out = reinterpret_cast<FaissGpuIndex*>(gpu_index);
} CATCH_AND_HANDLE } CATCH_AND_HANDLE
} }
int faiss_index_cpu_to_gpu_multiple(
FaissGpuResources* const* resources_vec,
const int* devices, size_t devices_size,
const FaissIndex* index, FaissGpuIndex** p_out)
{
try {
std::vector<GpuResources*> res(devices_size);
for (auto i = 0u; i < devices_size; ++i) {
res[i] = reinterpret_cast<GpuResources*>(resources_vec[i]);
}
std::vector<int> dev(devices, devices + devices_size);
auto gpu_index = faiss::gpu::index_cpu_to_gpu_multiple(
res, dev, reinterpret_cast<const Index*>(index));
*p_out = reinterpret_cast<FaissGpuIndex*>(gpu_index);
} CATCH_AND_HANDLE
}
int faiss_index_cpu_to_gpu_multiple_with_options(
FaissGpuResources** resources_vec, size_t resources_vec_size,
int* devices, size_t devices_size,
const FaissIndex* index, const FaissGpuMultipleClonerOptions* options,
FaissGpuIndex** p_out)
{
try {
std::vector<GpuResources*> res(resources_vec_size);
for (auto i = 0u; i < resources_vec_size; ++i) {
res[i] = reinterpret_cast<GpuResources*>(resources_vec[i]);
}
std::vector<int> dev(devices, devices + devices_size);
auto gpu_index = faiss::gpu::index_cpu_to_gpu_multiple(
res, dev, reinterpret_cast<const Index*>(index),
reinterpret_cast<const GpuMultipleClonerOptions*>(options));
*p_out = reinterpret_cast<FaissGpuIndex*>(gpu_index);
} CATCH_AND_HANDLE
}
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#ifndef FAISS_GPU_AUTO_TUNE_C_H #ifndef FAISS_GPU_AUTO_TUNE_C_H
#define FAISS_GPU_AUTO_TUNE_C_H #define FAISS_GPU_AUTO_TUNE_C_H
#include <stddef.h>
#include "faiss_c.h" #include "faiss_c.h"
#include "GpuClonerOptions_c.h" #include "GpuClonerOptions_c.h"
#include "GpuResources_c.h" #include "GpuResources_c.h"
...@@ -36,6 +37,17 @@ int faiss_index_cpu_to_gpu_with_options( ...@@ -36,6 +37,17 @@ int faiss_index_cpu_to_gpu_with_options(
const FaissIndex *index, const FaissGpuClonerOptions* options, const FaissIndex *index, const FaissGpuClonerOptions* options,
FaissGpuIndex** p_out); FaissGpuIndex** p_out);
/// converts any CPU index that can be converted to GPU
int faiss_index_cpu_to_gpu_multiple(
FaissGpuResources* const* resources_vec, const int* devices, size_t devices_size,
const FaissIndex* index, FaissGpuIndex** p_out);
/// converts any CPU index that can be converted to GPU
int faiss_index_cpu_to_gpu_multiple_with_options(
FaissGpuResources* const* resources_vec, const int* devices, size_t devices_size,
const FaissIndex* index, const FaissGpuMultipleClonerOptions* options,
FaissGpuIndex** p_out);
/// parameter space and setters for GPU indexes /// parameter space and setters for GPU indexes
FAISS_DECLARE_CLASS_INHERITED(GpuParameterSpace, ParameterSpace) FAISS_DECLARE_CLASS_INHERITED(GpuParameterSpace, ParameterSpace)
......
...@@ -23,7 +23,14 @@ int faiss_GpuClonerOptions_new(FaissGpuClonerOptions** p) { ...@@ -23,7 +23,14 @@ int faiss_GpuClonerOptions_new(FaissGpuClonerOptions** p) {
} CATCH_AND_HANDLE } CATCH_AND_HANDLE
} }
int faiss_GpuMultipleClonerOptions_new(FaissGpuMultipleClonerOptions** p) {
try {
*p = reinterpret_cast<FaissGpuMultipleClonerOptions*>(new GpuMultipleClonerOptions());
} CATCH_AND_HANDLE
}
DEFINE_DESTRUCTOR(GpuClonerOptions) DEFINE_DESTRUCTOR(GpuClonerOptions)
DEFINE_DESTRUCTOR(GpuMultipleClonerOptions)
DEFINE_GETTER(GpuClonerOptions, FaissIndicesOptions, indicesOptions) DEFINE_GETTER(GpuClonerOptions, FaissIndicesOptions, indicesOptions)
DEFINE_GETTER(GpuClonerOptions, int, useFloat16CoarseQuantizer) DEFINE_GETTER(GpuClonerOptions, int, useFloat16CoarseQuantizer)
......
...@@ -23,7 +23,7 @@ FAISS_DECLARE_CLASS(GpuClonerOptions) ...@@ -23,7 +23,7 @@ FAISS_DECLARE_CLASS(GpuClonerOptions)
FAISS_DECLARE_DESTRUCTOR(GpuClonerOptions) FAISS_DECLARE_DESTRUCTOR(GpuClonerOptions)
// Default constructor for GpuClonerOptions /// Default constructor for GpuClonerOptions
int faiss_GpuClonerOptions_new(FaissGpuClonerOptions**); int faiss_GpuClonerOptions_new(FaissGpuClonerOptions**);
/// how should indices be stored on index types that support indices /// how should indices be stored on index types that support indices
...@@ -51,6 +51,11 @@ FAISS_DECLARE_GETTER_SETTER(GpuClonerOptions, int, verbose) ...@@ -51,6 +51,11 @@ FAISS_DECLARE_GETTER_SETTER(GpuClonerOptions, int, verbose)
FAISS_DECLARE_CLASS_INHERITED(GpuMultipleClonerOptions, GpuClonerOptions) FAISS_DECLARE_CLASS_INHERITED(GpuMultipleClonerOptions, GpuClonerOptions)
FAISS_DECLARE_DESTRUCTOR(GpuMultipleClonerOptions)
/// Default constructor for GpuMultipleClonerOptions
int faiss_GpuMultipleClonerOptions_new(FaissGpuMultipleClonerOptions**);
/// (boolean) Whether to shard the index across GPUs, versus replication /// (boolean) Whether to shard the index across GPUs, versus replication
/// across GPUs /// across GPUs
FAISS_DECLARE_GETTER_SETTER(GpuMultipleClonerOptions, int, shard) FAISS_DECLARE_GETTER_SETTER(GpuMultipleClonerOptions, int, shard)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment