Faiss

- Main Page
- Namespaces
- Classes
- Files

- File List

All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
PerfClustering.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
10 #include "../../utils.h"
11 #include "../../Clustering.h"
12 #include "../GpuIndexFlat.h"
13 #include "../StandardGpuResources.h"
14 #include "IndexWrapper.h"
15 #include "../utils/DeviceUtils.h"
16 #include "../utils/Timer.h"
17 #include <gflags/gflags.h>
18 #include <memory>
19 #include <vector>
20
21 #include <cuda_profiler_api.h>
22
23 DEFINE_int32(num, 10000, "# of vecs");
24 DEFINE_int32(k, 100, "# of clusters");
25 DEFINE_int32(dim, 128, "# of dimensions");
26 DEFINE_int32(niter, 10, "# of iterations");
27 DEFINE_bool(L2_metric, true, "If true, use L2 metric. If false, use IP metric");
28 DEFINE_bool(use_float16, false, "use float16 vectors and math");
29 DEFINE_bool(transposed, false, "transposed vector storage");
30 DEFINE_bool(verbose, false, "turn on clustering logging");
31 DEFINE_int64(seed, -1, "specify random seed");
32 DEFINE_int32(num_gpus, 1, "number of gpus to use");
33 DEFINE_int64(min_paging_size, -1, "minimum size to use CPU -> GPU paged copies");
34 DEFINE_int64(pinned_mem, -1, "pinned memory allocation to use");
35 DEFINE_int32(max_points, -1, "max points per centroid");
36
37 using namespace faiss::gpu;
38
39 int main(int argc, char** argv) {
40 gflags::ParseCommandLineFlags(&argc, &argv, true);
41
42 cudaProfilerStop();
43
44 auto seed = FLAGS_seed != -1L ? FLAGS_seed : time(nullptr);
45 printf("using seed %ld\n", seed);
46
47 std::vector<float> vecs((size_t) FLAGS_num * FLAGS_dim);
48 faiss::float_rand(vecs.data(), vecs.size(), seed);
49
50 printf("K-means metric %s dim %d centroids %d num train %d niter %d\n",
51 FLAGS_L2_metric ? "L2" : "IP",
52 FLAGS_dim, FLAGS_k, FLAGS_num, FLAGS_niter);
53 printf("float16 math %s\n", FLAGS_use_float16 ? "enabled" : "disabled");
54 printf("transposed storage %s\n", FLAGS_transposed ? "enabled" : "disabled");
55 printf("verbose %s\n", FLAGS_verbose ? "enabled" : "disabled");
56
57 auto initFn = [](faiss::gpu::GpuResources* res, int dev) ->
58 std::unique_ptr<faiss::gpu::GpuIndexFlat> {
59 if (FLAGS_pinned_mem >= 0) {
60 ((faiss::gpu::StandardGpuResources*) res)->setPinnedMemory(
61 FLAGS_pinned_mem);
62 }
63
64 GpuIndexFlatConfig config;
65 config.device = dev;
66 config.useFloat16 = FLAGS_use_float16;
67 config.storeTransposed = FLAGS_transposed;
68
69 auto p = std::unique_ptr<faiss::gpu::GpuIndexFlat>(
70 FLAGS_L2_metric ?
71 (faiss::gpu::GpuIndexFlat*)
72 new faiss::gpu::GpuIndexFlatL2(res, FLAGS_dim, config) :
73 (faiss::gpu::GpuIndexFlat*)
74 new faiss::gpu::GpuIndexFlatIP(res, FLAGS_dim, config));
75
76 if (FLAGS_min_paging_size >= 0) {
77 p->setMinPagingSize(FLAGS_min_paging_size);
78 }
79 return p;
80 };
81
82 IndexWrapper<faiss::gpu::GpuIndexFlat> gpuIndex(FLAGS_num_gpus, initFn);
83
84 CUDA_VERIFY(cudaProfilerStart());
85 faiss::gpu::synchronizeAllDevices();
86
87 float gpuTime = 0.0f;
88
89 faiss::ClusteringParameters cp;
90 cp.niter = FLAGS_niter;
91 cp.verbose = FLAGS_verbose;
92
93 if (FLAGS_max_points > 0) {
94 cp.max_points_per_centroid = FLAGS_max_points;
95 }
96
97 faiss::Clustering kmeans(FLAGS_dim, FLAGS_k, cp);
98
99 // Time k-means
100 {
101 CpuTimer timer;
102
103 kmeans.train(FLAGS_num, vecs.data(), *(gpuIndex.getIndex()));
104
105 // There is a device -> host copy above, so no need to time
106 // additional synchronization with the GPU
107 gpuTime = timer.elapsedMilliseconds();
108 }
109
110 CUDA_VERIFY(cudaProfilerStop());
111 printf("k-means time %.3f ms\n", gpuTime);
112
113 CUDA_VERIFY(cudaDeviceSynchronize());
114
115 return 0;
116 }
faiss::gpu::CpuTimer::elapsedMilliseconds
float elapsedMilliseconds()
Returns elapsed time in milliseconds.
Definition: Timer.cpp:51
faiss::ClusteringParameters::niter
int niter
clustering iterations
Definition: Clustering.h:24
faiss::gpu::GpuIndexFlatConfig::storeTransposed
bool storeTransposed
Definition: GpuIndexFlat.h:47
faiss::gpu::GpuResources
Definition: GpuResources.h:22
faiss::gpu::CpuTimer
CPU wallclock elapsed timer.
Definition: Timer.h:41
faiss::gpu::GpuIndexFlatConfig::useFloat16
bool useFloat16
Whether or not data is stored as float16.
Definition: GpuIndexFlat.h:34
faiss::gpu::GpuIndexConfig::device
int device
GPU device on which the index is resident.
Definition: GpuIndex.h:26
faiss::gpu::GpuIndexFlatConfig
Definition: GpuIndexFlat.h:26
faiss::Clustering
Definition: Clustering.h:56
faiss::gpu::StandardGpuResources
Definition: StandardGpuResources.h:22
faiss::gpu::GpuIndexFlatIP
Definition: GpuIndexFlat.h:194
faiss::gpu::GpuIndexFlatL2
Definition: GpuIndexFlat.h:169
faiss::ClusteringParameters
Definition: Clustering.h:23
faiss::gpu::GpuIndexFlat
Definition: GpuIndexFlat.h:53
faiss::ClusteringParameters::max_points_per_centroid
int max_points_per_centroid
to limit size of dataset
Definition: Clustering.h:33
faiss::gpu::IndexWrapper
Definition: IndexWrapper.h:24