Skip to content

Commit

Permalink
vpr/src/place/compressed_grid.h
Browse files Browse the repository at this point in the history
  • Loading branch information
amin1377 committed Mar 3, 2025
1 parent 0a80a54 commit 5cf1b67
Showing 1 changed file with 32 additions and 24 deletions.
56 changes: 32 additions & 24 deletions vpr/src/place/compressed_grid.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef VPR_COMPRESSED_GRID_H
#define VPR_COMPRESSED_GRID_H

#include <algorithm>
#include "physical_types.h"

#include "vtr_geometry.h"
Expand Down Expand Up @@ -60,18 +61,22 @@ struct t_compressed_block_grid {
*
* This function takes a physical tile location in the grid and converts it to the corresponding
* compressed location. The conversion approximates by rounding up to the nearest valid compressed location.
* If all the compressed locations are less than the grid location, the function will return the last compressed location.
*
* @param grid_loc The physical tile location in the grid.
* @return The corresponding compressed location with the same layer number.
*/
inline t_physical_tile_loc grid_loc_to_compressed_loc_approx_round_up(t_physical_tile_loc grid_loc) const {
auto find_compressed_index = [](const std::vector<int>& compressed, int value) -> int {
auto itr = std::upper_bound(compressed.begin(), compressed.end(), value);
if (itr == compressed.begin())
return 0;
if (itr == compressed.end() || *(itr - 1) == value)
return (int)std::distance(compressed.begin(), itr - 1);
return (int)std::distance(compressed.begin(), itr);
// Get the first element that is not less than the value
auto itr = std::lower_bound(compressed.begin(), compressed.end(), value);
if (itr == compressed.end()) {
// If all the compressed locations are less than the grid location, return the last compressed location
return compressed.size() - 1;
} else {
// Return the index of the first element that is not less than the value
return std::distance(compressed.begin(), itr);
}
};

int layer_num = grid_loc.layer_num;
Expand All @@ -86,17 +91,22 @@ struct t_compressed_block_grid {
*
* This function takes a physical tile location in the grid and converts it to the corresponding
* compressed location. The conversion approximates by rounding down to the nearest valid compressed location.
* If all the compressed locations are bigger than the grid location, the function will return the first compressed location.
*
* @param grid_loc The physical tile location in the grid.
* @return The corresponding compressed location with the same layer number.
*/
inline t_physical_tile_loc grid_loc_to_compressed_loc_approx_round_down(t_physical_tile_loc grid_loc) const {
auto find_compressed_index = [](const std::vector<int>& compressed, int value) -> int {
auto itr = std::lower_bound(compressed.begin(), compressed.end(), value);
if (itr == compressed.end()) {
return (int)std::distance(compressed.begin(), itr - 1);
// Get the first element that is strictly bigger than the value
auto itr = std::upper_bound(compressed.begin(), compressed.end(), value);
if (itr == compressed.begin()) {
// If all the compressed locations are less than the grid location, return the first compressed location
return 0;
} else {
// Return the index of the first element that is less than or equal to the value
return std::distance(compressed.begin(), itr - 1);
}
return (int)std::distance(compressed.begin(), itr);
};

int layer_num = grid_loc.layer_num;
Expand All @@ -120,23 +130,21 @@ struct t_compressed_block_grid {

// Find the first element not less than loc
auto itr = std::lower_bound(compressed_grid_dim.begin(), compressed_grid_dim.end(), loc);

// If we found exact match or at beginning, return current position
if (itr == compressed_grid_dim.begin() || (itr != compressed_grid_dim.end() && *itr == loc)) {
return std::distance(compressed_grid_dim.begin(), itr);
}

// If we're past the end, return last element

if (itr == compressed_grid_dim.end()) {
// If all the compressed locations are less than the grid location, return the last compressed location
return compressed_grid_dim.size() - 1;
} else if (*itr == loc) {
// If we found exact match, return the index of the element
return std::distance(compressed_grid_dim.begin(), itr);
} else {
// If we didn't find exact match, return the index of the closest compressed location
int dist_prev = loc - *(itr - 1);
int dist_next = *itr - loc;
VTR_ASSERT_DEBUG(dist_prev > 0 && dist_next > 0);
return (dist_prev <= dist_next) ? (std::distance(compressed_grid_dim.begin(), itr - 1)) :
(std::distance(compressed_grid_dim.begin(), itr));
}

// Compare distances to previous and current elements
int dist_prev = loc - *(itr - 1);
int dist_next = *itr - loc;

return std::distance(compressed_grid_dim.begin(),
(dist_prev <= dist_next) ? (itr - 1) : itr);
};

const int layer_num = grid_loc.layer_num;
Expand Down

0 comments on commit 5cf1b67

Please sign in to comment.