Skip to content

Commit f33af07

Browse files
committed
upgrade router arg parser using more advanced arg parser; finished putting everything together; need to add test for conv_3_3
1 parent b2ff344 commit f33af07

File tree

9 files changed

+115
-28
lines changed

9 files changed

+115
-28
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@
77
[submodule "thunder/lib/leidenalg"]
88
path = thunder/lib/leidenalg
99
url = https://github.com/kuree/leidenalg
10+
[submodule "cyclone/extern/argparse"]
11+
path = cyclone/extern/argparse
12+
url = https://github.com/p-ranav/argparse

cyclone/example/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ target_link_libraries(example2 cyclone)
66

77
add_executable(router router.cc)
88
target_link_libraries(router PRIVATE cyclone)
9+
target_include_directories(router PRIVATE ../extern/argparse/include)
910

1011
target_link_libraries(router PUBLIC ${STATIC_FLAG})

cyclone/example/router.cc

Lines changed: 96 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
#include "../src/global.hh"
2-
#include "../src/graph.hh"
32
#include "../src/io.hh"
3+
#include "../src/timing.hh"
4+
#include "../src/thunder_io.hh"
45
#include <cstdio>
56
#include <fstream>
67
#include <iostream>
8+
#include "argparse/argparse.hpp"
79

810
using namespace std;
911

@@ -37,6 +39,73 @@ bool process_args(int argc, char *argv[], ::vector<::string> &args) {
3739
return pd_aware;
3840
}
3941

42+
void setup_argparse(argparse::ArgumentParser &parser) {
43+
parser.add_argument("--pd").help("If set, will use PD-oriented routing strategy").default_value(
44+
false).implicit_value(true);
45+
parser.add_argument("-p", "--packed").help("Packed netlist file").required();
46+
parser.add_argument("-P", "--placement").help("Placement file").required();
47+
parser.add_argument("-o", "-r", "--route").help("Routing result").required();
48+
parser.add_argument("-g").help("Routing graph information").required().append();
49+
parser.add_argument("-l", "--layout").help("Chip layout").default_value("");
50+
parser.add_argument("-t", "--retime").help(
51+
"Set timing file. Default is none, which turns off re-timing. Set to default to use the default timing information").default_value(
52+
"none");
53+
}
54+
55+
struct RouterInput {
56+
bool pd = false;
57+
std::string packed_filename;
58+
std::string placement_filename;
59+
std::string output_file;
60+
std::vector<std::pair<uint32_t, std::string>> graph_info;
61+
std::string timing_file;
62+
std::string chip_layout;
63+
};
64+
65+
std::optional<RouterInput> parse_args(int argc, char *argv[]) {
66+
argparse::ArgumentParser parser("CGRA Router");
67+
setup_argparse(parser);
68+
69+
try {
70+
parser.parse_args(argc, argv);
71+
}
72+
catch (const std::runtime_error &err) {
73+
std::cerr << err.what() << std::endl;
74+
std::cerr << parser;
75+
return std::nullopt;
76+
}
77+
// fill out information
78+
RouterInput result;
79+
result.pd = parser["--pd"] == true;
80+
result.packed_filename = parser.get<std::string>("-p");
81+
result.placement_filename = parser.get<std::string>("-P");
82+
result.output_file = parser.get<std::string>("-o");
83+
auto values = parser.get<std::vector<std::string>>("-g");
84+
if (values.size() % 2 != 0) {
85+
std::cerr << "Incorrect number of graph args" << std::endl;
86+
std::cerr << parser;
87+
return std::nullopt;
88+
}
89+
for (auto i = 0u; i < values.size(); i += 2) {
90+
auto bit_width = std::stoul(values[i * 2]);
91+
auto path = values[i * 2 + 1];
92+
result.graph_info.emplace_back(std::make_pair(bit_width, path));
93+
}
94+
95+
auto timing_file = parser.get<std::string>("-t");
96+
if (timing_file != "none") {
97+
auto layout = parser.get<std::string>("-l");
98+
if (layout.empty()) {
99+
std::cerr << "When re-timing is specified, layout file is required" << std::endl;
100+
std::cerr << parser << std::endl;
101+
return std::nullopt;
102+
}
103+
result.chip_layout = layout;
104+
}
105+
106+
return result;
107+
}
108+
40109
void
41110
adjust_node_cost_power_domain(RoutingGraph *graph,
42111
const std::map<std::string, std::pair<int, int>> &placement_result) {
@@ -52,30 +121,43 @@ adjust_node_cost_power_domain(RoutingGraph *graph,
52121
auto switchbox = tile.switchbox;
53122
for (uint32_t i = 0; i < 4; i++) {
54123
auto sbs = switchbox.get_sbs_by_side(SwitchBoxSide(i));
55-
for (const auto& sb: sbs) {
124+
for (const auto &sb: sbs) {
56125
sb->delay = power_domain_cost;
57126
}
58127
}
59128
}
60129
}
61130
}
62131

132+
void retime_router(Router &router, const RouterInput &args) {
133+
const auto &timing_file = args.timing_file;
134+
if (timing_file == "none") {
135+
return;
136+
} else if (timing_file == "default") {
137+
auto const &layout_file = args.chip_layout;
138+
auto layout = load_layout(layout_file);
139+
TimingAnalysis timing(router);
140+
timing.set_timing_cost(get_default_timing_info());
141+
timing.retime();
142+
} else {
143+
throw std::runtime_error("Timing file not implemented");
144+
}
145+
}
146+
63147
int main(int argc, char *argv[]) {
64-
if (argc < 6) {
65-
print_help(argv[0]);
148+
auto args_opt = parse_args(argc, argv);
149+
if (!args_opt) {
66150
return EXIT_FAILURE;
67151
}
68-
::vector<::string> args;
69-
bool power_domain = process_args(argc, argv, args);
152+
auto const &args = *args_opt;
70153

71-
string packed_filename = args[1];
72-
string placement_filename = args[2];
73-
// reassign the size
74-
argc = args.size();
154+
bool power_domain = args.pd;
155+
const auto &packed_filename = args.packed_filename;
156+
auto const &placement_filename = args.placement_filename;
75157

76158
auto[netlist, track_mode] = load_netlist(packed_filename);
77159
auto placement = load_placement(placement_filename);
78-
auto output_file = args[argc - 1];
160+
auto output_file = args.output_file;
79161

80162
// delete the old file if exists
81163
if (exists(output_file)) {
@@ -85,9 +167,7 @@ int main(int argc, char *argv[]) {
85167
}
86168
}
87169

88-
for (int arg_index = 3; arg_index < argc - 1; arg_index += 2) {
89-
auto bit_width = static_cast<uint32_t>(stoi(args[arg_index]));
90-
string graph_filename = args[arg_index + 1];
170+
for (auto const &[bit_width, graph_filename]: args.graph_info) {
91171
cout << "using bit_width " << bit_width << endl;
92172
auto graph = load_routing_graph(graph_filename);
93173

@@ -113,6 +193,8 @@ int main(int argc, char *argv[]) {
113193

114194
r.route();
115195

196+
retime_router(r, args);
197+
116198
dump_routing_result(r, output_file);
117199
}
118200
return EXIT_SUCCESS;

cyclone/extern/argparse

Submodule argparse added at ccf3920

cyclone/src/graph.cc

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ std::shared_ptr<Node> RoutedGraph::get_node(std::unordered_map<const Node *, std
386386
return node_mapping.at(node);
387387
}
388388

389-
std::vector<std::vector<std::shared_ptr<Node>>> RoutedGraph::get_route() const {
389+
std::map<uint32_t, std::vector<std::shared_ptr<Node>>> RoutedGraph::get_route() const {
390390
std::map<uint32_t, std::vector<std::shared_ptr<Node>>> result;
391391
std::unordered_set<const Node *> visited;
392392

@@ -417,12 +417,7 @@ std::vector<std::vector<std::shared_ptr<Node>>> RoutedGraph::get_route() const {
417417
result.emplace(pin_id, segment);
418418
}
419419

420-
std::vector<std::vector<std::shared_ptr<Node>>> segments;
421-
segments.reserve(result.size());
422-
for (auto const &iter: result) {
423-
segments.emplace_back(iter.second);
424-
}
425-
return segments;
420+
return result;
426421
}
427422

428423

cyclone/src/graph.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ public:
299299
explicit RoutedGraph(const std::map<uint32_t,
300300
std::vector<std::shared_ptr<Node>>>& route);
301301

302-
std::vector<std::vector<std::shared_ptr<Node>>> get_route() const;
302+
std::map<uint32_t, std::vector<std::shared_ptr<Node>>> get_route() const;
303303
[[nodiscard]] std::set<uint32_t> insert_pipeline_reg(uint32_t pin_id);
304304
[[nodiscard]] std::set<uint32_t> insert_reg_output(std::shared_ptr<Node> src_node);
305305
std::shared_ptr<Node> src_node() const { return src_node_; }

cyclone/src/route.hh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ public:
4040
// get final routed graph
4141
std::unordered_map<int, RoutedGraph> get_routed_graph() const;
4242

43+
void set_current_routes(const std::map<int,
44+
std::map<uint32_t,
45+
std::vector<std::shared_ptr<Node>>>> &routes) { current_routes = routes; }
46+
4347

4448
protected:
4549
RoutingGraph graph_;

cyclone/src/timing.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ uint64_t TimingAnalysis::retime() {
206206

207207
auto nodes = timing_graph.topological_sort();
208208
auto timing_node_mapping = get_timing_node_mapping(nodes);
209-
std::map<std::string, std::vector<std::vector<std::shared_ptr<Node>>>> final_result;
209+
std::map<int, std::map<uint32_t, std::vector<std::shared_ptr<Node>>>> final_result;
210210

211211
// start STA on each node
212212
for (auto const *timing_node: nodes) {
@@ -246,7 +246,7 @@ uint64_t TimingAnalysis::retime() {
246246
bool updated;
247247
do {
248248
updated = false;
249-
for (auto const &segment: segments) {
249+
for (auto const &[pin_id, segment]: segments) {
250250
for (uint64_t i = 1; i < segment.size(); i++) {
251251
auto const &current_node = segment[i];
252252
auto const &pre_node = segment[i - 1];
@@ -293,10 +293,11 @@ uint64_t TimingAnalysis::retime() {
293293

294294
// get the updated route
295295
segments = routed_graph.get_route();
296-
final_result.emplace(net.name, segments);
296+
final_result.emplace(net.id, segments);
297297
}
298298
}
299299

300+
router_.set_current_routes(final_result);
300301
auto r = get_max_wave_number(pin_wave_);
301302
return r;
302303
}

cyclone/src/timing.hh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ enum class TimingCost {
2020
};
2121

2222

23-
std::unordered_map<TimingCost, uint64_t> get_default_timing_info() {
23+
inline std::unordered_map<TimingCost, uint64_t> get_default_timing_info() {
2424
return {{TimingCost::CLB_OP, 1000},
2525
{TimingCost::MEM, 0},
2626
{TimingCost::CLB_SB, 200},
@@ -32,7 +32,7 @@ std::unordered_map<TimingCost, uint64_t> get_default_timing_info() {
3232

3333
class TimingAnalysis {
3434
public:
35-
explicit TimingAnalysis(const Router &router) : router_(router) {}
35+
explicit TimingAnalysis(Router &router) : router_(router) {}
3636

3737
void set_timing_cost(const std::unordered_map<TimingCost, uint64_t> &timing_cost) { timing_cost_ = timing_cost; }
3838

@@ -41,7 +41,7 @@ public:
4141
void set_minimum_frequency(uint64_t f) { min_frequency_ = f; }
4242

4343
private:
44-
const Router &router_;
44+
Router &router_;
4545
Layout layout_;
4646
uint64_t min_frequency_ = 200;
4747

0 commit comments

Comments
 (0)