Skip to content

Commit 5cae764

Browse files
replace some nested loops with calls to all_locations()
1 parent edde5be commit 5cae764

File tree

9 files changed

+141
-174
lines changed

9 files changed

+141
-174
lines changed

vpr/src/base/blk_loc_registry.cpp

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -154,27 +154,21 @@ void BlkLocRegistry::clear_block_type_grid_locs(const std::unordered_set<int>& u
154154

155155
bool clear_all_block_types = false;
156156

157-
/* check if all types should be cleared
158-
* logical_block_types contain empty type, needs to be ignored.
159-
* Not having any type in unplaced_blk_types_index means that it is the first iteration, hence all grids needs to be cleared
160-
*/
157+
// check if all types should be cleared
158+
// logical_block_types contain empty type, needs to be ignored.
159+
// Not having any type in unplaced_blk_types_index means that it is the first iteration, hence all grids needs to be cleared
161160
if (unplaced_blk_types_index.size() == device_ctx.logical_block_types.size() - 1) {
162161
clear_all_block_types = true;
163162
}
164163

165-
/* We'll use the grid to record where everything goes. Initialize to the grid has no
166-
* blocks placed anywhere.
167-
*/
168-
for (int layer_num = 0; layer_num < device_ctx.grid.get_num_layers(); layer_num++) {
169-
for (int i = 0; i < (int)device_ctx.grid.width(); i++) {
170-
for (int j = 0; j < (int)device_ctx.grid.height(); j++) {
171-
const t_physical_tile_type_ptr type = device_ctx.grid.get_physical_type({i, j, layer_num});
172-
int itype = type->index;
173-
if (clear_all_block_types || unplaced_blk_types_index.count(itype)) {
174-
for (int k = 0; k < device_ctx.physical_tile_types[itype].capacity; k++) {
175-
grid_blocks_.set_block_at_location({i, j, k, layer_num}, ClusterBlockId::INVALID());
176-
}
177-
}
164+
// We'll use the grid to record where everything goes. Initialize to the grid has no
165+
// blocks placed anywhere.
166+
for (const t_physical_tile_loc loc : device_ctx.grid.all_locations()) {
167+
const t_physical_tile_type_ptr type = device_ctx.grid.get_physical_type(loc);
168+
int itype = type->index;
169+
if (clear_all_block_types || unplaced_blk_types_index.count(itype)) {
170+
for (int k = 0; k < device_ctx.physical_tile_types[itype].capacity; k++) {
171+
grid_blocks_.set_block_at_location({loc, k}, ClusterBlockId::INVALID());
178172
}
179173
}
180174
}

vpr/src/base/setup_grid.cpp

Lines changed: 52 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ static std::vector<t_logical_block_type_ptr> grid_overused_resources(const Devic
3535
static bool grid_satisfies_instance_counts(const DeviceGrid& grid, const std::map<t_logical_block_type_ptr, size_t>& instance_counts, float maximum_utilization);
3636
static DeviceGrid build_device_grid(const t_grid_def& grid_def, size_t width, size_t height, bool warn_out_of_range = true, const std::vector<t_logical_block_type_ptr>& limiting_resources = std::vector<t_logical_block_type_ptr>());
3737

38-
static void CheckGrid(const DeviceGrid& grid);
38+
///@brief Check grid is valid
39+
static void check_grid(const DeviceGrid& grid);
3940

4041
static void set_grid_block_type(int priority,
4142
const t_physical_tile_type* type,
@@ -545,7 +546,7 @@ static DeviceGrid build_device_grid(const t_grid_def& grid_def, size_t grid_widt
545546

546547
auto device_grid = DeviceGrid(grid_def.name, grid, limiting_resources);
547548

548-
CheckGrid(device_grid);
549+
check_grid(device_grid);
549550

550551
return device_grid;
551552
}
@@ -615,8 +616,8 @@ static void set_grid_block_type(int priority,
615616
priority, type->name.c_str());
616617
}
617618

618-
//Mark all the grid tiles 'covered' by this block with the appropriate type
619-
//and width/height offsets
619+
// Mark all the grid tiles 'covered' by this block with the appropriate type
620+
// and width/height offsets
620621
std::set<TypeLocation> root_blocks_to_rip_up;
621622
auto& device_ctx = g_vpr_ctx.device();
622623
for (size_t x = x_root; x < x_root + type->width; ++x) {
@@ -654,9 +655,9 @@ static void set_grid_block_type(int priority,
654655
}
655656
}
656657

657-
//Rip-up any invalidated blocks
658+
// Rip-up any invalidated blocks
658659
for (auto invalidated_root : root_blocks_to_rip_up) {
659-
//Mark all the grid locations used by this root block as empty
660+
// Mark all the grid locations used by this root block as empty
660661
for (size_t x = invalidated_root.x; x < invalidated_root.x + invalidated_root.type->width; ++x) {
661662
int x_offset = x - invalidated_root.x;
662663
for (size_t y = invalidated_root.y; y < invalidated_root.y + invalidated_root.type->height; ++y) {
@@ -690,65 +691,53 @@ static void set_grid_block_type(int priority,
690691
}
691692
}
692693

693-
///@brief Check grid is valid
694-
static void CheckGrid(const DeviceGrid& grid) {
695-
for (int layer_num = 0; layer_num < grid.get_num_layers(); layer_num++) { //Check each die individually
696-
for (int i = 0; i < (int)grid.width(); ++i) {
697-
for (int j = 0; j < (int)grid.height(); ++j) {
698-
const t_physical_tile_loc tile_loc(i, j, layer_num);
699-
const auto& type = grid.get_physical_type(tile_loc);
700-
int width_offset = grid.get_width_offset(tile_loc);
701-
int height_offset = grid.get_height_offset(tile_loc);
702-
if (nullptr == type) {
703-
VPR_FATAL_ERROR(VPR_ERROR_OTHER, "Grid Location (%d,%d,%d) has no type.\n", i, j, layer_num);
704-
}
694+
static void check_grid(const DeviceGrid& grid) {
695+
for (const t_physical_tile_loc tile_loc : grid.all_locations()) {
696+
const t_physical_tile_type_ptr type = grid.get_physical_type(tile_loc);
697+
int width_offset = grid.get_width_offset(tile_loc);
698+
int height_offset = grid.get_height_offset(tile_loc);
699+
if (nullptr == type) {
700+
VPR_FATAL_ERROR(VPR_ERROR_OTHER, "Grid Location (%d,%d,%d) has no type.\n", tile_loc.layer_num, tile_loc.x, tile_loc.y);
701+
}
705702

706-
if ((grid.get_width_offset(tile_loc) < 0)
707-
|| (grid.get_width_offset(tile_loc) >= type->width)) {
708-
VPR_FATAL_ERROR(VPR_ERROR_OTHER, "Grid Location (%d,%d,%d) has invalid width offset (%d).\n",
709-
i,
710-
j,
711-
layer_num,
712-
width_offset);
713-
}
714-
if ((grid.get_height_offset(tile_loc) < 0)
715-
|| (grid.get_height_offset(tile_loc) >= type->height)) {
716-
VPR_FATAL_ERROR(VPR_ERROR_OTHER, "Grid Location (%d,%d,%d) has invalid height offset (%d).\n",
717-
i,
718-
j,
719-
layer_num,
720-
height_offset);
721-
}
703+
if ((grid.get_width_offset(tile_loc) < 0) || (grid.get_width_offset(tile_loc) >= type->width)) {
704+
VPR_FATAL_ERROR(VPR_ERROR_OTHER, "Grid Location (%d,%d,%d) has invalid width offset (%d).\n",
705+
tile_loc.layer_num, tile_loc.x, tile_loc.y,
706+
width_offset);
707+
}
708+
if ((grid.get_height_offset(tile_loc) < 0) || (grid.get_height_offset(tile_loc) >= type->height)) {
709+
VPR_FATAL_ERROR(VPR_ERROR_OTHER, "Grid Location (%d,%d,%d) has invalid height offset (%d).\n",
710+
tile_loc.layer_num, tile_loc.x, tile_loc.y,
711+
height_offset);
712+
}
722713

723-
//Verify that type and width/height offsets are correct (e.g. for dimension > 1 blocks)
724-
if (grid.get_width_offset(tile_loc) == 0 && grid.get_height_offset(tile_loc) == 0) {
725-
//From the root block check that all other blocks are correct
726-
for (int x = i; x < i + type->width; ++x) {
727-
int x_offset = x - i;
728-
for (int y = j; y < j + type->height; ++y) {
729-
int y_offset = y - j;
730-
const t_physical_tile_loc tile_loc_offset(x, y, layer_num);
731-
const auto& tile_type = grid.get_physical_type(tile_loc_offset);
732-
int tile_width_offset = grid.get_width_offset(tile_loc_offset);
733-
int tile_height_offset = grid.get_height_offset(tile_loc_offset);
734-
if (tile_type != type) {
735-
VPR_FATAL_ERROR(VPR_ERROR_OTHER,
736-
"Grid Location (%d,%d,%d) should have type '%s' (based on root location) but has type '%s'\n",
737-
i, j, layer_num, type->name.c_str(), type->name.c_str());
738-
}
739-
740-
if (tile_width_offset != x_offset) {
741-
VPR_FATAL_ERROR(VPR_ERROR_OTHER,
742-
"Grid Location (%d,%d,%d) of type '%s' should have width offset '%d' (based on root location) but has '%d'\n",
743-
i, j, layer_num, type->name.c_str(), x_offset, tile_width_offset);
744-
}
745-
746-
if (tile_height_offset != y_offset) {
747-
VPR_FATAL_ERROR(VPR_ERROR_OTHER,
748-
"Grid Location (%d,%d,%d) of type '%s' should have height offset '%d' (based on root location) but has '%d'\n",
749-
i, j, layer_num, type->name.c_str(), y_offset, tile_height_offset);
750-
}
751-
}
714+
// Verify that type and width/height offsets are correct (e.g. for dimension > 1 blocks)
715+
if (grid.get_width_offset(tile_loc) == 0 && grid.get_height_offset(tile_loc) == 0) {
716+
// From the root block check that all other blocks are correct
717+
for (int x = tile_loc.x; x < tile_loc.x + type->width; ++x) {
718+
int x_offset = x - tile_loc.x;
719+
for (int y = tile_loc.y; y < tile_loc.y + type->height; ++y) {
720+
int y_offset = y - tile_loc.y;
721+
const t_physical_tile_loc tile_loc_offset(x, y, tile_loc.layer_num);
722+
const auto& tile_type = grid.get_physical_type(tile_loc_offset);
723+
int tile_width_offset = grid.get_width_offset(tile_loc_offset);
724+
int tile_height_offset = grid.get_height_offset(tile_loc_offset);
725+
if (tile_type != type) {
726+
VPR_FATAL_ERROR(VPR_ERROR_OTHER,
727+
"Grid Location (%d,%d,%d) should have type '%s' (based on root location) but has type '%s'\n",
728+
tile_loc.layer_num, tile_loc.x, tile_loc.y, type->name.c_str(), type->name.c_str());
729+
}
730+
731+
if (tile_width_offset != x_offset) {
732+
VPR_FATAL_ERROR(VPR_ERROR_OTHER,
733+
"Grid Location (%d,%d,%d) of type '%s' should have width offset '%d' (based on root location) but has '%d'\n",
734+
tile_loc.layer_num, tile_loc.x, tile_loc.y, type->name.c_str(), x_offset, tile_width_offset);
735+
}
736+
737+
if (tile_height_offset != y_offset) {
738+
VPR_FATAL_ERROR(VPR_ERROR_OTHER,
739+
"Grid Location (%d,%d,%d) of type '%s' should have height offset '%d' (based on root location) but has '%d'\n",
740+
tile_loc.layer_num, tile_loc.x, tile_loc.y, type->name.c_str(), y_offset, tile_height_offset);
752741
}
753742
}
754743
}

vpr/src/base/stats.cpp

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -85,21 +85,18 @@ void routing_stats(const Netlist<>& net_list,
8585
VTR_LOG("Logic area (in minimum width transistor areas, excludes I/Os and empty grid tiles)...\n");
8686

8787
float area = 0;
88-
for (int layer_num = 0; layer_num < device_ctx.grid.get_num_layers(); layer_num++) {
89-
for (int i = 0; i < (int)device_ctx.grid.width(); i++) {
90-
for (int j = 0; j < (int)device_ctx.grid.height(); j++) {
91-
auto type = device_ctx.grid.get_physical_type({i, j, layer_num});
92-
int width_offset = device_ctx.grid.get_width_offset({i, j, layer_num});
93-
int height_offset = device_ctx.grid.get_height_offset({i, j, layer_num});
94-
if (width_offset == 0 && height_offset == 0 && !type->is_io() && !type->is_empty()) {
95-
if (type->area == UNDEFINED) {
96-
area += grid_logic_tile_area * type->width * type->height;
97-
} else {
98-
area += type->area;
99-
}
100-
}
88+
89+
for (const t_physical_tile_loc tile_loc : device_ctx.grid.all_locations()) {
90+
t_physical_tile_type_ptr type = device_ctx.grid.get_physical_type(tile_loc);
91+
int width_offset = device_ctx.grid.get_width_offset(tile_loc);
92+
int height_offset = device_ctx.grid.get_height_offset(tile_loc);
93+
if (width_offset == 0 && height_offset == 0 && !type->is_io() && !type->is_empty()) {
94+
if (type->area == UNDEFINED) {
95+
area += grid_logic_tile_area * type->width * type->height;
96+
} else {
97+
area += type->area;
10198
}
102-
}
99+
}
103100
}
104101
/* Todo: need to add pitch of routing to blocks with height > 3 */
105102
VTR_LOG("\tTotal logic block area (Warning, need to add pitch of routing to blocks with height > 3): %g\n", area);
@@ -540,22 +537,19 @@ int count_netlist_clocks() {
540537
}
541538

542539
float calculate_device_utilization(const DeviceGrid& grid, const std::map<t_logical_block_type_ptr, size_t>& instance_counts) {
543-
//Record the resources of the grid
540+
// Record the resources of the grid
544541
std::map<t_physical_tile_type_ptr, size_t> grid_resources;
545-
for (int layer_num = 0; layer_num < grid.get_num_layers(); ++layer_num) {
546-
for (int x = 0; x < (int)grid.width(); ++x) {
547-
for (int y = 0; y < (int)grid.height(); ++y) {
548-
int width_offset = grid.get_width_offset({x, y, layer_num});
549-
int height_offset = grid.get_height_offset({x, y, layer_num});
550-
if (width_offset == 0 && height_offset == 0) {
551-
const auto& type = grid.get_physical_type({x, y, layer_num});
552-
++grid_resources[type];
553-
}
554-
}
542+
543+
for (const t_physical_tile_loc tile_loc : grid.all_locations()) {
544+
int width_offset = grid.get_width_offset(tile_loc);
545+
int height_offset = grid.get_height_offset(tile_loc);
546+
if (width_offset == 0 && height_offset == 0) {
547+
const t_physical_tile_type_ptr type = grid.get_physical_type(tile_loc);
548+
++grid_resources[type];
555549
}
556550
}
557551

558-
//Determine the area of grid in tile units
552+
// Determine the area of grid in tile units
559553
float grid_area = 0.;
560554
for (auto& kv : grid_resources) {
561555
t_physical_tile_type_ptr type = kv.first;
@@ -566,7 +560,7 @@ float calculate_device_utilization(const DeviceGrid& grid, const std::map<t_logi
566560
grid_area += type_area * count;
567561
}
568562

569-
//Determine the area of instances in tile units
563+
// Determine the area of instances in tile units
570564
float instance_area = 0.;
571565
for (auto& kv : instance_counts) {
572566
if (is_empty_type(kv.first)) {

vpr/src/place/delay_model/compute_delta_delays_utils.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,8 @@ static bool verify_delta_delays(const vtr::NdMatrix<float, 4>& delta_delays) {
395395
const auto& device_ctx = g_vpr_ctx.device();
396396
const auto& grid = device_ctx.grid;
397397

398-
for (int from_layer_num = 0; from_layer_num < grid.get_num_layers(); ++from_layer_num) {
399-
for (int to_layer_num = 0; to_layer_num < grid.get_num_layers(); ++to_layer_num) {
398+
for (size_t from_layer_num = 0; from_layer_num < grid.get_num_layers(); ++from_layer_num) {
399+
for (size_t to_layer_num = 0; to_layer_num < grid.get_num_layers(); ++to_layer_num) {
400400
for (size_t x = 0; x < grid.width(); ++x) {
401401
for (size_t y = 0; y < grid.height(); ++y) {
402402
float delta_delay = delta_delays[from_layer_num][to_layer_num][x][y];
@@ -838,7 +838,7 @@ bool find_direct_connect_sample_locations(const t_direct_inf* direct,
838838
bool found = false;
839839
int found_layer_num = -1;
840840
//TODO: Function *FOR NOW* assumes that from/to blocks are at same die and have a same layer nums
841-
for (int layer_num = 0; layer_num < grid.get_num_layers() && !found; ++layer_num) {
841+
for (int layer_num = 0; layer_num < (int)grid.get_num_layers() && !found; ++layer_num) {
842842
for (int x = 0; x < (int)grid.width() && !found; ++x) {
843843
to_x = x + direct->x_offset;
844844
if (to_x < 0 || to_x >= (int)grid.width()) continue;

vpr/src/place/net_cost_handler.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ void NetCostHandler::get_non_updatable_cube_bb_(ClusterNetId net_id, bool use_ts
494494
bb_coord_new.ymax = source_pin_loc.y;
495495
bb_coord_new.layer_max = source_pin_loc.layer_num;
496496

497-
for (int layer_num = 0; layer_num < device_ctx.grid.get_num_layers(); layer_num++) {
497+
for (size_t layer_num = 0; layer_num < device_ctx.grid.get_num_layers(); layer_num++) {
498498
num_sink_pin_layer[layer_num] = 0;
499499
}
500500

@@ -1096,9 +1096,11 @@ static void update_bb_pin_sink_count(const t_physical_tile_loc& pin_old_loc,
10961096
vtr::NdMatrixProxy<int, 1> bb_pin_sink_count_new,
10971097
bool is_output_pin) {
10981098
VTR_ASSERT_SAFE(curr_layer_pin_sink_count[pin_old_loc.layer_num] > 0 || is_output_pin);
1099-
for (int layer_num = 0; layer_num < g_vpr_ctx.device().grid.get_num_layers(); layer_num++) {
1099+
1100+
for (size_t layer_num = 0; layer_num < g_vpr_ctx.device().grid.get_num_layers(); layer_num++) {
11001101
bb_pin_sink_count_new[layer_num] = curr_layer_pin_sink_count[layer_num];
11011102
}
1103+
11021104
if (!is_output_pin) {
11031105
bb_pin_sink_count_new[pin_old_loc.layer_num] -= 1;
11041106
bb_pin_sink_count_new[pin_new_loc.layer_num] += 1;
@@ -1197,7 +1199,7 @@ void NetCostHandler::get_bb_from_scratch_(ClusterNetId net_id, bool use_ts) {
11971199
int ymax_edge = 1;
11981200
int layer_max_edge = 1;
11991201

1200-
for (int layer_num = 0; layer_num < grid.get_num_layers(); layer_num++) {
1202+
for (size_t layer_num = 0; layer_num < grid.get_num_layers(); layer_num++) {
12011203
num_sink_pin_layer[layer_num] = 0;
12021204
}
12031205

@@ -1549,7 +1551,7 @@ void NetCostHandler::update_move_nets() {
15491551

15501552
set_ts_bb_coord_(net_id);
15511553

1552-
for (int layer_num = 0; layer_num < g_vpr_ctx.device().grid.get_num_layers(); layer_num++) {
1554+
for (size_t layer_num = 0; layer_num < g_vpr_ctx.device().grid.get_num_layers(); layer_num++) {
15531555
num_sink_pin_layer_[size_t(net_id)][layer_num] = ts_layer_sink_pin_count_[size_t(net_id)][layer_num];
15541556
}
15551557

0 commit comments

Comments
 (0)