blob: b2cd97c12eb0c82b9d9e81c68b249f7c00192923 [file] [log] [blame]
Clement Courbet96715412018-05-07 09:09:48 +00001//===-- Clustering.cpp ------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "Clustering.h"
Clement Courbet176388c2019-01-02 09:21:00 +000011#include "llvm/ADT/SetVector.h"
Roman Lebedev8aecb0c2018-11-19 13:28:22 +000012#include "llvm/ADT/SmallVector.h"
Clement Courbet96715412018-05-07 09:09:48 +000013#include <string>
Clement Courbet96715412018-05-07 09:09:48 +000014
Fangrui Song32401af2018-10-22 17:10:47 +000015namespace llvm {
Clement Courbet96715412018-05-07 09:09:48 +000016namespace exegesis {
17
18// The clustering problem has the following characteristics:
19// (A) - Low dimension (dimensions are typically proc resource units,
20// typically < 10).
21// (B) - Number of points : ~thousands (points are measurements of an MCInst)
22// (C) - Number of clusters: ~tens.
23// (D) - The number of clusters is not known /a priory/.
Clement Courbetdffc4ca2018-05-14 11:35:37 +000024// (E) - The amount of noise is relatively small.
Clement Courbet96715412018-05-07 09:09:48 +000025// The problem is rather small. In terms of algorithms, (D) disqualifies
26// k-means and makes algorithms such as DBSCAN[1] or OPTICS[2] more applicable.
27//
28// We've used DBSCAN here because it's simple to implement. This is a pretty
29// straightforward and inefficient implementation of the pseudocode in [2].
30//
31// [1] https://en.wikipedia.org/wiki/DBSCAN
32// [2] https://en.wikipedia.org/wiki/OPTICS_algorithm
33
Clement Courbet96715412018-05-07 09:09:48 +000034// Finds the points at distance less than sqrt(EpsilonSquared) of Q (not
35// including Q).
Roman Lebedev666d8552018-11-19 13:28:31 +000036void InstructionBenchmarkClustering::rangeQuery(
Roman Lebedev71fdb572018-11-19 13:28:41 +000037 const size_t Q, std::vector<size_t> &Neighbors) const {
Roman Lebedev666d8552018-11-19 13:28:31 +000038 Neighbors.clear();
Roman Lebedev71fdb572018-11-19 13:28:41 +000039 Neighbors.reserve(Points_.size() - 1); // The Q itself isn't a neighbor.
Clement Courbet72287212018-06-04 11:11:55 +000040 const auto &QMeasurements = Points_[Q].Measurements;
41 for (size_t P = 0, NumPoints = Points_.size(); P < NumPoints; ++P) {
Clement Courbet96715412018-05-07 09:09:48 +000042 if (P == Q)
43 continue;
Clement Courbet72287212018-06-04 11:11:55 +000044 const auto &PMeasurements = Points_[P].Measurements;
Clement Courbet96715412018-05-07 09:09:48 +000045 if (PMeasurements.empty()) // Error point.
46 continue;
Clement Courbet72287212018-06-04 11:11:55 +000047 if (isNeighbour(PMeasurements, QMeasurements)) {
Clement Courbet96715412018-05-07 09:09:48 +000048 Neighbors.push_back(P);
49 }
50 }
Clement Courbet96715412018-05-07 09:09:48 +000051}
52
Clement Courbet37f0ca02018-05-15 12:08:00 +000053InstructionBenchmarkClustering::InstructionBenchmarkClustering(
Clement Courbet72287212018-06-04 11:11:55 +000054 const std::vector<InstructionBenchmark> &Points,
55 const double EpsilonSquared)
56 : Points_(Points), EpsilonSquared_(EpsilonSquared),
57 NoiseCluster_(ClusterId::noise()), ErrorCluster_(ClusterId::error()) {}
Clement Courbet96715412018-05-07 09:09:48 +000058
Clement Courbet37f0ca02018-05-15 12:08:00 +000059llvm::Error InstructionBenchmarkClustering::validateAndSetup() {
60 ClusterIdForPoint_.resize(Points_.size());
Clement Courbet96715412018-05-07 09:09:48 +000061 // Mark erroneous measurements out.
62 // All points must have the same number of dimensions, in the same order.
63 const std::vector<BenchmarkMeasure> *LastMeasurement = nullptr;
Clement Courbet37f0ca02018-05-15 12:08:00 +000064 for (size_t P = 0, NumPoints = Points_.size(); P < NumPoints; ++P) {
65 const auto &Point = Points_[P];
Clement Courbet96715412018-05-07 09:09:48 +000066 if (!Point.Error.empty()) {
67 ClusterIdForPoint_[P] = ClusterId::error();
68 ErrorCluster_.PointIndices.push_back(P);
69 continue;
70 }
71 const auto *CurMeasurement = &Point.Measurements;
72 if (LastMeasurement) {
73 if (LastMeasurement->size() != CurMeasurement->size()) {
74 return llvm::make_error<llvm::StringError>(
75 "inconsistent measurement dimensions",
76 llvm::inconvertibleErrorCode());
77 }
78 for (size_t I = 0, E = LastMeasurement->size(); I < E; ++I) {
79 if (LastMeasurement->at(I).Key != CurMeasurement->at(I).Key) {
80 return llvm::make_error<llvm::StringError>(
81 "inconsistent measurement dimensions keys",
82 llvm::inconvertibleErrorCode());
83 }
84 }
85 }
86 LastMeasurement = CurMeasurement;
87 }
88 if (LastMeasurement) {
89 NumDimensions_ = LastMeasurement->size();
90 }
91 return llvm::Error::success();
92}
93
Clement Courbet72287212018-06-04 11:11:55 +000094void InstructionBenchmarkClustering::dbScan(const size_t MinPts) {
Clement Courbet176388c2019-01-02 09:21:00 +000095 std::vector<size_t> Neighbors; // Persistent buffer to avoid allocs.
96 for (size_t P = 0, NumPoints = Points_.size(); P < NumPoints; ++P) {
Clement Courbet96715412018-05-07 09:09:48 +000097 if (!ClusterIdForPoint_[P].isUndef())
98 continue; // Previously processed in inner loop.
Roman Lebedev666d8552018-11-19 13:28:31 +000099 rangeQuery(P, Neighbors);
Clement Courbet96715412018-05-07 09:09:48 +0000100 if (Neighbors.size() + 1 < MinPts) { // Density check.
101 // The region around P is not dense enough to create a new cluster, mark
102 // as noise for now.
103 ClusterIdForPoint_[P] = ClusterId::noise();
104 continue;
105 }
106
107 // Create a new cluster, add P.
108 Clusters_.emplace_back(ClusterId::makeValid(Clusters_.size()));
109 Cluster &CurrentCluster = Clusters_.back();
110 ClusterIdForPoint_[P] = CurrentCluster.Id; /* Label initial point */
111 CurrentCluster.PointIndices.push_back(P);
112
Clement Courbet176388c2019-01-02 09:21:00 +0000113 // Process P's neighbors.
114 llvm::SetVector<size_t, std::deque<size_t>> ToProcess;
115 ToProcess.insert(Neighbors.begin(), Neighbors.end());
116 while (!ToProcess.empty()) {
117 // Retrieve a point from the set.
118 const size_t Q = *ToProcess.begin();
119 ToProcess.erase(ToProcess.begin());
Clement Courbet96715412018-05-07 09:09:48 +0000120
Clement Courbet176388c2019-01-02 09:21:00 +0000121 if (ClusterIdForPoint_[Q].isNoise()) {
122 // Change noise point to border point.
123 ClusterIdForPoint_[Q] = CurrentCluster.Id;
124 CurrentCluster.PointIndices.push_back(Q);
Clement Courbet96715412018-05-07 09:09:48 +0000125 continue;
Clement Courbet176388c2019-01-02 09:21:00 +0000126 }
127 if (!ClusterIdForPoint_[Q].isUndef()) {
128 continue; // Previously processed.
129 }
130 // Add Q to the current custer.
131 ClusterIdForPoint_[Q] = CurrentCluster.Id;
132 CurrentCluster.PointIndices.push_back(Q);
133 // And extend to the neighbors of Q if the region is dense enough.
134 rangeQuery(Q, Neighbors);
135 if (Neighbors.size() + 1 >= MinPts) {
136 ToProcess.insert(Neighbors.begin(), Neighbors.end());
137 }
Clement Courbet96715412018-05-07 09:09:48 +0000138 }
139 }
Clement Courbet176388c2019-01-02 09:21:00 +0000140 // assert(Neighbors.capacity() == (Points_.size() - 1));
141 // ^ True, but it is not quaranteed to be true in all the cases.
Clement Courbet96715412018-05-07 09:09:48 +0000142
143 // Add noisy points to noise cluster.
Clement Courbet176388c2019-01-02 09:21:00 +0000144 for (size_t P = 0, NumPoints = Points_.size(); P < NumPoints; ++P) {
145 if (ClusterIdForPoint_[P].isNoise()) {
Clement Courbet96715412018-05-07 09:09:48 +0000146 NoiseCluster_.PointIndices.push_back(P);
Clement Courbet176388c2019-01-02 09:21:00 +0000147 }
148 }
Clement Courbet96715412018-05-07 09:09:48 +0000149}
150
151llvm::Expected<InstructionBenchmarkClustering>
152InstructionBenchmarkClustering::create(
153 const std::vector<InstructionBenchmark> &Points, const size_t MinPts,
154 const double Epsilon) {
Clement Courbet72287212018-06-04 11:11:55 +0000155 InstructionBenchmarkClustering Clustering(Points, Epsilon * Epsilon);
Clement Courbet37f0ca02018-05-15 12:08:00 +0000156 if (auto Error = Clustering.validateAndSetup()) {
Clement Courbetcdb0eb82018-05-15 12:38:06 +0000157 return std::move(Error);
Clement Courbet96715412018-05-07 09:09:48 +0000158 }
159 if (Clustering.ErrorCluster_.PointIndices.size() == Points.size()) {
160 return Clustering; // Nothing to cluster.
161 }
162
Clement Courbet72287212018-06-04 11:11:55 +0000163 Clustering.dbScan(MinPts);
Clement Courbet96715412018-05-07 09:09:48 +0000164 return Clustering;
165}
166
167} // namespace exegesis
Fangrui Song32401af2018-10-22 17:10:47 +0000168} // namespace llvm