Skip to content

Commit 16c1081

Browse files
refactor the code to use the same code for both x and y channels
1 parent 1206c72 commit 16c1081

File tree

1 file changed

+41
-68
lines changed

1 file changed

+41
-68
lines changed

vpr/src/base/stats.cpp

Lines changed: 41 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,21 @@ static void load_channel_occupancies(const Netlist<>& net_list,
3232
vtr::Matrix<int>& chany_occ);
3333

3434
/**
35-
* @brief Writes detailed channel occupancy info to text files.
35+
* @brief Writes channel occupancy data to a file.
3636
*
37-
* For each channel segment in X and Y directions, outputs:
38-
* - Channel coordinate (x, y)
37+
* Each row contains:
38+
* - (x, y) coordinate
3939
* - Occupancy count
4040
* - Occupancy percentage (occupancy / capacity)
4141
* - Channel capacity
4242
*
43-
* @param chanx_occ Matrix containing occupancy values for X-directed channels.
44-
* @param chany_occ Matrix containing occupancy values for Y-directed channels.
43+
* @param filename Output file path.
44+
* @param occupancy Matrix of occupancy counts.
45+
* @param capacity_list List of channel capacities (per y for chanx, per x for chany).
4546
*/
46-
static void write_channel_occupancy_to_file(const vtr::Matrix<int>& chanx_occ,
47-
const vtr::Matrix<int>& chany_occ);
47+
static void write_channel_occupancy_table(const std::string_view filename,
48+
const vtr::Matrix<int>& occupancy,
49+
const std::vector<int>& capacity_list);
4850

4951
/**
5052
* @brief Figures out maximum, minimum and average number of bends
@@ -201,9 +203,11 @@ static void get_channel_occupancy_stats(const Netlist<>& net_list, bool /***/) {
201203
device_ctx.grid.height() //[0 .. device_ctx.grid.height() - 1] (length of y channel)
202204
}},
203205
0);
206+
204207
load_channel_occupancies(net_list, chanx_occ, chany_occ);
205208

206-
write_channel_occupancy_to_file(chanx_occ, chany_occ);
209+
write_channel_occupancy_table("chanx_occupancy.txt", chanx_occ, device_ctx.chan_width.x_list);
210+
write_channel_occupancy_table("chany_occupancy.txt", chany_occ, device_ctx.chan_width.y_list);
207211

208212
VTR_LOG("\n");
209213
VTR_LOG("X - Directed channels: j max occ ave occ capacity\n");
@@ -245,73 +249,42 @@ static void get_channel_occupancy_stats(const Netlist<>& net_list, bool /***/) {
245249
VTR_LOG("\n");
246250
}
247251

248-
static void write_channel_occupancy_to_file(const vtr::Matrix<int>& chanx_occ,
249-
const vtr::Matrix<int>& chany_occ) {
250-
const auto& device_ctx = g_vpr_ctx.device();
251-
252+
static void write_channel_occupancy_table(const std::string_view filename,
253+
const vtr::Matrix<int>& occupancy,
254+
const std::vector<int>& capacity_list) {
252255
constexpr int w_coord = 6;
253256
constexpr int w_value = 12;
254-
constexpr int w_percent = 10;
255-
256-
// Write X-directed channels
257-
std::ofstream chanx_file("chanx_occupancy.txt");
258-
if (chanx_file.is_open()) {
259-
chanx_file << std::setw(w_coord) << "x"
260-
<< std::setw(w_coord) << "y"
261-
<< std::setw(w_value) << "occupancy"
262-
<< std::setw(w_percent) << "%"
263-
<< std::setw(w_value) << "capacity"
264-
<< "\n";
265-
266-
for (size_t j = 0; j < chanx_occ.dim_size(1); ++j) {
267-
int capacity = device_ctx.chan_width.x_list[j];
268-
for (size_t i = 0; i < chanx_occ.dim_size(0); ++i) {
269-
int occ = chanx_occ[i][j];
270-
float percent = capacity > 0 ? static_cast<float>(occ) / capacity * 100.0f : 0.0f;
271-
272-
chanx_file << std::setw(w_coord) << i
273-
<< std::setw(w_coord) << j
274-
<< std::setw(w_value) << occ
275-
<< std::setw(w_percent) << std::fixed << std::setprecision(3) << percent
276-
<< std::setw(w_value) << capacity
277-
<< "\n";
278-
}
279-
}
257+
constexpr int w_percent = 12;
280258

281-
chanx_file.close();
282-
} else {
283-
VTR_LOG_WARN("Failed to open chanx_occupancy.txt for writing.\n");
259+
std::ofstream file(filename.data());
260+
if (!file.is_open()) {
261+
VTR_LOG_WARN("Failed to open %s for writing.\n", filename.data());
262+
return;
284263
}
285264

286-
// Write Y-directed channels
287-
std::ofstream chany_file("chany_occupancy.txt");
288-
if (chany_file.is_open()) {
289-
chany_file << std::setw(w_coord) << "x"
290-
<< std::setw(w_coord) << "y"
291-
<< std::setw(w_value) << "occupancy"
292-
<< std::setw(w_percent) << "%"
293-
<< std::setw(w_value) << "capacity"
294-
<< "\n";
295-
296-
for (size_t i = 0; i < chany_occ.dim_size(0); ++i) {
297-
int capacity = device_ctx.chan_width.y_list[i];
298-
for (size_t j = 0; j < chany_occ.dim_size(1); ++j) {
299-
int occ = chany_occ[i][j];
300-
float percent = capacity > 0 ? static_cast<float>(occ) / capacity * 100.0f : 0.0f;
301-
302-
chany_file << std::setw(w_coord) << i
303-
<< std::setw(w_coord) << j
304-
<< std::setw(w_value) << occ
305-
<< std::setw(w_percent) << std::fixed << std::setprecision(3) << percent
306-
<< std::setw(w_value) << capacity
307-
<< "\n";
308-
}
265+
file << std::setw(w_coord) << "x"
266+
<< std::setw(w_coord) << "y"
267+
<< std::setw(w_value) << "occupancy"
268+
<< std::setw(w_percent) << "%"
269+
<< std::setw(w_value) << "capacity"
270+
<< "\n";
271+
272+
for (size_t y = 0; y < occupancy.dim_size(1); ++y) {
273+
int capacity = capacity_list[y];
274+
for (size_t x = 0; x < occupancy.dim_size(0); ++x) {
275+
int occ = occupancy[x][y];
276+
float percent = capacity > 0 ? static_cast<float>(occ) / capacity * 100.0f : 0.0f;
277+
278+
file << std::setw(w_coord) << x
279+
<< std::setw(w_coord) << y
280+
<< std::setw(w_value) << occ
281+
<< std::setw(w_percent) << std::fixed << std::setprecision(3) << percent
282+
<< std::setw(w_value) << capacity
283+
<< "\n";
309284
}
310-
311-
chany_file.close();
312-
} else {
313-
VTR_LOG_WARN("Failed to open chany_occupancy.txt for writing.\n");
314285
}
286+
287+
file.close();
315288
}
316289

317290
static void load_channel_occupancies(const Netlist<>& net_list,

0 commit comments

Comments
 (0)