blob: acd2276260b532e0c39f7df05b98d374d6fe2f51 [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
32namespace {
33
34// Finds the points at distance less than sqrt(EpsilonSquared) of Q (not
35// including Q).
36std::vector<size_t> rangeQuery(const std::vector<InstructionBenchmark> &Points,
37 const size_t Q, const double EpsilonSquared) {
38 std::vector<size_t> Neighbors;
39 const auto &QMeasurements = Points[Q].Measurements;
40 for (size_t P = 0, NumPoints = Points.size(); P < NumPoints; ++P) {
41 if (P == Q)
42 continue;
43 const auto &PMeasurements = Points[P].Measurements;
44 if (PMeasurements.empty()) // Error point.
45 continue;
46 double DistanceSquared = 0;
47 for (size_t I = 0, E = QMeasurements.size(); I < E; ++I) {
48 const auto Diff = PMeasurements[I].Value - QMeasurements[I].Value;
49 DistanceSquared += Diff * Diff;
50 }
51 if (DistanceSquared <= EpsilonSquared) {
52 Neighbors.push_back(P);
53 }
54 }
55 return Neighbors;
56}
57
58} // namespace
59
Clement Courbet37f0ca02018-05-15 12:08:00 +000060InstructionBenchmarkClustering::InstructionBenchmarkClustering(
61 const std::vector<InstructionBenchmark> &Points)
62 : Points_(Points), NoiseCluster_(ClusterId::noise()),
63 ErrorCluster_(ClusterId::error()) {}
Clement Courbet96715412018-05-07 09:09:48 +000064
Clement Courbet37f0ca02018-05-15 12:08:00 +000065llvm::Error InstructionBenchmarkClustering::validateAndSetup() {
66 ClusterIdForPoint_.resize(Points_.size());
Clement Courbet96715412018-05-07 09:09:48 +000067 // Mark erroneous measurements out.
68 // All points must have the same number of dimensions, in the same order.
69 const std::vector<BenchmarkMeasure> *LastMeasurement = nullptr;
Clement Courbet37f0ca02018-05-15 12:08:00 +000070 for (size_t P = 0, NumPoints = Points_.size(); P < NumPoints; ++P) {
71 const auto &Point = Points_[P];
Clement Courbet96715412018-05-07 09:09:48 +000072 if (!Point.Error.empty()) {
73 ClusterIdForPoint_[P] = ClusterId::error();
74 ErrorCluster_.PointIndices.push_back(P);
75 continue;
76 }
77 const auto *CurMeasurement = &Point.Measurements;
78 if (LastMeasurement) {
79 if (LastMeasurement->size() != CurMeasurement->size()) {
80 return llvm::make_error<llvm::StringError>(
81 "inconsistent measurement dimensions",
82 llvm::inconvertibleErrorCode());
83 }
84 for (size_t I = 0, E = LastMeasurement->size(); I < E; ++I) {
85 if (LastMeasurement->at(I).Key != CurMeasurement->at(I).Key) {
86 return llvm::make_error<llvm::StringError>(
87 "inconsistent measurement dimensions keys",
88 llvm::inconvertibleErrorCode());
89 }
90 }
91 }
92 LastMeasurement = CurMeasurement;
93 }
94 if (LastMeasurement) {
95 NumDimensions_ = LastMeasurement->size();
96 }
97 return llvm::Error::success();
98}
99
Clement Courbet37f0ca02018-05-15 12:08:00 +0000100void InstructionBenchmarkClustering::dbScan(const size_t MinPts,
101 const double EpsilonSquared) {
102 for (size_t P = 0, NumPoints = Points_.size(); P < NumPoints; ++P) {
Clement Courbet96715412018-05-07 09:09:48 +0000103 if (!ClusterIdForPoint_[P].isUndef())
104 continue; // Previously processed in inner loop.
Clement Courbet37f0ca02018-05-15 12:08:00 +0000105 const auto Neighbors = rangeQuery(Points_, P, EpsilonSquared);
Clement Courbet96715412018-05-07 09:09:48 +0000106 if (Neighbors.size() + 1 < MinPts) { // Density check.
107 // The region around P is not dense enough to create a new cluster, mark
108 // as noise for now.
109 ClusterIdForPoint_[P] = ClusterId::noise();
110 continue;
111 }
112
113 // Create a new cluster, add P.
114 Clusters_.emplace_back(ClusterId::makeValid(Clusters_.size()));
115 Cluster &CurrentCluster = Clusters_.back();
116 ClusterIdForPoint_[P] = CurrentCluster.Id; /* Label initial point */
117 CurrentCluster.PointIndices.push_back(P);
118
119 // Process P's neighbors.
120 std::unordered_set<size_t> ToProcess(Neighbors.begin(), Neighbors.end());
121 while (!ToProcess.empty()) {
122 // Retrieve a point from the set.
123 const size_t Q = *ToProcess.begin();
124 ToProcess.erase(Q);
125
126 if (ClusterIdForPoint_[Q].isNoise()) {
127 // Change noise point to border point.
128 ClusterIdForPoint_[Q] = CurrentCluster.Id;
129 CurrentCluster.PointIndices.push_back(Q);
130 continue;
131 }
132 if (!ClusterIdForPoint_[Q].isUndef()) {
133 continue; // Previously processed.
134 }
135 // Add Q to the current custer.
136 ClusterIdForPoint_[Q] = CurrentCluster.Id;
137 CurrentCluster.PointIndices.push_back(Q);
138 // And extend to the neighbors of Q if the region is dense enough.
Clement Courbet37f0ca02018-05-15 12:08:00 +0000139 const auto Neighbors = rangeQuery(Points_, Q, EpsilonSquared);
Clement Courbet96715412018-05-07 09:09:48 +0000140 if (Neighbors.size() + 1 >= MinPts) {
141 ToProcess.insert(Neighbors.begin(), Neighbors.end());
142 }
143 }
144 }
145
146 // Add noisy points to noise cluster.
Clement Courbet37f0ca02018-05-15 12:08:00 +0000147 for (size_t P = 0, NumPoints = Points_.size(); P < NumPoints; ++P) {
Clement Courbet96715412018-05-07 09:09:48 +0000148 if (ClusterIdForPoint_[P].isNoise()) {
149 NoiseCluster_.PointIndices.push_back(P);
150 }
151 }
152}
153
154llvm::Expected<InstructionBenchmarkClustering>
155InstructionBenchmarkClustering::create(
156 const std::vector<InstructionBenchmark> &Points, const size_t MinPts,
157 const double Epsilon) {
Clement Courbet37f0ca02018-05-15 12:08:00 +0000158 InstructionBenchmarkClustering Clustering(Points);
159 if (auto Error = Clustering.validateAndSetup()) {
Clement Courbetcdb0eb82018-05-15 12:38:06 +0000160 return std::move(Error);
Clement Courbet96715412018-05-07 09:09:48 +0000161 }
162 if (Clustering.ErrorCluster_.PointIndices.size() == Points.size()) {
163 return Clustering; // Nothing to cluster.
164 }
165
Clement Courbet37f0ca02018-05-15 12:08:00 +0000166 Clustering.dbScan(MinPts, Epsilon * Epsilon);
Clement Courbet96715412018-05-07 09:09:48 +0000167 return Clustering;
168}
169
170} // namespace exegesis