Skip to content

Commit e1ccc99

Browse files
authored
Merge pull request #10080 from gadfort/speedup1
psm: speedup
2 parents 55f3a06 + 30a92f4 commit e1ccc99

10 files changed

Lines changed: 110 additions & 91 deletions

File tree

src/psm/src/connection.cpp

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Connection::Connection(Node* node0, Node* node1) : node0_(node0), node1_(node1)
2020

2121
void Connection::ensureNodeOrder()
2222
{
23-
if (node0_ && !node0_->compare(node1_)) {
23+
if (node0_ && node0_->compare(node1_) > 0) {
2424
std::swap(node0_, node1_);
2525
}
2626
}
@@ -70,31 +70,42 @@ int Connection::getDBUs() const
7070

7171
bool Connection::compare(const Connection* other) const
7272
{
73-
return compareTuple() < other->compareTuple();
74-
}
75-
76-
bool Connection::compare(const std::unique_ptr<Connection>& other) const
77-
{
78-
return compare(other.get());
79-
}
73+
if (other == nullptr) {
74+
return false;
75+
}
8076

81-
Connection::CompareInformation Connection::compareTuple() const
82-
{
83-
Node::CompareInformation node0_info;
84-
if (node0_ != nullptr) {
85-
node0_info = node0_->compareTuple();
86-
} else {
87-
node0_info = Node::dummyCompareTuple();
77+
// Compare node0_ with null handling
78+
if (node0_ == nullptr && other->node0_ != nullptr) {
79+
return true;
80+
}
81+
if (node0_ != nullptr && other->node0_ == nullptr) {
82+
return false;
83+
}
84+
if (node0_ != nullptr && other->node0_ != nullptr) {
85+
const int cmp0 = node0_->compare(other->node0_);
86+
if (cmp0 != 0) {
87+
return cmp0 < 0;
88+
}
8889
}
8990

90-
Node::CompareInformation node1_info;
91-
if (node1_ != nullptr) {
92-
node1_info = node1_->compareTuple();
93-
} else {
94-
node1_info = Node::dummyCompareTuple();
91+
// Compare node1_ with null handling
92+
if (node1_ == nullptr && other->node1_ != nullptr) {
93+
return true;
94+
}
95+
if (node1_ != nullptr && other->node1_ == nullptr) {
96+
return false;
9597
}
98+
if (node1_ != nullptr && other->node1_ != nullptr) {
99+
const int cmp1 = node1_->compare(other->node1_);
100+
return cmp1 < 0;
101+
}
102+
103+
return false;
104+
}
96105

97-
return {node0_info, node1_info};
106+
bool Connection::compare(const std::unique_ptr<Connection>& other) const
107+
{
108+
return compare(other.get());
98109
}
99110

100111
std::string Connection::describeWithNodes() const

src/psm/src/connection.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,6 @@ class Connection
8484
Node* node1_;
8585

8686
private:
87-
using CompareInformation
88-
= std::tuple<Node::CompareInformation, Node::CompareInformation>;
89-
CompareInformation compareTuple() const;
90-
9187
template <typename T>
9288
bool hasNodeOfType() const;
9389
};

src/psm/src/debug_gui.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ bool NodeDescriptor::lessThan(const std::any& l, const std::any& r) const
138138
{
139139
auto l_node = std::any_cast<Node*>(l);
140140
auto r_node = std::any_cast<Node*>(r);
141-
return l_node->compare(r_node);
141+
return l_node->compare(r_node) < 0;
142142
}
143143

144144
void NodeDescriptor::highlight(const std::any& object,
@@ -195,7 +195,7 @@ bool ITermNodeDescriptor::lessThan(const std::any& l, const std::any& r) const
195195
{
196196
auto l_node = std::any_cast<ITermNode*>(l);
197197
auto r_node = std::any_cast<ITermNode*>(r);
198-
return l_node->compare(r_node);
198+
return l_node->compare(r_node) < 0;
199199
}
200200

201201
void ITermNodeDescriptor::highlight(const std::any& object,
@@ -251,7 +251,7 @@ bool BPinNodeDescriptor::lessThan(const std::any& l, const std::any& r) const
251251
{
252252
auto l_node = std::any_cast<BPinNode*>(l);
253253
auto r_node = std::any_cast<BPinNode*>(r);
254-
return l_node->compare(r_node);
254+
return l_node->compare(r_node) < 0;
255255
}
256256

257257
void BPinNodeDescriptor::highlight(const std::any& object,

src/psm/src/ir_network.cpp

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -709,9 +709,9 @@ std::set<Node*> IRNetwork::getSharedShapeNodes() const
709709
const auto layer_shapes = getShapeTree(layer);
710710

711711
for (const auto& node : nodes) {
712-
const Point pt(node->getPoint().x(), node->getPoint().y());
713712
const auto shapes = std::distance(
714-
layer_shapes.qbegin(boost::geometry::index::intersects(pt)),
713+
layer_shapes.qbegin(
714+
boost::geometry::index::intersects(node->getPoint())),
715715
layer_shapes.qend());
716716
if (shapes > 1) {
717717
shared_nodes.insert(node.get());
@@ -804,7 +804,7 @@ void IRNetwork::sortNodes()
804804

805805
for (auto& [layer, nodes] : nodes_) {
806806
std::ranges::stable_sort(nodes, [](const auto& lhs, const auto& rhs) {
807-
return lhs->compare(rhs);
807+
return lhs->compare(rhs) < 0;
808808
});
809809
}
810810
}
@@ -813,10 +813,9 @@ void IRNetwork::sortConnections()
813813
{
814814
const utl::DebugScopedTimer timer(
815815
logger_, utl::PSM, "timer", 1, "Sorting connections: {}");
816-
std::ranges::stable_sort(
817-
connections_,
818-
819-
[](const auto& lhs, const auto& rhs) { return lhs->compare(rhs); });
816+
std::ranges::stable_sort(connections_, [](const auto& lhs, const auto& rhs) {
817+
return lhs->compare(rhs);
818+
});
820819
}
821820

822821
int IRNetwork::getEffectiveNumberOfCuts(const odb::dbShape& shape) const
@@ -1299,4 +1298,19 @@ Node::NodeSet IRNetwork::getBPinShapeNodes() const
12991298
return pin_nodes;
13001299
}
13011300

1301+
void IRNetwork::clearVisitedNodes()
1302+
{
1303+
for (const auto& [layer, nodes] : nodes_) {
1304+
for (const auto& node : nodes) {
1305+
node->setVisited(false);
1306+
}
1307+
}
1308+
for (const auto& node : iterm_nodes_) {
1309+
node->setVisited(false);
1310+
}
1311+
for (const auto& node : bpin_nodes_) {
1312+
node->setVisited(false);
1313+
}
1314+
}
1315+
13021316
} // namespace psm

src/psm/src/ir_network.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@ class IRNetwork
4545
template <typename T>
4646
using LayerMap = std::map<odb::dbTechLayer*, T>;
4747

48-
using Point
49-
= boost::geometry::model::d2::point_xy<int,
50-
boost::geometry::cs::cartesian>;
51-
5248
using TerminalTree
5349
= boost::geometry::index::rtree<TerminalNode*,
5450
boost::geometry::index::quadratic<16>,
@@ -106,6 +102,8 @@ class IRNetwork
106102

107103
std::size_t getNodeCount(bool include_iterms = false) const;
108104

105+
void clearVisitedNodes();
106+
109107
const Connections& getConnections() const { return connections_; }
110108
NodePtrMap<Connection> getConnectionMap() const;
111109

src/psm/src/ir_solver.cpp

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -141,27 +141,12 @@ bool IRSolver::check(bool check_bterms)
141141
return connected_.value();
142142
}
143143

144-
bool IRSolver::wasNodeVisited(const std::unique_ptr<ITermNode>& node) const
145-
{
146-
return wasNodeVisited(node.get());
147-
}
148-
149-
bool IRSolver::wasNodeVisited(const std::unique_ptr<Node>& node) const
150-
{
151-
return wasNodeVisited(node.get());
152-
}
153-
154-
bool IRSolver::wasNodeVisited(const Node* node) const
155-
{
156-
return visited_.find(node) != visited_.end();
157-
}
158-
159144
bool IRSolver::checkOpen()
160145
{
161146
const utl::DebugScopedTimer timer(
162147
logger_, utl::PSM, "timer", 1, "Check open: {}");
163148

164-
visited_.clear();
149+
network_->clearVisitedNodes();
165150
const std::size_t total_nodes = network_->getNodeCount(true);
166151
const auto connections_map = network_->getConnectionMap();
167152

@@ -179,7 +164,7 @@ bool IRSolver::checkOpen()
179164
while (!queue.empty()) {
180165
Node* node = queue.front();
181166
queue.pop();
182-
if (wasNodeVisited(node)) {
167+
if (node->isVisited()) {
183168
// already been here, so we can continue to next node
184169
continue;
185170
}
@@ -196,11 +181,11 @@ bool IRSolver::checkOpen()
196181
total_nodes);
197182
}
198183

199-
visited_.insert(node);
184+
node->setVisited(true);
200185

201186
for (const auto* conn : connections_map.at(node)) {
202187
Node* next = conn->getOtherNode(node);
203-
if (wasNodeVisited(next)) {
188+
if (next->isVisited()) {
204189
// already been here, so we do not need to add it to the queue
205190
continue;
206191
}
@@ -210,7 +195,7 @@ bool IRSolver::checkOpen()
210195

211196
for (const auto& [layer, layer_nodes] : network_->getNodes()) {
212197
for (const auto& node : layer_nodes) {
213-
if (wasNodeVisited(node)) {
198+
if (node->isVisited()) {
214199
continue;
215200
}
216201

@@ -249,7 +234,7 @@ IRSolver::ConnectivityResults IRSolver::getConnectivityResults() const
249234

250235
for (const auto& [layer, nodes] : network_->getNodes()) {
251236
for (const auto& node : nodes) {
252-
if (wasNodeVisited(node)) {
237+
if (node->isVisited()) {
253238
continue;
254239
}
255240

@@ -258,7 +243,7 @@ IRSolver::ConnectivityResults IRSolver::getConnectivityResults() const
258243
}
259244

260245
for (const auto& node : network_->getITermNodes()) {
261-
if (wasNodeVisited(node)) {
246+
if (node->isVisited()) {
262247
continue;
263248
}
264249

src/psm/src/ir_solver.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,6 @@ class IRSolver
162162

163163
void reportUnconnectedNodes() const;
164164
void reportMissingBTerm() const;
165-
bool wasNodeVisited(const std::unique_ptr<ITermNode>& node) const;
166-
bool wasNodeVisited(const std::unique_ptr<Node>& node) const;
167-
bool wasNodeVisited(const Node* node) const;
168165

169166
std::map<Node*, Connection::ConnectionSet> getNodeConnectionMap(
170167
const Connection::ConnectionMap<Connection::Conductance>& conductance)
@@ -216,8 +213,6 @@ class IRSolver
216213

217214
const PDNSim::GeneratedSourceSettings& generated_source_settings_;
218215

219-
// Holds nodes that were visited during the open net check
220-
std::set<const Node*> visited_;
221216
std::optional<bool> connected_;
222217

223218
std::map<sta::Scene*, ValueNodeMap<Voltage>> voltages_;

src/psm/src/node.cpp

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,50 @@ Node::Node(const odb::Point& pt, odb::dbTechLayer* layer)
1818
{
1919
}
2020

21-
Node::CompareInformation Node::compareTuple() const
21+
int Node::compare(const Node* other) const
2222
{
23-
const int x = pt_.getX();
24-
const int y = pt_.getY();
25-
const int layer = layer_->getNumber();
23+
if (other == nullptr) {
24+
return 1;
25+
}
2626

27-
return {layer, x, y, getType(), getTypeCompareInfo()};
28-
}
27+
// Compare layer
28+
const int layer1 = layer_->getNumber();
29+
const int layer2 = other->layer_->getNumber();
30+
if (layer1 != layer2) {
31+
return layer1 < layer2 ? -1 : 1;
32+
}
2933

30-
Node::CompareInformation Node::dummyCompareTuple()
31-
{
32-
return {-1, 0, 0, NodeType::kUnknown, -1};
33-
}
34+
// Compare x coordinate
35+
const int x1 = pt_.getX();
36+
const int x2 = other->pt_.getX();
37+
if (x1 != x2) {
38+
return x1 < x2 ? -1 : 1;
39+
}
3440

35-
bool Node::compare(const Node* other) const
36-
{
37-
if (other == nullptr) {
38-
return true;
41+
// Compare y coordinate
42+
const int y1 = pt_.getY();
43+
const int y2 = other->pt_.getY();
44+
if (y1 != y2) {
45+
return y1 < y2 ? -1 : 1;
3946
}
4047

41-
return compareTuple() < other->compareTuple();
48+
// Compare node type
49+
const NodeType type1 = getType();
50+
const NodeType type2 = other->getType();
51+
if (type1 != type2) {
52+
return type1 < type2 ? -1 : 1;
53+
}
54+
55+
// Compare type-specific info
56+
const int info1 = getTypeCompareInfo();
57+
const int info2 = other->getTypeCompareInfo();
58+
if (info1 == info2) {
59+
return 0;
60+
}
61+
return info1 < info2 ? -1 : 1;
4262
}
4363

44-
bool Node::compare(const std::unique_ptr<Node>& other) const
64+
int Node::compare(const std::unique_ptr<Node>& other) const
4565
{
4666
return compare(other.get());
4767
}

src/psm/src/node.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class Node
4343
{
4444
bool operator()(const Node* lhs, const Node* rhs) const
4545
{
46-
return lhs->compare(rhs);
46+
return lhs->compare(rhs) < 0;
4747
}
4848
};
4949

@@ -52,22 +52,22 @@ class Node
5252
Node(const odb::Point& pt, odb::dbTechLayer* layer);
5353
virtual ~Node() = default;
5454

55-
bool compare(const Node* other) const;
56-
bool compare(const std::unique_ptr<Node>& other) const;
55+
// Returns -1 if this < other, 0 if equal, +1 if this > other
56+
int compare(const Node* other) const;
57+
int compare(const std::unique_ptr<Node>& other) const;
5758

5859
const odb::Point& getPoint() const { return pt_; };
5960
odb::dbTechLayer* getLayer() const { return layer_; };
6061

62+
bool isVisited() const { return visited_; }
63+
void setVisited(bool visited) { visited_ = visited; }
64+
6165
void print(utl::Logger* logger, const std::string& prefix = "") const;
6266
virtual std::string describe(const std::string& prefix) const;
6367

6468
std::string getName() const;
6569
std::string getTypeName() const;
6670

67-
using CompareInformation = std::tuple<int, int, int, NodeType, int>;
68-
CompareInformation compareTuple() const;
69-
static CompareInformation dummyCompareTuple();
70-
7171
protected:
7272
virtual NodeType getType() const { return NodeType::kNode; }
7373

@@ -78,6 +78,7 @@ class Node
7878

7979
odb::Point pt_;
8080
odb::dbTechLayer* layer_;
81+
bool visited_ = false;
8182
};
8283

8384
class SourceNode : public Node

0 commit comments

Comments
 (0)