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 | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 60 | InstructionBenchmarkClustering::InstructionBenchmarkClustering( |
| 61 | const std::vector<InstructionBenchmark> &Points) |
| 62 | : Points_(Points), NoiseCluster_(ClusterId::noise()), |
| 63 | ErrorCluster_(ClusterId::error()) {} |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 64 | |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 65 | llvm::Error InstructionBenchmarkClustering::validateAndSetup() { |
| 66 | ClusterIdForPoint_.resize(Points_.size()); |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 67 | // Mark erroneous measurements out. |
| 68 | // All points must have the same number of dimensions, in the same order. |
| 69 | const std::vector<BenchmarkMeasure> *LastMeasurement = nullptr; |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 70 | for (size_t P = 0, NumPoints = Points_.size(); P < NumPoints; ++P) { |
| 71 | const auto &Point = Points_[P]; |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 72 | if (!Point.Error.empty()) { |
| 73 | ClusterIdForPoint_[P] = ClusterId::error(); |
| 74 | ErrorCluster_.PointIndices.push_back(P); |
| 75 | continue; |
| 76 | } |
| 77 | const auto *CurMeasurement = &Point.Measurements; |
| 78 | if (LastMeasurement) { |
| 79 | if (LastMeasurement->size() != CurMeasurement->size()) { |
| 80 | return llvm::make_error<llvm::StringError>( |
| 81 | "inconsistent measurement dimensions", |
| 82 | llvm::inconvertibleErrorCode()); |
| 83 | } |
| 84 | for (size_t I = 0, E = LastMeasurement->size(); I < E; ++I) { |
| 85 | if (LastMeasurement->at(I).Key != CurMeasurement->at(I).Key) { |
| 86 | return llvm::make_error<llvm::StringError>( |
| 87 | "inconsistent measurement dimensions keys", |
| 88 | llvm::inconvertibleErrorCode()); |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | LastMeasurement = CurMeasurement; |
| 93 | } |
| 94 | if (LastMeasurement) { |
| 95 | NumDimensions_ = LastMeasurement->size(); |
| 96 | } |
| 97 | return llvm::Error::success(); |
| 98 | } |
| 99 | |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 100 | void InstructionBenchmarkClustering::dbScan(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 | 37f0ca0 | 2018-05-15 12:08:00 +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 | 37f0ca0 | 2018-05-15 12:08:00 +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 | 37f0ca0 | 2018-05-15 12:08:00 +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 | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 158 | InstructionBenchmarkClustering Clustering(Points); |
| 159 | if (auto Error = Clustering.validateAndSetup()) { |
Clement Courbet | cdb0eb8 | 2018-05-15 12:38:06 +0000 | [diff] [blame] | 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 | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 166 | Clustering.dbScan(MinPts, Epsilon * Epsilon); |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 167 | return Clustering; |
| 168 | } |
| 169 | |
| 170 | } // namespace exegesis |