Skip to content

Commit 81f144d

Browse files
committed
vpr: base: use structs in --write_block_usage
Signed-off-by: Pawel Czarnecki <[email protected]>
1 parent 4c7ef87 commit 81f144d

File tree

4 files changed

+118
-80
lines changed

4 files changed

+118
-80
lines changed

vpr/src/base/ShowSetup.cpp

Lines changed: 90 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -63,23 +63,69 @@ void ShowSetup(const t_vpr_setup& vpr_setup) {
6363
}
6464
}
6565

66-
void printClusteredNetlistStats(std::string block_usage_filename) {
66+
void ClusteredNetlistStats::writeHuman(std::ostream& output) const {
67+
output << "Netlist num_nets: " << num_nets << "\n";
68+
output << "Netlist num_blocks: " << num_blocks << "\n";
69+
for (const auto& type : logical_block_types) {
70+
output << "Netlist " << type.name << " blocks: " << num_blocks_type[type.index] << ".\n";
71+
}
72+
73+
output << "Netlist inputs pins: " << L_num_p_inputs << "\n";
74+
output << "Netlist output pins: " << L_num_p_outputs << "\n";
75+
}
76+
void ClusteredNetlistStats::writeJSON(std::ostream& output) const {
77+
output << "{\n";
78+
79+
output << " \"num_nets\": \"" << num_nets << "\",\n";
80+
output << " \"num_blocks\": \"" << num_blocks << "\",\n";
81+
82+
output << " \"input_pins\": \"" << L_num_p_inputs << "\",\n";
83+
output << " \"output_pins\": \"" << L_num_p_outputs << "\",\n";
84+
85+
output << " \"blocks\": {\n";
86+
87+
for (const auto& type : logical_block_types) {
88+
output << " \"" << type.name << "\": " << num_blocks_type[type.index];
89+
if ((int)type.index < (int)logical_block_types.size() - 1)
90+
output << ",\n";
91+
else
92+
output << "\n";
93+
}
94+
output << " }\n";
95+
output << "}\n";
96+
}
97+
98+
void ClusteredNetlistStats::writeXML(std::ostream& output) const {
99+
output << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
100+
output << "<block_usage_report>\n";
101+
102+
output << " <nets num=\"" << num_nets << "\"></nets>\n";
103+
output << " <blocks num=\"" << num_blocks << "\">\n";
104+
105+
for (const auto& type : logical_block_types) {
106+
output << " <block type=\"" << type.name << "\" usage=\"" << num_blocks_type[type.index] << "\"></block>\n";
107+
}
108+
output << " </blocks>\n";
109+
110+
output << " <input_pins num=\"" << L_num_p_inputs << "\"></input_pins>\n";
111+
output << " <output_pins num=\"" << L_num_p_outputs << "\"></output_pins>\n";
112+
113+
output << "</block_usage_report>\n";
114+
}
115+
116+
ClusteredNetlistStats::ClusteredNetlistStats() {
67117
auto& device_ctx = g_vpr_ctx.device();
68118
auto& cluster_ctx = g_vpr_ctx.clustering();
69119

70-
int j, L_num_p_inputs, L_num_p_outputs, num_nets, num_blocks;
71-
std::vector<int> num_blocks_type(device_ctx.logical_block_types.size(), 0);
120+
int j;
121+
L_num_p_inputs = 0;
122+
L_num_p_outputs = 0;
123+
num_blocks_type = std::vector<int>(device_ctx.logical_block_types.size(), 0);
72124
num_nets = (int)cluster_ctx.clb_nlist.nets().size();
73125
num_blocks = (int)cluster_ctx.clb_nlist.blocks().size();
74-
75-
VTR_LOG("\n");
76-
VTR_LOG("Netlist num_nets: %d\n", num_nets);
77-
VTR_LOG("Netlist num_blocks: %d\n", num_blocks);
126+
logical_block_types = device_ctx.logical_block_types;
78127

79128
/* Count I/O input and output pads */
80-
L_num_p_inputs = 0;
81-
L_num_p_outputs = 0;
82-
83129
for (auto blk_id : cluster_ctx.clb_nlist.blocks()) {
84130
auto logical_block = cluster_ctx.clb_nlist.block_type(blk_id);
85131
auto physical_tile = pick_physical_type(logical_block);
@@ -101,77 +147,51 @@ void printClusteredNetlistStats(std::string block_usage_filename) {
101147
}
102148
}
103149
}
150+
}
104151

105-
for (const auto& type : device_ctx.logical_block_types) {
106-
VTR_LOG("Netlist %s blocks: %d.\n", type.name, num_blocks_type[type.index]);
152+
void ClusteredNetlistStats::write(OutputFormat fmt, std::ostream& output) const {
153+
switch (fmt) {
154+
case HumanReadable:
155+
writeHuman(output);
156+
break;
157+
case JSON:
158+
writeJSON(output);
159+
break;
160+
case XML:
161+
writeXML(output);
162+
break;
163+
default:
164+
VPR_FATAL_ERROR(VPR_ERROR_PACK,
165+
"Unknown extension on in block usage summary file");
166+
break;
107167
}
108-
109-
/* Print out each block separately instead */
110-
VTR_LOG("Netlist inputs pins: %d\n", L_num_p_inputs);
111-
VTR_LOG("Netlist output pins: %d\n", L_num_p_outputs);
112-
VTR_LOG("\n");
113-
if (!block_usage_filename.empty())
114-
writeClusteredNetlistStats(block_usage_filename, num_nets, num_blocks,
115-
L_num_p_inputs, L_num_p_outputs,
116-
num_blocks_type,
117-
device_ctx.logical_block_types);
118-
119-
num_blocks_type.clear();
120168
}
121169

122-
void writeClusteredNetlistStats(std::string block_usage_filename,
123-
int num_nets,
124-
int num_blocks,
125-
int L_num_p_inputs,
126-
int L_num_p_outputs,
127-
std::vector<int> num_blocks_type,
128-
std::vector<t_logical_block_type> logical_block_types) {
129-
if (vtr::check_file_name_extension(block_usage_filename.c_str(), ".json")) {
130-
// write report in JSON format
131-
std::fstream fp;
132-
fp.open(block_usage_filename, std::fstream::out | std::fstream::trunc);
133-
fp << "{\n";
134-
135-
fp << " \"num_nets\": \"" << num_nets << "\",\n";
136-
fp << " \"num_blocks\": \"" << num_blocks << "\",\n";
170+
void writeClusteredNetlistStats(std::string block_usage_filename) {
171+
const auto stats = ClusteredNetlistStats();
137172

138-
fp << " \"input_pins\": \"" << L_num_p_inputs << "\",\n";
139-
fp << " \"output_pins\": \"" << L_num_p_outputs << "\",\n";
173+
// Print out the human readable version to stdout
140174

141-
fp << " \"blocks\": {\n";
175+
stats.write(ClusteredNetlistStats::OutputFormat::HumanReadable, std::cout);
142176

143-
for (const auto& type : logical_block_types) {
144-
fp << " \"" << type.name << "\": " << num_blocks_type[type.index];
145-
if ((int)type.index < (int)logical_block_types.size() - 1)
146-
fp << ",\n";
147-
else
148-
fp << "\n";
149-
}
150-
fp << " }\n";
151-
fp << "}\n";
152-
} else if (vtr::check_file_name_extension(block_usage_filename.c_str(), ".xml")) {
153-
// write report in XML format
154-
std::fstream fp;
155-
fp.open(block_usage_filename, std::fstream::out | std::fstream::trunc);
156-
fp << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
157-
fp << "<block_usage_report>\n";
177+
if (block_usage_filename.size() > 0) {
178+
ClusteredNetlistStats::OutputFormat fmt;
158179

159-
fp << " <nets num=\"" << num_nets << "\"></nets>\n";
160-
fp << " <blocks num=\"" << num_blocks << "\">\n";
161-
162-
for (const auto& type : logical_block_types) {
163-
fp << " <block type=\"" << type.name << "\" usage=\"" << num_blocks_type[type.index] << "\"></block>\n";
180+
if (vtr::check_file_name_extension(block_usage_filename.c_str(), ".json")) {
181+
fmt = ClusteredNetlistStats::OutputFormat::JSON;
182+
} else if (vtr::check_file_name_extension(block_usage_filename.c_str(), ".xml")) {
183+
fmt = ClusteredNetlistStats::OutputFormat::XML;
184+
} else if (vtr::check_file_name_extension(block_usage_filename.c_str(), ".txt")) {
185+
fmt = ClusteredNetlistStats::OutputFormat::HumanReadable;
186+
} else {
187+
VPR_FATAL_ERROR(VPR_ERROR_PACK, "Unknown extension on output %s", block_usage_filename.c_str());
164188
}
165-
fp << " </blocks>\n";
166189

167-
fp << " <input_pins num=\"" << L_num_p_inputs << "\"></input_pins>\n";
168-
fp << " <output_pins num=\"" << L_num_p_outputs << "\"></output_pins>\n";
190+
std::fstream fp;
169191

170-
fp << "</block_usage_report>\n";
171-
} else {
172-
VPR_FATAL_ERROR(VPR_ERROR_PACK,
173-
"Unknown extension on output %s",
174-
block_usage_filename.c_str());
192+
fp.open(block_usage_filename, std::fstream::out | std::fstream::trunc);
193+
stats.write(fmt, fp);
194+
fp.close();
175195
}
176196
}
177197

vpr/src/base/ShowSetup.h

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,32 @@
11
#ifndef SHOWSETUP_H
22
#define SHOWSETUP_H
33

4+
struct ClusteredNetlistStats {
5+
private:
6+
void writeHuman(std::ostream& output) const;
7+
void writeJSON(std::ostream& output) const;
8+
void writeXML(std::ostream& output) const;
9+
10+
public:
11+
ClusteredNetlistStats();
12+
13+
enum OutputFormat {
14+
HumanReadable,
15+
JSON,
16+
XML
17+
};
18+
19+
int num_nets;
20+
int num_blocks;
21+
int L_num_p_inputs;
22+
int L_num_p_outputs;
23+
std::vector<int> num_blocks_type;
24+
std::vector<t_logical_block_type> logical_block_types;
25+
26+
void write(OutputFormat fmt, std::ostream& output) const;
27+
};
28+
429
void ShowSetup(const t_vpr_setup& vpr_setup);
5-
void printClusteredNetlistStats(std::string block_usage_filename);
6-
void writeClusteredNetlistStats(std::string block_usage_filename,
7-
int num_nets,
8-
int num_blocks,
9-
int L_num_p_inputs,
10-
int L_num_p_outputs,
11-
std::vector<int> num_blocks_type,
12-
std::vector<t_logical_block_type> logical_block_types);
30+
void writeClusteredNetlistStats(std::string block_usage_filename);
1331

1432
#endif

vpr/src/base/read_options.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1491,7 +1491,7 @@ argparse::ArgumentParser create_arg_parser(std::string prog_name, t_options& arg
14911491
.show_in(argparse::ShowIn::HELP_ONLY);
14921492

14931493
file_grp.add_argument(args.write_block_usage, "--write_block_usage")
1494-
.help("Writes the packing block types usage summary to the specified JSON or XML file.")
1494+
.help("Writes the packing block types usage summary to the specified JSON, XML or TXT file.")
14951495
.show_in(argparse::ShowIn::HELP_ONLY);
14961496

14971497
auto& netlist_grp = parser.add_argument_group("netlist options");

vpr/src/base/vpr_api.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ bool vpr_pack_flow(t_vpr_setup& vpr_setup, const t_arch& arch) {
517517
check_netlist(packer_opts.pack_verbosity);
518518

519519
/* Output the netlist stats to console and optionally to file. */
520-
printClusteredNetlistStats(vpr_setup.FileNameOpts.write_block_usage.c_str());
520+
writeClusteredNetlistStats(vpr_setup.FileNameOpts.write_block_usage.c_str());
521521

522522
// print the total number of used physical blocks for each
523523
// physical block type after finishing the packing stage

0 commit comments

Comments
 (0)