blob: f61bfbc31607a5c2d903c2de3188df6969a36679 [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"
Clement Courbet96715412018-05-07 09:09:48 +000012#include <string>
Clement Courbet96715412018-05-07 09:09:48 +000013
Fangrui Song32401af2018-10-22 17:10:47 +000014namespace llvm {
Clement Courbet96715412018-05-07 09:09:48 +000015namespace exegesis {
16
17// The clustering problem has the following characteristics:
18// (A) - Low dimension (dimensions are typically proc resource units,
19// typically < 10).
20// (B) - Number of points : ~thousands (points are measurements of an MCInst)
21// (C) - Number of clusters: ~tens.
22// (D) - The number of clusters is not known /a priory/.
Clement Courbetdffc4ca2018-05-14 11:35:37 +000023// (E) - The amount of noise is relatively small.
Clement Courbet96715412018-05-07 09:09:48 +000024// The problem is rather small. In terms of algorithms, (D) disqualifies
25// k-means and makes algorithms such as DBSCAN[1] or OPTICS[2] more applicable.
26//
27// We've used DBSCAN here because it's simple to implement. This is a pretty
28// straightforward and inefficient implementation of the pseudocode in [2].
29//
30// [1] https://en.wikipedia.org/wiki/DBSCAN
31// [2] https://en.wikipedia.org/wiki/OPTICS_algorithm
32
Clement Courbet96715412018-05-07 09:09:48 +000033// Finds the points at distance less than sqrt(EpsilonSquared) of Q (not
34// including Q).
Clement Courbet72287212018-06-04 11:11:55 +000035std::vector<size_t>
36InstructionBenchmarkClustering::rangeQuery(const size_t Q) const {
Clement Courbet96715412018-05-07 09:09:48 +000037 std::vector<size_t> Neighbors;
Clement Courbet72287212018-06-04 11:11:55 +000038 const auto &QMeasurements = Points_[Q].Measurements;
39 for (size_t P = 0, NumPoints = Points_.size(); P < NumPoints; ++P) {
Clement Courbet96715412018-05-07 09:09:48 +000040 if (P == Q)
41 continue;
Clement Courbet72287212018-06-04 11:11:55 +000042 const auto &PMeasurements = Points_[P].Measurements;
Clement Courbet96715412018-05-07 09:09:48 +000043 if (PMeasurements.empty()) // Error point.
44 continue;
Clement Courbet72287212018-06-04 11:11:55 +000045 if (isNeighbour(PMeasurements, QMeasurements)) {
Clement Courbet96715412018-05-07 09:09:48 +000046 Neighbors.push_back(P);
47 }
48 }
49 return Neighbors;
50}
51
Clement Courbet72287212018-06-04 11:11:55 +000052bool InstructionBenchmarkClustering::isNeighbour(
53 const std::vector<BenchmarkMeasure> &P,
54 const std::vector<BenchmarkMeasure> &Q) const {
55 double DistanceSquared = 0.0;
56 for (size_t I = 0, E = P.size(); I < E; ++I) {
Clement Courbet684a5f62018-09-26 08:37:21 +000057 const auto Diff = P[I].PerInstructionValue - Q[I].PerInstructionValue;
Clement Courbet72287212018-06-04 11:11:55 +000058 DistanceSquared += Diff * Diff;
59 }
60 return DistanceSquared <= EpsilonSquared_;
61}
Clement Courbet96715412018-05-07 09:09:48 +000062
Clement Courbet37f0ca02018-05-15 12:08:00 +000063InstructionBenchmarkClustering::InstructionBenchmarkClustering(
Clement Courbet72287212018-06-04 11:11:55 +000064 const std::vector<InstructionBenchmark> &Points,
65 const double EpsilonSquared)
66 : Points_(Points), EpsilonSquared_(EpsilonSquared),
67 NoiseCluster_(ClusterId::noise()), ErrorCluster_(ClusterId::error()) {}
Clement Courbet96715412018-05-07 09:09:48 +000068
Clement Courbet37f0ca02018-05-15 12:08:00 +000069llvm::Error InstructionBenchmarkClustering::validateAndSetup() {
70 ClusterIdForPoint_.resize(Points_.size());
Clement Courbet96715412018-05-07 09:09:48 +000071 // Mark erroneous measurements out.
72 // All points must have the same number of dimensions, in the same order.
73 const std::vector<BenchmarkMeasure> *LastMeasurement = nullptr;
Clement Courbet37f0ca02018-05-15 12:08:00 +000074 for (size_t P = 0, NumPoints = Points_.size(); P < NumPoints; ++P) {
75 const auto &Point = Points_[P];
Clement Courbet96715412018-05-07 09:09:48 +000076 if (!Point.Error.empty()) {
77 ClusterIdForPoint_[P] = ClusterId::error();
78 ErrorCluster_.PointIndices.push_back(P);
79 continue;
80 }
81 const auto *CurMeasurement = &Point.Measurements;
82 if (LastMeasurement) {
83 if (LastMeasurement->size() != CurMeasurement->size()) {
84 return llvm::make_error<llvm::StringError>(
85 "inconsistent measurement dimensions",
86 llvm::inconvertibleErrorCode());
87 }
88 for (size_t I = 0, E = LastMeasurement->size(); I < E; ++I) {
89 if (LastMeasurement->at(I).Key != CurMeasurement->at(I).Key) {
90 return llvm::make_error<llvm::StringError>(
91 "inconsistent measurement dimensions keys",
92 llvm::inconvertibleErrorCode());
93 }
94 }
95 }
96 LastMeasurement = CurMeasurement;
97 }
98 if (LastMeasurement) {
99 NumDimensions_ = LastMeasurement->size();
100 }
101 return llvm::Error::success();
102}
103
Clement Courbet72287212018-06-04 11:11:55 +0000104void InstructionBenchmarkClustering::dbScan(const size_t MinPts) {
Clement Courbet37f0ca02018-05-15 12:08:00 +0000105 for (size_t P = 0, NumPoints = Points_.size(); P < NumPoints; ++P) {
Clement Courbet96715412018-05-07 09:09:48 +0000106 if (!ClusterIdForPoint_[P].isUndef())
107 continue; // Previously processed in inner loop.
Clement Courbet72287212018-06-04 11:11:55 +0000108 const auto Neighbors = rangeQuery(P);
Clement Courbet96715412018-05-07 09:09:48 +0000109 if (Neighbors.size() + 1 < MinPts) { // Density check.
110 // The region around P is not dense enough to create a new cluster, mark
111 // as noise for now.
112 ClusterIdForPoint_[P] = ClusterId::noise();
113 continue;
114 }
115
116 // Create a new cluster, add P.
117 Clusters_.emplace_back(ClusterId::makeValid(Clusters_.size()));
118 Cluster &CurrentCluster = Clusters_.back();
119 ClusterIdForPoint_[P] = CurrentCluster.Id; /* Label initial point */
120 CurrentCluster.PointIndices.push_back(P);
121
122 // Process P's neighbors.
Roman Lebedev0b4b5122018-11-19 13:28:09 +0000123 llvm::SetVector<size_t> ToProcess;
124 ToProcess.insert(Neighbors.begin(), Neighbors.end());
Clement Courbet96715412018-05-07 09:09:48 +0000125 while (!ToProcess.empty()) {
126 // Retrieve a point from the set.
127 const size_t Q = *ToProcess.begin();
Roman Lebedev0b4b5122018-11-19 13:28:09 +0000128 ToProcess.erase(ToProcess.begin());
Clement Courbet96715412018-05-07 09:09:48 +0000129
130 if (ClusterIdForPoint_[Q].isNoise()) {
131 // Change noise point to border point.
132 ClusterIdForPoint_[Q] = CurrentCluster.Id;
133 CurrentCluster.PointIndices.push_back(Q);
134 continue;
135 }
136 if (!ClusterIdForPoint_[Q].isUndef()) {
137 continue; // Previously processed.
138 }
139 // Add Q to the current custer.
140 ClusterIdForPoint_[Q] = CurrentCluster.Id;
141 CurrentCluster.PointIndices.push_back(Q);
142 // And extend to the neighbors of Q if the region is dense enough.
Clement Courbet72287212018-06-04 11:11:55 +0000143 const auto Neighbors = rangeQuery(Q);
Clement Courbet96715412018-05-07 09:09:48 +0000144 if (Neighbors.size() + 1 >= MinPts) {
145 ToProcess.insert(Neighbors.begin(), Neighbors.end());
146 }
147 }
148 }
149
150 // Add noisy points to noise cluster.
Clement Courbet37f0ca02018-05-15 12:08:00 +0000151 for (size_t P = 0, NumPoints = Points_.size(); P < NumPoints; ++P) {
Clement Courbet96715412018-05-07 09:09:48 +0000152 if (ClusterIdForPoint_[P].isNoise()) {
153 NoiseCluster_.PointIndices.push_back(P);
154 }
155 }
156}
157
158llvm::Expected<InstructionBenchmarkClustering>
159InstructionBenchmarkClustering::create(
160 const std::vector<InstructionBenchmark> &Points, const size_t MinPts,
161 const double Epsilon) {
Clement Courbet72287212018-06-04 11:11:55 +0000162 InstructionBenchmarkClustering Clustering(Points, Epsilon * Epsilon);
Clement Courbet37f0ca02018-05-15 12:08:00 +0000163 if (auto Error = Clustering.validateAndSetup()) {
Clement Courbetcdb0eb82018-05-15 12:38:06 +0000164 return std::move(Error);
Clement Courbet96715412018-05-07 09:09:48 +0000165 }
166 if (Clustering.ErrorCluster_.PointIndices.size() == Points.size()) {
167 return Clustering; // Nothing to cluster.
168 }
169
Clement Courbet72287212018-06-04 11:11:55 +0000170 Clustering.dbScan(MinPts);
Clement Courbet96715412018-05-07 09:09:48 +0000171 return Clustering;
172}
173
174} // namespace exegesis
Fangrui Song32401af2018-10-22 17:10:47 +0000175} // namespace llvm