Skip to content

Commit b89f681

Browse files
authored
Merge pull request #2919 from verilog-to-routing/loc_to_compressed_debug
[Place] Grid Location to Compressed Location
2 parents 12c4600 + e287e30 commit b89f681

File tree

2 files changed

+39
-29
lines changed

2 files changed

+39
-29
lines changed

vpr/src/pack/prepack.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1668,7 +1668,7 @@ static void print_chain_starting_points(t_pack_patterns* chain_pattern) {
16681668

16691669
Prepacker::Prepacker(const AtomNetlist& atom_nlist,
16701670
const std::vector<t_logical_block_type>& logical_block_types) {
1671-
VTR_ASSERT(pack_molecules_.empty() && "Prepacker cannot be initialized twice.");
1671+
VTR_ASSERT_MSG(pack_molecules_.empty(), "Prepacker cannot be initialized twice.");
16721672

16731673
// Allocate the pack patterns from the logical block types.
16741674
list_of_pack_patterns = alloc_and_load_pack_patterns(logical_block_types);

vpr/src/place/compressed_grid.h

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#ifndef VPR_COMPRESSED_GRID_H
22
#define VPR_COMPRESSED_GRID_H
33

4+
#include <algorithm>
45
#include "physical_types.h"
56

7+
#include "vtr_assert.h"
68
#include "vtr_geometry.h"
79
#include "vtr_flat_map.h"
810

@@ -60,18 +62,22 @@ struct t_compressed_block_grid {
6062
*
6163
* This function takes a physical tile location in the grid and converts it to the corresponding
6264
* compressed location. The conversion approximates by rounding up to the nearest valid compressed location.
65+
* If all the compressed locations are less than the grid location, the function will return the last compressed location.
6366
*
6467
* @param grid_loc The physical tile location in the grid.
6568
* @return The corresponding compressed location with the same layer number.
6669
*/
6770
inline t_physical_tile_loc grid_loc_to_compressed_loc_approx_round_up(t_physical_tile_loc grid_loc) const {
6871
auto find_compressed_index = [](const std::vector<int>& compressed, int value) -> int {
69-
auto itr = std::upper_bound(compressed.begin(), compressed.end(), value);
70-
if (itr == compressed.begin())
71-
return 0;
72-
if (itr == compressed.end() || *(itr - 1) == value)
73-
return (int)std::distance(compressed.begin(), itr - 1);
74-
return (int)std::distance(compressed.begin(), itr);
72+
// Get the first element that is not less than the value
73+
auto itr = std::lower_bound(compressed.begin(), compressed.end(), value);
74+
if (itr == compressed.end()) {
75+
// If all the compressed locations are less than the grid location, return the last compressed location
76+
return compressed.size() - 1;
77+
} else {
78+
// Return the index of the first element that is not less than the value
79+
return std::distance(compressed.begin(), itr);
80+
}
7581
};
7682

7783
int layer_num = grid_loc.layer_num;
@@ -86,17 +92,22 @@ struct t_compressed_block_grid {
8692
*
8793
* This function takes a physical tile location in the grid and converts it to the corresponding
8894
* compressed location. The conversion approximates by rounding down to the nearest valid compressed location.
95+
* If all the compressed locations are bigger than the grid location, the function will return the first compressed location.
8996
*
9097
* @param grid_loc The physical tile location in the grid.
9198
* @return The corresponding compressed location with the same layer number.
9299
*/
93100
inline t_physical_tile_loc grid_loc_to_compressed_loc_approx_round_down(t_physical_tile_loc grid_loc) const {
94101
auto find_compressed_index = [](const std::vector<int>& compressed, int value) -> int {
95-
auto itr = std::lower_bound(compressed.begin(), compressed.end(), value);
96-
if (itr == compressed.end()) {
97-
return (int)std::distance(compressed.begin(), itr - 1);
102+
// Get the first element that is strictly bigger than the value
103+
auto itr = std::upper_bound(compressed.begin(), compressed.end(), value);
104+
if (itr == compressed.begin()) {
105+
// If all the compressed locations are bigger than the grid location, return the first compressed location
106+
return 0;
107+
} else {
108+
// Return the index of the first element that is less than or equal to the value
109+
return std::distance(compressed.begin(), itr - 1);
98110
}
99-
return (int)std::distance(compressed.begin(), itr);
100111
};
101112

102113
int layer_num = grid_loc.layer_num;
@@ -111,31 +122,30 @@ struct t_compressed_block_grid {
111122
*
112123
* Useful when the point is of a different block type from coords.
113124
*
114-
* @param point represents a coordinate in one dimension of the point
115-
* @param coords represents vector of coordinate values of a single type only
116-
*
117-
* Hence, the exact point coordinate will not be found in coords if they are of different block types. In this case the function will return
118-
* the nearest compressed location to point by rounding it down
125+
* @param grid_loc non-compressed physical tile location in the grid
126+
* @return Nearest x and y compressed locations in the grid (in the same layer)
119127
*/
120128
inline t_physical_tile_loc grid_loc_to_compressed_loc_approx(t_physical_tile_loc grid_loc) const {
121129
auto find_closest_compressed_point = [](int loc, const std::vector<int>& compressed_grid_dim) -> int {
130+
VTR_ASSERT_DEBUG(compressed_grid_dim.size() > 0);
131+
132+
// Find the first element not less than loc
122133
auto itr = std::lower_bound(compressed_grid_dim.begin(), compressed_grid_dim.end(), loc);
123-
int cx;
124-
if (itr < compressed_grid_dim.end() - 1) {
125-
int dist_prev = abs(loc - *itr);
126-
int dist_next = abs(loc - *(itr+1));
127-
if (dist_prev < dist_next) {
128-
cx = std::distance(compressed_grid_dim.begin(), itr);
129-
} else {
130-
cx = std::distance(compressed_grid_dim.begin(), itr + 1);
131-
}
134+
135+
if (itr == compressed_grid_dim.begin()) {
136+
// If all the compressed locations are bigger that or equal to loc, return the first compressed location
137+
return 0;
132138
} else if (itr == compressed_grid_dim.end()) {
133-
cx = std::distance(compressed_grid_dim.begin(), itr - 1);
139+
// If all the compressed locations are less than loc, return the last compressed location
140+
return compressed_grid_dim.size() - 1;
134141
} else {
135-
cx = std::distance(compressed_grid_dim.begin(), itr);
142+
// Find the nearest compressed location.
143+
int dist_prev = loc - *(itr - 1);
144+
int dist_next = *itr - loc;
145+
VTR_ASSERT_DEBUG(dist_prev >= 0 && dist_next >= 0);
146+
return (dist_prev <= dist_next) ? (std::distance(compressed_grid_dim.begin(), itr - 1)) :
147+
(std::distance(compressed_grid_dim.begin(), itr));
136148
}
137-
138-
return cx;
139149
};
140150

141151
const int layer_num = grid_loc.layer_num;

0 commit comments

Comments
 (0)