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