blob: b63afec945fa7c5b7eb6c56d972f1c919ce86927 [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"
11#include <string>
12#include <unordered_set>
13
14namespace exegesis {
15
16// The clustering problem has the following characteristics:
17// (A) - Low dimension (dimensions are typically proc resource units,
18// typically < 10).
19// (B) - Number of points : ~thousands (points are measurements of an MCInst)
20// (C) - Number of clusters: ~tens.
21// (D) - The number of clusters is not known /a priory/.
Clement Courbetdffc4ca2018-05-14 11:35:37 +000022// (E) - The amount of noise is relatively small.
Clement Courbet96715412018-05-07 09:09:48 +000023// The problem is rather small. In terms of algorithms, (D) disqualifies
24// k-means and makes algorithms such as DBSCAN[1] or OPTICS[2] more applicable.
25//
26// We've used DBSCAN here because it's simple to implement. This is a pretty
27// straightforward and inefficient implementation of the pseudocode in [2].
28//
29// [1] https://en.wikipedia.org/wiki/DBSCAN
30// [2] https://en.wikipedia.org/wiki/OPTICS_algorithm
31
Clement Courbet96715412018-05-07 09:09:48 +000032// Finds the points at distance less than sqrt(EpsilonSquared) of Q (not
33// including Q).
Clement Courbet72287212018-06-04 11:11:55 +000034std::vector<size_t>
35InstructionBenchmarkClustering::rangeQuery(const size_t Q) const {
Clement Courbet96715412018-05-07 09:09:48 +000036 std::vector<size_t> Neighbors;
Clement Courbet72287212018-06-04 11:11:55 +000037 const auto &QMeasurements = Points_[Q].Measurements;
38 for (size_t P = 0, NumPoints = Points_.size(); P < NumPoints; ++P) {
Clement Courbet96715412018-05-07 09:09:48 +000039 if (P == Q)
40 continue;
Clement Courbet72287212018-06-04 11:11:55 +000041 const auto &PMeasurements = Points_[P].Measurements;
Clement Courbet96715412018-05-07 09:09:48 +000042 if (PMeasurements.empty()) // Error point.
43 continue;
Clement Courbet72287212018-06-04 11:11:55 +000044 if (isNeighbour(PMeasurements, QMeasurements)) {
Clement Courbet96715412018-05-07 09:09:48 +000045 Neighbors.push_back(P);
46 }
47 }
48 return Neighbors;
49}
50
Clement Courbet72287212018-06-04 11:11:55 +000051bool InstructionBenchmarkClustering::isNeighbour(
52 const std::vector<BenchmarkMeasure> &P,
53 const std::vector<BenchmarkMeasure> &Q) const {
54 double DistanceSquared = 0.0;
55 for (size_t I = 0, E = P.size(); I < E; ++I) {
Clement Courbet684a5f62018-09-26 08:37:21 +000056 const auto Diff = P[I].PerInstructionValue - Q[I].PerInstructionValue;
Clement Courbet72287212018-06-04 11:11:55 +000057 DistanceSquared += Diff * Diff;
58 }
59 return DistanceSquared <= EpsilonSquared_;
60}
Clement Courbet96715412018-05-07 09:09:48 +000061
Clement Courbet37f0ca02018-05-15 12:08:00 +000062InstructionBenchmarkClustering::InstructionBenchmarkClustering(
Clement Courbet72287212018-06-04 11:11:55 +000063 const std::vector<InstructionBenchmark> &Points,
64 const double EpsilonSquared)
65 : Points_(Points), EpsilonSquared_(EpsilonSquared),
66 NoiseCluster_(ClusterId::noise()), ErrorCluster_(ClusterId::error()) {}
Clement Courbet96715412018-05-07 09:09:48 +000067
Clement Courbet37f0ca02018-05-15 12:08:00 +000068llvm::Error InstructionBenchmarkClustering::validateAndSetup() {
69 ClusterIdForPoint_.resize(Points_.size());
Clement Courbet96715412018-05-07 09:09:48 +000070 // Mark erroneous measurements out.
71 // All points must have the same number of dimensions, in the same order.
72 const std::vector<BenchmarkMeasure> *LastMeasurement = nullptr;
Clement Courbet37f0ca02018-05-15 12:08:00 +000073 for (size_t P = 0, NumPoints = Points_.size(); P < NumPoints; ++P) {
74 const auto &Point = Points_[P];
Clement Courbet96715412018-05-07 09:09:48 +000075 if (!Point.Error.empty()) {
76 ClusterIdForPoint_[P] = ClusterId::error();
77 ErrorCluster_.PointIndices.push_back(P);
78 continue;
79 }
80 const auto *CurMeasurement = &Point.Measurements;
81 if (LastMeasurement) {
82 if (LastMeasurement->size() != CurMeasurement->size()) {
83 return llvm::make_error<llvm::StringError>(
84 "inconsistent measurement dimensions",
85 llvm::inconvertibleErrorCode());
86 }
87 for (size_t I = 0, E = LastMeasurement->size(); I < E; ++I) {
88 if (LastMeasurement->at(I).Key != CurMeasurement->at(I).Key) {
89 return llvm::make_error<llvm::StringError>(
90 "inconsistent measurement dimensions keys",
91 llvm::inconvertibleErrorCode());
92 }
93 }
94 }
95 LastMeasurement = CurMeasurement;
96 }
97 if (LastMeasurement) {
98 NumDimensions_ = LastMeasurement->size();
99 }
100 return llvm::Error::success();
101}
102
Clement Courbet72287212018-06-04 11:11:55 +0000103void InstructionBenchmarkClustering::dbScan(const size_t MinPts) {
Clement Courbet37f0ca02018-05-15 12:08:00 +0000104 for (size_t P = 0, NumPoints = Points_.size(); P < NumPoints; ++P) {
Clement Courbet96715412018-05-07 09:09:48 +0000105 if (!ClusterIdForPoint_[P].isUndef())
106 continue; // Previously processed in inner loop.
Clement Courbet72287212018-06-04 11:11:55 +0000107 const auto Neighbors = rangeQuery(P);
Clement Courbet96715412018-05-07 09:09:48 +0000108 if (Neighbors.size() + 1 < MinPts) { // Density check.
109 // The region around P is not dense enough to create a new cluster, mark
110 // as noise for now.
111 ClusterIdForPoint_[P] = ClusterId::noise();
112 continue;
113 }
114
115 // Create a new cluster, add P.
116 Clusters_.emplace_back(ClusterId::makeValid(Clusters_.size()));
117 Cluster &CurrentCluster = Clusters_.back();
118 ClusterIdForPoint_[P] = CurrentCluster.Id; /* Label initial point */
119 CurrentCluster.PointIndices.push_back(P);
120
121 // Process P's neighbors.
122 std::unordered_set<size_t> ToProcess(Neighbors.begin(), Neighbors.end());
123 while (!ToProcess.empty()) {
124 // Retrieve a point from the set.
125 const size_t Q = *ToProcess.begin();
126 ToProcess.erase(Q);
127
128 if (ClusterIdForPoint_[Q].isNoise()) {
129 // Change noise point to border point.
130 ClusterIdForPoint_[Q] = CurrentCluster.Id;
131 CurrentCluster.PointIndices.push_back(Q);
132 continue;
133 }
134 if (!ClusterIdForPoint_[Q].isUndef()) {
135 continue; // Previously processed.
136 }
137 // Add Q to the current custer.
138 ClusterIdForPoint_[Q] = CurrentCluster.Id;
139 CurrentCluster.PointIndices.push_back(Q);
140 // And extend to the neighbors of Q if the region is dense enough.
Clement Courbet72287212018-06-04 11:11:55 +0000141 const auto Neighbors = rangeQuery(Q);
Clement Courbet96715412018-05-07 09:09:48 +0000142 if (Neighbors.size() + 1 >= MinPts) {
143 ToProcess.insert(Neighbors.begin(), Neighbors.end());
144 }
145 }
146 }
147
148 // Add noisy points to noise cluster.
Clement Courbet37f0ca02018-05-15 12:08:00 +0000149 for (size_t P = 0, NumPoints = Points_.size(); P < NumPoints; ++P) {
Clement Courbet96715412018-05-07 09:09:48 +0000150 if (ClusterIdForPoint_[P].isNoise()) {
151 NoiseCluster_.PointIndices.push_back(P);
152 }
153 }
154}
155
156llvm::Expected<InstructionBenchmarkClustering>
157InstructionBenchmarkClustering::create(
158 const std::vector<InstructionBenchmark> &Points, const size_t MinPts,
159 const double Epsilon) {
Clement Courbet72287212018-06-04 11:11:55 +0000160 InstructionBenchmarkClustering Clustering(Points, Epsilon * Epsilon);
Clement Courbet37f0ca02018-05-15 12:08:00 +0000161 if (auto Error = Clustering.validateAndSetup()) {
Clement Courbetcdb0eb82018-05-15 12:38:06 +0000162 return std::move(Error);
Clement Courbet96715412018-05-07 09:09:48 +0000163 }
164 if (Clustering.ErrorCluster_.PointIndices.size() == Points.size()) {
165 return Clustering; // Nothing to cluster.
166 }
167
Clement Courbet72287212018-06-04 11:11:55 +0000168 Clustering.dbScan(MinPts);
Clement Courbet96715412018-05-07 09:09:48 +0000169 return Clustering;
170}
171
172} // namespace exegesis