Skip to content

Commit d9a7360

Browse files
committed
[vpr][route] make fit_model a private RoutingPredictor method
fit_model's callers are all RoutingPredictor members passing the same member vectors, so make it a private const method that reads them directly, leaving only the history factor as a parameter. Wrap the remaining file-local math helpers (LinearModel, variance, covariance, simple_linear_regression) in an anonymous namespace, fixing their accidental external linkage, and drop the now-unneeded forward declarations.
1 parent 8e8aff6 commit d9a7360

2 files changed

Lines changed: 19 additions & 23 deletions

File tree

vpr/src/route/routing_predictor.cpp

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
#include "routing_predictor.h"
99

10+
namespace {
11+
1012
class LinearModel {
1113
public:
1214
LinearModel(float slope = std::numeric_limits<float>::quiet_NaN(), float y_intercept = std::numeric_limits<float>::quiet_NaN())
@@ -35,15 +37,6 @@ class LinearModel {
3537
float y_intercept_;
3638
};
3739

38-
template<typename T>
39-
float variance(std::vector<float> values, float avg);
40-
41-
float covariance(const std::vector<size_t>& x_values, const std::vector<float>& y_values, float x_avg, float y_avg);
42-
LinearModel simple_linear_regression(const std::vector<size_t>& x_values, const std::vector<float>& y_values);
43-
t_routing_predictor_fit fit_model(const std::vector<size_t>& iterations,
44-
const std::vector<size_t>& overuse,
45-
float history_factor);
46-
4740
template<typename T>
4841
float variance(const std::vector<T>& values, float avg) {
4942
float var = 0;
@@ -65,11 +58,6 @@ float covariance(const std::vector<size_t>& x_values, const std::vector<float>&
6558
return cov;
6659
}
6760

68-
float RoutingPredictor::get_slope() const {
69-
//Return cached slope, computed in add_iteration_overuse()
70-
return slope_;
71-
}
72-
7361
LinearModel simple_linear_regression(const std::vector<size_t>& x_values, const std::vector<float>& y_values) {
7462
float y_avg = std::accumulate(y_values.begin(), y_values.end(), 0.) / y_values.size();
7563
float x_avg = std::accumulate(x_values.begin(), x_values.end(), 0.) / x_values.size();
@@ -83,9 +71,14 @@ LinearModel simple_linear_regression(const std::vector<size_t>& x_values, const
8371
return LinearModel(beta, alpha);
8472
}
8573

86-
t_routing_predictor_fit fit_model(const std::vector<size_t>& iterations,
87-
const std::vector<size_t>& overuse,
88-
float history_factor) {
74+
} // namespace
75+
76+
float RoutingPredictor::get_slope() const {
77+
//Return cached slope, computed in add_iteration_overuse()
78+
return slope_;
79+
}
80+
81+
t_routing_predictor_fit RoutingPredictor::fit_model_(float history_factor) const {
8982
//For pathfinder-based routing overuse tends to follow a negative-exponential:
9083
//
9184
// ^
@@ -138,15 +131,15 @@ t_routing_predictor_fit fit_model(const std::vector<size_t>& iterations,
138131
//(since the history inspected grows as the number of iterations increases,
139132
//later iterations use a longer history which helps reduce the noise caused by
140133
//small numbers of overused nodes)
141-
size_t start = overuse.size() - std::round(history_factor * overuse.size());
142-
size_t end = overuse.size();
134+
size_t start = iterations_.size() - std::round(history_factor * iterations_.size());
135+
size_t end = iterations_.size();
143136

144137
//Calculate the log overuse for the history we are interested in
145138
std::vector<float> hist_log_overuse;
146139
std::vector<size_t> hist_iters;
147140
for (size_t i = start; i < end; ++i) {
148-
hist_log_overuse.push_back(std::log(overuse[i]));
149-
hist_iters.push_back(iterations[i]);
141+
hist_log_overuse.push_back(std::log(iteration_overused_rr_node_counts_[i]));
142+
hist_iters.push_back(iterations_[i]);
150143
}
151144

152145
//We fit a linear model to the log of the overuse, this keeps the model simple but
@@ -210,7 +203,7 @@ float RoutingPredictor::estimate_overuse_slope() {
210203
float history_factor = FIXED_HISTORY_SIZE / iterations_.size(); //Fixed history size
211204

212205
if (iterations_.size() >= FIXED_HISTORY_SIZE) {
213-
t_routing_predictor_fit fit = fit_model(iterations_, iteration_overused_rr_node_counts_, history_factor);
206+
t_routing_predictor_fit fit = fit_model_(history_factor);
214207
LinearModel model(fit.slope, fit.y_intercept);
215208

216209
float log_curr_usage = model.find_y_for_x_value(*(--iterations_.end()));
@@ -236,7 +229,7 @@ void RoutingPredictor::add_iteration_overuse(size_t iteration, size_t overused_r
236229
last_fit_ = t_routing_predictor_fit();
237230
last_estimate_ = std::numeric_limits<float>::quiet_NaN();
238231
if (iterations_.size() > min_history_) {
239-
last_fit_ = fit_model(iterations_, iteration_overused_rr_node_counts_, history_factor_);
232+
last_fit_ = fit_model_(history_factor_);
240233
slope_ = last_fit_.slope;
241234

242235
LinearModel model(last_fit_.slope, last_fit_.y_intercept);

vpr/src/route/routing_predictor.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ class RoutingPredictor {
6060
///@brief True while safe mode is tolerating the predictor's initial run of degenerate fits
6161
bool awaiting_usable_prediction_() const;
6262

63+
///@brief Fits a linear model to the log of the last history_factor of the overuse history
64+
t_routing_predictor_fit fit_model_(float history_factor) const;
65+
6366
size_t min_history_;
6467
bool safe_mode_;
6568
int verbosity_;

0 commit comments

Comments
 (0)