Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 1 | //===-- 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" |
| 11 | #include <string> |
| 12 | #include <unordered_set> |
| 13 | |
| 14 | namespace exegesis { |
| 15 | |
| 16 | // The clustering problem has the following characteristics: |
| 17 | // (A) - Low dimension (dimensions are typically proc resource units, |
| 18 | // typically < 10). |
| 19 | // (B) - Number of points : ~thousands (points are measurements of an MCInst) |
| 20 | // (C) - Number of clusters: ~tens. |
| 21 | // (D) - The number of clusters is not known /a priory/. |
Clement Courbet | dffc4ca | 2018-05-14 11:35:37 +0000 | [diff] [blame^] | 22 | // (E) - The amount of noise is relatively small. |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 23 | // The problem is rather small. In terms of algorithms, (D) disqualifies |
| 24 | // k-means and makes algorithms such as DBSCAN[1] or OPTICS[2] more applicable. |
| 25 | // |
| 26 | // We've used DBSCAN here because it's simple to implement. This is a pretty |
| 27 | // straightforward and inefficient implementation of the pseudocode in [2]. |
| 28 | // |
| 29 | // [1] https://en.wikipedia.org/wiki/DBSCAN |
| 30 | // [2] https://en.wikipedia.org/wiki/OPTICS_algorithm |
| 31 | |
| 32 | namespace { |
| 33 | |
| 34 | // Finds the points at distance less than sqrt(EpsilonSquared) of Q (not |
| 35 | // including Q). |
| 36 | std::vector<size_t> rangeQuery(const std::vector<InstructionBenchmark> &Points, |
| 37 | const size_t Q, const double EpsilonSquared) { |
| 38 | std::vector<size_t> Neighbors; |
| 39 | const auto &QMeasurements = Points[Q].Measurements; |
| 40 | for (size_t P = 0, NumPoints = Points.size(); P < NumPoints; ++P) { |
| 41 | if (P == Q) |
| 42 | continue; |
| 43 | const auto &PMeasurements = Points[P].Measurements; |
| 44 | if (PMeasurements.empty()) // Error point. |
| 45 | continue; |
| 46 | double DistanceSquared = 0; |
| 47 | for (size_t I = 0, E = QMeasurements.size(); I < E; ++I) { |
| 48 | const auto Diff = PMeasurements[I].Value - QMeasurements[I].Value; |
| 49 | DistanceSquared += Diff * Diff; |
| 50 | } |
| 51 | if (DistanceSquared <= EpsilonSquared) { |
| 52 | Neighbors.push_back(P); |
| 53 | } |
| 54 | } |
| 55 | return Neighbors; |
| 56 | } |
| 57 | |
| 58 | } // namespace |
| 59 | |
Clement Courbet | dffc4ca | 2018-05-14 11:35:37 +0000 | [diff] [blame^] | 60 | InstructionBenchmarkClustering::InstructionBenchmarkClustering() |
| 61 | : NoiseCluster_(ClusterId::noise()), ErrorCluster_(ClusterId::error()) {} |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 62 | |
Clement Courbet | dffc4ca | 2018-05-14 11:35:37 +0000 | [diff] [blame^] | 63 | llvm::Error InstructionBenchmarkClustering::validateAndSetup( |
| 64 | const std::vector<InstructionBenchmark> &Points) { |
| 65 | ClusterIdForPoint_.resize(Points.size()); |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 66 | // Mark erroneous measurements out. |
| 67 | // All points must have the same number of dimensions, in the same order. |
| 68 | const std::vector<BenchmarkMeasure> *LastMeasurement = nullptr; |
Clement Courbet | dffc4ca | 2018-05-14 11:35:37 +0000 | [diff] [blame^] | 69 | for (size_t P = 0, NumPoints = Points.size(); P < NumPoints; ++P) { |
| 70 | const auto &Point = Points[P]; |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 71 | if (!Point.Error.empty()) { |
| 72 | ClusterIdForPoint_[P] = ClusterId::error(); |
| 73 | ErrorCluster_.PointIndices.push_back(P); |
| 74 | continue; |
| 75 | } |
| 76 | const auto *CurMeasurement = &Point.Measurements; |
| 77 | if (LastMeasurement) { |
| 78 | if (LastMeasurement->size() != CurMeasurement->size()) { |
| 79 | return llvm::make_error<llvm::StringError>( |
| 80 | "inconsistent measurement dimensions", |
| 81 | llvm::inconvertibleErrorCode()); |
| 82 | } |
| 83 | for (size_t I = 0, E = LastMeasurement->size(); I < E; ++I) { |
| 84 | if (LastMeasurement->at(I).Key != CurMeasurement->at(I).Key) { |
| 85 | return llvm::make_error<llvm::StringError>( |
| 86 | "inconsistent measurement dimensions keys", |
| 87 | llvm::inconvertibleErrorCode()); |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | LastMeasurement = CurMeasurement; |
| 92 | } |
| 93 | if (LastMeasurement) { |
| 94 | NumDimensions_ = LastMeasurement->size(); |
| 95 | } |
| 96 | return llvm::Error::success(); |
| 97 | } |
| 98 | |
Clement Courbet | dffc4ca | 2018-05-14 11:35:37 +0000 | [diff] [blame^] | 99 | void InstructionBenchmarkClustering::dbScan( |
| 100 | const std::vector<InstructionBenchmark> &Points, const size_t MinPts, |
| 101 | const double EpsilonSquared) { |
| 102 | for (size_t P = 0, NumPoints = Points.size(); P < NumPoints; ++P) { |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 103 | if (!ClusterIdForPoint_[P].isUndef()) |
| 104 | continue; // Previously processed in inner loop. |
Clement Courbet | dffc4ca | 2018-05-14 11:35:37 +0000 | [diff] [blame^] | 105 | const auto Neighbors = rangeQuery(Points, P, EpsilonSquared); |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 106 | if (Neighbors.size() + 1 < MinPts) { // Density check. |
| 107 | // The region around P is not dense enough to create a new cluster, mark |
| 108 | // as noise for now. |
| 109 | ClusterIdForPoint_[P] = ClusterId::noise(); |
| 110 | continue; |
| 111 | } |
| 112 | |
| 113 | // Create a new cluster, add P. |
| 114 | Clusters_.emplace_back(ClusterId::makeValid(Clusters_.size())); |
| 115 | Cluster &CurrentCluster = Clusters_.back(); |
| 116 | ClusterIdForPoint_[P] = CurrentCluster.Id; /* Label initial point */ |
| 117 | CurrentCluster.PointIndices.push_back(P); |
| 118 | |
| 119 | // Process P's neighbors. |
| 120 | std::unordered_set<size_t> ToProcess(Neighbors.begin(), Neighbors.end()); |
| 121 | while (!ToProcess.empty()) { |
| 122 | // Retrieve a point from the set. |
| 123 | const size_t Q = *ToProcess.begin(); |
| 124 | ToProcess.erase(Q); |
| 125 | |
| 126 | if (ClusterIdForPoint_[Q].isNoise()) { |
| 127 | // Change noise point to border point. |
| 128 | ClusterIdForPoint_[Q] = CurrentCluster.Id; |
| 129 | CurrentCluster.PointIndices.push_back(Q); |
| 130 | continue; |
| 131 | } |
| 132 | if (!ClusterIdForPoint_[Q].isUndef()) { |
| 133 | continue; // Previously processed. |
| 134 | } |
| 135 | // Add Q to the current custer. |
| 136 | ClusterIdForPoint_[Q] = CurrentCluster.Id; |
| 137 | CurrentCluster.PointIndices.push_back(Q); |
| 138 | // And extend to the neighbors of Q if the region is dense enough. |
Clement Courbet | dffc4ca | 2018-05-14 11:35:37 +0000 | [diff] [blame^] | 139 | const auto Neighbors = rangeQuery(Points, Q, EpsilonSquared); |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 140 | if (Neighbors.size() + 1 >= MinPts) { |
| 141 | ToProcess.insert(Neighbors.begin(), Neighbors.end()); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | // Add noisy points to noise cluster. |
Clement Courbet | dffc4ca | 2018-05-14 11:35:37 +0000 | [diff] [blame^] | 147 | for (size_t P = 0, NumPoints = Points.size(); P < NumPoints; ++P) { |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 148 | if (ClusterIdForPoint_[P].isNoise()) { |
| 149 | NoiseCluster_.PointIndices.push_back(P); |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | llvm::Expected<InstructionBenchmarkClustering> |
| 155 | InstructionBenchmarkClustering::create( |
| 156 | const std::vector<InstructionBenchmark> &Points, const size_t MinPts, |
| 157 | const double Epsilon) { |
Clement Courbet | dffc4ca | 2018-05-14 11:35:37 +0000 | [diff] [blame^] | 158 | InstructionBenchmarkClustering Clustering; |
| 159 | if (auto Error = Clustering.validateAndSetup(Points)) { |
| 160 | return std::move(Error); |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 161 | } |
| 162 | if (Clustering.ErrorCluster_.PointIndices.size() == Points.size()) { |
| 163 | return Clustering; // Nothing to cluster. |
| 164 | } |
| 165 | |
Clement Courbet | dffc4ca | 2018-05-14 11:35:37 +0000 | [diff] [blame^] | 166 | Clustering.dbScan(Points, MinPts, Epsilon * Epsilon); |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 167 | return Clustering; |
| 168 | } |
| 169 | |
| 170 | } // namespace exegesis |