blob: 3d6413d2256a41d1fbe7abcd61d8819c1abf6974 [file] [log] [blame]
Clement Courbet96715412018-05-07 09:09:48 +00001//===-- 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
22namespace exegesis {
23
24class InstructionBenchmarkClustering {
25public:
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 Courbet17d3c252018-05-22 13:31:29 +000036 static ClusterId makeValid(size_t Id) {
Clement Courbet96715412018-05-07 09:09:48 +000037 return ClusterId(Id);
38 }
39 ClusterId() : Id_(kUndef) {}
40 bool operator==(const ClusterId &O) const { return Id_ == O.Id_; }
Clement Courbet17d3c252018-05-22 13:31:29 +000041 bool operator<(const ClusterId &O) const {return Id_ < O.Id_; }
Clement Courbet96715412018-05-07 09:09:48 +000042
Clement Courbet17d3c252018-05-22 13:31:29 +000043 bool isValid() const { return Id_ <= kMaxValid; }
Clement Courbet96715412018-05-07 09:09:48 +000044 bool isUndef() const { return Id_ == kUndef; }
45 bool isNoise() const { return Id_ == kNoise; }
46 bool isError() const { return Id_ == kError; }
47
48 // Precondition: isValid().
49 size_t getId() const {
50 assert(isValid());
Clement Courbet17d3c252018-05-22 13:31:29 +000051 return Id_;
Clement Courbet96715412018-05-07 09:09:48 +000052 }
53
54 private:
Clement Courbet17d3c252018-05-22 13:31:29 +000055 explicit ClusterId(size_t Id) : Id_(Id) {}
56 static constexpr const size_t kMaxValid = std::numeric_limits<size_t>::max() - 4;
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 Courbet96715412018-05-07 09:09:48 +000061 };
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 Courbet37f0ca02018-05-15 12:08:00 +000076 const std::vector<InstructionBenchmark> &getPoints() const { return Points_; }
77
Clement Courbet96715412018-05-07 09:09:48 +000078 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
91private:
Clement Courbet37f0ca02018-05-15 12:08:00 +000092 InstructionBenchmarkClustering(
93 const std::vector<InstructionBenchmark> &Points);
94 llvm::Error validateAndSetup();
95 void dbScan(size_t MinPts, double EpsilonSquared);
96 const std::vector<InstructionBenchmark> &Points_;
Clement Courbet96715412018-05-07 09:09:48 +000097 int NumDimensions_ = 0;
98 // ClusterForPoint_[P] is the cluster id for Points[P].
99 std::vector<ClusterId> ClusterIdForPoint_;
100 std::vector<Cluster> Clusters_;
101 Cluster NoiseCluster_;
102 Cluster ErrorCluster_;
103};
104
105} // namespace exegesis
106
107#endif // LLVM_TOOLS_LLVM_EXEGESIS_CLUSTERING_H