Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 32 additions & 21 deletions src/psm/src/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Connection::Connection(Node* node0, Node* node1) : node0_(node0), node1_(node1)

void Connection::ensureNodeOrder()
{
if (node0_ && !node0_->compare(node1_)) {
if (node0_ && node0_->compare(node1_) > 0) {
std::swap(node0_, node1_);
}
}
Expand Down Expand Up @@ -70,31 +70,42 @@ int Connection::getDBUs() const

bool Connection::compare(const Connection* other) const
{
return compareTuple() < other->compareTuple();
}

bool Connection::compare(const std::unique_ptr<Connection>& other) const
{
return compare(other.get());
}
if (other == nullptr) {
return false;
}
Comment thread
gadfort marked this conversation as resolved.

Connection::CompareInformation Connection::compareTuple() const
{
Node::CompareInformation node0_info;
if (node0_ != nullptr) {
node0_info = node0_->compareTuple();
} else {
node0_info = Node::dummyCompareTuple();
// Compare node0_ with null handling
if (node0_ == nullptr && other->node0_ != nullptr) {
return true;
}
if (node0_ != nullptr && other->node0_ == nullptr) {
return false;
}
if (node0_ != nullptr && other->node0_ != nullptr) {
const int cmp0 = node0_->compare(other->node0_);
if (cmp0 != 0) {
return cmp0 < 0;
}
}

Node::CompareInformation node1_info;
if (node1_ != nullptr) {
node1_info = node1_->compareTuple();
} else {
node1_info = Node::dummyCompareTuple();
// Compare node1_ with null handling
if (node1_ == nullptr && other->node1_ != nullptr) {
return true;
}
if (node1_ != nullptr && other->node1_ == nullptr) {
return false;
}
if (node1_ != nullptr && other->node1_ != nullptr) {
const int cmp1 = node1_->compare(other->node1_);
return cmp1 < 0;
}

return false;
}

return {node0_info, node1_info};
bool Connection::compare(const std::unique_ptr<Connection>& other) const
{
return compare(other.get());
}

std::string Connection::describeWithNodes() const
Expand Down
4 changes: 0 additions & 4 deletions src/psm/src/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,6 @@ class Connection
Node* node1_;

private:
using CompareInformation
= std::tuple<Node::CompareInformation, Node::CompareInformation>;
CompareInformation compareTuple() const;

template <typename T>
bool hasNodeOfType() const;
};
Expand Down
6 changes: 3 additions & 3 deletions src/psm/src/debug_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ bool NodeDescriptor::lessThan(const std::any& l, const std::any& r) const
{
auto l_node = std::any_cast<Node*>(l);
auto r_node = std::any_cast<Node*>(r);
return l_node->compare(r_node);
return l_node->compare(r_node) < 0;
}

void NodeDescriptor::highlight(const std::any& object,
Expand Down Expand Up @@ -195,7 +195,7 @@ bool ITermNodeDescriptor::lessThan(const std::any& l, const std::any& r) const
{
auto l_node = std::any_cast<ITermNode*>(l);
auto r_node = std::any_cast<ITermNode*>(r);
return l_node->compare(r_node);
return l_node->compare(r_node) < 0;
}

void ITermNodeDescriptor::highlight(const std::any& object,
Expand Down Expand Up @@ -251,7 +251,7 @@ bool BPinNodeDescriptor::lessThan(const std::any& l, const std::any& r) const
{
auto l_node = std::any_cast<BPinNode*>(l);
auto r_node = std::any_cast<BPinNode*>(r);
return l_node->compare(r_node);
return l_node->compare(r_node) < 0;
}

void BPinNodeDescriptor::highlight(const std::any& object,
Expand Down
28 changes: 21 additions & 7 deletions src/psm/src/ir_network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -709,9 +709,9 @@ std::set<Node*> IRNetwork::getSharedShapeNodes() const
const auto layer_shapes = getShapeTree(layer);

for (const auto& node : nodes) {
const Point pt(node->getPoint().x(), node->getPoint().y());
const auto shapes = std::distance(
layer_shapes.qbegin(boost::geometry::index::intersects(pt)),
layer_shapes.qbegin(
boost::geometry::index::intersects(node->getPoint())),
layer_shapes.qend());
if (shapes > 1) {
shared_nodes.insert(node.get());
Expand Down Expand Up @@ -804,7 +804,7 @@ void IRNetwork::sortNodes()

for (auto& [layer, nodes] : nodes_) {
std::ranges::stable_sort(nodes, [](const auto& lhs, const auto& rhs) {
return lhs->compare(rhs);
return lhs->compare(rhs) < 0;
});
}
}
Expand All @@ -813,10 +813,9 @@ void IRNetwork::sortConnections()
{
const utl::DebugScopedTimer timer(
logger_, utl::PSM, "timer", 1, "Sorting connections: {}");
std::ranges::stable_sort(
connections_,

[](const auto& lhs, const auto& rhs) { return lhs->compare(rhs); });
std::ranges::stable_sort(connections_, [](const auto& lhs, const auto& rhs) {
return lhs->compare(rhs);
});
}

int IRNetwork::getEffectiveNumberOfCuts(const odb::dbShape& shape) const
Expand Down Expand Up @@ -1299,4 +1298,19 @@ Node::NodeSet IRNetwork::getBPinShapeNodes() const
return pin_nodes;
}

void IRNetwork::clearVisitedNodes()
{
for (const auto& [layer, nodes] : nodes_) {
for (const auto& node : nodes) {
node->setVisited(false);
}
}
for (const auto& node : iterm_nodes_) {
node->setVisited(false);
}
for (const auto& node : bpin_nodes_) {
node->setVisited(false);
}
}

} // namespace psm
6 changes: 2 additions & 4 deletions src/psm/src/ir_network.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ class IRNetwork
template <typename T>
using LayerMap = std::map<odb::dbTechLayer*, T>;

using Point
= boost::geometry::model::d2::point_xy<int,
boost::geometry::cs::cartesian>;

using TerminalTree
= boost::geometry::index::rtree<TerminalNode*,
boost::geometry::index::quadratic<16>,
Expand Down Expand Up @@ -106,6 +102,8 @@ class IRNetwork

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

void clearVisitedNodes();

const Connections& getConnections() const { return connections_; }
NodePtrMap<Connection> getConnectionMap() const;

Expand Down
29 changes: 7 additions & 22 deletions src/psm/src/ir_solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,27 +141,12 @@ bool IRSolver::check(bool check_bterms)
return connected_.value();
}

bool IRSolver::wasNodeVisited(const std::unique_ptr<ITermNode>& node) const
{
return wasNodeVisited(node.get());
}

bool IRSolver::wasNodeVisited(const std::unique_ptr<Node>& node) const
{
return wasNodeVisited(node.get());
}

bool IRSolver::wasNodeVisited(const Node* node) const
{
return visited_.find(node) != visited_.end();
}

bool IRSolver::checkOpen()
{
const utl::DebugScopedTimer timer(
logger_, utl::PSM, "timer", 1, "Check open: {}");

visited_.clear();
network_->clearVisitedNodes();
const std::size_t total_nodes = network_->getNodeCount(true);
const auto connections_map = network_->getConnectionMap();

Expand All @@ -179,7 +164,7 @@ bool IRSolver::checkOpen()
while (!queue.empty()) {
Node* node = queue.front();
queue.pop();
if (wasNodeVisited(node)) {
if (node->isVisited()) {
// already been here, so we can continue to next node
continue;
}
Expand All @@ -196,11 +181,11 @@ bool IRSolver::checkOpen()
total_nodes);
}

visited_.insert(node);
node->setVisited(true);

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

for (const auto& [layer, layer_nodes] : network_->getNodes()) {
for (const auto& node : layer_nodes) {
if (wasNodeVisited(node)) {
if (node->isVisited()) {
continue;
}

Expand Down Expand Up @@ -249,7 +234,7 @@ IRSolver::ConnectivityResults IRSolver::getConnectivityResults() const

for (const auto& [layer, nodes] : network_->getNodes()) {
for (const auto& node : nodes) {
if (wasNodeVisited(node)) {
if (node->isVisited()) {
continue;
}

Expand All @@ -258,7 +243,7 @@ IRSolver::ConnectivityResults IRSolver::getConnectivityResults() const
}

for (const auto& node : network_->getITermNodes()) {
if (wasNodeVisited(node)) {
if (node->isVisited()) {
continue;
}

Expand Down
5 changes: 0 additions & 5 deletions src/psm/src/ir_solver.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,6 @@ class IRSolver

void reportUnconnectedNodes() const;
void reportMissingBTerm() const;
bool wasNodeVisited(const std::unique_ptr<ITermNode>& node) const;
bool wasNodeVisited(const std::unique_ptr<Node>& node) const;
bool wasNodeVisited(const Node* node) const;

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

const PDNSim::GeneratedSourceSettings& generated_source_settings_;

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

std::map<sta::Scene*, ValueNodeMap<Voltage>> voltages_;
Expand Down
52 changes: 36 additions & 16 deletions src/psm/src/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,50 @@ Node::Node(const odb::Point& pt, odb::dbTechLayer* layer)
{
}

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

return {layer, x, y, getType(), getTypeCompareInfo()};
}
// Compare layer
const int layer1 = layer_->getNumber();
const int layer2 = other->layer_->getNumber();
if (layer1 != layer2) {
return layer1 < layer2 ? -1 : 1;
}

Node::CompareInformation Node::dummyCompareTuple()
{
return {-1, 0, 0, NodeType::kUnknown, -1};
}
// Compare x coordinate
const int x1 = pt_.getX();
const int x2 = other->pt_.getX();
if (x1 != x2) {
return x1 < x2 ? -1 : 1;
}

bool Node::compare(const Node* other) const
{
if (other == nullptr) {
return true;
// Compare y coordinate
const int y1 = pt_.getY();
const int y2 = other->pt_.getY();
if (y1 != y2) {
return y1 < y2 ? -1 : 1;
}

return compareTuple() < other->compareTuple();
// Compare node type
const NodeType type1 = getType();
const NodeType type2 = other->getType();
if (type1 != type2) {
return type1 < type2 ? -1 : 1;
}

// Compare type-specific info
const int info1 = getTypeCompareInfo();
const int info2 = other->getTypeCompareInfo();
if (info1 == info2) {
return 0;
}
return info1 < info2 ? -1 : 1;
}

bool Node::compare(const std::unique_ptr<Node>& other) const
int Node::compare(const std::unique_ptr<Node>& other) const
{
return compare(other.get());
}
Expand Down
15 changes: 8 additions & 7 deletions src/psm/src/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Node
{
bool operator()(const Node* lhs, const Node* rhs) const
{
return lhs->compare(rhs);
return lhs->compare(rhs) < 0;
}
};

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

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably this should be operator<=> but that can wait for another PR


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

bool isVisited() const { return visited_; }
void setVisited(bool visited) { visited_ = visited; }

void print(utl::Logger* logger, const std::string& prefix = "") const;
virtual std::string describe(const std::string& prefix) const;

std::string getName() const;
std::string getTypeName() const;

using CompareInformation = std::tuple<int, int, int, NodeType, int>;
CompareInformation compareTuple() const;
static CompareInformation dummyCompareTuple();

protected:
virtual NodeType getType() const { return NodeType::kNode; }

Expand All @@ -78,6 +78,7 @@ class Node

odb::Point pt_;
odb::dbTechLayer* layer_;
bool visited_ = false;
};

class SourceNode : public Node
Expand Down
Loading
Loading