1 //===-- Clustering.cpp ------------------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "Clustering.h" 10 #include "llvm/ADT/SetVector.h" 11 #include "llvm/ADT/SmallSet.h" 12 #include "llvm/ADT/SmallVector.h" 13 #include <algorithm> 14 #include <string> 15 #include <vector> 16 17 namespace llvm { 18 namespace exegesis { 19 20 // The clustering problem has the following characteristics: 21 // (A) - Low dimension (dimensions are typically proc resource units, 22 // typically < 10). 23 // (B) - Number of points : ~thousands (points are measurements of an MCInst) 24 // (C) - Number of clusters: ~tens. 25 // (D) - The number of clusters is not known /a priory/. 26 // (E) - The amount of noise is relatively small. 27 // The problem is rather small. In terms of algorithms, (D) disqualifies 28 // k-means and makes algorithms such as DBSCAN[1] or OPTICS[2] more applicable. 29 // 30 // We've used DBSCAN here because it's simple to implement. This is a pretty 31 // straightforward and inefficient implementation of the pseudocode in [2]. 32 // 33 // [1] https://en.wikipedia.org/wiki/DBSCAN 34 // [2] https://en.wikipedia.org/wiki/OPTICS_algorithm 35 36 // Finds the points at distance less than sqrt(EpsilonSquared) of Q (not 37 // including Q). 38 void InstructionBenchmarkClustering::rangeQuery( 39 const size_t Q, std::vector<size_t> &Neighbors) const { 40 Neighbors.clear(); 41 Neighbors.reserve(Points_.size() - 1); // The Q itself isn't a neighbor. 42 const auto &QMeasurements = Points_[Q].Measurements; 43 for (size_t P = 0, NumPoints = Points_.size(); P < NumPoints; ++P) { 44 if (P == Q) 45 continue; 46 const auto &PMeasurements = Points_[P].Measurements; 47 if (PMeasurements.empty()) // Error point. 48 continue; 49 if (isNeighbour(PMeasurements, QMeasurements)) { 50 Neighbors.push_back(P); 51 } 52 } 53 } 54 55 InstructionBenchmarkClustering::InstructionBenchmarkClustering( 56 const std::vector<InstructionBenchmark> &Points, 57 const double EpsilonSquared) 58 : Points_(Points), EpsilonSquared_(EpsilonSquared), 59 NoiseCluster_(ClusterId::noise()), ErrorCluster_(ClusterId::error()) {} 60 61 llvm::Error InstructionBenchmarkClustering::validateAndSetup() { 62 ClusterIdForPoint_.resize(Points_.size()); 63 // Mark erroneous measurements out. 64 // All points must have the same number of dimensions, in the same order. 65 const std::vector<BenchmarkMeasure> *LastMeasurement = nullptr; 66 for (size_t P = 0, NumPoints = Points_.size(); P < NumPoints; ++P) { 67 const auto &Point = Points_[P]; 68 if (!Point.Error.empty()) { 69 ClusterIdForPoint_[P] = ClusterId::error(); 70 ErrorCluster_.PointIndices.push_back(P); 71 continue; 72 } 73 const auto *CurMeasurement = &Point.Measurements; 74 if (LastMeasurement) { 75 if (LastMeasurement->size() != CurMeasurement->size()) { 76 return llvm::make_error<llvm::StringError>( 77 "inconsistent measurement dimensions", 78 llvm::inconvertibleErrorCode()); 79 } 80 for (size_t I = 0, E = LastMeasurement->size(); I < E; ++I) { 81 if (LastMeasurement->at(I).Key != CurMeasurement->at(I).Key) { 82 return llvm::make_error<llvm::StringError>( 83 "inconsistent measurement dimensions keys", 84 llvm::inconvertibleErrorCode()); 85 } 86 } 87 } 88 LastMeasurement = CurMeasurement; 89 } 90 if (LastMeasurement) { 91 NumDimensions_ = LastMeasurement->size(); 92 } 93 return llvm::Error::success(); 94 } 95 96 void InstructionBenchmarkClustering::dbScan(const size_t MinPts) { 97 std::vector<size_t> Neighbors; // Persistent buffer to avoid allocs. 98 for (size_t P = 0, NumPoints = Points_.size(); P < NumPoints; ++P) { 99 if (!ClusterIdForPoint_[P].isUndef()) 100 continue; // Previously processed in inner loop. 101 rangeQuery(P, Neighbors); 102 if (Neighbors.size() + 1 < MinPts) { // Density check. 103 // The region around P is not dense enough to create a new cluster, mark 104 // as noise for now. 105 ClusterIdForPoint_[P] = ClusterId::noise(); 106 continue; 107 } 108 109 // Create a new cluster, add P. 110 Clusters_.emplace_back(ClusterId::makeValid(Clusters_.size())); 111 Cluster &CurrentCluster = Clusters_.back(); 112 ClusterIdForPoint_[P] = CurrentCluster.Id; /* Label initial point */ 113 CurrentCluster.PointIndices.push_back(P); 114 115 // Process P's neighbors. 116 llvm::SetVector<size_t, std::deque<size_t>> ToProcess; 117 ToProcess.insert(Neighbors.begin(), Neighbors.end()); 118 while (!ToProcess.empty()) { 119 // Retrieve a point from the set. 120 const size_t Q = *ToProcess.begin(); 121 ToProcess.erase(ToProcess.begin()); 122 123 if (ClusterIdForPoint_[Q].isNoise()) { 124 // Change noise point to border point. 125 ClusterIdForPoint_[Q] = CurrentCluster.Id; 126 CurrentCluster.PointIndices.push_back(Q); 127 continue; 128 } 129 if (!ClusterIdForPoint_[Q].isUndef()) { 130 continue; // Previously processed. 131 } 132 // Add Q to the current custer. 133 ClusterIdForPoint_[Q] = CurrentCluster.Id; 134 CurrentCluster.PointIndices.push_back(Q); 135 // And extend to the neighbors of Q if the region is dense enough. 136 rangeQuery(Q, Neighbors); 137 if (Neighbors.size() + 1 >= MinPts) { 138 ToProcess.insert(Neighbors.begin(), Neighbors.end()); 139 } 140 } 141 } 142 // assert(Neighbors.capacity() == (Points_.size() - 1)); 143 // ^ True, but it is not quaranteed to be true in all the cases. 144 145 // Add noisy points to noise cluster. 146 for (size_t P = 0, NumPoints = Points_.size(); P < NumPoints; ++P) { 147 if (ClusterIdForPoint_[P].isNoise()) { 148 NoiseCluster_.PointIndices.push_back(P); 149 } 150 } 151 } 152 153 // Given an instruction Opcode, we can make benchmarks (measurements) of the 154 // instruction characteristics/performance. Then, to facilitate further analysis 155 // we group the benchmarks with *similar* characteristics into clusters. 156 // Now, this is all not entirely deterministic. Some instructions have variable 157 // characteristics, depending on their arguments. And thus, if we do several 158 // benchmarks of the same instruction Opcode, we may end up with *different* 159 // performance characteristics measurements. And when we then do clustering, 160 // these several benchmarks of the same instruction Opcode may end up being 161 // clustered into *different* clusters. This is not great for further analysis. 162 // We shall find every opcode with benchmarks not in just one cluster, and move 163 // *all* the benchmarks of said Opcode into one new unstable cluster per Opcode. 164 void InstructionBenchmarkClustering::stabilize(unsigned NumOpcodes) { 165 // Given an instruction Opcode, in which clusters do benchmarks of this 166 // instruction lie? Normally, they all should be in the same cluster. 167 std::vector<llvm::SmallSet<ClusterId, 1>> OpcodeToClusterIDs; 168 OpcodeToClusterIDs.resize(NumOpcodes); 169 // The list of opcodes that have more than one cluster. 170 llvm::SetVector<size_t> UnstableOpcodes; 171 // Populate OpcodeToClusterIDs and UnstableOpcodes data structures. 172 assert(ClusterIdForPoint_.size() == Points_.size() && "size mismatch"); 173 for (const auto &Point : zip(Points_, ClusterIdForPoint_)) { 174 const ClusterId &ClusterIdOfPoint = std::get<1>(Point); 175 if (!ClusterIdOfPoint.isValid()) 176 continue; // Only process fully valid clusters. 177 const unsigned Opcode = std::get<0>(Point).keyInstruction().getOpcode(); 178 assert(Opcode < NumOpcodes && "NumOpcodes is incorrect (too small)"); 179 llvm::SmallSet<ClusterId, 1> &ClusterIDsOfOpcode = 180 OpcodeToClusterIDs[Opcode]; 181 ClusterIDsOfOpcode.insert(ClusterIdOfPoint); 182 // Is there more than one ClusterID for this opcode?. 183 if (ClusterIDsOfOpcode.size() < 2) 184 continue; // If not, then at this moment this Opcode is stable. 185 // Else let's record this unstable opcode for future use. 186 UnstableOpcodes.insert(Opcode); 187 } 188 assert(OpcodeToClusterIDs.size() == NumOpcodes && "sanity check"); 189 190 // We know with how many [new] clusters we will end up with. 191 const auto NewTotalClusterCount = Clusters_.size() + UnstableOpcodes.size(); 192 Clusters_.reserve(NewTotalClusterCount); 193 for (const size_t UnstableOpcode : UnstableOpcodes.getArrayRef()) { 194 const llvm::SmallSet<ClusterId, 1> &ClusterIDs = 195 OpcodeToClusterIDs[UnstableOpcode]; 196 assert(ClusterIDs.size() > 1 && 197 "Should only have Opcodes with more than one cluster."); 198 199 // Create a new unstable cluster, one per Opcode. 200 Clusters_.emplace_back(ClusterId::makeValidUnstable(Clusters_.size())); 201 Cluster &UnstableCluster = Clusters_.back(); 202 // We will find *at least* one point in each of these clusters. 203 UnstableCluster.PointIndices.reserve(ClusterIDs.size()); 204 205 // Go through every cluster which we recorded as containing benchmarks 206 // of this UnstableOpcode. NOTE: we only recorded valid clusters. 207 for (const ClusterId &CID : ClusterIDs) { 208 assert(CID.isValid() && 209 "We only recorded valid clusters, not noise/error clusters."); 210 Cluster &OldCluster = Clusters_[CID.getId()]; // Valid clusters storage. 211 // Within each cluster, go through each point, and either move it to the 212 // new unstable cluster, or 'keep' it. 213 // In this case, we'll reshuffle OldCluster.PointIndices vector 214 // so that all the points that are *not* for UnstableOpcode are first, 215 // and the rest of the points is for the UnstableOpcode. 216 const auto it = std::stable_partition( 217 OldCluster.PointIndices.begin(), OldCluster.PointIndices.end(), 218 [this, UnstableOpcode](size_t P) { 219 return Points_[P].keyInstruction().getOpcode() != UnstableOpcode; 220 }); 221 assert(std::distance(it, OldCluster.PointIndices.end()) > 0 && 222 "Should have found at least one bad point"); 223 // Mark to-be-moved points as belonging to the new cluster. 224 std::for_each(it, OldCluster.PointIndices.end(), 225 [this, &UnstableCluster](size_t P) { 226 ClusterIdForPoint_[P] = UnstableCluster.Id; 227 }); 228 // Actually append to-be-moved points to the new cluster. 229 UnstableCluster.PointIndices.insert(UnstableCluster.PointIndices.cend(), 230 it, OldCluster.PointIndices.end()); 231 // And finally, remove "to-be-moved" points form the old cluster. 232 OldCluster.PointIndices.erase(it, OldCluster.PointIndices.cend()); 233 // Now, the old cluster may end up being empty, but let's just keep it 234 // in whatever state it ended up. Purging empty clusters isn't worth it. 235 }; 236 assert(UnstableCluster.PointIndices.size() > 1 && 237 "New unstable cluster should end up with more than one point."); 238 assert(UnstableCluster.PointIndices.size() >= ClusterIDs.size() && 239 "New unstable cluster should end up with no less points than there " 240 "was clusters"); 241 } 242 assert(Clusters_.size() == NewTotalClusterCount && "sanity check"); 243 } 244 245 llvm::Expected<InstructionBenchmarkClustering> 246 InstructionBenchmarkClustering::create( 247 const std::vector<InstructionBenchmark> &Points, const size_t MinPts, 248 const double Epsilon, llvm::Optional<unsigned> NumOpcodes) { 249 InstructionBenchmarkClustering Clustering(Points, Epsilon * Epsilon); 250 if (auto Error = Clustering.validateAndSetup()) { 251 return std::move(Error); 252 } 253 if (Clustering.ErrorCluster_.PointIndices.size() == Points.size()) { 254 return Clustering; // Nothing to cluster. 255 } 256 257 Clustering.dbScan(MinPts); 258 259 if (NumOpcodes.hasValue()) 260 Clustering.stabilize(NumOpcodes.getValue()); 261 262 return Clustering; 263 } 264 265 } // namespace exegesis 266 } // namespace llvm 267