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