Skip to content

Commit 66fa02d

Browse files
avoid copying Region and PartitionRegion unnecessarily
1 parent bcc45db commit 66fa02d

19 files changed

+197
-174
lines changed

vpr/src/base/partition.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
11
#include "partition.h"
22
#include "partition_region.h"
33
#include <algorithm>
4-
#include <vector>
4+
#include <utility>
55

6-
const std::string Partition::get_name() {
6+
const std::string& Partition::get_name() const{
77
return name;
88
}
99

1010
void Partition::set_name(std::string _part_name) {
11-
name = _part_name;
11+
name = std::move(_part_name);
1212
}
1313

14-
const PartitionRegion Partition::get_part_region() {
14+
const PartitionRegion& Partition::get_part_region() const {
15+
return part_region;
16+
}
17+
18+
PartitionRegion& Partition::get_mutable_part_region() {
1519
return part_region;
1620
}
1721

1822
void Partition::set_part_region(PartitionRegion pr) {
19-
part_region = pr;
23+
part_region = std::move(pr);
2024
}
2125

22-
void print_partition(FILE* fp, Partition part) {
23-
std::string name = part.get_name();
26+
void print_partition(FILE* fp, const Partition& part) {
27+
const std::string& name = part.get_name();
2428
fprintf(fp, "partition_name: %s\n", name.c_str());
2529

26-
PartitionRegion pr = part.get_part_region();
30+
const PartitionRegion& pr = part.get_part_region();
2731

2832
print_partition_region(fp, pr);
2933
}

vpr/src/base/partition.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Partition {
2828
/**
2929
* @brief Get the unique name of the partition
3030
*/
31-
const std::string get_name();
31+
const std::string& get_name() const;
3232

3333
/**
3434
* @brief Set the name of the partition
@@ -46,14 +46,19 @@ class Partition {
4646
/**
4747
* @brief Get the PartitionRegion (union of rectangular regions) for this partition
4848
*/
49-
const PartitionRegion get_part_region();
49+
const PartitionRegion& get_part_region() const;
50+
51+
/**
52+
* @brief Get the mutable PartitionRegion (union of rectangular regions) for this partition
53+
*/
54+
PartitionRegion& get_mutable_part_region();
5055

5156
private:
5257
std::string name; ///< name of the partition, name will be unique across partitions
5358
PartitionRegion part_region; ///< the union of regions that the partition can be placed in
5459
};
5560

5661
///@brief used to print data from a Partition
57-
void print_partition(FILE* fp, Partition part);
62+
void print_partition(FILE* fp, const Partition& part);
5863

5964
#endif /* PARTITION_H */

vpr/src/base/partition_region.cpp

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,34 @@
11
#include "partition_region.h"
22
#include "region.h"
33

4+
#include <utility>
5+
46
void PartitionRegion::add_to_part_region(Region region) {
5-
partition_region.push_back(region);
7+
regions.push_back(region);
68
}
79

8-
std::vector<Region> PartitionRegion::get_partition_region() {
9-
return partition_region;
10+
const std::vector<Region>& PartitionRegion::get_regions() const {
11+
return regions;
1012
}
1113

12-
std::vector<Region> PartitionRegion::get_partition_region() const {
13-
return partition_region;
14+
std::vector<Region>& PartitionRegion::get_mutable_regions() {
15+
return regions;
1416
}
1517

1618
void PartitionRegion::set_partition_region(std::vector<Region> pr) {
17-
partition_region = pr;
19+
regions = std::move(pr);
1820
}
1921

20-
bool PartitionRegion::empty() {
21-
return partition_region.size() == 0;
22+
bool PartitionRegion::empty() const {
23+
return regions.empty();
2224
}
2325

24-
bool PartitionRegion::is_loc_in_part_reg(t_pl_loc loc) {
26+
bool PartitionRegion::is_loc_in_part_reg(const t_pl_loc& loc) const {
2527
bool is_in_pr = false;
2628

27-
for (unsigned int i = 0; i < partition_region.size(); i++) {
28-
is_in_pr = partition_region[i].is_loc_in_reg(loc);
29-
if (is_in_pr == true) {
29+
for (const auto & region : regions) {
30+
is_in_pr = region.is_loc_in_reg(loc);
31+
if (is_in_pr) {
3032
break;
3133
}
3234
}
@@ -41,12 +43,13 @@ PartitionRegion intersection(const PartitionRegion& cluster_pr, const PartitionR
4143
* Rectangles are not merged even if it would be possible
4244
*/
4345
PartitionRegion pr;
46+
auto& pr_regions = pr.get_mutable_regions();
4447
Region intersect_region;
45-
for (unsigned int i = 0; i < cluster_pr.partition_region.size(); i++) {
46-
for (unsigned int j = 0; j < new_pr.partition_region.size(); j++) {
47-
intersect_region = intersection(cluster_pr.partition_region[i], new_pr.partition_region[j]);
48+
for (const auto& cluster_region : cluster_pr.get_regions()) {
49+
for (const auto& new_region : new_pr.get_regions()) {
50+
intersect_region = intersection(cluster_region, new_region);
4851
if (!intersect_region.empty()) {
49-
pr.partition_region.push_back(intersect_region);
52+
pr_regions.push_back(intersect_region);
5053
}
5154
}
5255
}
@@ -55,11 +58,11 @@ PartitionRegion intersection(const PartitionRegion& cluster_pr, const PartitionR
5558
}
5659

5760
void update_cluster_part_reg(PartitionRegion& cluster_pr, const PartitionRegion& new_pr) {
58-
Region intersect_region;
5961
std::vector<Region> int_regions;
60-
for (unsigned int i = 0; i < cluster_pr.partition_region.size(); i++) {
61-
for (unsigned int j = 0; j < new_pr.partition_region.size(); j++) {
62-
intersect_region = intersection(cluster_pr.partition_region[i], new_pr.partition_region[j]);
62+
63+
for (const auto& cluster_region : cluster_pr.get_regions()) {
64+
for (const auto& new_region : new_pr.get_regions()) {
65+
Region intersect_region = intersection(cluster_region, new_region);
6366
if (!intersect_region.empty()) {
6467
int_regions.push_back(intersect_region);
6568
}
@@ -68,14 +71,14 @@ void update_cluster_part_reg(PartitionRegion& cluster_pr, const PartitionRegion&
6871
cluster_pr.set_partition_region(int_regions);
6972
}
7073

71-
void print_partition_region(FILE* fp, PartitionRegion pr) {
72-
std::vector<Region> part_region = pr.get_partition_region();
74+
void print_partition_region(FILE* fp, const PartitionRegion& pr) {
75+
const std::vector<Region>& regions = pr.get_regions();
7376

74-
int pr_size = part_region.size();
77+
int pr_size = regions.size();
7578

7679
fprintf(fp, "\tNumber of regions in partition is: %d\n", pr_size);
7780

78-
for (unsigned int i = 0; i < part_region.size(); i++) {
79-
print_region(fp, part_region[i]);
81+
for (const auto & region : regions) {
82+
print_region(fp, region);
8083
}
8184
}

vpr/src/base/partition_region.h

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,12 @@ class PartitionRegion {
2525
/**
2626
* @brief Return the union of regions
2727
*/
28-
std::vector<Region> get_partition_region();
29-
std::vector<Region> get_partition_region() const;
28+
std::vector<Region>& get_mutable_regions();
29+
30+
/**
31+
* @brief Return the union of regions
32+
*/
33+
const std::vector<Region>& get_regions() const;
3034

3135
/**
3236
* @brief Set the union of regions
@@ -36,38 +40,38 @@ class PartitionRegion {
3640
/**
3741
* @brief Check if the PartitionRegion is empty (meaning there is no constraint on the object the PartitionRegion belongs to)
3842
*/
39-
bool empty();
43+
bool empty() const;
4044

4145
/**
4246
* @brief Check if the given location is within the legal bounds of the PartitionRegion.
4347
* The location provided is assumed to be valid.
4448
*
4549
* @param loc The location to be checked
4650
*/
47-
bool is_loc_in_part_reg(t_pl_loc loc);
48-
49-
/**
50-
* @brief Global friend function that returns the intersection of two PartitionRegions
51-
*
52-
* @param cluster_pr One of the PartitionRegions to be intersected
53-
* @param new_pr One of the PartitionRegions to be intersected
54-
*/
55-
friend PartitionRegion intersection(const PartitionRegion& cluster_pr, const PartitionRegion& new_pr);
56-
57-
/**
58-
* @brief Global friend function that updates the PartitionRegion of a cluster with the intersection
59-
* of the cluster PartitionRegion and a new PartitionRegion
60-
*
61-
* @param cluster_pr The cluster PartitionRegion that is to be updated
62-
* @param new_pr The new PartitionRegion that the cluster PartitionRegion will be intersected with
63-
*/
64-
friend void update_cluster_part_reg(PartitionRegion& cluster_pr, const PartitionRegion& new_pr);
51+
bool is_loc_in_part_reg(const t_pl_loc& loc) const;
6552

6653
private:
67-
std::vector<Region> partition_region; ///< union of rectangular regions that a partition can be placed in
54+
std::vector<Region> regions; ///< union of rectangular regions that a partition can be placed in
6855
};
6956

7057
///@brief used to print data from a PartitionRegion
71-
void print_partition_region(FILE* fp, PartitionRegion pr);
58+
void print_partition_region(FILE* fp, const PartitionRegion& pr);
59+
60+
/**
61+
* @brief Global friend function that returns the intersection of two PartitionRegions
62+
*
63+
* @param cluster_pr One of the PartitionRegions to be intersected
64+
* @param new_pr One of the PartitionRegions to be intersected
65+
*/
66+
PartitionRegion intersection(const PartitionRegion& cluster_pr, const PartitionRegion& new_pr);
67+
68+
/**
69+
* @brief Global friend function that updates the PartitionRegion of a cluster with the intersection
70+
* of the cluster PartitionRegion and a new PartitionRegion
71+
*
72+
* @param cluster_pr The cluster PartitionRegion that is to be updated
73+
* @param new_pr The new PartitionRegion that the cluster PartitionRegion will be intersected with
74+
*/
75+
void update_cluster_part_reg(PartitionRegion& cluster_pr, const PartitionRegion& new_pr);
7276

7377
#endif /* PARTITION_REGIONS_H */

vpr/src/base/region.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ bool Region::empty() {
4242
|| layer_num < 0);
4343
}
4444

45-
bool Region::is_loc_in_reg(t_pl_loc loc) {
45+
bool Region::is_loc_in_reg(t_pl_loc loc) const {
4646
bool is_loc_in_reg = false;
4747
int loc_layer_num = loc.layer;
4848

@@ -149,7 +149,7 @@ Region intersection(const Region& r1, const Region& r2) {
149149
return intersect;
150150
}
151151

152-
void print_region(FILE* fp, Region region) {
152+
void print_region(FILE* fp, const Region& region) {
153153
const auto region_coord = region.get_region_rect();
154154
const auto region_rect = vtr::Rect<int>(region_coord.xmin, region_coord.ymin, region_coord.xmax, region_coord.ymax);
155155
fprintf(fp, "\tRegion: \n");

vpr/src/base/region.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ struct RegionRectCoord {
4343
bool operator==(const RegionRectCoord& rhs) const {
4444
vtr::Rect<int> lhs_rect(xmin, ymin, xmax, ymax);
4545
vtr::Rect<int> rhs_rect(rhs.xmin, rhs.ymin, rhs.xmax, rhs.ymax);
46-
return lhs_rect == rhs_rect
47-
&& layer_num == rhs.layer_num;
46+
return (lhs_rect == rhs_rect) && (layer_num == rhs.layer_num);
4847
}
4948
};
5049

@@ -105,7 +104,7 @@ class Region {
105104
*
106105
* @param loc The location to be checked
107106
*/
108-
bool is_loc_in_reg(t_pl_loc loc);
107+
bool is_loc_in_reg(t_pl_loc loc) const;
109108

110109
bool operator==(const Region& reg) const {
111110
return (reg.get_region_rect() == this->get_region_rect()
@@ -142,7 +141,7 @@ bool do_regions_intersect(Region r1, Region r2);
142141
Region intersection(const Region& r1, const Region& r2);
143142

144143
///@brief Used to print data from a Region
145-
void print_region(FILE* fp, Region region);
144+
void print_region(FILE* fp, const Region& region);
146145

147146
namespace std {
148147
template<>

vpr/src/base/vpr_constraints.cpp

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "vpr_constraints.h"
22
#include "partition.h"
33

4-
void VprConstraints::add_constrained_atom(const AtomBlockId blk_id, const PartitionId part_id) {
4+
void VprConstraints::add_constrained_atom(AtomBlockId blk_id, PartitionId part_id) {
55
auto got = constrained_atoms.find(blk_id);
66

77
/**
@@ -16,27 +16,29 @@ void VprConstraints::add_constrained_atom(const AtomBlockId blk_id, const Partit
1616
}
1717
}
1818

19-
PartitionId VprConstraints::get_atom_partition(AtomBlockId blk_id) {
20-
PartitionId part_id;
21-
19+
PartitionId VprConstraints::get_atom_partition(AtomBlockId blk_id) const {
2220
auto got = constrained_atoms.find(blk_id);
2321

2422
if (got == constrained_atoms.end()) {
25-
return part_id = PartitionId::INVALID(); ///< atom is not in a partition, i.e. unconstrained
23+
return PartitionId::INVALID(); ///< atom is not in a partition, i.e. unconstrained
2624
} else {
2725
return got->second;
2826
}
2927
}
3028

31-
void VprConstraints::add_partition(Partition part) {
29+
void VprConstraints::add_partition(const Partition& part) {
3230
partitions.push_back(part);
3331
}
3432

35-
Partition VprConstraints::get_partition(PartitionId part_id) {
33+
const Partition& VprConstraints::get_partition(PartitionId part_id) const {
34+
return partitions[part_id];
35+
}
36+
37+
Partition& VprConstraints::get_mutable_partition(PartitionId part_id) {
3638
return partitions[part_id];
3739
}
3840

39-
std::vector<AtomBlockId> VprConstraints::get_part_atoms(PartitionId part_id) {
41+
std::vector<AtomBlockId> VprConstraints::get_part_atoms(PartitionId part_id) const {
4042
std::vector<AtomBlockId> part_atoms;
4143

4244
for (auto& it : constrained_atoms) {
@@ -48,18 +50,19 @@ std::vector<AtomBlockId> VprConstraints::get_part_atoms(PartitionId part_id) {
4850
return part_atoms;
4951
}
5052

51-
int VprConstraints::get_num_partitions() {
53+
int VprConstraints::get_num_partitions() const {
5254
return partitions.size();
5355
}
5456

55-
PartitionRegion VprConstraints::get_partition_pr(PartitionId part_id) {
56-
PartitionRegion pr;
57-
pr = partitions[part_id].get_part_region();
58-
return pr;
57+
const PartitionRegion& VprConstraints::get_partition_pr(PartitionId part_id) const {
58+
return partitions[part_id].get_part_region();
59+
}
60+
61+
PartitionRegion& VprConstraints::get_mutable_partition_pr(PartitionId part_id) {
62+
return partitions[part_id].get_mutable_part_region();
5963
}
6064

61-
void print_constraints(FILE* fp, VprConstraints constraints) {
62-
Partition temp_part;
65+
void print_constraints(FILE* fp, const VprConstraints& constraints) {
6366
std::vector<AtomBlockId> atoms;
6467

6568
int num_parts = constraints.get_num_partitions();
@@ -69,7 +72,7 @@ void print_constraints(FILE* fp, VprConstraints constraints) {
6972
for (int i = 0; i < num_parts; i++) {
7073
PartitionId part_id(i);
7174

72-
temp_part = constraints.get_partition(part_id);
75+
const Partition& temp_part = constraints.get_partition(part_id);
7376

7477
fprintf(fp, "\npartition_id: %zu\n", size_t(part_id));
7578
print_partition(fp, temp_part);
@@ -80,8 +83,7 @@ void print_constraints(FILE* fp, VprConstraints constraints) {
8083

8184
fprintf(fp, "\tAtom vector size is %d\n", atoms_size);
8285
fprintf(fp, "\tIds of atoms in partition: \n");
83-
for (unsigned int j = 0; j < atoms.size(); j++) {
84-
AtomBlockId atom_id = atoms[j];
86+
for (auto atom_id : atoms) {
8587
fprintf(fp, "\t#%zu\n", size_t(atom_id));
8688
}
8789
}

0 commit comments

Comments
 (0)