| 1 |
/* |
| 2 |
* Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 |
* |
| 4 |
* This source code is licensed under the MIT license found in the |
| 5 |
* LICENSE file in the root directory of this source tree. |
| 6 |
*/ |
| 7 |
|
| 8 |
// -*- c++ -*- |
| 9 |
|
| 10 |
#include "GpuAutoTune_c.h" |
| 11 |
#include <faiss/Index.h> |
| 12 |
#include <faiss/gpu/GpuAutoTune.h> |
| 13 |
#include <faiss/gpu/GpuCloner.h> |
| 14 |
#include <faiss/gpu/GpuClonerOptions.h> |
| 15 |
#include <faiss/gpu/GpuResources.h> |
| 16 |
#include <vector> |
| 17 |
#include "GpuClonerOptions_c.h" |
| 18 |
#include "macros_impl.h" |
| 19 |
|
| 20 |
using faiss::Index; |
| 21 |
using faiss::gpu::GpuClonerOptions; |
| 22 |
using faiss::gpu::GpuMultipleClonerOptions; |
| 23 |
using faiss::gpu::GpuResourcesProvider; |
| 24 |
|
| 25 |
int faiss_index_gpu_to_cpu(const FaissIndex* gpu_index, FaissIndex** p_out) { |
| 26 |
try { |
| 27 |
auto cpu_index = faiss::gpu::index_gpu_to_cpu( |
| 28 |
reinterpret_cast<const Index*>(gpu_index)); |
| 29 |
*p_out = reinterpret_cast<FaissIndex*>(cpu_index); |
| 30 |
} |
| 31 |
CATCH_AND_HANDLE |
| 32 |
} |
| 33 |
|
| 34 |
/// converts any CPU index that can be converted to GPU |
| 35 |
int faiss_index_cpu_to_gpu( |
| 36 |
FaissGpuResourcesProvider* provider, |
| 37 |
int device, |
| 38 |
const FaissIndex* index, |
| 39 |
FaissGpuIndex** p_out) { |
| 40 |
try { |
| 41 |
auto res = reinterpret_cast<GpuResourcesProvider*>(provider); |
| 42 |
auto gpu_index = faiss::gpu::index_cpu_to_gpu( |
| 43 |
res, device, reinterpret_cast<const Index*>(index)); |
| 44 |
*p_out = reinterpret_cast<FaissGpuIndex*>(gpu_index); |
| 45 |
} |
| 46 |
CATCH_AND_HANDLE |
| 47 |
} |
| 48 |
|
| 49 |
int faiss_index_cpu_to_gpu_with_options( |
| 50 |
FaissGpuResourcesProvider* provider, |
| 51 |
int device, |
| 52 |
const FaissIndex* index, |
| 53 |
const FaissGpuClonerOptions* options, |
| 54 |
FaissGpuIndex** p_out) { |
| 55 |
try { |
| 56 |
auto res = reinterpret_cast<GpuResourcesProvider*>(provider); |
| 57 |
auto gpu_index = faiss::gpu::index_cpu_to_gpu( |
| 58 |
res, |
| 59 |
device, |
| 60 |
reinterpret_cast<const Index*>(index), |
| 61 |
reinterpret_cast<const GpuClonerOptions*>(options)); |
| 62 |
*p_out = reinterpret_cast<FaissGpuIndex*>(gpu_index); |
| 63 |
} |
| 64 |
CATCH_AND_HANDLE |
| 65 |
} |
| 66 |
|
| 67 |
int faiss_index_cpu_to_gpu_multiple( |
| 68 |
FaissGpuResourcesProvider* const* providers_vec, |
| 69 |
const int* devices, |
| 70 |
size_t devices_size, |
| 71 |
const FaissIndex* index, |
| 72 |
FaissGpuIndex** p_out) { |
| 73 |
try { |
| 74 |
std::vector<GpuResourcesProvider*> res(devices_size); |
| 75 |
for (auto i = 0u; i < devices_size; ++i) { |
| 76 |
res[i] = reinterpret_cast<GpuResourcesProvider*>(providers_vec[i]); |
| 77 |
} |
| 78 |
|
| 79 |
std::vector<int> dev(devices, devices + devices_size); |
| 80 |
|
| 81 |
auto gpu_index = faiss::gpu::index_cpu_to_gpu_multiple( |
| 82 |
res, dev, reinterpret_cast<const Index*>(index)); |
| 83 |
*p_out = reinterpret_cast<FaissGpuIndex*>(gpu_index); |
| 84 |
} |
| 85 |
CATCH_AND_HANDLE |
| 86 |
} |
| 87 |
|
| 88 |
int faiss_index_cpu_to_gpu_multiple_with_options( |
| 89 |
FaissGpuResourcesProvider* const* providers_vec, |
| 90 |
size_t providers_vec_size, |
| 91 |
const int* devices, |
| 92 |
size_t devices_size, |
| 93 |
const FaissIndex* index, |
| 94 |
const FaissGpuMultipleClonerOptions* options, |
| 95 |
FaissGpuIndex** p_out) { |
| 96 |
try { |
| 97 |
std::vector<GpuResourcesProvider*> res(providers_vec_size); |
| 98 |
for (auto i = 0u; i < providers_vec_size; ++i) { |
| 99 |
res[i] = reinterpret_cast<GpuResourcesProvider*>(providers_vec[i]); |
| 100 |
} |
| 101 |
|
| 102 |
std::vector<int> dev(devices, devices + devices_size); |
| 103 |
|
| 104 |
auto gpu_index = faiss::gpu::index_cpu_to_gpu_multiple( |
| 105 |
res, |
| 106 |
dev, |
| 107 |
reinterpret_cast<const Index*>(index), |
| 108 |
reinterpret_cast<const GpuMultipleClonerOptions*>(options)); |
| 109 |
*p_out = reinterpret_cast<FaissGpuIndex*>(gpu_index); |
| 110 |
} |
| 111 |
CATCH_AND_HANDLE |
| 112 |
} |