Skip to content

Commit 46ed168

Browse files
committed
rmp: review fixes
Signed-off-by: Bartłomiej Chmiel <[email protected]>
1 parent f392c9e commit 46ed168

17 files changed

+582
-538
lines changed

src/rmp/BUILD

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,13 @@ cc_library(
5353
"//third-party/abc:abc-lib",
5454
"@boost.bind",
5555
"@boost.config",
56-
"@boost.container_hash",
5756
"@boost.fusion",
5857
"@boost.lambda",
5958
"@boost.optional",
6059
"@boost.phoenix",
6160
"@boost.spirit",
61+
"@com_google_absl//absl/hash",
62+
"@com_google_absl//absl/random",
6263
],
6364
)
6465

src/rmp/README.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,41 @@ resynth_annealing
9898

9999
### Resynth with genetic slack tuning
100100

101-
TODO
101+
Resynthesize parts of the design with an ABC script found via genetic algorithm.
102+
An individual in a population is a series of operations on ABC's internal AIG data structure.
103+
Each such operation is considered a gene. Genotype can be changed by a mutation with operations such
104+
as adding, removing or swapping genes with a `mut_prob` probability. Individual can also be changed
105+
by crossing two genes together with a `cross_prob` probability.
106+
The optimization function is defined as the worst slack.
107+
108+
```tcl
109+
resynth_genetic
110+
[-corner corner]
111+
[-slack_threshold slack_threshold]
112+
[-seed seed]
113+
[-pop_size pop_size]
114+
[-mut_prob mut_prob]
115+
[-cross_prob cross_prob]
116+
[-tourn_prob tourn_prob]
117+
[-tourn_size tourn_size]
118+
[-iters iters]
119+
[-initial_ops initial_ops]
120+
```
121+
122+
#### Options
123+
124+
| Switch Name | Description |
125+
| ----- | ----- |
126+
| `-corner` | Process corner to use. |
127+
| `-slack_threshold` | Specifies a (setup) timing slack value below which timing paths need to be analyzed for restructuring. The default value is `0`. |
128+
| `-seed` | Seed to use for randomness. |
129+
| `-pop_size` | Population size. |
130+
| `-mut_prob` | Probability of applying mutation operator. |
131+
| `-cross_prob` | Probability of applying crossover operator. |
132+
| `-tourn_prob` | Tournament probability. |
133+
| `-tourn_size` | Tournament size. |
134+
| `-iters` | Number of iterations to run genetic algorithm. |
135+
| `-initial_ops` | Size of the initial random solution (number of commands in the script for ABC). |
102136

103137

104138
## Example scripts

src/rmp/src/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,13 @@ target_include_directories(rmp
4646
# Unfortunate side-effect of swig include StaTcl.i
4747
${OPENSTA_HOME}/include/sta
4848
${OPENSTA_HOME}
49-
${Boost_INCLUDE_DIRS}
5049
)
5150

5251
target_include_directories(rmp_lib
5352
PUBLIC
5453
../include
5554
PRIVATE
5655
.
57-
${Boost_INCLUDE_DIRS}
5856
)
5957

6058
target_link_libraries(rmp_lib
@@ -66,6 +64,7 @@ target_link_libraries(rmp_lib
6664
utl_lib
6765
cut
6866
${ABC_LIBRARY}
67+
absl_hash
6968
)
7069

7170
target_link_libraries(rmp

src/rmp/src/annealing_strategy.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,6 @@
1313
#include "sta/Delay.hh"
1414
#include "utl/unique_name.h"
1515

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-
3016
namespace rmp {
3117

3218
class AnnealingStrategy final : public SlackTuningStrategy

src/rmp/src/genetic_strategy.cpp

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
#include <algorithm>
77
#include <cstddef>
88
#include <iterator>
9-
#include <random>
109
#include <ranges>
1110
#include <unordered_set>
1211
#include <utility>
1312
#include <vector>
1413

15-
#include "boost/container_hash/hash.hpp"
14+
#include "absl/hash/hash.h"
15+
#include "absl/random/random.h"
1616
#include "cut/abc_library_factory.h"
1717
#include "db_sta/dbNetwork.hh"
1818
#include "db_sta/dbSta.hh"
@@ -29,21 +29,8 @@ using utl::RMP;
2929
static void removeDuplicates(std::vector<SolutionSlack>& population,
3030
utl::Logger* logger)
3131
{
32-
struct HashVector final
33-
{
34-
size_t operator()(const SolutionSlack::Type& sol) const
35-
{
36-
size_t res = 0;
37-
for (const auto& item : sol) {
38-
size_t hash = boost::hash<size_t>()(item.id);
39-
// TODO set seed
40-
boost::hash_combine(hash, res);
41-
}
42-
return res;
43-
}
44-
};
45-
46-
std::unordered_set<SolutionSlack::Type, HashVector> taken;
32+
std::unordered_set<SolutionSlack::Type, absl::Hash<SolutionSlack::Type>>
33+
taken;
4734
population.erase(
4835
std::ranges::begin(std::ranges::remove_if(
4936
population,
@@ -78,7 +65,8 @@ std::vector<GiaOp> GeneticStrategy::RunStrategy(
7865
for (auto& ind : population) {
7966
ind.solution_.reserve(initial_ops_);
8067
for (size_t i = 0; i < initial_ops_; i++) {
81-
ind.solution_.push_back(all_ops[random_() % all_ops.size()]);
68+
const auto idx = absl::Uniform<int>(random_, 0, all_ops.size());
69+
ind.solution_.push_back(all_ops[idx]);
8270
}
8371
}
8472

@@ -95,8 +83,8 @@ std::vector<GiaOp> GeneticStrategy::RunStrategy(
9583
// Crossover
9684
unsigned cross_size = std::max<unsigned>(cross_prob_ * generation_size, 1);
9785
for (unsigned j = 0; j < cross_size; j++) {
98-
auto rand1 = random_() % generation_size;
99-
auto rand2 = random_() % generation_size;
86+
auto rand1 = absl::Uniform<int>(random_, 0, generation_size);
87+
auto rand2 = absl::Uniform<int>(random_, 0, generation_size);
10088
if (rand1 == rand2) {
10189
continue;
10290
}
@@ -115,7 +103,7 @@ std::vector<GiaOp> GeneticStrategy::RunStrategy(
115103
unsigned mut_size = std::max<unsigned>(mut_prob_ * generation_size, 1);
116104
for (unsigned j = 0; j < mut_size; j++) {
117105
SolutionSlack sol_slack;
118-
auto rand = random_() % generation_size;
106+
auto rand = absl::Uniform<int>(random_, 0, generation_size);
119107
sol_slack.solution_
120108
= population[rand].RandomNeighbor(all_ops, logger, random_);
121109
population.emplace_back(sol_slack);
@@ -134,34 +122,34 @@ std::vector<GiaOp> GeneticStrategy::RunStrategy(
134122
logger);
135123
}
136124
// Selection
137-
std::ranges::sort(population, std::less{});
125+
std::ranges::sort(population, std::greater{}, &SolutionSlack::worst_slack_);
138126
std::vector<SolutionSlack> newPopulation;
139127
newPopulation.reserve(pop_size_);
140128
for (int j = 0; j < pop_size_; j++) {
141129
std::vector<size_t> tournament(tourn_size_);
142130
std::generate_n(tournament.begin(), tourn_size_, [&]() {
143-
return random_() % population.size();
131+
return absl::Uniform<int>(random_, 0, population.size());
144132
});
145133
std::ranges::sort(tournament);
146134
tournament.erase(std::ranges::begin(std::ranges::unique(tournament)),
147135
tournament.end());
148-
std::bernoulli_distribution bern_dist{tourn_prob_};
149-
for (const auto& candidateId : tournament) {
150-
if (bern_dist(random_)) {
151-
newPopulation.push_back(population[candidateId]);
152-
break;
153-
}
136+
auto winner = std::ranges::find_if(tournament, [&](auto const& _) {
137+
return absl::Bernoulli(random_, static_cast<double>(tourn_prob_));
138+
});
139+
if (winner != tournament.end()) {
140+
newPopulation.push_back(population[*winner]);
154141
}
155142
}
156143
removeDuplicates(newPopulation, logger);
157-
population = newPopulation;
144+
population = std::move(newPopulation);
158145

159146
for (const auto& candidate : population) {
160147
debugPrint(logger, RMP, "genetic", 1, candidate.toString());
161148
}
162149
}
163150

164-
auto best_it = std::ranges::min_element(population, std::less{});
151+
auto best_it
152+
= std::ranges::max_element(population, {}, &SolutionSlack::worst_slack_);
165153
logger->info(RMP,
166154
66,
167155
"Resynthesis: Best result is of individual {}",

src/rmp/src/genetic_strategy.h

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,8 @@
1313
#include "sta/Delay.hh"
1414
#include "utl/unique_name.h"
1515

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-
3016
namespace rmp {
3117

32-
// TODO docs
3318
class GeneticStrategy final : public SlackTuningStrategy
3419
{
3520
public:

0 commit comments

Comments
 (0)