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