Skip to content

Commit 1918f90

Browse files
committed
rmp: restructure, clang-tidy
Signed-off-by: Bartłomiej Chmiel <[email protected]>
1 parent 36929da commit 1918f90

14 files changed

+317
-242
lines changed

src/rmp/include/rmp/Restructure.h

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,21 @@ class Restructure
8989
annealing_revert_after_ = revert_after;
9090
}
9191
void setAnnealingInitialOps(unsigned ops) { annealing_init_ops_ = ops; }
92-
void setGeneticSeed(std::mt19937::result_type seed)
93-
{
94-
genetic_seed_ = seed;
95-
}
92+
void setGeneticSeed(std::mt19937::result_type seed) { genetic_seed_ = seed; }
9693
void setGeneticPopSize(unsigned pop_size) { genetic_pop_size_ = pop_size; }
9794
void setGeneticMutProb(float mut_prob) { genetic_mut_prob_ = mut_prob; }
98-
void setGeneticCrossProb(float cross_prob) { genetic_cross_prob_ = cross_prob; }
99-
void setGeneticTournSize(unsigned tourn_size) { genetic_tourn_size_ = tourn_size; }
100-
void setGeneticTournProb(float tourn_prob) { genetic_tourn_prob_ = tourn_prob; }
95+
void setGeneticCrossProb(float cross_prob)
96+
{
97+
genetic_cross_prob_ = cross_prob;
98+
}
99+
void setGeneticTournSize(unsigned tourn_size)
100+
{
101+
genetic_tourn_size_ = tourn_size;
102+
}
103+
void setGeneticTournProb(float tourn_prob)
104+
{
105+
genetic_tourn_prob_ = tourn_prob;
106+
}
101107
void setGeneticIters(unsigned iters) { genetic_iters_ = iters; }
102108
void setGeneticInitialOps(unsigned ops) { genetic_init_ops_ = ops; }
103109
void setSlackThreshold(sta::Slack thresh) { slack_threshold_ = thresh; }

src/rmp/src/annealing_strategy.cpp

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33

44
#include "annealing_strategy.h"
55

6-
#include <fcntl.h>
7-
8-
#include <algorithm>
96
#include <cassert>
7+
#include <cmath>
108
#include <cstddef>
119
#include <cstdio>
10+
#include <random>
11+
#include <utility>
1212
#include <vector>
1313

1414
#include "cut/abc_library_factory.h"
@@ -17,10 +17,10 @@
1717
#include "gia.h"
1818
#include "odb/db.h"
1919
#include "rsz/Resizer.hh"
20+
#include "slack_tuning_strategy.h"
2021
#include "sta/Delay.hh"
2122
#include "sta/Graph.hh"
2223
#include "sta/MinMax.hh"
23-
#include "utils.h"
2424
#include "utl/Logger.h"
2525
#include "utl/deleter.h"
2626
#include "utl/unique_name.h"
@@ -50,15 +50,14 @@ std::vector<GiaOp> AnnealingStrategy::RunStrategy(
5050
}
5151

5252
SolutionSlack sol_slack;
53-
sol_slack.solution = ops;
54-
auto* worst_vertex = EvaluateSlack(sol_slack,
55-
candidate_vertices,
56-
abc_library,
57-
corner_,
58-
sta,
59-
name_generator,
60-
logger);
61-
float worst_slack = *sol_slack.worst_slack;
53+
sol_slack.solution_ = ops;
54+
auto* worst_vertex = sol_slack.Evaluate(
55+
candidate_vertices, abc_library, corner_, sta, name_generator, logger);
56+
57+
if (!sol_slack.worst_slack_) {
58+
logger->error(RMP, 51, "Should be evaluated");
59+
}
60+
float worst_slack = *sol_slack.worst_slack_;
6261

6362
if (!temperature_) {
6463
sta::Delay required = sta->vertexRequired(worst_vertex, sta::MinMax::max());
@@ -72,8 +71,7 @@ std::vector<GiaOp> AnnealingStrategy::RunStrategy(
7271
*temperature_,
7372
worst_slack);
7473

75-
float best_worst_slack = worst_slack;
76-
auto best_ops = ops;
74+
SolutionSlack best_sol{.solution_ = ops, .worst_slack_ = worst_slack};
7775
size_t worse_iters = 0;
7876

7977
for (unsigned i = 0; i < iterations_; i++) {
@@ -82,8 +80,8 @@ std::vector<GiaOp> AnnealingStrategy::RunStrategy(
8280

8381
if (revert_after_ && worse_iters >= *revert_after_) {
8482
logger->info(RMP, 57, "Reverting to the best found solution");
85-
ops = best_ops;
86-
worst_slack = best_worst_slack;
83+
ops = best_sol.solution_;
84+
worst_slack = *best_sol.worst_slack_;
8785
worse_iters = 0;
8886
}
8987

@@ -93,7 +91,7 @@ std::vector<GiaOp> AnnealingStrategy::RunStrategy(
9391
"Iteration: {}, temperature: {}, best worst slack: {}",
9492
i + 1,
9593
current_temp,
96-
best_worst_slack);
94+
*best_sol.worst_slack_);
9795
} else {
9896
debugPrint(logger,
9997
RMP,
@@ -102,23 +100,23 @@ std::vector<GiaOp> AnnealingStrategy::RunStrategy(
102100
"Iteration: {}, temperature: {}, best worst slack: {}",
103101
i + 1,
104102
current_temp,
105-
best_worst_slack);
103+
best_sol.worst_slack_ ? *best_sol.worst_slack_ : 0);
106104
}
107105

108-
auto new_ops = RandomNeighbor(ops, all_ops, logger);
109-
sol_slack.solution = new_ops;
106+
SolutionSlack s;
107+
s.solution_ = ops;
108+
auto new_ops = s.RandomNeighbor(all_ops, logger, random_);
109+
sol_slack.solution_ = new_ops;
110110

111-
EvaluateSlack(sol_slack,
112-
candidate_vertices,
113-
abc_library,
114-
corner_,
115-
sta,
116-
name_generator,
117-
logger);
111+
sol_slack.Evaluate(
112+
candidate_vertices, abc_library, corner_, sta, name_generator, logger);
118113

119-
float worst_slack_new = *sol_slack.worst_slack;
114+
if (!sol_slack.worst_slack_) {
115+
logger->error(RMP, 55, "Should be evaluated");
116+
}
117+
float worst_slack_new = *sol_slack.worst_slack_;
120118

121-
if (worst_slack_new < best_worst_slack) {
119+
if (best_sol.worst_slack_ && worst_slack_new < *best_sol.worst_slack_) {
122120
worse_iters++;
123121
} else {
124122
worse_iters = 0;
@@ -167,12 +165,12 @@ std::vector<GiaOp> AnnealingStrategy::RunStrategy(
167165
ops = std::move(new_ops);
168166
worst_slack = worst_slack_new;
169167

170-
if (worst_slack > best_worst_slack) {
171-
best_worst_slack = worst_slack;
172-
best_ops = ops;
168+
if (best_sol.worst_slack_ && worst_slack > *best_sol.worst_slack_) {
169+
*best_sol.worst_slack_ = worst_slack;
170+
best_sol.solution_ = ops;
173171
}
174172
}
175-
return best_ops;
173+
return best_sol.solution_;
176174
}
177175

178176
} // namespace rmp

src/rmp/src/annealing_strategy.h

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,28 @@
55

66
#include <optional>
77
#include <random>
8+
#include <vector>
89

9-
#include "db_sta/dbSta.hh"
10-
#include "rsz/Resizer.hh"
10+
#include "cut/abc_library_factory.h"
11+
#include "gia.h"
1112
#include "slack_tuning_strategy.h"
12-
#include "sta/Corner.hh"
1313
#include "sta/Delay.hh"
14-
#include "utl/Logger.h"
1514
#include "utl/unique_name.h"
1615

16+
namespace rsz {
17+
class Resizer;
18+
} // namespace rsz
19+
20+
namespace sta {
21+
class dbSta;
22+
class Corner;
23+
class Vertex;
24+
} // namespace sta
25+
26+
namespace utl {
27+
class Logger;
28+
} // namespace utl
29+
1730
namespace rmp {
1831

1932
class AnnealingStrategy final : public SlackTuningStrategy
@@ -27,10 +40,10 @@ class AnnealingStrategy final : public SlackTuningStrategy
2740
std::optional<unsigned> revert_after,
2841
unsigned initial_ops)
2942
: SlackTuningStrategy(corner,
30-
slack_threshold,
31-
seed,
32-
iterations,
33-
initial_ops),
43+
slack_threshold,
44+
seed,
45+
iterations,
46+
initial_ops),
3447
temperature_(temperature),
3548
revert_after_(revert_after)
3649
{

src/rmp/src/genetic_strategy.cpp

Lines changed: 35 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33

44
#include "genetic_strategy.h"
55

6-
#include <fcntl.h>
7-
86
#include <algorithm>
7+
#include <cstddef>
8+
#include <iterator>
9+
#include <random>
10+
#include <ranges>
911
#include <unordered_set>
12+
#include <utility>
1013
#include <vector>
1114

1215
#include "boost/container_hash/hash.hpp"
@@ -15,10 +18,7 @@
1518
#include "db_sta/dbSta.hh"
1619
#include "gia.h"
1720
#include "rsz/Resizer.hh"
18-
#include "sta/Graph.hh"
19-
#include "sta/MinMax.hh"
20-
#include "sta/Search.hh"
21-
#include "utils.h"
21+
#include "slack_tuning_strategy.h"
2222
#include "utl/Logger.h"
2323
#include "utl/deleter.h"
2424
#include "utl/unique_name.h"
@@ -31,7 +31,7 @@ static void removeDuplicates(std::vector<SolutionSlack>& population,
3131
{
3232
struct HashVector final
3333
{
34-
size_t operator()(const Solution& sol) const
34+
size_t operator()(const SolutionSlack::Type& sol) const
3535
{
3636
size_t res = 0;
3737
for (const auto& item : sol) {
@@ -43,20 +43,19 @@ static void removeDuplicates(std::vector<SolutionSlack>& population,
4343
}
4444
};
4545

46-
std::unordered_set<Solution, HashVector> taken;
46+
std::unordered_set<SolutionSlack::Type, HashVector> taken;
4747
population.erase(
48-
std::remove_if(
49-
population.begin(),
50-
population.end(),
48+
std::ranges::begin(std::ranges::remove_if(
49+
population,
5150
[&taken, logger](const SolutionSlack& s) {
52-
if (!taken.insert(s.solution).second) {
51+
if (!taken.insert(s.solution_).second) {
5352
debugPrint(
5453
logger, RMP, "genetic", 2, "Removing: " + s.toString());
5554
return true;
5655
}
5756
debugPrint(logger, RMP, "genetic", 2, "Keeping: " + s.toString());
5857
return false;
59-
}),
58+
})),
6059
population.end());
6160
}
6261

@@ -77,25 +76,15 @@ std::vector<GiaOp> GeneticStrategy::RunStrategy(
7776
"Generating and evaluating the initial population");
7877
std::vector<SolutionSlack> population(pop_size_);
7978
for (auto& ind : population) {
80-
ind.solution.reserve(initial_ops_);
79+
ind.solution_.reserve(initial_ops_);
8180
for (size_t i = 0; i < initial_ops_; i++) {
82-
ind.solution.push_back(all_ops[random_() % all_ops.size()]);
81+
ind.solution_.push_back(all_ops[random_() % all_ops.size()]);
8382
}
8483
}
8584

86-
logger->info(RMP,
87-
62,
88-
"Resynthesis: starting genetic algorithm, Worst slack is {}",
89-
GetWorstSlack(sta, corner_));
90-
9185
for (auto& candidate : population) {
92-
EvaluateSlack(candidate,
93-
candidate_vertices,
94-
abc_library,
95-
corner_,
96-
sta,
97-
name_generator,
98-
logger);
86+
candidate.Evaluate(
87+
candidate_vertices, abc_library, corner_, sta, name_generator, logger);
9988

10089
debugPrint(logger, RMP, "genetic", 1, candidate.toString());
10190
}
@@ -111,51 +100,50 @@ std::vector<GiaOp> GeneticStrategy::RunStrategy(
111100
if (rand1 == rand2) {
112101
continue;
113102
}
114-
Solution& parent1_sol = population[rand1].solution;
115-
Solution& parent2_sol = population[rand2].solution;
116-
Solution child_sol(parent1_sol.begin(),
117-
parent1_sol.begin() + parent1_sol.size() / 2);
103+
SolutionSlack::Type& parent1_sol = population[rand1].solution_;
104+
SolutionSlack::Type& parent2_sol = population[rand2].solution_;
105+
SolutionSlack::Type child_sol(
106+
parent1_sol.begin(), parent1_sol.begin() + parent1_sol.size() / 2);
118107
child_sol.insert(child_sol.end(),
119108
parent2_sol.begin() + parent2_sol.size() / 2,
120109
parent2_sol.end());
121110
SolutionSlack child_sol_slack;
122-
child_sol_slack.solution = std::move(child_sol);
111+
child_sol_slack.solution_ = std::move(child_sol);
123112
population.emplace_back(child_sol_slack);
124113
}
125114
// Mutations
126115
unsigned mut_size = std::max<unsigned>(mut_prob_ * generation_size, 1);
127116
for (unsigned j = 0; j < mut_size; j++) {
128117
SolutionSlack sol_slack;
129118
auto rand = random_() % generation_size;
130-
sol_slack.solution
131-
= RandomNeighbor(population[rand].solution, all_ops, logger);
119+
sol_slack.solution_
120+
= population[rand].RandomNeighbor(all_ops, logger, random_);
132121
population.emplace_back(sol_slack);
133122
}
134123
removeDuplicates(population, logger);
135124
// Evaluation
136125
for (auto& sol_slack : population) {
137-
if (sol_slack.worst_slack) {
126+
if (sol_slack.worst_slack_) {
138127
continue;
139128
}
140-
EvaluateSlack(sol_slack,
141-
candidate_vertices,
142-
abc_library,
143-
corner_,
144-
sta,
145-
name_generator,
146-
logger);
129+
sol_slack.Evaluate(candidate_vertices,
130+
abc_library,
131+
corner_,
132+
sta,
133+
name_generator,
134+
logger);
147135
}
148136
// Selection
149-
std::sort(population.begin(), population.end());
137+
std::ranges::sort(population, std::less{});
150138
std::vector<SolutionSlack> newPopulation;
151139
newPopulation.reserve(pop_size_);
152140
for (int j = 0; j < pop_size_; j++) {
153141
std::vector<size_t> tournament(tourn_size_);
154142
std::generate_n(tournament.begin(), tourn_size_, [&]() {
155143
return random_() % population.size();
156144
});
157-
std::sort(tournament.begin(), tournament.end());
158-
tournament.erase(std::unique(tournament.begin(), tournament.end()),
145+
std::ranges::sort(tournament);
146+
tournament.erase(std::ranges::begin(std::ranges::unique(tournament)),
159147
tournament.end());
160148
std::bernoulli_distribution bern_dist{tourn_prob_};
161149
for (const auto& candidateId : tournament) {
@@ -173,11 +161,11 @@ std::vector<GiaOp> GeneticStrategy::RunStrategy(
173161
}
174162
}
175163

176-
auto best_it = std::min_element(population.begin(), population.end());
164+
auto best_it = std::ranges::min_element(population, std::less{});
177165
logger->info(RMP,
178166
66,
179167
"Resynthesis: Best result is of individual {}",
180168
std::distance(population.begin(), best_it));
181-
return best_it->solution;
169+
return best_it->solution_;
182170
}
183171
} // namespace rmp

0 commit comments

Comments
 (0)