2018-02-20 22:53:56 +08:00
|
|
|
/**
|
2019-05-28 22:17:22 +08:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2018-02-20 22:53:56 +08:00
|
|
|
*
|
2019-05-28 22:17:22 +08:00
|
|
|
* This source code is licensed under the MIT license found in the
|
2018-02-20 22:53:56 +08:00
|
|
|
* LICENSE file in the root directory of this source tree.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
2020-08-24 15:41:13 +08:00
|
|
|
#include <random>
|
2018-02-20 22:53:56 +08:00
|
|
|
|
|
|
|
#include <faiss/IndexFlat.h>
|
|
|
|
#include <faiss/gpu/GpuAutoTune.h>
|
2020-01-16 23:46:11 +08:00
|
|
|
#include <faiss/gpu/GpuCloner.h>
|
2018-02-20 22:53:56 +08:00
|
|
|
#include <faiss/gpu/GpuIndexFlat.h>
|
|
|
|
#include <faiss/gpu/StandardGpuResources.h>
|
|
|
|
#include <faiss/gpu/utils/DeviceUtils.h>
|
|
|
|
|
|
|
|
int main() {
|
2021-02-25 20:44:50 +08:00
|
|
|
int d = 64; // dimension
|
|
|
|
int nb = 100000; // database size
|
|
|
|
int nq = 10000; // nb of queries
|
2018-02-20 22:53:56 +08:00
|
|
|
|
2020-08-24 15:41:13 +08:00
|
|
|
std::mt19937 rng;
|
|
|
|
std::uniform_real_distribution<> distrib;
|
|
|
|
|
2021-02-25 20:44:50 +08:00
|
|
|
float* xb = new float[d * nb];
|
|
|
|
float* xq = new float[d * nq];
|
2018-02-20 22:53:56 +08:00
|
|
|
|
2021-02-25 20:44:50 +08:00
|
|
|
for (int i = 0; i < nb; i++) {
|
|
|
|
for (int j = 0; j < d; j++)
|
2020-08-24 15:41:13 +08:00
|
|
|
xb[d * i + j] = distrib(rng);
|
2018-02-20 22:53:56 +08:00
|
|
|
xb[d * i] += i / 1000.;
|
|
|
|
}
|
|
|
|
|
2021-02-25 20:44:50 +08:00
|
|
|
for (int i = 0; i < nq; i++) {
|
|
|
|
for (int j = 0; j < d; j++)
|
2020-08-24 15:41:13 +08:00
|
|
|
xq[d * i + j] = distrib(rng);
|
2018-02-20 22:53:56 +08:00
|
|
|
xq[d * i] += i / 1000.;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ngpus = faiss::gpu::getNumDevices();
|
|
|
|
|
2018-02-21 22:36:29 +08:00
|
|
|
printf("Number of GPUs: %d\n", ngpus);
|
|
|
|
|
2020-09-17 22:31:13 +08:00
|
|
|
std::vector<faiss::gpu::GpuResourcesProvider*> res;
|
2018-02-20 22:53:56 +08:00
|
|
|
std::vector<int> devs;
|
2021-02-25 20:44:50 +08:00
|
|
|
for (int i = 0; i < ngpus; i++) {
|
2018-02-20 22:53:56 +08:00
|
|
|
res.push_back(new faiss::gpu::StandardGpuResources);
|
|
|
|
devs.push_back(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
faiss::IndexFlatL2 cpu_index(d);
|
|
|
|
|
2021-02-25 20:44:50 +08:00
|
|
|
faiss::Index* gpu_index =
|
|
|
|
faiss::gpu::index_cpu_to_gpu_multiple(res, devs, &cpu_index);
|
2018-02-20 22:53:56 +08:00
|
|
|
|
|
|
|
printf("is_trained = %s\n", gpu_index->is_trained ? "true" : "false");
|
2021-02-25 20:44:50 +08:00
|
|
|
gpu_index->add(nb, xb); // add vectors to the index
|
2018-02-20 22:53:56 +08:00
|
|
|
printf("ntotal = %ld\n", gpu_index->ntotal);
|
|
|
|
|
|
|
|
int k = 4;
|
|
|
|
|
2021-02-25 20:44:50 +08:00
|
|
|
{ // search xq
|
|
|
|
long* I = new long[k * nq];
|
|
|
|
float* D = new float[k * nq];
|
2018-02-20 22:53:56 +08:00
|
|
|
|
|
|
|
gpu_index->search(nq, xq, k, D, I);
|
|
|
|
|
|
|
|
// print results
|
|
|
|
printf("I (5 first results)=\n");
|
2021-02-25 20:44:50 +08:00
|
|
|
for (int i = 0; i < 5; i++) {
|
|
|
|
for (int j = 0; j < k; j++)
|
2018-02-20 22:53:56 +08:00
|
|
|
printf("%5ld ", I[i * k + j]);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("I (5 last results)=\n");
|
2021-02-25 20:44:50 +08:00
|
|
|
for (int i = nq - 5; i < nq; i++) {
|
|
|
|
for (int j = 0; j < k; j++)
|
2018-02-20 22:53:56 +08:00
|
|
|
printf("%5ld ", I[i * k + j]);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2021-02-25 20:44:50 +08:00
|
|
|
delete[] I;
|
|
|
|
delete[] D;
|
2018-02-20 22:53:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
delete gpu_index;
|
|
|
|
|
2021-02-25 20:44:50 +08:00
|
|
|
for (int i = 0; i < ngpus; i++) {
|
2018-02-20 22:53:56 +08:00
|
|
|
delete res[i];
|
|
|
|
}
|
|
|
|
|
2021-02-25 20:44:50 +08:00
|
|
|
delete[] xb;
|
|
|
|
delete[] xq;
|
2018-02-20 22:53:56 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|