blob: e57a46d8ba0d86e1e114134fe84c6122d85db72b [file] [log] [blame]
Clement Courbet96715412018-05-07 09:09:48 +00001//===-- Clustering.h --------------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Courbet96715412018-05-07 09:09:48 +00006//
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 Lebedev69716392019-02-20 09:14:04 +000018#include "llvm/ADT/Optional.h"
Clement Courbet96715412018-05-07 09:09:48 +000019#include "llvm/Support/Error.h"
Roman Lebedev69716392019-02-20 09:14:04 +000020#include <limits>
Clement Courbet96715412018-05-07 09:09:48 +000021#include <vector>
22
Fangrui Song32401af2018-10-22 17:10:47 +000023namespace llvm {
Clement Courbet96715412018-05-07 09:09:48 +000024namespace exegesis {
25
26class InstructionBenchmarkClustering {
27public:
Roman Lebedevc2423fe2019-03-28 08:55:01 +000028 enum ModeE { Dbscan, Naive };
29
Clement Courbet96715412018-05-07 09:09:48 +000030 // 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 Lebedevc2423fe2019-03-28 08:55:01 +000033 create(const std::vector<InstructionBenchmark> &Points, ModeE Mode,
34 size_t DbscanMinPts, double AnalysisClusteringEpsilon,
Roman Lebedev542e5d72019-02-25 09:36:12 +000035 llvm::Optional<unsigned> NumOpcodes = llvm::None);
Clement Courbet96715412018-05-07 09:09:48 +000036
37 class ClusterId {
38 public:
39 static ClusterId noise() { return ClusterId(kNoise); }
40 static ClusterId error() { return ClusterId(kError); }
Roman Lebedevc2423fe2019-03-28 08:55:01 +000041 static ClusterId makeValid(size_t Id, bool IsUnstable = false) {
42 return ClusterId(Id, IsUnstable);
43 }
Roman Lebedev69716392019-02-20 09:14:04 +000044 static ClusterId makeValidUnstable(size_t Id) {
Roman Lebedevc2423fe2019-03-28 08:55:01 +000045 return makeValid(Id, /*IsUnstable=*/true);
Roman Lebedev69716392019-02-20 09:14:04 +000046 }
47
48 ClusterId() : Id_(kUndef), IsUnstable_(false) {}
49
50 // Compare id's, ignoring the 'unstability' bit.
Clement Courbet96715412018-05-07 09:09:48 +000051 bool operator==(const ClusterId &O) const { return Id_ == O.Id_; }
Clement Courbet72287212018-06-04 11:11:55 +000052 bool operator<(const ClusterId &O) const { return Id_ < O.Id_; }
Clement Courbet96715412018-05-07 09:09:48 +000053
Clement Courbet17d3c252018-05-22 13:31:29 +000054 bool isValid() const { return Id_ <= kMaxValid; }
Roman Lebedev69716392019-02-20 09:14:04 +000055 bool isUnstable() const { return IsUnstable_; }
Clement Courbet96715412018-05-07 09:09:48 +000056 bool isNoise() const { return Id_ == kNoise; }
57 bool isError() const { return Id_ == kError; }
Roman Lebedev69716392019-02-20 09:14:04 +000058 bool isUndef() const { return Id_ == kUndef; }
Clement Courbet96715412018-05-07 09:09:48 +000059
60 // Precondition: isValid().
61 size_t getId() const {
62 assert(isValid());
Clement Courbet17d3c252018-05-22 13:31:29 +000063 return Id_;
Clement Courbet96715412018-05-07 09:09:48 +000064 }
65
66 private:
Roman Lebedev69716392019-02-20 09:14:04 +000067 ClusterId(size_t Id, bool IsUnstable = false)
68 : Id_(Id), IsUnstable_(IsUnstable) {}
69
Clement Courbet72287212018-06-04 11:11:55 +000070 static constexpr const size_t kMaxValid =
Roman Lebedev69716392019-02-20 09:14:04 +000071 (std::numeric_limits<size_t>::max() >> 1) - 4;
Clement Courbet17d3c252018-05-22 13:31:29 +000072 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 Lebedev69716392019-02-20 09:14:04 +000075
76 size_t Id_ : (std::numeric_limits<size_t>::digits - 1);
77 size_t IsUnstable_ : 1;
Clement Courbet96715412018-05-07 09:09:48 +000078 };
Roman Lebedev69716392019-02-20 09:14:04 +000079 static_assert(sizeof(ClusterId) == sizeof(size_t), "should be a bit field.");
Clement Courbet96715412018-05-07 09:09:48 +000080
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 Courbet37f0ca02018-05-15 12:08:00 +000094 const std::vector<InstructionBenchmark> &getPoints() const { return Points_; }
95
Clement Courbet96715412018-05-07 09:09:48 +000096 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 Courbet72287212018-06-04 11:11:55 +0000109 // Returns true if the given point is within a distance Epsilon of each other.
110 bool isNeighbour(const std::vector<BenchmarkMeasure> &P,
Roman Lebedev542e5d72019-02-25 09:36:12 +0000111 const std::vector<BenchmarkMeasure> &Q,
112 const double EpsilonSquared_) const {
Roman Lebedev8e315b62018-11-19 13:28:36 +0000113 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 Courbet72287212018-06-04 11:11:55 +0000120
Clement Courbet96715412018-05-07 09:09:48 +0000121private:
Clement Courbet37f0ca02018-05-15 12:08:00 +0000122 InstructionBenchmarkClustering(
Roman Lebedev542e5d72019-02-25 09:36:12 +0000123 const std::vector<InstructionBenchmark> &Points,
124 double AnalysisClusteringEpsilonSquared);
Roman Lebedev69716392019-02-20 09:14:04 +0000125
Clement Courbet37f0ca02018-05-15 12:08:00 +0000126 llvm::Error validateAndSetup();
Roman Lebedevc2423fe2019-03-28 08:55:01 +0000127
128 void clusterizeDbScan(size_t MinPts);
129 void clusterizeNaive(unsigned NumOpcodes);
130
131 // Stabilization is only needed if dbscan was used to clusterize.
Roman Lebedev69716392019-02-20 09:14:04 +0000132 void stabilize(unsigned NumOpcodes);
Roman Lebedevc2423fe2019-03-28 08:55:01 +0000133
Roman Lebedev71fdb572018-11-19 13:28:41 +0000134 void rangeQuery(size_t Q, std::vector<size_t> &Scratchpad) const;
Clement Courbet72287212018-06-04 11:11:55 +0000135
Roman Lebedevc2423fe2019-03-28 08:55:01 +0000136 bool areAllNeighbours(ArrayRef<size_t> Pts) const;
137
Clement Courbet37f0ca02018-05-15 12:08:00 +0000138 const std::vector<InstructionBenchmark> &Points_;
Roman Lebedev542e5d72019-02-25 09:36:12 +0000139 const double AnalysisClusteringEpsilonSquared_;
Roman Lebedevc2423fe2019-03-28 08:55:01 +0000140
Clement Courbet96715412018-05-07 09:09:48 +0000141 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 Lebedevc2423fe2019-03-28 08:55:01 +0000149class SchedClassClusterCentroid {
150public:
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
159private:
160 // Measurement stats for the points in the SchedClassCluster.
161 std::vector<PerInstructionStats> Representative;
162};
163
Clement Courbet96715412018-05-07 09:09:48 +0000164} // namespace exegesis
Fangrui Song32401af2018-10-22 17:10:47 +0000165} // namespace llvm
Clement Courbet96715412018-05-07 09:09:48 +0000166
167#endif // LLVM_TOOLS_LLVM_EXEGESIS_CLUSTERING_H