From 5cf1b67ec048178fc0de64bd28b57a14511cd4c6 Mon Sep 17 00:00:00 2001 From: amin1377 Date: Mon, 3 Mar 2025 08:59:46 -0500 Subject: [PATCH] vpr/src/place/compressed_grid.h --- vpr/src/place/compressed_grid.h | 56 +++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 24 deletions(-) diff --git a/vpr/src/place/compressed_grid.h b/vpr/src/place/compressed_grid.h index 2cc95034e8..72ebff4dbc 100644 --- a/vpr/src/place/compressed_grid.h +++ b/vpr/src/place/compressed_grid.h @@ -1,6 +1,7 @@ #ifndef VPR_COMPRESSED_GRID_H #define VPR_COMPRESSED_GRID_H +#include #include "physical_types.h" #include "vtr_geometry.h" @@ -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& 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; @@ -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& 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; @@ -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;