blob: 8a42e0b28db9a086af3107d7f6c4bf074f79e191 [file] [log] [blame]
Clement Courbet96715412018-05-07 09:09:48 +00001//===-- Clustering.cpp ------------------------------------------*- 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#include "Clustering.h"
Roman Lebedev0b4b5122018-11-19 13:28:09 +000011#include "llvm/ADT/SetVector.h"
Roman Lebedev8aecb0c2018-11-19 13:28:22 +000012#include "llvm/ADT/SmallVector.h"
Clement Courbet96715412018-05-07 09:09:48 +000013#include <string>
Clement Courbet96715412018-05-07 09:09:48 +000014
Fangrui Song32401af2018-10-22 17:10:47 +000015namespace llvm {
Clement Courbet96715412018-05-07 09:09:48 +000016namespace exegesis {
17
18// The clustering problem has the following characteristics:
19// (A) - Low dimension (dimensions are typically proc resource units,
20// typically < 10).
21// (B) - Number of points : ~thousands (points are measurements of an MCInst)
22// (C) - Number of clusters: ~tens.
23// (D) - The number of clusters is not known /a priory/.
Clement Courbetdffc4ca2018-05-14 11:35:37 +000024// (E) - The amount of noise is relatively small.
Clement Courbet96715412018-05-07 09:09:48 +000025// The problem is rather small. In terms of algorithms, (D) disqualifies
26// k-means and makes algorithms such as DBSCAN[1] or OPTICS[2] more applicable.
27//
28// We've used DBSCAN here because it's simple to implement. This is a pretty
29// straightforward and inefficient implementation of the pseudocode in [2].
30//
31// [1] https://en.wikipedia.org/wiki/DBSCAN
32// [2] https://en.wikipedia.org/wiki/OPTICS_algorithm
33
Clement Courbet96715412018-05-07 09:09:48 +000034// Finds the points at distance less than sqrt(EpsilonSquared) of Q (not
35// including Q).
Roman Lebedev8aecb0c2018-11-19 13:28:22 +000036llvm::SmallVector<size_t, 0>
Clement Courbet72287212018-06-04 11:11:55 +000037InstructionBenchmarkClustering::rangeQuery(const size_t Q) const {
Roman Lebedev8aecb0c2018-11-19 13:28:22 +000038 llvm::SmallVector<size_t, 0> Neighbors;
Clement Courbet72287212018-06-04 11:11:55 +000039 const auto &QMeasurements = Points_[Q].Measurements;
40 for (size_t P = 0, NumPoints = Points_.size(); P < NumPoints; ++P) {
Clement Courbet96715412018-05-07 09:09:48 +000041 if (P == Q)
42 continue;
Clement Courbet72287212018-06-04 11:11:55 +000043 const auto &PMeasurements = Points_[P].Measurements;
Clement Courbet96715412018-05-07 09:09:48 +000044 if (PMeasurements.empty()) // Error point.
45 continue;
Clement Courbet72287212018-06-04 11:11:55 +000046 if (isNeighbour(PMeasurements, QMeasurements)) {
Clement Courbet96715412018-05-07 09:09:48 +000047 Neighbors.push_back(P);
48 }
49 }
50 return Neighbors;
51}
52
Clement Courbet72287212018-06-04 11:11:55 +000053bool InstructionBenchmarkClustering::isNeighbour(
54 const std::vector<BenchmarkMeasure> &P,
55 const std::vector<BenchmarkMeasure> &Q) const {
56 double DistanceSquared = 0.0;
57 for (size_t I = 0, E = P.size(); I < E; ++I) {
Clement Courbet684a5f62018-09-26 08:37:21 +000058 const auto Diff = P[I].PerInstructionValue - Q[I].PerInstructionValue;
Clement Courbet72287212018-06-04 11:11:55 +000059 DistanceSquared += Diff * Diff;
60 }
61 return DistanceSquared <= EpsilonSquared_;
62}
Clement Courbet96715412018-05-07 09:09:48 +000063
Clement Courbet37f0ca02018-05-15 12:08:00 +000064InstructionBenchmarkClustering::InstructionBenchmarkClustering(
Clement Courbet72287212018-06-04 11:11:55 +000065 const std::vector<InstructionBenchmark> &Points,
66 const double EpsilonSquared)
67 : Points_(Points), EpsilonSquared_(EpsilonSquared),
68 NoiseCluster_(ClusterId::noise()), ErrorCluster_(ClusterId::error()) {}
Clement Courbet96715412018-05-07 09:09:48 +000069
Clement Courbet37f0ca02018-05-15 12:08:00 +000070llvm::Error InstructionBenchmarkClustering::validateAndSetup() {
71 ClusterIdForPoint_.resize(Points_.size());
Clement Courbet96715412018-05-07 09:09:48 +000072 // Mark erroneous measurements out.
73 // All points must have the same number of dimensions, in the same order.
74 const std::vector<BenchmarkMeasure> *LastMeasurement = nullptr;
Clement Courbet37f0ca02018-05-15 12:08:00 +000075 for (size_t P = 0, NumPoints = Points_.size(); P < NumPoints; ++P) {
76 const auto &Point = Points_[P];
Clement Courbet96715412018-05-07 09:09:48 +000077 if (!Point.Error.empty()) {
78 ClusterIdForPoint_[P] = ClusterId::error();
79 ErrorCluster_.PointIndices.push_back(P);
80 continue;
81 }
82 const auto *CurMeasurement = &Point.Measurements;
83 if (LastMeasurement) {
84 if (LastMeasurement->size() != CurMeasurement->size()) {
85 return llvm::make_error<llvm::StringError>(
86 "inconsistent measurement dimensions",
87 llvm::inconvertibleErrorCode());
88 }
89 for (size_t I = 0, E = LastMeasurement->size(); I < E; ++I) {
90 if (LastMeasurement->at(I).Key != CurMeasurement->at(I).Key) {
91 return llvm::make_error<llvm::StringError>(
92 "inconsistent measurement dimensions keys",
93 llvm::inconvertibleErrorCode());
94 }
95 }
96 }
97 LastMeasurement = CurMeasurement;
98 }
99 if (LastMeasurement) {
100 NumDimensions_ = LastMeasurement->size();
101 }
102 return llvm::Error::success();
103}
104
Clement Courbet72287212018-06-04 11:11:55 +0000105void InstructionBenchmarkClustering::dbScan(const size_t MinPts) {
Clement Courbet37f0ca02018-05-15 12:08:00 +0000106 for (size_t P = 0, NumPoints = Points_.size(); P < NumPoints; ++P) {
Clement Courbet96715412018-05-07 09:09:48 +0000107 if (!ClusterIdForPoint_[P].isUndef())
108 continue; // Previously processed in inner loop.
Clement Courbet72287212018-06-04 11:11:55 +0000109 const auto Neighbors = rangeQuery(P);
Clement Courbet96715412018-05-07 09:09:48 +0000110 if (Neighbors.size() + 1 < MinPts) { // Density check.
111 // The region around P is not dense enough to create a new cluster, mark
112 // as noise for now.
113 ClusterIdForPoint_[P] = ClusterId::noise();
114 continue;
115 }
116
117 // Create a new cluster, add P.
118 Clusters_.emplace_back(ClusterId::makeValid(Clusters_.size()));
119 Cluster &CurrentCluster = Clusters_.back();
120 ClusterIdForPoint_[P] = CurrentCluster.Id; /* Label initial point */
121 CurrentCluster.PointIndices.push_back(P);
122
123 // Process P's neighbors.
Roman Lebedev5c5b1ea2018-11-19 13:28:26 +0000124 llvm::SetVector<size_t, std::deque<size_t>> ToProcess;
Roman Lebedev0b4b5122018-11-19 13:28:09 +0000125 ToProcess.insert(Neighbors.begin(), Neighbors.end());
Clement Courbet96715412018-05-07 09:09:48 +0000126 while (!ToProcess.empty()) {
127 // Retrieve a point from the set.
128 const size_t Q = *ToProcess.begin();
Roman Lebedev0b4b5122018-11-19 13:28:09 +0000129 ToProcess.erase(ToProcess.begin());
Clement Courbet96715412018-05-07 09:09:48 +0000130
131 if (ClusterIdForPoint_[Q].isNoise()) {
132 // Change noise point to border point.
133 ClusterIdForPoint_[Q] = CurrentCluster.Id;
134 CurrentCluster.PointIndices.push_back(Q);
135 continue;
136 }
137 if (!ClusterIdForPoint_[Q].isUndef()) {
138 continue; // Previously processed.
139 }
140 // Add Q to the current custer.
141 ClusterIdForPoint_[Q] = CurrentCluster.Id;
142 CurrentCluster.PointIndices.push_back(Q);
143 // And extend to the neighbors of Q if the region is dense enough.
Clement Courbet72287212018-06-04 11:11:55 +0000144 const auto Neighbors = rangeQuery(Q);
Clement Courbet96715412018-05-07 09:09:48 +0000145 if (Neighbors.size() + 1 >= MinPts) {
146 ToProcess.insert(Neighbors.begin(), Neighbors.end());
147 }
148 }
149 }
150
151 // Add noisy points to noise cluster.
Clement Courbet37f0ca02018-05-15 12:08:00 +0000152 for (size_t P = 0, NumPoints = Points_.size(); P < NumPoints; ++P) {
Clement Courbet96715412018-05-07 09:09:48 +0000153 if (ClusterIdForPoint_[P].isNoise()) {
154 NoiseCluster_.PointIndices.push_back(P);
155 }
156 }
157}
158
159llvm::Expected<InstructionBenchmarkClustering>
160InstructionBenchmarkClustering::create(
161 const std::vector<InstructionBenchmark> &Points, const size_t MinPts,
162 const double Epsilon) {
Clement Courbet72287212018-06-04 11:11:55 +0000163 InstructionBenchmarkClustering Clustering(Points, Epsilon * Epsilon);
Clement Courbet37f0ca02018-05-15 12:08:00 +0000164 if (auto Error = Clustering.validateAndSetup()) {
Clement Courbetcdb0eb82018-05-15 12:38:06 +0000165 return std::move(Error);
Clement Courbet96715412018-05-07 09:09:48 +0000166 }
167 if (Clustering.ErrorCluster_.PointIndices.size() == Points.size()) {
168 return Clustering; // Nothing to cluster.
169 }
170
Clement Courbet72287212018-06-04 11:11:55 +0000171 Clustering.dbScan(MinPts);
Clement Courbet96715412018-05-07 09:09:48 +0000172 return Clustering;
173}
174
175} // namespace exegesis
Fangrui Song32401af2018-10-22 17:10:47 +0000176} // namespace llvm