Skip to content

Commit 88b3970

Browse files
create a grid in test_compressed_grid
1 parent 55e2328 commit 88b3970

File tree

5 files changed

+199
-19
lines changed

5 files changed

+199
-19
lines changed

libs/libarchfpga/src/device_grid.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ DeviceGrid::DeviceGrid(std::string grid_name, vtr::NdMatrix<t_grid_tile, 3> grid
88
count_instances();
99
}
1010

11-
DeviceGrid::DeviceGrid(std::string grid_name, vtr::NdMatrix<t_grid_tile, 3> grid, std::vector<t_logical_block_type_ptr> limiting_res)
11+
DeviceGrid::DeviceGrid(std::string grid_name,
12+
vtr::NdMatrix<t_grid_tile, 3> grid,
13+
std::vector<t_logical_block_type_ptr> limiting_res)
1214
: DeviceGrid(std::move(grid_name), std::move(grid)) {
1315
limiting_resources_ = std::move(limiting_res);
1416
}

libs/libarchfpga/src/device_grid.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ class DeviceGrid {
118118
* @note which can be used for indexing in the second dimension, allowing
119119
* @note traditional 2-d indexing to be used
120120
*/
121-
vtr::NdMatrix<t_grid_tile, 3> grid_; //This stores the grid of complex blocks. It is a a 3D matrix: [0..num_layers-1][0..grid.width()-1][0..grid_height()-1]
121+
vtr::NdMatrix<t_grid_tile, 3> grid_; //This stores the grid of complex blocks. It is a 3D matrix: [0..num_layers-1][0..grid.width()-1][0..grid_height()-1]
122122

123123
///@brief instance_counts_ stores the number of each tile type on each layer. It is initialized in count_instances().
124124
std::vector<std::map<t_physical_tile_type_ptr, size_t>> instance_counts_; /* [layer_num][physical_tile_type_ptr] */

vpr/src/place/compressed_grid.cpp

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,54 @@
22
#include "arch_util.h"
33
#include "globals.h"
44

5+
/**
6+
* @brief Creates a compressed grid from the given locations.
7+
*
8+
*
9+
* @param locations The location of logical blocks of a specific type.
10+
* [0...layer-1][0...num_instances_on_layer-1] --> (x, y)
11+
* @param num_layers The number of dice.
12+
* @return t_compressed_block_grid The compressed grid created from the given locations.
13+
*/
14+
static t_compressed_block_grid create_compressed_block_grid(const std::vector<std::vector<vtr::Point<int>>>& locations,
15+
int num_layers);
16+
17+
518
std::vector<t_compressed_block_grid> create_compressed_block_grids() {
619
auto& device_ctx = g_vpr_ctx.device();
720
auto& grid = device_ctx.grid;
821
const int num_layers = grid.get_num_layers();
922

10-
//Collect the set of x/y locations for each instace of a block type
11-
std::vector<std::vector<std::vector<vtr::Point<int>>>> block_locations(device_ctx.logical_block_types.size()); // [logical_block_type][layer_num][0...num_instance_on_layer] -> (x, y)
12-
for (int block_type_num = 0; block_type_num < (int)device_ctx.logical_block_types.size(); block_type_num++) {
13-
block_locations[block_type_num].resize(num_layers);
23+
//Collect the set of x/y locations for each instance of a block type
24+
// [logical_block_type][layer_num][0...num_instance_on_layer] -> (x, y)
25+
std::vector<std::vector<std::vector<vtr::Point<int>>>> block_locations(device_ctx.logical_block_types.size());
26+
27+
for (const auto& block_type : device_ctx.logical_block_types) {
28+
block_locations[block_type.index].resize(num_layers);
1429
}
1530

1631
for (int layer_num = 0; layer_num < num_layers; layer_num++) {
1732
for (int x = 0; x < (int)grid.width(); ++x) {
1833
for (int y = 0; y < (int)grid.height(); ++y) {
1934
int width_offset = grid.get_width_offset({x, y, layer_num});
2035
int height_offset = grid.get_height_offset(t_physical_tile_loc(x, y, layer_num));
21-
if (width_offset == 0 && height_offset == 0) {
36+
37+
if (width_offset == 0 && height_offset == 0) { // the bottom left corner of a tile
2238
const auto& type = grid.get_physical_type({x, y, layer_num});
2339
auto equivalent_sites = get_equivalent_sites_set(type);
2440

25-
for (auto& block : equivalent_sites) {
41+
for (t_logical_block_type_ptr block_type : equivalent_sites) {
2642
//Only record at block root location
27-
block_locations[block->index][layer_num].emplace_back(x, y);
43+
block_locations[block_type->index][layer_num].emplace_back(x, y);
2844
}
2945
}
3046
}
3147
}
3248
}
3349

50+
// each logical block type has its own compressed grid
3451
std::vector<t_compressed_block_grid> compressed_type_grids(device_ctx.logical_block_types.size());
52+
3553
for (const auto& logical_block : device_ctx.logical_block_types) {
3654
auto compressed_block_grid = create_compressed_block_grid(block_locations[logical_block.index], num_layers);
3755

@@ -62,22 +80,22 @@ std::vector<t_compressed_block_grid> create_compressed_block_grids() {
6280
}
6381

6482
//Given a set of locations, returns a 2D matrix in a compressed space
65-
t_compressed_block_grid create_compressed_block_grid(const std::vector<std::vector<vtr::Point<int>>>& locations, int num_layers) {
83+
static t_compressed_block_grid create_compressed_block_grid(const std::vector<std::vector<vtr::Point<int>>>& locations,
84+
int num_layers) {
6685
t_compressed_block_grid compressed_grid;
6786

6887
if (locations.empty()) {
6988
return compressed_grid;
7089
}
7190

7291
{
73-
std::vector<std::vector<int>> x_locs(num_layers);
74-
std::vector<std::vector<int>> y_locs(num_layers);
7592
compressed_grid.compressed_to_grid_x.resize(num_layers);
7693
compressed_grid.compressed_to_grid_y.resize(num_layers);
94+
7795
for (int layer_num = 0; layer_num < num_layers; layer_num++) {
78-
auto& layer_x_locs = x_locs[layer_num];
79-
auto& layer_y_locs = y_locs[layer_num];
80-
//Record all the x/y locations seperately
96+
std::vector<int> layer_x_locs;
97+
std::vector<int> layer_y_locs;
98+
//Record all the x/y locations separately
8199
for (auto point : locations[layer_num]) {
82100
layer_x_locs.emplace_back(point.x());
83101
layer_y_locs.emplace_back(point.y());
@@ -97,6 +115,9 @@ t_compressed_block_grid create_compressed_block_grid(const std::vector<std::vect
97115
}
98116
compressed_grid.compressed_to_grid_x[layer_num] = std::move(layer_x_locs);
99117
compressed_grid.compressed_to_grid_y[layer_num] = std::move(layer_y_locs);
118+
119+
layer_x_locs.clear();
120+
layer_y_locs.clear();
100121
}
101122
}
102123

@@ -142,7 +163,7 @@ t_compressed_block_grid create_compressed_block_grid(const std::vector<std::vect
142163
}
143164

144165
/*Print the contents of the compressed grids to an echo file*/
145-
void echo_compressed_grids(char* filename, const std::vector<t_compressed_block_grid>& comp_grids) {
166+
void echo_compressed_grids(const char* filename, const std::vector<t_compressed_block_grid>& comp_grids) {
146167
FILE* fp;
147168
fp = vtr::fopen(filename, "w");
148169

vpr/src/place/compressed_grid.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,13 @@ typedef std::vector<t_compressed_block_grid> t_compressed_block_grids;
119119

120120
std::vector<t_compressed_block_grid> create_compressed_block_grids();
121121

122-
t_compressed_block_grid create_compressed_block_grid(const std::vector<std::vector<vtr::Point<int>>>& locations, int num_layers);
123-
124122
/**
125123
* @brief print the contents of the compressed grids to an echo file
126124
*
127125
* @param filename the name of the file to print to
128126
* @param comp_grids the compressed grids that are used during placement
129127
*
130128
*/
131-
void echo_compressed_grids(char* filename, const std::vector<t_compressed_block_grid>& comp_grids);
129+
void echo_compressed_grids(const char* filename, const std::vector<t_compressed_block_grid>& comp_grids);
132130

133131
#endif

vpr/test/test_compressed_grid.cpp

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
#include "catch2/catch_test_macros.hpp"
2+
#include "catch2/matchers/catch_matchers_all.hpp"
3+
4+
#include "compressed_grid.h"
5+
#include "globals.h"
6+
#include "physical_types.h"
7+
8+
// for comparing floats
9+
#include "vtr_math.h"
10+
11+
namespace {
12+
13+
void set_type_tile_to_empty(const int x, const int y,
14+
vtr::NdMatrix<t_grid_tile, 3>& grid) {
15+
t_physical_tile_type_ptr type = grid[0][x][y].type;
16+
const int width_offset = grid[0][x][y].width_offset;
17+
const int height_offset = grid[0][x][y].height_offset;
18+
const int x_anchor = x - width_offset;
19+
const int y_anchor = y - height_offset;
20+
21+
for (int i = x_anchor; i < x_anchor + type->width; i++) {
22+
for (int j = y_anchor; j < y_anchor + type->height; j++) {
23+
if (grid[0][i][j].type == type && grid[0][i][j].width_offset == i - x_anchor && grid[0][i][j].height_offset == j - y_anchor) {
24+
grid[0][i][j].type = g_vpr_ctx.device().EMPTY_PHYSICAL_TILE_TYPE;
25+
grid[0][i][j].width_offset = 0;
26+
grid[0][i][j].height_offset = 0;
27+
}
28+
}
29+
}
30+
31+
}
32+
33+
void set_tile_type_at_loc(const int x_anchor, const int y_anchor,
34+
vtr::NdMatrix<t_grid_tile, 3>& grid,
35+
const t_physical_tile_type& tile_type) {
36+
37+
for (int i = x_anchor; i < x_anchor + tile_type.width; i++) {
38+
for (int j = y_anchor; j < y_anchor + tile_type.height; j++) {
39+
if (grid[0][i][j].type != g_vpr_ctx.device().EMPTY_PHYSICAL_TILE_TYPE) {
40+
set_type_tile_to_empty(i, j, grid);
41+
}
42+
grid[0][i][j].type = &tile_type;
43+
grid[0][i][j].width_offset = i - x_anchor;
44+
grid[0][i][j].height_offset = j - y_anchor;
45+
}
46+
}
47+
}
48+
49+
TEST_CASE("test_compressed_grid", "[vpr_compressed_grid]") {
50+
// test device grid name
51+
std::string device_grid_name = "test";
52+
53+
// creating a reference for the empty tile name and router name
54+
char empty_tile_name[] = "empty";
55+
char io_tile_name[] = "io";
56+
char small_tile_name[] = "small";
57+
char tall_tile_name[] = "tall";
58+
char large_tile_name[] = "large";
59+
60+
// device grid parameters
61+
const int test_grid_width = 100;
62+
const int test_grid_height = 100;
63+
64+
// create the test device grid (10x10)
65+
auto test_grid = vtr::NdMatrix<t_grid_tile, 3>({1, test_grid_width, test_grid_height});
66+
67+
t_logical_block_type EMPTY_LOGICAL_BLOCK_TYPE = get_empty_logical_type();
68+
EMPTY_LOGICAL_BLOCK_TYPE.index = 0;
69+
70+
t_physical_tile_type empty_tile;
71+
empty_tile.name = empty_tile_name;
72+
empty_tile.height = 1;
73+
empty_tile.width = 1;
74+
75+
g_vpr_ctx.mutable_device().EMPTY_PHYSICAL_TILE_TYPE = &empty_tile;
76+
77+
// create an io physical tile and assign its parameters
78+
t_physical_tile_type io_tile;
79+
io_tile.name = io_tile_name;
80+
io_tile.height = 1;
81+
io_tile.width = 1;
82+
83+
t_logical_block_type io_logical_type;
84+
io_logical_type.index = 1;
85+
io_logical_type.equivalent_tiles.push_back(&io_tile);
86+
87+
// create a small tile and assign its parameters
88+
t_physical_tile_type small_tile;
89+
small_tile.name = small_tile_name;
90+
small_tile.height = 1;
91+
small_tile.width = 1;
92+
93+
t_logical_block_type small_logical_type;
94+
small_logical_type.index = 2;
95+
small_logical_type.equivalent_tiles.push_back(&small_tile);
96+
97+
// create a small tile and assign its parameters
98+
t_physical_tile_type tall_tile;
99+
tall_tile.name = tall_tile_name;
100+
tall_tile.height = 4;
101+
tall_tile.width = 1;
102+
103+
t_logical_block_type tall_logical_type;
104+
tall_logical_type.index = 3;
105+
tall_logical_type.equivalent_tiles.push_back(&tall_tile);
106+
107+
t_physical_tile_type large_tile;
108+
large_tile.name = large_tile_name;
109+
large_tile.height = 3;
110+
large_tile.width = 3;
111+
112+
t_logical_block_type large_logical_type;
113+
large_logical_type.index = 4;
114+
large_logical_type.equivalent_tiles.push_back(&large_tile);
115+
116+
117+
for (int x = 0; x < test_grid_width; x++) {
118+
for (int y = 0; y < test_grid_height; y++) {
119+
test_grid[0][x][y].type = &io_tile;
120+
test_grid[0][x][y].height_offset = 0;
121+
test_grid[0][x][y].width_offset = 0;
122+
}
123+
}
124+
125+
for (int x = 1; x < test_grid_width - 1; x++) {
126+
for (int y = 1; y < test_grid_height - 1; y++) {
127+
set_tile_type_at_loc(x, y, test_grid, small_tile);
128+
}
129+
}
130+
131+
for (int x = 7; x < test_grid_width - 7; x += 10) {
132+
for (int y = 5; y < test_grid_height - 5; y += 5) {
133+
set_tile_type_at_loc(x, y, test_grid, tall_tile);
134+
}
135+
}
136+
137+
for (int x = 8; x < test_grid_width - 8; x += 17) {
138+
for (int y = 7; y < test_grid_height - 6; y += 13) {
139+
set_tile_type_at_loc(x, y, test_grid, large_tile);
140+
}
141+
}
142+
143+
auto& logical_block_types = g_vpr_ctx.mutable_device().logical_block_types;
144+
logical_block_types.clear();
145+
146+
auto& grid = g_vpr_ctx.mutable_device().grid;
147+
grid = DeviceGrid("test_device_grid", test_grid);
148+
149+
std::vector<t_compressed_block_grid> compressed_grids = create_compressed_block_grids();
150+
151+
echo_compressed_grids("havij", compressed_grids);
152+
153+
// SECTION("All routers are seperated by one or more grid spaces") {
154+
// // in this test, the routers will be on the 4 corners of the FPGA
155+
// }
156+
157+
}
158+
159+
} // namespace

0 commit comments

Comments
 (0)