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" |
Clement Courbet | 176388c | 2019-01-02 09:21:00 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/SetVector.h" |
Roman Lebedev | 8aecb0c | 2018-11-19 13:28:22 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/SmallVector.h" |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 13 | #include <string> |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 14 | |
Fangrui Song | 32401af | 2018-10-22 17:10:47 +0000 | [diff] [blame] | 15 | namespace llvm { |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 16 | namespace 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 Courbet | dffc4ca | 2018-05-14 11:35:37 +0000 | [diff] [blame] | 24 | // (E) - The amount of noise is relatively small. |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 25 | // 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 Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 34 | // Finds the points at distance less than sqrt(EpsilonSquared) of Q (not |
| 35 | // including Q). |
Roman Lebedev | 666d855 | 2018-11-19 13:28:31 +0000 | [diff] [blame] | 36 | void InstructionBenchmarkClustering::rangeQuery( |
Roman Lebedev | 71fdb57 | 2018-11-19 13:28:41 +0000 | [diff] [blame] | 37 | const size_t Q, std::vector<size_t> &Neighbors) const { |
Roman Lebedev | 666d855 | 2018-11-19 13:28:31 +0000 | [diff] [blame] | 38 | Neighbors.clear(); |
Roman Lebedev | 71fdb57 | 2018-11-19 13:28:41 +0000 | [diff] [blame] | 39 | Neighbors.reserve(Points_.size() - 1); // The Q itself isn't a neighbor. |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 40 | const auto &QMeasurements = Points_[Q].Measurements; |
| 41 | for (size_t P = 0, NumPoints = Points_.size(); P < NumPoints; ++P) { |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 42 | if (P == Q) |
| 43 | continue; |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 44 | const auto &PMeasurements = Points_[P].Measurements; |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 45 | if (PMeasurements.empty()) // Error point. |
| 46 | continue; |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 47 | if (isNeighbour(PMeasurements, QMeasurements)) { |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 48 | Neighbors.push_back(P); |
| 49 | } |
| 50 | } |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 53 | InstructionBenchmarkClustering::InstructionBenchmarkClustering( |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 54 | const std::vector<InstructionBenchmark> &Points, |
| 55 | const double EpsilonSquared) |
| 56 | : Points_(Points), EpsilonSquared_(EpsilonSquared), |
| 57 | NoiseCluster_(ClusterId::noise()), ErrorCluster_(ClusterId::error()) {} |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 58 | |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 59 | llvm::Error InstructionBenchmarkClustering::validateAndSetup() { |
| 60 | ClusterIdForPoint_.resize(Points_.size()); |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 61 | // 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 Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 64 | for (size_t P = 0, NumPoints = Points_.size(); P < NumPoints; ++P) { |
| 65 | const auto &Point = Points_[P]; |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 66 | 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 Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 94 | void InstructionBenchmarkClustering::dbScan(const size_t MinPts) { |
Clement Courbet | 176388c | 2019-01-02 09:21:00 +0000 | [diff] [blame] | 95 | std::vector<size_t> Neighbors; // Persistent buffer to avoid allocs. |
| 96 | for (size_t P = 0, NumPoints = Points_.size(); P < NumPoints; ++P) { |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 97 | if (!ClusterIdForPoint_[P].isUndef()) |
| 98 | continue; // Previously processed in inner loop. |
Roman Lebedev | 666d855 | 2018-11-19 13:28:31 +0000 | [diff] [blame] | 99 | rangeQuery(P, Neighbors); |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 100 | 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 Courbet | 176388c | 2019-01-02 09:21:00 +0000 | [diff] [blame] | 113 | // 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 Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 120 | |
Clement Courbet | 176388c | 2019-01-02 09:21:00 +0000 | [diff] [blame] | 121 | if (ClusterIdForPoint_[Q].isNoise()) { |
| 122 | // Change noise point to border point. |
| 123 | ClusterIdForPoint_[Q] = CurrentCluster.Id; |
| 124 | CurrentCluster.PointIndices.push_back(Q); |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 125 | continue; |
Clement Courbet | 176388c | 2019-01-02 09:21:00 +0000 | [diff] [blame] | 126 | } |
| 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 Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 138 | } |
| 139 | } |
Clement Courbet | 176388c | 2019-01-02 09:21:00 +0000 | [diff] [blame] | 140 | // assert(Neighbors.capacity() == (Points_.size() - 1)); |
| 141 | // ^ True, but it is not quaranteed to be true in all the cases. |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 142 | |
| 143 | // Add noisy points to noise cluster. |
Clement Courbet | 176388c | 2019-01-02 09:21:00 +0000 | [diff] [blame] | 144 | for (size_t P = 0, NumPoints = Points_.size(); P < NumPoints; ++P) { |
| 145 | if (ClusterIdForPoint_[P].isNoise()) { |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 146 | NoiseCluster_.PointIndices.push_back(P); |
Clement Courbet | 176388c | 2019-01-02 09:21:00 +0000 | [diff] [blame] | 147 | } |
| 148 | } |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | llvm::Expected<InstructionBenchmarkClustering> |
| 152 | InstructionBenchmarkClustering::create( |
| 153 | const std::vector<InstructionBenchmark> &Points, const size_t MinPts, |
| 154 | const double Epsilon) { |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 155 | InstructionBenchmarkClustering Clustering(Points, Epsilon * Epsilon); |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 156 | if (auto Error = Clustering.validateAndSetup()) { |
Clement Courbet | cdb0eb8 | 2018-05-15 12:38:06 +0000 | [diff] [blame] | 157 | return std::move(Error); |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 158 | } |
| 159 | if (Clustering.ErrorCluster_.PointIndices.size() == Points.size()) { |
| 160 | return Clustering; // Nothing to cluster. |
| 161 | } |
| 162 | |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 163 | Clustering.dbScan(MinPts); |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 164 | return Clustering; |
| 165 | } |
| 166 | |
| 167 | } // namespace exegesis |
Fangrui Song | 32401af | 2018-10-22 17:10:47 +0000 | [diff] [blame] | 168 | } // namespace llvm |