Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 1 | //===-- Clustering.h --------------------------------------------*- 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 | /// \file |
| 11 | /// Utilities to compute benchmark result clusters. |
| 12 | /// |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef LLVM_TOOLS_LLVM_EXEGESIS_CLUSTERING_H |
| 16 | #define LLVM_TOOLS_LLVM_EXEGESIS_CLUSTERING_H |
| 17 | |
| 18 | #include "BenchmarkResult.h" |
| 19 | #include "llvm/Support/Error.h" |
| 20 | #include <vector> |
| 21 | |
Fangrui Song | 32401af | 2018-10-22 17:10:47 +0000 | [diff] [blame^] | 22 | namespace llvm { |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 23 | namespace exegesis { |
| 24 | |
| 25 | class InstructionBenchmarkClustering { |
| 26 | public: |
| 27 | // Clusters `Points` using DBSCAN with the given parameters. See the cc file |
| 28 | // for more explanations on the algorithm. |
| 29 | static llvm::Expected<InstructionBenchmarkClustering> |
| 30 | create(const std::vector<InstructionBenchmark> &Points, size_t MinPts, |
| 31 | double Epsilon); |
| 32 | |
| 33 | class ClusterId { |
| 34 | public: |
| 35 | static ClusterId noise() { return ClusterId(kNoise); } |
| 36 | static ClusterId error() { return ClusterId(kError); } |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 37 | static ClusterId makeValid(size_t Id) { return ClusterId(Id); } |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 38 | ClusterId() : Id_(kUndef) {} |
| 39 | bool operator==(const ClusterId &O) const { return Id_ == O.Id_; } |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 40 | bool operator<(const ClusterId &O) const { return Id_ < O.Id_; } |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 41 | |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame] | 42 | bool isValid() const { return Id_ <= kMaxValid; } |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 43 | bool isUndef() const { return Id_ == kUndef; } |
| 44 | bool isNoise() const { return Id_ == kNoise; } |
| 45 | bool isError() const { return Id_ == kError; } |
| 46 | |
| 47 | // Precondition: isValid(). |
| 48 | size_t getId() const { |
| 49 | assert(isValid()); |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame] | 50 | return Id_; |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | private: |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame] | 54 | explicit ClusterId(size_t Id) : Id_(Id) {} |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 55 | static constexpr const size_t kMaxValid = |
| 56 | std::numeric_limits<size_t>::max() - 4; |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame] | 57 | static constexpr const size_t kNoise = kMaxValid + 1; |
| 58 | static constexpr const size_t kError = kMaxValid + 2; |
| 59 | static constexpr const size_t kUndef = kMaxValid + 3; |
| 60 | size_t Id_; |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | struct Cluster { |
| 64 | Cluster() = delete; |
| 65 | explicit Cluster(const ClusterId &Id) : Id(Id) {} |
| 66 | |
| 67 | const ClusterId Id; |
| 68 | // Indices of benchmarks within the cluster. |
| 69 | std::vector<int> PointIndices; |
| 70 | }; |
| 71 | |
| 72 | ClusterId getClusterIdForPoint(size_t P) const { |
| 73 | return ClusterIdForPoint_[P]; |
| 74 | } |
| 75 | |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 76 | const std::vector<InstructionBenchmark> &getPoints() const { return Points_; } |
| 77 | |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 78 | const Cluster &getCluster(ClusterId Id) const { |
| 79 | assert(!Id.isUndef() && "unlabeled cluster"); |
| 80 | if (Id.isNoise()) { |
| 81 | return NoiseCluster_; |
| 82 | } |
| 83 | if (Id.isError()) { |
| 84 | return ErrorCluster_; |
| 85 | } |
| 86 | return Clusters_[Id.getId()]; |
| 87 | } |
| 88 | |
| 89 | const std::vector<Cluster> &getValidClusters() const { return Clusters_; } |
| 90 | |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 91 | // Returns true if the given point is within a distance Epsilon of each other. |
| 92 | bool isNeighbour(const std::vector<BenchmarkMeasure> &P, |
| 93 | const std::vector<BenchmarkMeasure> &Q) const; |
| 94 | |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 95 | private: |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 96 | InstructionBenchmarkClustering( |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 97 | const std::vector<InstructionBenchmark> &Points, double EpsilonSquared); |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 98 | llvm::Error validateAndSetup(); |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 99 | void dbScan(size_t MinPts); |
| 100 | std::vector<size_t> rangeQuery(size_t Q) const; |
| 101 | |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 102 | const std::vector<InstructionBenchmark> &Points_; |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 103 | const double EpsilonSquared_; |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 104 | int NumDimensions_ = 0; |
| 105 | // ClusterForPoint_[P] is the cluster id for Points[P]. |
| 106 | std::vector<ClusterId> ClusterIdForPoint_; |
| 107 | std::vector<Cluster> Clusters_; |
| 108 | Cluster NoiseCluster_; |
| 109 | Cluster ErrorCluster_; |
| 110 | }; |
| 111 | |
| 112 | } // namespace exegesis |
Fangrui Song | 32401af | 2018-10-22 17:10:47 +0000 | [diff] [blame^] | 113 | } // namespace llvm |
Clement Courbet | 9671541 | 2018-05-07 09:09:48 +0000 | [diff] [blame] | 114 | |
| 115 | #endif // LLVM_TOOLS_LLVM_EXEGESIS_CLUSTERING_H |