2018-02-26 18:23:17 +08:00
|
|
|
/**
|
2019-06-12 21:46:08 +08:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2018-02-26 18:23:17 +08:00
|
|
|
*
|
2019-06-12 21:46:08 +08:00
|
|
|
* This source code is licensed under the MIT license found in the
|
2018-02-26 18:23:17 +08:00
|
|
|
* LICENSE file in the root directory of this source tree.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Copyright 2004-present Facebook. All Rights Reserved.
|
|
|
|
// -*- c++ -*-
|
|
|
|
|
|
|
|
#include "GpuAutoTune_c.h"
|
2021-03-04 21:01:59 +08:00
|
|
|
#include <faiss/Index.h>
|
2020-07-27 23:26:17 +08:00
|
|
|
#include <faiss/gpu/GpuAutoTune.h>
|
2021-02-25 20:44:50 +08:00
|
|
|
#include <faiss/gpu/GpuCloner.h>
|
2020-07-27 23:26:17 +08:00
|
|
|
#include <faiss/gpu/GpuClonerOptions.h>
|
2021-02-25 20:44:50 +08:00
|
|
|
#include <faiss/gpu/GpuResources.h>
|
2018-12-24 01:46:07 +08:00
|
|
|
#include <vector>
|
2021-02-25 20:44:50 +08:00
|
|
|
#include "GpuClonerOptions_c.h"
|
|
|
|
#include "macros_impl.h"
|
2018-02-26 18:23:17 +08:00
|
|
|
|
|
|
|
using faiss::Index;
|
|
|
|
using faiss::gpu::GpuClonerOptions;
|
2018-12-24 01:46:07 +08:00
|
|
|
using faiss::gpu::GpuMultipleClonerOptions;
|
2021-03-04 21:01:59 +08:00
|
|
|
using faiss::gpu::GpuResourcesProvider;
|
2018-02-26 18:23:17 +08:00
|
|
|
|
|
|
|
int faiss_index_gpu_to_cpu(const FaissIndex* gpu_index, FaissIndex** p_out) {
|
|
|
|
try {
|
|
|
|
auto cpu_index = faiss::gpu::index_gpu_to_cpu(
|
2021-02-25 20:44:50 +08:00
|
|
|
reinterpret_cast<const Index*>(gpu_index));
|
2018-02-26 18:23:17 +08:00
|
|
|
*p_out = reinterpret_cast<FaissIndex*>(cpu_index);
|
2021-02-25 20:44:50 +08:00
|
|
|
}
|
|
|
|
CATCH_AND_HANDLE
|
2018-02-26 18:23:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// converts any CPU index that can be converted to GPU
|
2021-02-25 20:44:50 +08:00
|
|
|
int faiss_index_cpu_to_gpu(
|
2021-03-04 21:01:59 +08:00
|
|
|
FaissGpuResourcesProvider* provider,
|
2021-02-25 20:44:50 +08:00
|
|
|
int device,
|
|
|
|
const FaissIndex* index,
|
|
|
|
FaissGpuIndex** p_out) {
|
2018-02-26 18:23:17 +08:00
|
|
|
try {
|
2021-03-04 21:01:59 +08:00
|
|
|
auto res = reinterpret_cast<GpuResourcesProvider*>(provider);
|
2018-02-26 18:23:17 +08:00
|
|
|
auto gpu_index = faiss::gpu::index_cpu_to_gpu(
|
2021-02-25 20:44:50 +08:00
|
|
|
res, device, reinterpret_cast<const Index*>(index));
|
2018-02-26 18:23:17 +08:00
|
|
|
*p_out = reinterpret_cast<FaissGpuIndex*>(gpu_index);
|
2021-02-25 20:44:50 +08:00
|
|
|
}
|
|
|
|
CATCH_AND_HANDLE
|
2018-02-26 18:23:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int faiss_index_cpu_to_gpu_with_options(
|
2021-03-04 21:01:59 +08:00
|
|
|
FaissGpuResourcesProvider* provider,
|
2021-02-25 20:44:50 +08:00
|
|
|
int device,
|
|
|
|
const FaissIndex* index,
|
|
|
|
const FaissGpuClonerOptions* options,
|
|
|
|
FaissGpuIndex** p_out) {
|
2018-02-26 18:23:17 +08:00
|
|
|
try {
|
2021-03-04 21:01:59 +08:00
|
|
|
auto res = reinterpret_cast<GpuResourcesProvider*>(provider);
|
2018-02-26 18:23:17 +08:00
|
|
|
auto gpu_index = faiss::gpu::index_cpu_to_gpu(
|
2021-02-25 20:44:50 +08:00
|
|
|
res,
|
|
|
|
device,
|
|
|
|
reinterpret_cast<const Index*>(index),
|
|
|
|
reinterpret_cast<const GpuClonerOptions*>(options));
|
2018-02-26 18:23:17 +08:00
|
|
|
*p_out = reinterpret_cast<FaissGpuIndex*>(gpu_index);
|
2021-02-25 20:44:50 +08:00
|
|
|
}
|
|
|
|
CATCH_AND_HANDLE
|
2018-02-26 18:23:17 +08:00
|
|
|
}
|
2018-12-24 01:46:07 +08:00
|
|
|
|
|
|
|
int faiss_index_cpu_to_gpu_multiple(
|
2021-03-04 21:01:59 +08:00
|
|
|
FaissGpuResourcesProvider* const* providers_vec,
|
2021-02-25 20:44:50 +08:00
|
|
|
const int* devices,
|
|
|
|
size_t devices_size,
|
|
|
|
const FaissIndex* index,
|
|
|
|
FaissGpuIndex** p_out) {
|
2018-12-24 01:46:07 +08:00
|
|
|
try {
|
2021-03-04 21:01:59 +08:00
|
|
|
std::vector<GpuResourcesProvider*> res(devices_size);
|
2018-12-24 01:46:07 +08:00
|
|
|
for (auto i = 0u; i < devices_size; ++i) {
|
2021-03-04 21:01:59 +08:00
|
|
|
res[i] = reinterpret_cast<GpuResourcesProvider*>(providers_vec[i]);
|
2018-12-24 01:46:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<int> dev(devices, devices + devices_size);
|
|
|
|
|
|
|
|
auto gpu_index = faiss::gpu::index_cpu_to_gpu_multiple(
|
2021-02-25 20:44:50 +08:00
|
|
|
res, dev, reinterpret_cast<const Index*>(index));
|
2018-12-24 01:46:07 +08:00
|
|
|
*p_out = reinterpret_cast<FaissGpuIndex*>(gpu_index);
|
2021-02-25 20:44:50 +08:00
|
|
|
}
|
|
|
|
CATCH_AND_HANDLE
|
2018-12-24 01:46:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int faiss_index_cpu_to_gpu_multiple_with_options(
|
2021-03-04 21:01:59 +08:00
|
|
|
FaissGpuResourcesProvider** providers_vec,
|
|
|
|
size_t providers_vec_size,
|
2021-02-25 20:44:50 +08:00
|
|
|
int* devices,
|
|
|
|
size_t devices_size,
|
|
|
|
const FaissIndex* index,
|
|
|
|
const FaissGpuMultipleClonerOptions* options,
|
|
|
|
FaissGpuIndex** p_out) {
|
2018-12-24 01:46:07 +08:00
|
|
|
try {
|
2021-03-04 21:01:59 +08:00
|
|
|
std::vector<GpuResourcesProvider*> res(providers_vec_size);
|
|
|
|
for (auto i = 0u; i < providers_vec_size; ++i) {
|
|
|
|
res[i] = reinterpret_cast<GpuResourcesProvider*>(providers_vec[i]);
|
2018-12-24 01:46:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<int> dev(devices, devices + devices_size);
|
|
|
|
|
|
|
|
auto gpu_index = faiss::gpu::index_cpu_to_gpu_multiple(
|
2021-02-25 20:44:50 +08:00
|
|
|
res,
|
|
|
|
dev,
|
|
|
|
reinterpret_cast<const Index*>(index),
|
|
|
|
reinterpret_cast<const GpuMultipleClonerOptions*>(options));
|
2018-12-24 01:46:07 +08:00
|
|
|
*p_out = reinterpret_cast<FaissGpuIndex*>(gpu_index);
|
2021-02-25 20:44:50 +08:00
|
|
|
}
|
|
|
|
CATCH_AND_HANDLE
|
2018-12-24 01:46:07 +08:00
|
|
|
}
|