Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 1 | //===-- Clustering.h --------------------------------------------*- 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 | /// \file |
| 10 | /// Utilities to compute benchmark result clusters. |
| 11 | /// |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef LLVM_TOOLS_LLVM_EXEGESIS_CLUSTERING_H |
| 15 | #define LLVM_TOOLS_LLVM_EXEGESIS_CLUSTERING_H |
| 16 | |
| 17 | #include "BenchmarkResult.h" |
Roman Lebedev | 6971639 | 2019-02-20 09:14:04 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/Optional.h" |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Error.h" |
Roman Lebedev | 6971639 | 2019-02-20 09:14:04 +0000 | [diff] [blame] | 20 | #include <limits> |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 21 | #include <vector> |
| 22 | |
Fangrui Song | 32401af | 2018-10-22 17:10:47 +0000 | [diff] [blame] | 23 | namespace llvm { |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 24 | namespace exegesis { |
| 25 | |
| 26 | class InstructionBenchmarkClustering { |
| 27 | public: |
Roman Lebedev | c2423fe | 2019-03-28 08:55:01 +0000 | [diff] [blame^] | 28 | enum ModeE { Dbscan, Naive }; |
| 29 | |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 30 | // Clusters `Points` using DBSCAN with the given parameters. See the cc file |
| 31 | // for more explanations on the algorithm. |
| 32 | static llvm::Expected<InstructionBenchmarkClustering> |
Roman Lebedev | c2423fe | 2019-03-28 08:55:01 +0000 | [diff] [blame^] | 33 | create(const std::vector<InstructionBenchmark> &Points, ModeE Mode, |
| 34 | size_t DbscanMinPts, double AnalysisClusteringEpsilon, |
Roman Lebedev | 542e5d7 | 2019-02-25 09:36:12 +0000 | [diff] [blame] | 35 | llvm::Optional<unsigned> NumOpcodes = llvm::None); |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 36 | |
| 37 | class ClusterId { |
| 38 | public: |
| 39 | static ClusterId noise() { return ClusterId(kNoise); } |
| 40 | static ClusterId error() { return ClusterId(kError); } |
Roman Lebedev | c2423fe | 2019-03-28 08:55:01 +0000 | [diff] [blame^] | 41 | static ClusterId makeValid(size_t Id, bool IsUnstable = false) { |
| 42 | return ClusterId(Id, IsUnstable); |
| 43 | } |
Roman Lebedev | 6971639 | 2019-02-20 09:14:04 +0000 | [diff] [blame] | 44 | static ClusterId makeValidUnstable(size_t Id) { |
Roman Lebedev | c2423fe | 2019-03-28 08:55:01 +0000 | [diff] [blame^] | 45 | return makeValid(Id, /*IsUnstable=*/true); |
Roman Lebedev | 6971639 | 2019-02-20 09:14:04 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | ClusterId() : Id_(kUndef), IsUnstable_(false) {} |
| 49 | |
| 50 | // Compare id's, ignoring the 'unstability' bit. |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 51 | bool operator==(const ClusterId &O) const { return Id_ == O.Id_; } |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 52 | bool operator<(const ClusterId &O) const { return Id_ < O.Id_; } |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 53 | |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame] | 54 | bool isValid() const { return Id_ <= kMaxValid; } |
Roman Lebedev | 6971639 | 2019-02-20 09:14:04 +0000 | [diff] [blame] | 55 | bool isUnstable() const { return IsUnstable_; } |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 56 | bool isNoise() const { return Id_ == kNoise; } |
| 57 | bool isError() const { return Id_ == kError; } |
Roman Lebedev | 6971639 | 2019-02-20 09:14:04 +0000 | [diff] [blame] | 58 | bool isUndef() const { return Id_ == kUndef; } |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 59 | |
| 60 | // Precondition: isValid(). |
| 61 | size_t getId() const { |
| 62 | assert(isValid()); |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame] | 63 | return Id_; |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | private: |
Roman Lebedev | 6971639 | 2019-02-20 09:14:04 +0000 | [diff] [blame] | 67 | ClusterId(size_t Id, bool IsUnstable = false) |
| 68 | : Id_(Id), IsUnstable_(IsUnstable) {} |
| 69 | |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 70 | static constexpr const size_t kMaxValid = |
Roman Lebedev | 6971639 | 2019-02-20 09:14:04 +0000 | [diff] [blame] | 71 | (std::numeric_limits<size_t>::max() >> 1) - 4; |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame] | 72 | static constexpr const size_t kNoise = kMaxValid + 1; |
| 73 | static constexpr const size_t kError = kMaxValid + 2; |
| 74 | static constexpr const size_t kUndef = kMaxValid + 3; |
Roman Lebedev | 6971639 | 2019-02-20 09:14:04 +0000 | [diff] [blame] | 75 | |
| 76 | size_t Id_ : (std::numeric_limits<size_t>::digits - 1); |
| 77 | size_t IsUnstable_ : 1; |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 78 | }; |
Roman Lebedev | 6971639 | 2019-02-20 09:14:04 +0000 | [diff] [blame] | 79 | static_assert(sizeof(ClusterId) == sizeof(size_t), "should be a bit field."); |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 80 | |
| 81 | struct Cluster { |
| 82 | Cluster() = delete; |
| 83 | explicit Cluster(const ClusterId &Id) : Id(Id) {} |
| 84 | |
| 85 | const ClusterId Id; |
| 86 | // Indices of benchmarks within the cluster. |
| 87 | std::vector<int> PointIndices; |
| 88 | }; |
| 89 | |
| 90 | ClusterId getClusterIdForPoint(size_t P) const { |
| 91 | return ClusterIdForPoint_[P]; |
| 92 | } |
| 93 | |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 94 | const std::vector<InstructionBenchmark> &getPoints() const { return Points_; } |
| 95 | |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 96 | const Cluster &getCluster(ClusterId Id) const { |
| 97 | assert(!Id.isUndef() && "unlabeled cluster"); |
| 98 | if (Id.isNoise()) { |
| 99 | return NoiseCluster_; |
| 100 | } |
| 101 | if (Id.isError()) { |
| 102 | return ErrorCluster_; |
| 103 | } |
| 104 | return Clusters_[Id.getId()]; |
| 105 | } |
| 106 | |
| 107 | const std::vector<Cluster> &getValidClusters() const { return Clusters_; } |
| 108 | |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 109 | // Returns true if the given point is within a distance Epsilon of each other. |
| 110 | bool isNeighbour(const std::vector<BenchmarkMeasure> &P, |
Roman Lebedev | 542e5d7 | 2019-02-25 09:36:12 +0000 | [diff] [blame] | 111 | const std::vector<BenchmarkMeasure> &Q, |
| 112 | const double EpsilonSquared_) const { |
Roman Lebedev | 8e315b6 | 2018-11-19 13:28:36 +0000 | [diff] [blame] | 113 | double DistanceSquared = 0.0; |
| 114 | for (size_t I = 0, E = P.size(); I < E; ++I) { |
| 115 | const auto Diff = P[I].PerInstructionValue - Q[I].PerInstructionValue; |
| 116 | DistanceSquared += Diff * Diff; |
| 117 | } |
| 118 | return DistanceSquared <= EpsilonSquared_; |
| 119 | } |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 120 | |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 121 | private: |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 122 | InstructionBenchmarkClustering( |
Roman Lebedev | 542e5d7 | 2019-02-25 09:36:12 +0000 | [diff] [blame] | 123 | const std::vector<InstructionBenchmark> &Points, |
| 124 | double AnalysisClusteringEpsilonSquared); |
Roman Lebedev | 6971639 | 2019-02-20 09:14:04 +0000 | [diff] [blame] | 125 | |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 126 | llvm::Error validateAndSetup(); |
Roman Lebedev | c2423fe | 2019-03-28 08:55:01 +0000 | [diff] [blame^] | 127 | |
| 128 | void clusterizeDbScan(size_t MinPts); |
| 129 | void clusterizeNaive(unsigned NumOpcodes); |
| 130 | |
| 131 | // Stabilization is only needed if dbscan was used to clusterize. |
Roman Lebedev | 6971639 | 2019-02-20 09:14:04 +0000 | [diff] [blame] | 132 | void stabilize(unsigned NumOpcodes); |
Roman Lebedev | c2423fe | 2019-03-28 08:55:01 +0000 | [diff] [blame^] | 133 | |
Roman Lebedev | 71fdb57 | 2018-11-19 13:28:41 +0000 | [diff] [blame] | 134 | void rangeQuery(size_t Q, std::vector<size_t> &Scratchpad) const; |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 135 | |
Roman Lebedev | c2423fe | 2019-03-28 08:55:01 +0000 | [diff] [blame^] | 136 | bool areAllNeighbours(ArrayRef<size_t> Pts) const; |
| 137 | |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 138 | const std::vector<InstructionBenchmark> &Points_; |
Roman Lebedev | 542e5d7 | 2019-02-25 09:36:12 +0000 | [diff] [blame] | 139 | const double AnalysisClusteringEpsilonSquared_; |
Roman Lebedev | c2423fe | 2019-03-28 08:55:01 +0000 | [diff] [blame^] | 140 | |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 141 | int NumDimensions_ = 0; |
| 142 | // ClusterForPoint_[P] is the cluster id for Points[P]. |
| 143 | std::vector<ClusterId> ClusterIdForPoint_; |
| 144 | std::vector<Cluster> Clusters_; |
| 145 | Cluster NoiseCluster_; |
| 146 | Cluster ErrorCluster_; |
| 147 | }; |
| 148 | |
Roman Lebedev | c2423fe | 2019-03-28 08:55:01 +0000 | [diff] [blame^] | 149 | class SchedClassClusterCentroid { |
| 150 | public: |
| 151 | const std::vector<PerInstructionStats> &getStats() const { |
| 152 | return Representative; |
| 153 | } |
| 154 | |
| 155 | std::vector<BenchmarkMeasure> getAsPoint() const; |
| 156 | |
| 157 | void addPoint(ArrayRef<BenchmarkMeasure> Point); |
| 158 | |
| 159 | private: |
| 160 | // Measurement stats for the points in the SchedClassCluster. |
| 161 | std::vector<PerInstructionStats> Representative; |
| 162 | }; |
| 163 | |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 164 | } // namespace exegesis |
Fangrui Song | 32401af | 2018-10-22 17:10:47 +0000 | [diff] [blame] | 165 | } // namespace llvm |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 166 | |
| 167 | #endif // LLVM_TOOLS_LLVM_EXEGESIS_CLUSTERING_H |