Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 1 | //===-- Clustering.cpp ------------------------------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "Clustering.h" |
Clement Courbet | 176388c | 2019-01-02 09:21:00 +0000 | [diff] [blame] | 10 | #include "llvm/ADT/SetVector.h" |
Roman Lebedev | 6971639 | 2019-02-20 09:14:04 +0000 | [diff] [blame^] | 11 | #include "llvm/ADT/SmallSet.h" |
Roman Lebedev | 8aecb0c | 2018-11-19 13:28:22 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/SmallVector.h" |
Roman Lebedev | 6971639 | 2019-02-20 09:14:04 +0000 | [diff] [blame^] | 13 | #include <algorithm> |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 14 | #include <string> |
Roman Lebedev | 6971639 | 2019-02-20 09:14:04 +0000 | [diff] [blame^] | 15 | #include <vector> |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 16 | |
Fangrui Song | 32401af | 2018-10-22 17:10:47 +0000 | [diff] [blame] | 17 | namespace llvm { |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 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/. |
Clement Courbet | dffc4ca | 2018-05-14 11:35:37 +0000 | [diff] [blame] | 26 | // (E) - The amount of noise is relatively small. |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 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 | |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 36 | // Finds the points at distance less than sqrt(EpsilonSquared) of Q (not |
| 37 | // including Q). |
Roman Lebedev | 666d855 | 2018-11-19 13:28:31 +0000 | [diff] [blame] | 38 | void InstructionBenchmarkClustering::rangeQuery( |
Roman Lebedev | 71fdb57 | 2018-11-19 13:28:41 +0000 | [diff] [blame] | 39 | const size_t Q, std::vector<size_t> &Neighbors) const { |
Roman Lebedev | 666d855 | 2018-11-19 13:28:31 +0000 | [diff] [blame] | 40 | Neighbors.clear(); |
Roman Lebedev | 71fdb57 | 2018-11-19 13:28:41 +0000 | [diff] [blame] | 41 | Neighbors.reserve(Points_.size() - 1); // The Q itself isn't a neighbor. |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 42 | const auto &QMeasurements = Points_[Q].Measurements; |
| 43 | for (size_t P = 0, NumPoints = Points_.size(); P < NumPoints; ++P) { |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 44 | if (P == Q) |
| 45 | continue; |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 46 | const auto &PMeasurements = Points_[P].Measurements; |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 47 | if (PMeasurements.empty()) // Error point. |
| 48 | continue; |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 49 | if (isNeighbour(PMeasurements, QMeasurements)) { |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 50 | Neighbors.push_back(P); |
| 51 | } |
| 52 | } |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 55 | InstructionBenchmarkClustering::InstructionBenchmarkClustering( |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 56 | const std::vector<InstructionBenchmark> &Points, |
| 57 | const double EpsilonSquared) |
| 58 | : Points_(Points), EpsilonSquared_(EpsilonSquared), |
| 59 | NoiseCluster_(ClusterId::noise()), ErrorCluster_(ClusterId::error()) {} |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 60 | |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 61 | llvm::Error InstructionBenchmarkClustering::validateAndSetup() { |
| 62 | ClusterIdForPoint_.resize(Points_.size()); |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 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; |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 66 | for (size_t P = 0, NumPoints = Points_.size(); P < NumPoints; ++P) { |
| 67 | const auto &Point = Points_[P]; |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 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 | |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 96 | void InstructionBenchmarkClustering::dbScan(const size_t MinPts) { |
Clement Courbet | 176388c | 2019-01-02 09:21:00 +0000 | [diff] [blame] | 97 | std::vector<size_t> Neighbors; // Persistent buffer to avoid allocs. |
| 98 | for (size_t P = 0, NumPoints = Points_.size(); P < NumPoints; ++P) { |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 99 | if (!ClusterIdForPoint_[P].isUndef()) |
| 100 | continue; // Previously processed in inner loop. |
Roman Lebedev | 666d855 | 2018-11-19 13:28:31 +0000 | [diff] [blame] | 101 | rangeQuery(P, Neighbors); |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 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 | |
Clement Courbet | 176388c | 2019-01-02 09:21:00 +0000 | [diff] [blame] | 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()); |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 122 | |
Clement Courbet | 176388c | 2019-01-02 09:21:00 +0000 | [diff] [blame] | 123 | if (ClusterIdForPoint_[Q].isNoise()) { |
| 124 | // Change noise point to border point. |
| 125 | ClusterIdForPoint_[Q] = CurrentCluster.Id; |
| 126 | CurrentCluster.PointIndices.push_back(Q); |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 127 | continue; |
Clement Courbet | 176388c | 2019-01-02 09:21:00 +0000 | [diff] [blame] | 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 | } |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 140 | } |
| 141 | } |
Clement Courbet | 176388c | 2019-01-02 09:21:00 +0000 | [diff] [blame] | 142 | // assert(Neighbors.capacity() == (Points_.size() - 1)); |
| 143 | // ^ 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] | 144 | |
| 145 | // Add noisy points to noise cluster. |
Clement Courbet | 176388c | 2019-01-02 09:21:00 +0000 | [diff] [blame] | 146 | for (size_t P = 0, NumPoints = Points_.size(); P < NumPoints; ++P) { |
| 147 | if (ClusterIdForPoint_[P].isNoise()) { |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 148 | NoiseCluster_.PointIndices.push_back(P); |
Clement Courbet | 176388c | 2019-01-02 09:21:00 +0000 | [diff] [blame] | 149 | } |
| 150 | } |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Roman Lebedev | 6971639 | 2019-02-20 09:14:04 +0000 | [diff] [blame^] | 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 | |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 245 | llvm::Expected<InstructionBenchmarkClustering> |
| 246 | InstructionBenchmarkClustering::create( |
| 247 | const std::vector<InstructionBenchmark> &Points, const size_t MinPts, |
Roman Lebedev | 6971639 | 2019-02-20 09:14:04 +0000 | [diff] [blame^] | 248 | const double Epsilon, llvm::Optional<unsigned> NumOpcodes) { |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 249 | InstructionBenchmarkClustering Clustering(Points, Epsilon * Epsilon); |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 250 | if (auto Error = Clustering.validateAndSetup()) { |
Clement Courbet | cdb0eb8 | 2018-05-15 12:38:06 +0000 | [diff] [blame] | 251 | return std::move(Error); |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 252 | } |
| 253 | if (Clustering.ErrorCluster_.PointIndices.size() == Points.size()) { |
| 254 | return Clustering; // Nothing to cluster. |
| 255 | } |
| 256 | |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 257 | Clustering.dbScan(MinPts); |
Roman Lebedev | 6971639 | 2019-02-20 09:14:04 +0000 | [diff] [blame^] | 258 | |
| 259 | if (NumOpcodes.hasValue()) |
| 260 | Clustering.stabilize(NumOpcodes.getValue()); |
| 261 | |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 262 | return Clustering; |
| 263 | } |
| 264 | |
| 265 | } // namespace exegesis |
Fangrui Song | 32401af | 2018-10-22 17:10:47 +0000 | [diff] [blame] | 266 | } // namespace llvm |