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(verbose, false, "turn on clustering logging");
32 DEFINE_int64(seed, -1, "specify random seed");
33 DEFINE_int32(num_gpus, 1, "number of gpus to use");
34 DEFINE_int64(min_paging_size, -1, "minimum size to use CPU -> GPU paged copies");
35 DEFINE_int64(pinned_mem, -1, "pinned memory allocation to use");
36 DEFINE_int32(max_points, -1, "max points per centroid");
37 
38 using namespace faiss::gpu;
39 
40 int main(int argc, char** argv) {
41  google::ParseCommandLineFlags(&argc, &argv, true);
42 
43  cudaProfilerStop();
44 
45  auto seed = FLAGS_seed != -1L ? FLAGS_seed : time(nullptr);
46  printf("using seed %ld\n", seed);
47 
48  std::vector<float> vecs((size_t) FLAGS_num * FLAGS_dim);
49  faiss::float_rand(vecs.data(), vecs.size(), seed);
50 
51  printf("K-means metric %s dim %d centroids %d num train %d niter %d\n",
52  FLAGS_L2_metric ? "L2" : "IP",
53  FLAGS_dim, FLAGS_k, FLAGS_num, FLAGS_niter);
54  printf("float16 math %s\n", FLAGS_use_float16 ? "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  auto p = std::unique_ptr<faiss::gpu::GpuIndexFlat>(
65  FLAGS_L2_metric ?
67  new faiss::gpu::GpuIndexFlatL2(res, dev, FLAGS_dim, FLAGS_use_float16) :
68  (faiss::gpu::GpuIndexFlat*)
69  new faiss::gpu::GpuIndexFlatIP(res, dev, FLAGS_dim, FLAGS_use_float16));
70 
71  if (FLAGS_min_paging_size >= 0) {
72  p->setMinPagingSize(FLAGS_min_paging_size);
73  }
74  return p;
75  };
76 
77  IndexWrapper<faiss::gpu::GpuIndexFlat> gpuIndex(FLAGS_num_gpus, initFn);
78 
79  CUDA_VERIFY(cudaProfilerStart());
80  faiss::gpu::synchronizeAllDevices();
81 
82  float gpuTime = 0.0f;
83 
85  cp.niter = FLAGS_niter;
86  cp.verbose = FLAGS_verbose;
87 
88  if (FLAGS_max_points > 0) {
89  cp.max_points_per_centroid = FLAGS_max_points;
90  }
91 
92  faiss::Clustering kmeans(FLAGS_dim, FLAGS_k, cp);
93 
94  // Time k-means
95  {
96  CpuTimer timer;
97 
98  kmeans.train(FLAGS_num, vecs.data(), *(gpuIndex.getIndex()));
99 
100  // There is a device -> host copy above, so no need to time
101  // additional synchronization with the GPU
102  gpuTime = timer.elapsedMilliseconds();
103  }
104 
105  CUDA_VERIFY(cudaProfilerStop());
106  printf("k-means time %.3f ms\n", gpuTime);
107 
108  CUDA_VERIFY(cudaDeviceSynchronize());
109 
110  return 0;
111 }
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:35