| 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: |
| 28 | // Clusters `Points` using DBSCAN with the given parameters. See the cc file |
| 29 | // for more explanations on the algorithm. |
| 30 | static llvm::Expected<InstructionBenchmarkClustering> |
| 31 | create(const std::vector<InstructionBenchmark> &Points, size_t MinPts, |
| Roman Lebedev | 542e5d7 | 2019-02-25 09:36:12 +0000 | [diff] [blame] | 32 | double AnalysisClusteringEpsilon, |
| 33 | llvm::Optional<unsigned> NumOpcodes = llvm::None); |
| Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 34 | |
| 35 | class ClusterId { |
| 36 | public: |
| 37 | static ClusterId noise() { return ClusterId(kNoise); } |
| 38 | static ClusterId error() { return ClusterId(kError); } |
| Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 39 | static ClusterId makeValid(size_t Id) { return ClusterId(Id); } |
| Roman Lebedev | 6971639 | 2019-02-20 09:14:04 +0000 | [diff] [blame] | 40 | static ClusterId makeValidUnstable(size_t Id) { |
| 41 | return ClusterId(Id, /*IsUnstable=*/true); |
| 42 | } |
| 43 | |
| 44 | ClusterId() : Id_(kUndef), IsUnstable_(false) {} |
| 45 | |
| 46 | // Compare id's, ignoring the 'unstability' bit. |
| Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 47 | bool operator==(const ClusterId &O) const { return Id_ == O.Id_; } |
| Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 48 | bool operator<(const ClusterId &O) const { return Id_ < O.Id_; } |
| Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 49 | |
| Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame] | 50 | bool isValid() const { return Id_ <= kMaxValid; } |
| Roman Lebedev | 6971639 | 2019-02-20 09:14:04 +0000 | [diff] [blame] | 51 | bool isUnstable() const { return IsUnstable_; } |
| Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 52 | bool isNoise() const { return Id_ == kNoise; } |
| 53 | bool isError() const { return Id_ == kError; } |
| Roman Lebedev | 6971639 | 2019-02-20 09:14:04 +0000 | [diff] [blame] | 54 | bool isUndef() const { return Id_ == kUndef; } |
| Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 55 | |
| 56 | // Precondition: isValid(). |
| 57 | size_t getId() const { |
| 58 | assert(isValid()); |
| Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame] | 59 | return Id_; |
| Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | private: |
| Roman Lebedev | 6971639 | 2019-02-20 09:14:04 +0000 | [diff] [blame] | 63 | ClusterId(size_t Id, bool IsUnstable = false) |
| 64 | : Id_(Id), IsUnstable_(IsUnstable) {} |
| 65 | |
| Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 66 | static constexpr const size_t kMaxValid = |
| Roman Lebedev | 6971639 | 2019-02-20 09:14:04 +0000 | [diff] [blame] | 67 | (std::numeric_limits<size_t>::max() >> 1) - 4; |
| Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame] | 68 | static constexpr const size_t kNoise = kMaxValid + 1; |
| 69 | static constexpr const size_t kError = kMaxValid + 2; |
| 70 | static constexpr const size_t kUndef = kMaxValid + 3; |
| Roman Lebedev | 6971639 | 2019-02-20 09:14:04 +0000 | [diff] [blame] | 71 | |
| 72 | size_t Id_ : (std::numeric_limits<size_t>::digits - 1); |
| 73 | size_t IsUnstable_ : 1; |
| Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 74 | }; |
| Roman Lebedev | 6971639 | 2019-02-20 09:14:04 +0000 | [diff] [blame] | 75 | static_assert(sizeof(ClusterId) == sizeof(size_t), "should be a bit field."); |
| Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 76 | |
| 77 | struct Cluster { |
| 78 | Cluster() = delete; |
| 79 | explicit Cluster(const ClusterId &Id) : Id(Id) {} |
| 80 | |
| 81 | const ClusterId Id; |
| 82 | // Indices of benchmarks within the cluster. |
| 83 | std::vector<int> PointIndices; |
| 84 | }; |
| 85 | |
| 86 | ClusterId getClusterIdForPoint(size_t P) const { |
| 87 | return ClusterIdForPoint_[P]; |
| 88 | } |
| 89 | |
| Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 90 | const std::vector<InstructionBenchmark> &getPoints() const { return Points_; } |
| 91 | |
| Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 92 | const Cluster &getCluster(ClusterId Id) const { |
| 93 | assert(!Id.isUndef() && "unlabeled cluster"); |
| 94 | if (Id.isNoise()) { |
| 95 | return NoiseCluster_; |
| 96 | } |
| 97 | if (Id.isError()) { |
| 98 | return ErrorCluster_; |
| 99 | } |
| 100 | return Clusters_[Id.getId()]; |
| 101 | } |
| 102 | |
| 103 | const std::vector<Cluster> &getValidClusters() const { return Clusters_; } |
| 104 | |
| Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 105 | // Returns true if the given point is within a distance Epsilon of each other. |
| 106 | bool isNeighbour(const std::vector<BenchmarkMeasure> &P, |
| Roman Lebedev | 542e5d7 | 2019-02-25 09:36:12 +0000 | [diff] [blame] | 107 | const std::vector<BenchmarkMeasure> &Q, |
| 108 | const double EpsilonSquared_) const { |
| Roman Lebedev | 8e315b6 | 2018-11-19 13:28:36 +0000 | [diff] [blame] | 109 | double DistanceSquared = 0.0; |
| 110 | for (size_t I = 0, E = P.size(); I < E; ++I) { |
| 111 | const auto Diff = P[I].PerInstructionValue - Q[I].PerInstructionValue; |
| 112 | DistanceSquared += Diff * Diff; |
| 113 | } |
| 114 | return DistanceSquared <= EpsilonSquared_; |
| 115 | } |
| Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 116 | |
| Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 117 | private: |
| Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 118 | InstructionBenchmarkClustering( |
| Roman Lebedev | 542e5d7 | 2019-02-25 09:36:12 +0000 | [diff] [blame] | 119 | const std::vector<InstructionBenchmark> &Points, |
| 120 | double AnalysisClusteringEpsilonSquared); |
| Roman Lebedev | 6971639 | 2019-02-20 09:14:04 +0000 | [diff] [blame] | 121 | |
| Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 122 | llvm::Error validateAndSetup(); |
| Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 123 | void dbScan(size_t MinPts); |
| Roman Lebedev | 6971639 | 2019-02-20 09:14:04 +0000 | [diff] [blame] | 124 | void stabilize(unsigned NumOpcodes); |
| Roman Lebedev | 71fdb57 | 2018-11-19 13:28:41 +0000 | [diff] [blame] | 125 | void rangeQuery(size_t Q, std::vector<size_t> &Scratchpad) const; |
| Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 126 | |
| Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 127 | const std::vector<InstructionBenchmark> &Points_; |
| Roman Lebedev | 542e5d7 | 2019-02-25 09:36:12 +0000 | [diff] [blame] | 128 | const double AnalysisClusteringEpsilonSquared_; |
| Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 129 | int NumDimensions_ = 0; |
| 130 | // ClusterForPoint_[P] is the cluster id for Points[P]. |
| 131 | std::vector<ClusterId> ClusterIdForPoint_; |
| 132 | std::vector<Cluster> Clusters_; |
| 133 | Cluster NoiseCluster_; |
| 134 | Cluster ErrorCluster_; |
| 135 | }; |
| 136 | |
| 137 | } // namespace exegesis |
| Fangrui Song | 32401af | 2018-10-22 17:10:47 +0000 | [diff] [blame] | 138 | } // namespace llvm |
| Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 139 | |
| 140 | #endif // LLVM_TOOLS_LLVM_EXEGESIS_CLUSTERING_H |