Skip to content

Commit

Permalink
psm: ensure all connection maps are sorted
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Gadfort <[email protected]>
  • Loading branch information
gadfort committed Oct 22, 2024
1 parent 6833b0f commit 627808b
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 60 deletions.
28 changes: 10 additions & 18 deletions src/psm/src/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,31 +102,23 @@ bool Connection::compare(const std::unique_ptr<Connection>& other) const
return compare(other.get());
}

std::tuple<int, int, int, int, int, int> Connection::compareTuple() const
Connection::CompareInformation Connection::compareTuple() const
{
int node0_x = 0;
int node0_y = 0;
int node0_l = 0;

int node1_x = 0;
int node1_y = 0;
int node1_l = 0;

Node::CompareInformation node0_info;
if (node0_ != nullptr) {
const auto& pt = node0_->getPoint();
node0_x = pt.getX();
node0_y = pt.getY();
node0_l = node0_->getLayer()->getNumber();
node0_info = node0_->compareTuple();
} else {
node0_info = Node::dummyCompareTuple();
}

Node::CompareInformation node1_info;
if (node1_ != nullptr) {
const auto& pt = node1_->getPoint();
node1_x = pt.getX();
node1_y = pt.getY();
node1_l = node1_->getLayer()->getNumber();
node1_info = node1_->compareTuple();
} else {
node1_info = Node::dummyCompareTuple();
}

return {node0_l, node0_x, node0_y, node1_l, node1_x, node1_y};
return {node0_info, node1_info};
}

std::string Connection::describeWithNodes() const
Expand Down
10 changes: 8 additions & 2 deletions src/psm/src/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
#include <string>
#include <tuple>

#include "node.h"

namespace utl {
class Logger;
}
Expand All @@ -47,7 +49,6 @@ class dbTechLayer;
}

namespace psm {
class Node;

class Connection
{
Expand All @@ -69,6 +70,9 @@ class Connection

using ConnectionSet = std::set<Connection*, Compare>;

template <typename T>
using ConnectionMap = std::map<Connection*, T, Compare>;

Connection(Node* node0, Node* node1);
virtual ~Connection() = default;

Expand Down Expand Up @@ -108,7 +112,9 @@ class Connection
Node* node1_;

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

template <typename T>
bool hasNodeOfType() const;
Expand Down
1 change: 1 addition & 0 deletions src/psm/src/ir_network.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <optional>
#include <vector>

#include "connection.h"
#include "node.h"
#include "odb/db.h"
#include "psm/pdnsim.h"
Expand Down
33 changes: 14 additions & 19 deletions src/psm/src/ir_solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,15 +391,15 @@ Connection::ResistanceMap IRSolver::getResistanceMap(sta::Corner* corner) const
return resistance;
}

std::map<Connection*, Connection::Conductance> IRSolver::generateConductanceMap(
sta::Corner* corner) const
Connection::ConnectionMap<Connection::Conductance>
IRSolver::generateConductanceMap(sta::Corner* corner) const
{
const utl::DebugScopedTimer timer(
logger_, utl::PSM, "timer", 1, "Generate conductance map: {}");

const Connection::ResistanceMap resistance = getResistanceMap(corner);

std::map<Connection*, Connection::Conductance> conductance;
Connection::ConnectionMap<Connection::Conductance> conductance;
for (const auto& conn : network_->getConnections()) {
const auto res = conn->getResistance(resistance);
conductance[conn.get()] = 1.0 / res;
Expand Down Expand Up @@ -771,8 +771,7 @@ void IRSolver::buildNodeCurrentMap(sta::Corner* corner,
}

std::map<Node*, Connection::ConnectionSet> IRSolver::getNodeConnectionMap(
const std::map<psm::Connection*, Connection::Conductance>& conductance)
const
const Connection::ConnectionMap<Connection::Conductance>& conductance) const
{
const utl::DebugScopedTimer timer(
logger_, utl::PSM, "timer", 1, "Build node/connection mapping: {}");
Expand Down Expand Up @@ -815,7 +814,7 @@ void IRSolver::buildCondMatrixAndVoltages(
bool is_ground,
const std::map<Node*, Connection::ConnectionSet>& node_connections,
const ValueNodeMap<Current>& currents,
const std::map<psm::Connection*, Connection::Conductance>& conductance,
const Connection::ConnectionMap<Connection::Conductance>& conductance,
const std::map<Node*, std::size_t>& node_index,
Eigen::SparseMatrix<Connection::Conductance>& G,
Eigen::VectorXd& J) const
Expand Down Expand Up @@ -1403,12 +1402,8 @@ void IRSolver::writeEMFile(const std::string& em_file,
report << "Node0 Layer,Node0 X location,Node0 Y location,Node1 Layer,Node1 X "
"location,Node1 Y location,Current\n";

const auto current_map = generateCurrentMap(corner);
const std::map<Connection*, Current, Connection::Compare> sorted_current_map(
current_map.begin(), current_map.end());

const double dbus = getBlock()->getDbUnitsPerMicron();
for (const auto& [connection, current] : sorted_current_map) {
for (const auto& [connection, current] : generateCurrentMap(corner)) {
const Node* node0 = connection->getNode0();
const Node* node1 = connection->getNode1();

Expand Down Expand Up @@ -1499,11 +1494,11 @@ void IRSolver::writeSpiceFile(GeneratedSourceType source_type,
spice << ".END\n\n";
}

std::map<Connection*, IRSolver::Current> IRSolver::generateCurrentMap(
Connection::ConnectionMap<IRSolver::Current> IRSolver::generateCurrentMap(
sta::Corner* corner) const
{
const auto& voltages = voltages_.at(corner);
std::map<Connection*, IRSolver::Current> currents;
Connection::ConnectionMap<IRSolver::Current> currents;
for (const auto& [connection, cond] : generateConductanceMap(corner)) {
if (connection->hasITermNode() || connection->hasBPinNode()) {
continue;
Expand Down Expand Up @@ -1554,7 +1549,7 @@ void IRSolver::dumpMatrix(
}

void IRSolver::dumpConductance(
const std::map<Connection*, Connection::Conductance>& cond,
const Connection::ConnectionMap<Connection::Conductance>& cond,
const std::string& name) const
{
const std::string report_file = fmt::format("psm_{}.txt", name);
Expand All @@ -1563,14 +1558,14 @@ void IRSolver::dumpConductance(
logger_->report("Failed to open {} for {}", report_file, name);
return;
}
const std::map<Connection*, Connection::Conductance, Connection::Compare>
sorted_cond(cond.begin(), cond.end());

for (const auto& [connection, cond] : sorted_cond) {
for (const auto& [connection, conductance] : cond) {
const Node* node0 = connection->getNode0();
const Node* node1 = connection->getNode1();
report << fmt::format(
"{} -> {}: {:.15e}", node0->describe(""), node1->describe(""), cond)
report << fmt::format("{} -> {}: {:.15e}",
node0->describe(""),
node1->describe(""),
conductance)
<< '\n';
}
}
Expand Down
12 changes: 7 additions & 5 deletions src/psm/src/ir_solver.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include <optional>
#include <vector>

#include "connection.h"
#include "debug_gui.h"
#include "ir_network.h"
#include "node.h"
Expand Down Expand Up @@ -158,9 +159,10 @@ class IRSolver
std::map<odb::dbInst*, Power> getInstancePower(sta::Corner* corner) const;
Voltage getPowerNetVoltage(sta::Corner* corner) const;

std::map<Connection*, Current> generateCurrentMap(sta::Corner* corner) const;
Connection::ConnectionMap<Current> generateCurrentMap(
sta::Corner* corner) const;

std::map<Connection*, Connection::Conductance> generateConductanceMap(
Connection::ConnectionMap<Connection::Conductance> generateConductanceMap(
sta::Corner* corner) const;
Voltage generateSourceNodes(
GeneratedSourceType source_type,
Expand Down Expand Up @@ -188,7 +190,7 @@ class IRSolver
bool wasNodeVisited(const Node* node) const;

std::map<Node*, Connection::ConnectionSet> getNodeConnectionMap(
const std::map<psm::Connection*, Connection::Conductance>& conductance)
const Connection::ConnectionMap<Connection::Conductance>& conductance)
const;
void buildNodeCurrentMap(sta::Corner* corner,
ValueNodeMap<Current>& currents) const;
Expand All @@ -201,7 +203,7 @@ class IRSolver
bool is_ground,
const std::map<Node*, Connection::ConnectionSet>& node_connections,
const ValueNodeMap<Current>& currents,
const std::map<psm::Connection*, Connection::Conductance>& conductance,
const Connection::ConnectionMap<Connection::Conductance>& conductance,
const std::map<Node*, std::size_t>& node_index,
Eigen::SparseMatrix<Connection::Conductance>& G,
Eigen::VectorXd& J) const;
Expand All @@ -218,7 +220,7 @@ class IRSolver
void dumpMatrix(const Eigen::SparseMatrix<Connection::Conductance>& matrix,
const std::string& name) const;
void dumpConductance(
const std::map<Connection*, Connection::Conductance>& cond,
const Connection::ConnectionMap<Connection::Conductance>& cond,
const std::string& name) const;

odb::dbNet* net_;
Expand Down
31 changes: 17 additions & 14 deletions src/psm/src/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,27 @@ Node::Node(const odb::Point& pt, odb::dbTechLayer* layer)
{
}

Node::CompareInformation Node::compareTuple() const
{
const int x = pt_.getX();
const int y = pt_.getY();
const int layer = layer_->getNumber();

return {layer, x, y, getType(), getTypeCompareInfo()};
}

Node::CompareInformation Node::dummyCompareTuple()
{
return {-1, 0, 0, NodeType::Unknown, -1};
}

bool Node::compare(const Node* other) const
{
if (other == nullptr) {
return true;
}

const int lhs_x = pt_.getX();
const int lhs_y = pt_.getY();
const int lhs_l = layer_->getNumber();
const NodeType lhs_t = getType();
const int lhs_s = getTypeCompareInfo();

const int rhs_x = other->pt_.getX();
const int rhs_y = other->pt_.getY();
const int rhs_l = other->layer_->getNumber();
const NodeType rhs_t = other->getType();
const int rhs_s = other->getTypeCompareInfo();

return std::tie(lhs_l, lhs_x, lhs_y, lhs_t, lhs_s)
< std::tie(rhs_l, rhs_x, rhs_y, rhs_t, rhs_s);
return compareTuple() < other->compareTuple();
}

bool Node::compare(const std::unique_ptr<Node>& other) const
Expand Down Expand Up @@ -112,6 +113,8 @@ std::string Node::getTypeName() const
return "ITerm Node";
case NodeType::BPin:
return "BPin Node";
case NodeType::Unknown:
return "Unknown";
}

return "unknown";
Expand Down
8 changes: 6 additions & 2 deletions src/psm/src/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
#include <string>
#include <vector>

#include "connection.h"
#include "odb/geom_boost.h"

namespace odb {
Expand All @@ -62,7 +61,8 @@ class Node
Node,
Source,
ITerm,
BPin
BPin,
Unknown
};

struct Compare
Expand Down Expand Up @@ -90,6 +90,10 @@ class Node
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::Node; }

Expand Down

0 comments on commit 627808b

Please sign in to comment.