Faiss

- Main Page
- Namespaces
- Classes
- Files

- File List

All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
5-Multiple-GPUs.cpp
1 /**
2 * Copyright (c) 2015-present, Facebook, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD+Patents license found in the
6 * LICENSE file in the root directory of this source tree.
7 */
8
9 #include <cstdio>
10 #include <cstdlib>
11
12 #include <faiss/IndexFlat.h>
13 #include <faiss/gpu/GpuAutoTune.h>
14 #include <faiss/gpu/GpuIndexFlat.h>
15 #include <faiss/gpu/StandardGpuResources.h>
16 #include <faiss/gpu/utils/DeviceUtils.h>
17
18
19 int main() {
20 int d = 64; // dimension
21 int nb = 100000; // database size
22 int nq = 10000; // nb of queries
23
24 float *xb = new float[d * nb];
25 float *xq = new float[d * nq];
26
27 for(int i = 0; i < nb; i++) {
28 for(int j = 0; j < d; j++)
29 xb[d * i + j] = drand48();
30 xb[d * i] += i / 1000.;
31 }
32
33 for(int i = 0; i < nq; i++) {
34 for(int j = 0; j < d; j++)
35 xq[d * i + j] = drand48();
36 xq[d * i] += i / 1000.;
37 }
38
39 int ngpus = faiss::gpu::getNumDevices();
40
41 printf("Number of GPUs: %d\n", ngpus);
42
43 std::vector<faiss::gpu::GpuResources*> res;
44 std::vector<int> devs;
45 for(int i = 0; i < ngpus; i++) {
46 res.push_back(new faiss::gpu::StandardGpuResources);
47 devs.push_back(i);
48 }
49
50 faiss::IndexFlatL2 cpu_index(d);
51
52 faiss::Index *gpu_index =
53 faiss::gpu::index_cpu_to_gpu_multiple(
54 res,
55 devs,
56 &cpu_index
57 );
58
59 printf("is_trained = %s\n", gpu_index->is_trained ? "true" : "false");
60 gpu_index->add(nb, xb); // add vectors to the index
61 printf("ntotal = %ld\n", gpu_index->ntotal);
62
63 int k = 4;
64
65 { // search xq
66 long *I = new long[k * nq];
67 float *D = new float[k * nq];
68
69 gpu_index->search(nq, xq, k, D, I);
70
71 // print results
72 printf("I (5 first results)=\n");
73 for(int i = 0; i < 5; i++) {
74 for(int j = 0; j < k; j++)
75 printf("%5ld ", I[i * k + j]);
76 printf("\n");
77 }
78
79 printf("I (5 last results)=\n");
80 for(int i = nq - 5; i < nq; i++) {
81 for(int j = 0; j < k; j++)
82 printf("%5ld ", I[i * k + j]);
83 printf("\n");
84 }
85
86 delete [] I;
87 delete [] D;
88 }
89
90 delete gpu_index;
91
92 for(int i = 0; i < ngpus; i++) {
93 delete res[i];
94 }
95
96 delete [] xb;
97 delete [] xq;
98
99 return 0;
100 }
faiss::IndexFlatL2
Definition: IndexFlat.h:79
faiss::Index::add
virtual void add(idx_t n, const float *x)=0
faiss::gpu::StandardGpuResources
Definition: StandardGpuResources.h:23
faiss::Index::ntotal
idx_t ntotal
total nb of indexed vectors
Definition: Index.h:65
faiss::Index::search
virtual void search(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels) const =0
faiss::Index
Definition: Index.h:60
faiss::Index::is_trained
bool is_trained
set if the Index does not require training, or if training is done already
Definition: Index.h:69