File: GpuAutoTune_c.cpp

package info (click to toggle)
faiss 1.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,572 kB
  • sloc: cpp: 85,627; python: 27,889; sh: 905; ansic: 425; makefile: 41
file content (112 lines) | stat: -rw-r--r-- 3,692 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

// -*- c++ -*-

#include "GpuAutoTune_c.h"
#include <faiss/Index.h>
#include <faiss/gpu/GpuAutoTune.h>
#include <faiss/gpu/GpuCloner.h>
#include <faiss/gpu/GpuClonerOptions.h>
#include <faiss/gpu/GpuResources.h>
#include <vector>
#include "GpuClonerOptions_c.h"
#include "macros_impl.h"

using faiss::Index;
using faiss::gpu::GpuClonerOptions;
using faiss::gpu::GpuMultipleClonerOptions;
using faiss::gpu::GpuResourcesProvider;

int faiss_index_gpu_to_cpu(const FaissIndex* gpu_index, FaissIndex** p_out) {
    try {
        auto cpu_index = faiss::gpu::index_gpu_to_cpu(
                reinterpret_cast<const Index*>(gpu_index));
        *p_out = reinterpret_cast<FaissIndex*>(cpu_index);
    }
    CATCH_AND_HANDLE
}

/// converts any CPU index that can be converted to GPU
int faiss_index_cpu_to_gpu(
        FaissGpuResourcesProvider* provider,
        int device,
        const FaissIndex* index,
        FaissGpuIndex** p_out) {
    try {
        auto res = reinterpret_cast<GpuResourcesProvider*>(provider);
        auto gpu_index = faiss::gpu::index_cpu_to_gpu(
                res, device, reinterpret_cast<const Index*>(index));
        *p_out = reinterpret_cast<FaissGpuIndex*>(gpu_index);
    }
    CATCH_AND_HANDLE
}

int faiss_index_cpu_to_gpu_with_options(
        FaissGpuResourcesProvider* provider,
        int device,
        const FaissIndex* index,
        const FaissGpuClonerOptions* options,
        FaissGpuIndex** p_out) {
    try {
        auto res = reinterpret_cast<GpuResourcesProvider*>(provider);
        auto gpu_index = faiss::gpu::index_cpu_to_gpu(
                res,
                device,
                reinterpret_cast<const Index*>(index),
                reinterpret_cast<const GpuClonerOptions*>(options));
        *p_out = reinterpret_cast<FaissGpuIndex*>(gpu_index);
    }
    CATCH_AND_HANDLE
}

int faiss_index_cpu_to_gpu_multiple(
        FaissGpuResourcesProvider* const* providers_vec,
        const int* devices,
        size_t devices_size,
        const FaissIndex* index,
        FaissGpuIndex** p_out) {
    try {
        std::vector<GpuResourcesProvider*> res(devices_size);
        for (auto i = 0u; i < devices_size; ++i) {
            res[i] = reinterpret_cast<GpuResourcesProvider*>(providers_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(
        FaissGpuResourcesProvider* const* providers_vec,
        size_t providers_vec_size,
        const int* devices,
        size_t devices_size,
        const FaissIndex* index,
        const FaissGpuMultipleClonerOptions* options,
        FaissGpuIndex** p_out) {
    try {
        std::vector<GpuResourcesProvider*> res(providers_vec_size);
        for (auto i = 0u; i < providers_vec_size; ++i) {
            res[i] = reinterpret_cast<GpuResourcesProvider*>(providers_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
}