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