blob: 360966a94789c76b0b56fab38914560269bc18ef [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 Lebedev666d8552018-11-19 13:28:31 +000036void InstructionBenchmarkClustering::rangeQuery(
37 const size_t Q, llvm::SmallVectorImpl<size_t> &Neighbors) const {
38 Neighbors.clear();
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 }
Clement Courbet96715412018-05-07 09:09:48 +000050}
51
Clement Courbet37f0ca02018-05-15 12:08:00 +000052InstructionBenchmarkClustering::InstructionBenchmarkClustering(
Clement Courbet72287212018-06-04 11:11:55 +000053 const std::vector<InstructionBenchmark> &Points,
54 const double EpsilonSquared)
55 : Points_(Points), EpsilonSquared_(EpsilonSquared),
56 NoiseCluster_(ClusterId::noise()), ErrorCluster_(ClusterId::error()) {}
Clement Courbet96715412018-05-07 09:09:48 +000057
Clement Courbet37f0ca02018-05-15 12:08:00 +000058llvm::Error InstructionBenchmarkClustering::validateAndSetup() {
59 ClusterIdForPoint_.resize(Points_.size());
Clement Courbet96715412018-05-07 09:09:48 +000060 // Mark erroneous measurements out.
61 // All points must have the same number of dimensions, in the same order.
62 const std::vector<BenchmarkMeasure> *LastMeasurement = nullptr;
Clement Courbet37f0ca02018-05-15 12:08:00 +000063 for (size_t P = 0, NumPoints = Points_.size(); P < NumPoints; ++P) {
64 const auto &Point = Points_[P];
Clement Courbet96715412018-05-07 09:09:48 +000065 if (!Point.Error.empty()) {
66 ClusterIdForPoint_[P] = ClusterId::error();
67 ErrorCluster_.PointIndices.push_back(P);
68 continue;
69 }
70 const auto *CurMeasurement = &Point.Measurements;
71 if (LastMeasurement) {
72 if (LastMeasurement->size() != CurMeasurement->size()) {
73 return llvm::make_error<llvm::StringError>(
74 "inconsistent measurement dimensions",
75 llvm::inconvertibleErrorCode());
76 }
77 for (size_t I = 0, E = LastMeasurement->size(); I < E; ++I) {
78 if (LastMeasurement->at(I).Key != CurMeasurement->at(I).Key) {
79 return llvm::make_error<llvm::StringError>(
80 "inconsistent measurement dimensions keys",
81 llvm::inconvertibleErrorCode());
82 }
83 }
84 }
85 LastMeasurement = CurMeasurement;
86 }
87 if (LastMeasurement) {
88 NumDimensions_ = LastMeasurement->size();
89 }
90 return llvm::Error::success();
91}
92
Clement Courbet72287212018-06-04 11:11:55 +000093void InstructionBenchmarkClustering::dbScan(const size_t MinPts) {
Roman Lebedev666d8552018-11-19 13:28:31 +000094 llvm::SmallVector<size_t, 0> Neighbors; // Persistent buffer to avoid allocs.
Clement Courbet37f0ca02018-05-15 12:08:00 +000095 for (size_t P = 0, NumPoints = Points_.size(); P < NumPoints; ++P) {
Clement Courbet96715412018-05-07 09:09:48 +000096 if (!ClusterIdForPoint_[P].isUndef())
97 continue; // Previously processed in inner loop.
Roman Lebedev666d8552018-11-19 13:28:31 +000098 rangeQuery(P, Neighbors);
Clement Courbet96715412018-05-07 09:09:48 +000099 if (Neighbors.size() + 1 < MinPts) { // Density check.
100 // The region around P is not dense enough to create a new cluster, mark
101 // as noise for now.
102 ClusterIdForPoint_[P] = ClusterId::noise();
103 continue;
104 }
105
106 // Create a new cluster, add P.
107 Clusters_.emplace_back(ClusterId::makeValid(Clusters_.size()));
108 Cluster &CurrentCluster = Clusters_.back();
109 ClusterIdForPoint_[P] = CurrentCluster.Id; /* Label initial point */
110 CurrentCluster.PointIndices.push_back(P);
111
112 // Process P's neighbors.
Roman Lebedev5c5b1ea2018-11-19 13:28:26 +0000113 llvm::SetVector<size_t, std::deque<size_t>> ToProcess;
Roman Lebedev0b4b5122018-11-19 13:28:09 +0000114 ToProcess.insert(Neighbors.begin(), Neighbors.end());
Clement Courbet96715412018-05-07 09:09:48 +0000115 while (!ToProcess.empty()) {
116 // Retrieve a point from the set.
117 const size_t Q = *ToProcess.begin();
Roman Lebedev0b4b5122018-11-19 13:28:09 +0000118 ToProcess.erase(ToProcess.begin());
Clement Courbet96715412018-05-07 09:09:48 +0000119
120 if (ClusterIdForPoint_[Q].isNoise()) {
121 // Change noise point to border point.
122 ClusterIdForPoint_[Q] = CurrentCluster.Id;
123 CurrentCluster.PointIndices.push_back(Q);
124 continue;
125 }
126 if (!ClusterIdForPoint_[Q].isUndef()) {
127 continue; // Previously processed.
128 }
129 // Add Q to the current custer.
130 ClusterIdForPoint_[Q] = CurrentCluster.Id;
131 CurrentCluster.PointIndices.push_back(Q);
132 // And extend to the neighbors of Q if the region is dense enough.
Roman Lebedev666d8552018-11-19 13:28:31 +0000133 rangeQuery(Q, Neighbors);
Clement Courbet96715412018-05-07 09:09:48 +0000134 if (Neighbors.size() + 1 >= MinPts) {
135 ToProcess.insert(Neighbors.begin(), Neighbors.end());
136 }
137 }
138 }
139
140 // Add noisy points to noise cluster.
Clement Courbet37f0ca02018-05-15 12:08:00 +0000141 for (size_t P = 0, NumPoints = Points_.size(); P < NumPoints; ++P) {
Clement Courbet96715412018-05-07 09:09:48 +0000142 if (ClusterIdForPoint_[P].isNoise()) {
143 NoiseCluster_.PointIndices.push_back(P);
144 }
145 }
146}
147
148llvm::Expected<InstructionBenchmarkClustering>
149InstructionBenchmarkClustering::create(
150 const std::vector<InstructionBenchmark> &Points, const size_t MinPts,
151 const double Epsilon) {
Clement Courbet72287212018-06-04 11:11:55 +0000152 InstructionBenchmarkClustering Clustering(Points, Epsilon * Epsilon);
Clement Courbet37f0ca02018-05-15 12:08:00 +0000153 if (auto Error = Clustering.validateAndSetup()) {
Clement Courbetcdb0eb82018-05-15 12:38:06 +0000154 return std::move(Error);
Clement Courbet96715412018-05-07 09:09:48 +0000155 }
156 if (Clustering.ErrorCluster_.PointIndices.size() == Points.size()) {
157 return Clustering; // Nothing to cluster.
158 }
159
Clement Courbet72287212018-06-04 11:11:55 +0000160 Clustering.dbScan(MinPts);
Clement Courbet96715412018-05-07 09:09:48 +0000161 return Clustering;
162}
163
164} // namespace exegesis
Fangrui Song32401af2018-10-22 17:10:47 +0000165} // namespace llvm