blob: 56b1a939c41cde207c63c8008569da19692f2ba8 [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 Lebedev8aecb0c2018-11-19 13:28:22 +000011#include "llvm/ADT/SmallVector.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).
Roman Lebedev666d8552018-11-19 13:28:31 +000035void InstructionBenchmarkClustering::rangeQuery(
Roman Lebedev71fdb572018-11-19 13:28:41 +000036 const size_t Q, std::vector<size_t> &Neighbors) const {
Roman Lebedev666d8552018-11-19 13:28:31 +000037 Neighbors.clear();
Roman Lebedev71fdb572018-11-19 13:28:41 +000038 Neighbors.reserve(Points_.size() - 1); // The Q itself isn't a neighbor.
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) {
Fangrui Songcd93d7e2018-12-23 20:48:52 +000094 const size_t NumPoints = Points_.size();
95
96 // Persistent buffers to avoid allocs.
97 std::vector<size_t> Neighbors;
98 std::vector<size_t> ToProcess(NumPoints);
99 std::vector<char> Processed(NumPoints);
100
101 for (size_t P = 0; P < NumPoints; ++P) {
Clement Courbet96715412018-05-07 09:09:48 +0000102 if (!ClusterIdForPoint_[P].isUndef())
103 continue; // Previously processed in inner loop.
Roman Lebedev666d8552018-11-19 13:28:31 +0000104 rangeQuery(P, Neighbors);
Clement Courbet96715412018-05-07 09:09:48 +0000105 if (Neighbors.size() + 1 < MinPts) { // Density check.
106 // The region around P is not dense enough to create a new cluster, mark
107 // as noise for now.
108 ClusterIdForPoint_[P] = ClusterId::noise();
109 continue;
110 }
111
112 // Create a new cluster, add P.
113 Clusters_.emplace_back(ClusterId::makeValid(Clusters_.size()));
114 Cluster &CurrentCluster = Clusters_.back();
115 ClusterIdForPoint_[P] = CurrentCluster.Id; /* Label initial point */
116 CurrentCluster.PointIndices.push_back(P);
Fangrui Songcd93d7e2018-12-23 20:48:52 +0000117 Processed[P] = 1;
Clement Courbet96715412018-05-07 09:09:48 +0000118
Fangrui Songcd93d7e2018-12-23 20:48:52 +0000119 // Enqueue P's neighbors.
120 size_t Tail = 0;
121 auto EnqueueUnprocessed = [&](const std::vector<size_t> &Neighbors) {
122 for (size_t Q : Neighbors)
123 if (!Processed[Q]) {
124 ToProcess[Tail++] = Q;
125 Processed[Q] = 1;
126 }
127 };
128 EnqueueUnprocessed(Neighbors);
Clement Courbet96715412018-05-07 09:09:48 +0000129
Fangrui Songcd93d7e2018-12-23 20:48:52 +0000130 for (size_t Head = 0; Head < Tail; ++Head) {
131 // Retrieve a point from the queue and add it to the current cluster.
132 P = ToProcess[Head];
133 ClusterId OldCID = ClusterIdForPoint_[P];
134 ClusterIdForPoint_[P] = CurrentCluster.Id;
135 CurrentCluster.PointIndices.push_back(P);
136 if (OldCID.isNoise())
Clement Courbet96715412018-05-07 09:09:48 +0000137 continue;
Fangrui Songcd93d7e2018-12-23 20:48:52 +0000138 assert(OldCID.isUndef());
139
140 // And extend to the neighbors of P if the region is dense enough.
141 rangeQuery(P, Neighbors);
142 if (Neighbors.size() + 1 >= MinPts)
143 EnqueueUnprocessed(Neighbors);
Clement Courbet96715412018-05-07 09:09:48 +0000144 }
145 }
146
147 // Add noisy points to noise cluster.
Fangrui Songcd93d7e2018-12-23 20:48:52 +0000148 for (size_t P = 0; P < NumPoints; ++P)
149 if (ClusterIdForPoint_[P].isNoise())
Clement Courbet96715412018-05-07 09:09:48 +0000150 NoiseCluster_.PointIndices.push_back(P);
Clement Courbet96715412018-05-07 09:09:48 +0000151}
152
153llvm::Expected<InstructionBenchmarkClustering>
154InstructionBenchmarkClustering::create(
155 const std::vector<InstructionBenchmark> &Points, const size_t MinPts,
156 const double Epsilon) {
Clement Courbet72287212018-06-04 11:11:55 +0000157 InstructionBenchmarkClustering Clustering(Points, Epsilon * Epsilon);
Clement Courbet37f0ca02018-05-15 12:08:00 +0000158 if (auto Error = Clustering.validateAndSetup()) {
Clement Courbetcdb0eb82018-05-15 12:38:06 +0000159 return std::move(Error);
Clement Courbet96715412018-05-07 09:09:48 +0000160 }
161 if (Clustering.ErrorCluster_.PointIndices.size() == Points.size()) {
162 return Clustering; // Nothing to cluster.
163 }
164
Clement Courbet72287212018-06-04 11:11:55 +0000165 Clustering.dbScan(MinPts);
Clement Courbet96715412018-05-07 09:09:48 +0000166 return Clustering;
167}
168
169} // namespace exegesis
Fangrui Song32401af2018-10-22 17:10:47 +0000170} // namespace llvm