Skip to content

Commit

Permalink
Refine cpp code to avoid always including torch and pybind
Browse files Browse the repository at this point in the history
  • Loading branch information
liulixinkerry committed Jun 5, 2024
1 parent 8e7c817 commit 7e6a0b0
Show file tree
Hide file tree
Showing 35 changed files with 225 additions and 197 deletions.
11 changes: 0 additions & 11 deletions cpp_to_py/common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,8 @@

#include <thread>

// Torch library
#include <torch/extension.h>

// utils
#include "common/utils/utils.h"

// Pybind11
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/stl_bind.h>
#include <pybind11/numpy.h>

using namespace std;

using utils::assert_msg;
using utils::logger;
2 changes: 1 addition & 1 deletion cpp_to_py/common/db/Cell.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class Cell {
void unplace();
unsigned numPins() const { return _pins.size(); }

friend ostream& operator<<(ostream& os, const Cell& c) {
friend std::ostream& operator<<(std::ostream& os, const Cell& c) {
return os << c._name << "\t(" << c.lx() << ", " << c.ly() << ')';
}
};
Expand Down
12 changes: 6 additions & 6 deletions cpp_to_py/common/db/Database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ void Database::SetupRows() {
// NOTE: currently we only support horizontal row

for (unsigned y = 0; y != nSitesY; ++y) {
rows[y] = new Row("core_SITE_ROW_" + to_string(y), "core", coreLX, coreLY + y * siteH);
rows[y] = new Row("core_SITE_ROW_" + std::to_string(y), "core", coreLX, coreLY + y * siteH);
rows[y]->xStep(stepX);
rows[y]->yStep(0);
rows[y]->xNum(nSitesX);
Expand Down Expand Up @@ -814,10 +814,10 @@ long long Database::getHPWL() {
Pin* pin = nets[i]->pins[j];
int x, y;
pin->getPinCenter(x, y);
lx = min(lx, x);
ly = min(ly, y);
hx = max(hx, x);
hy = max(hy, y);
lx = std::min(lx, x);
ly = std::min(ly, y);
hx = std::max(hx, x);
hy = std::max(hy, y);
}
hpwl += (hx - lx) + (hy - ly);
}
Expand Down Expand Up @@ -896,7 +896,7 @@ Region* Database::getRegion(const unsigned char id) {
}

NDR* Database::getNDR(const string& name) const {
map<string, NDR*>::const_iterator mi = ndrs.find(name);
std::map<string, NDR*>::const_iterator mi = ndrs.find(name);
if (mi == ndrs.end()) {
return nullptr;
}
Expand Down
22 changes: 13 additions & 9 deletions cpp_to_py/common/db/Database.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
#pragma once

#include "common/common.h"
#include "Setting.h"
#include "common/common.h"

namespace db {

using std::string;
using std::vector;

class Rectangle;
class Geometry;
class GeoMap;
Expand Down Expand Up @@ -31,20 +35,20 @@ class GCellGrid;
class BsRouteInfo;
} // namespace db

#include "BsRouteInfo.h"
#include "Cell.h"
#include "DesignRule.h"
#include "GCellGrid.h"
#include "Geometry.h"
#include "Layer.h"
#include "SiteMap.h"
#include "Net.h"
#include "Pin.h"
#include "Region.h"
#include "GCellGrid.h"
#include "Row.h"
#include "Site.h"
#include "SNet.h"
#include "Site.h"
#include "SiteMap.h"
#include "Via.h"
#include "BsRouteInfo.h"

namespace db {

Expand Down Expand Up @@ -91,7 +95,7 @@ class Database {
vector<Net*> nets;
vector<Row*> rows;
vector<Region*> regions;
map<string, NDR*> ndrs;
std::map<string, NDR*> ndrs;
vector<SNet*> snets;
vector<Track*> tracks;

Expand Down Expand Up @@ -220,9 +224,9 @@ class Database {
bool writeDEF(const std::string& file);
bool writeICCAD2017(const string& inputDef, const string& outputDef);
bool writeICCAD2017(const string& outputDef);
bool writeComponents(ofstream& ofs);
bool writeBuffer(ofstream& ofs, const string& line);
void writeBufferFlush(ofstream& ofs);
bool writeComponents(std::ofstream& ofs);
bool writeBuffer(std::ofstream& ofs, const string& line);
void writeBufferFlush(std::ofstream& ofs);

bool readBSAux(const std::string& auxFile, const std::string& plFile);
bool readBSNodes(const std::string& file);
Expand Down
16 changes: 8 additions & 8 deletions cpp_to_py/common/db/Geometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ using namespace db;
/***** Rectangle *****/

Rectangle& Rectangle::operator+=(const Rectangle& geo) {
lx = min(lx, geo.lx);
ly = min(ly, geo.ly);
hx = max(hx, geo.hx);
hy = max(hy, geo.hy);
lx = std::min(lx, geo.lx);
ly = std::min(ly, geo.ly);
hx = std::max(hx, geo.hx);
hy = std::max(hy, geo.hy);
return *this;
}

Expand Down Expand Up @@ -45,8 +45,8 @@ void Rectangle::sliceH(vector<Rectangle>& rects) {
continue;
}
if (L.hx >= R.lx) {
R.lx = min(L.lx, R.lx);
R.hx = max(L.hx, R.hx);
R.lx = std::min(L.lx, R.lx);
R.hx = std::max(L.hx, R.hx);
L.hx = L.lx;
}
}
Expand Down Expand Up @@ -92,8 +92,8 @@ void Rectangle::sliceV(vector<Rectangle>& rects) {
continue;
}
if (L.hy >= R.ly) {
R.ly = min(L.ly, R.ly);
R.hy = max(L.hy, R.hy);
R.ly = std::min(L.ly, R.ly);
R.hy = std::max(L.hy, R.hy);
L.hy = L.ly;
}
}
Expand Down
10 changes: 5 additions & 5 deletions cpp_to_py/common/db/Geometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Rectangle {
static void sliceH(vector<Rectangle>& rects);
static void sliceV(vector<Rectangle>& rects);

friend ostream& operator<<(ostream& os, const Rectangle& r) {
friend std::ostream& operator<<(std::ostream& os, const Rectangle& r) {
return os << '(' << r.lx << ", " << r.ly << ")\t(" << r.hx << ", " << r.hy << ')';
}
};
Expand All @@ -52,17 +52,17 @@ class Geometry : public Rectangle {

class GeoMap : public Rectangle {
private:
map<int, Geometry> _map;
std::map<int, Geometry> _map;

public:
bool empty() const noexcept { return _map.empty(); }
size_t size() const noexcept { return _map.size(); }
const Geometry& front() const { return _map.begin()->second; }
const Geometry& front2() const { return (++_map.begin())->second; }
const Geometry& back() const { return _map.rbegin()->second; }
map<int, Geometry>::const_iterator begin() const noexcept { return _map.begin(); }
map<int, Geometry>::const_iterator end() const noexcept { return _map.end(); }
map<int, Geometry>::const_iterator find(const int k) const { return _map.find(k); }
std::map<int, Geometry>::const_iterator begin() const noexcept { return _map.begin(); }
std::map<int, Geometry>::const_iterator end() const noexcept { return _map.end(); }
std::map<int, Geometry>::const_iterator find(const int k) const { return _map.find(k); }

const Geometry& at(const int k) const { return _map.at(k); }

Expand Down
6 changes: 3 additions & 3 deletions cpp_to_py/common/db/Net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ void Net::addPin(Pin* pin) {
/***** PowerNet *****/

void PowerNet::addRail(SNet* snet, int lx, int hx, int y) {
map<int, SNet*>::iterator rail = rails.find(y);
std::map<int, SNet*>::iterator rail = rails.find(y);
if (rail != rails.end() && rail->second != snet) {
logger.error("rail %s already exists at y=%d , new rail %s is from %d to %d", rail->second->name.c_str(),
y, snet->name.c_str(),
Expand All @@ -125,13 +125,13 @@ bool PowerNet::getRowPower(int ly, int hy, char& topPower, char& botPower) {
bool valid = true;
topPower = 'x';
botPower = 'x';
map<int, SNet*>::iterator topRail = rails.find(hy);
std::map<int, SNet*>::iterator topRail = rails.find(hy);
if (topRail != rails.end()) {
topPower = topRail->second->type;
} else {
valid = false;
}
map<int, SNet*>::iterator botRail = rails.find(ly);
std::map<int, SNet*>::iterator botRail = rails.find(ly);
if (botRail != rails.end()) {
botPower = botRail->second->type;
} else {
Expand Down
2 changes: 1 addition & 1 deletion cpp_to_py/common/db/Net.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class NetRouteSegment {
unsigned toNode;
// path = [<direction,len>]
// direction : N,S,E,W,U,D
vector<pair<char, int>> path;
vector<std::pair<char, int>> path;

NetRouteSegment(const unsigned fromi = 0, const unsigned toi = 0, const char dir = '\0', const unsigned len = 0);

Expand Down
8 changes: 4 additions & 4 deletions cpp_to_py/common/db/Pin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ utils::PointT<int> Pin::getPinParentCenter() const {
/***** Pin Type *****/

void PinType::addShape(const Layer& layer, const int lx, const int ly, const int hx, const int hy) {
boundLX = min(boundLX, lx);
boundLY = min(boundLY, ly);
boundHX = max(boundHX, hx);
boundHY = max(boundHY, hy);
boundLX = std::min(boundLX, lx);
boundLY = std::min(boundLY, ly);
boundHX = std::max(boundHX, hx);
boundHY = std::max(boundHY, hy);
shapes.emplace_back(layer, lx, ly, hx, hy);
}

Expand Down
8 changes: 4 additions & 4 deletions cpp_to_py/common/db/Region.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ using namespace db;

void Region::addRect(const int xl, const int yl, const int xh, const int yh) {
rects.emplace_back(xl, yl, xh, yh);
lx = min(lx, xl);
ly = min(ly, yl);
hx = max(hx, xh);
hy = max(hy, yh);
lx = std::min(lx, xl);
ly = std::min(ly, yl);
hx = std::max(hx, xh);
hy = std::max(hy, yh);
}
2 changes: 1 addition & 1 deletion cpp_to_py/common/db/Via.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class ViaType {

public:
string name = "";
set<Geometry> rects;
std::set<Geometry> rects;
ViaRule rule;

ViaType(const string& name = "", const bool isDef = false) : isDef_(isDef), name(name) {}
Expand Down
Loading

0 comments on commit 7e6a0b0

Please sign in to comment.