Skip to content

Commit

Permalink
drt: Use bidirectional A*
Browse files Browse the repository at this point in the history
Co-authored-by: Maciej Torhan <[email protected]>
Signed-off-by: Krzysztof Bieganski <[email protected]>
Signed-off-by: Maciej Torhan <[email protected]>
  • Loading branch information
kbieganski and m-torhan committed Jan 7, 2025
1 parent f6a30a6 commit e7b21a8
Show file tree
Hide file tree
Showing 5 changed files with 319 additions and 67 deletions.
7 changes: 6 additions & 1 deletion src/drt/src/dr/FlexDR_maze.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2448,7 +2448,12 @@ void FlexDRWorker::routeNet_postAstarUpdate(
if (!path.empty()) {
auto mi = path[0];
std::vector<drPin*> tmpPins;
for (auto pin : mazeIdx2unConnPins[mi]) {
auto miPins = mazeIdx2unConnPins.find(mi);
if (miPins == mazeIdx2unConnPins.end()) {
std::cout << "Error: Failed to find pin at " << mi << std::endl;
return;
}
for (auto pin : miPins->second) {
tmpPins.push_back(pin);
}
for (auto pin : tmpPins) {
Expand Down
2 changes: 1 addition & 1 deletion src/drt/src/dr/FlexGridGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ void FlexGridGraph::initGrids(
srcs_.clear();
dsts_.clear();

prevDirs_.resize(capacity * 3, false);
prevDirs_.resize(capacity * 4, false);
srcs_.resize(capacity, false);
dsts_.resize(capacity, false);
guides_.clear();
Expand Down
80 changes: 67 additions & 13 deletions src/drt/src/dr/FlexGridGraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#pragma once

#include <unordered_map>
#include <cstdint>
#include <cstring>
#include <iostream>
Expand All @@ -40,6 +41,10 @@
#include "frDesign.h"

namespace drt {
enum class ExpandDir {
FORWARD,
BACKWARD,
};

class FlexDRWorker;
class FlexDRGraphics;
Expand Down Expand Up @@ -103,6 +108,26 @@ class FlexGridGraph
return nodes_[getIdx(x, y, z)].hasGridCostUp;
}

void setPathCost(frMIdx x, frMIdx y, frMIdx z, frCost in)
{
path_costs_[getIdx(x, y, z)] = in;
}
void setPathCost(const FlexMazeIdx& mIdx, frCost in)
{
path_costs_[getIdx(mIdx.x(), mIdx.y(), mIdx.z())] = in;
}
frCost getPathCost(frMIdx x, frMIdx y, frMIdx z)
{
return path_costs_[getIdx(x, y, z)];
}
bool isClosed(frMIdx x, frMIdx y, frMIdx z, ExpandDir expandDir) const
{
auto baseIdx = 4 * getIdx(x, y, z);
return getPrevAstarNodeDir({x, y, z}) != frDirEnum::UNKNOWN
&& (prevDirs_[baseIdx + 3] ? expandDir == ExpandDir::FORWARD
: expandDir == ExpandDir::BACKWARD);
}

void getBBox(Rect& in) const
{
if (!xCoords_.empty() && !yCoords_.empty()) {
Expand Down Expand Up @@ -944,6 +969,11 @@ class FlexGridGraph
void resetPrevNodeDir();
void resetSrc();
void resetDst();
void traceBackPath(std::vector<FlexMazeIdx>& connComps,
std::vector<FlexMazeIdx>& path,
FlexMazeIdx& ccMazeIdx1,
FlexMazeIdx& ccMazeIdx2,
ExpandDir expandDir);
bool search(std::vector<FlexMazeIdx>& connComps,
drPin* nextPin,
std::vector<FlexMazeIdx>& path,
Expand Down Expand Up @@ -987,8 +1017,10 @@ class FlexGridGraph
yCoords_.shrink_to_fit();
yCoords_.clear();
yCoords_.shrink_to_fit();
wavefront_.cleanup();
wavefront_.fit();
wavefrontForward_.cleanup();
wavefrontForward_.fit();
wavefrontBackward_.cleanup();
wavefrontBackward_.fit();
}

void printNode(frMIdx x, frMIdx y, frMIdx z)
Expand Down Expand Up @@ -1093,6 +1125,7 @@ class FlexGridGraph
std::vector<bool> srcs_;
std::vector<bool> dsts_;
std::vector<bool> guides_;
std::unordered_map<frMIdx, frCost> path_costs_;
frVector<frCoord> xCoords_;
frVector<frCoord> yCoords_;
frVector<frLayerNum> zCoords_;
Expand All @@ -1103,7 +1136,14 @@ class FlexGridGraph
frUInt4 ggMarkerCost_ = 0;
frUInt4 ggFixedShapeCost_ = 0;
// temporary variables
FlexWavefront wavefront_;
FlexWavefront wavefrontForward_;
FlexWavefront wavefrontBackward_;
struct Meeting {
FlexWavefrontGrid wavefront;
ExpandDir wavefrontDir;
frCost mazeCost;
};
std::optional<Meeting> meetingGrid_;
const std::vector<std::pair<frCoord, frCoord>>* halfViaEncArea_
= nullptr; // std::pair<layer1area, layer2area>
// ndr related
Expand All @@ -1117,32 +1157,35 @@ class FlexGridGraph
FlexGridGraph() = default;

// unsafe access, no idx check
void setPrevAstarNodeDir(frMIdx x, frMIdx y, frMIdx z, frDirEnum dir)
void setPrevAstarNodeDir(frMIdx x, frMIdx y, frMIdx z, ExpandDir expandDir, frDirEnum dir)
{
auto baseIdx = 3 * getIdx(x, y, z);
auto baseIdx = 4 * getIdx(x, y, z);
prevDirs_[baseIdx] = ((uint16_t) dir >> 2) & 1;
prevDirs_[baseIdx + 1] = ((uint16_t) dir >> 1) & 1;
prevDirs_[baseIdx + 2] = ((uint16_t) dir) & 1;
prevDirs_[baseIdx + 3] = expandDir == ExpandDir::FORWARD;
}

// unsafe access, no check
frDirEnum getPrevAstarNodeDir(const FlexMazeIdx& idx) const
{
auto baseIdx = 3 * getIdx(idx.x(), idx.y(), idx.z());
auto baseIdx = 4 * getIdx(idx.x(), idx.y(), idx.z());
return (frDirEnum) (((uint16_t) (prevDirs_[baseIdx]) << 2)
+ ((uint16_t) (prevDirs_[baseIdx + 1]) << 1)
+ ((uint16_t) (prevDirs_[baseIdx + 2]) << 0));
}

// unsafe access, no check
bool isSrc(frMIdx x, frMIdx y, frMIdx z) const
bool isSrc(frMIdx x, frMIdx y, frMIdx z, ExpandDir expandDir) const
{
return srcs_[getIdx(x, y, z)];
auto& srcs = expandDir == ExpandDir::FORWARD ? srcs_ : dsts_;
return srcs[getIdx(x, y, z)];
}
// unsafe access, no check
bool isDst(frMIdx x, frMIdx y, frMIdx z) const
bool isDst(frMIdx x, frMIdx y, frMIdx z, ExpandDir expandDir) const
{
return dsts_[getIdx(x, y, z)];
auto& dsts = expandDir == ExpandDir::FORWARD ? dsts_ : srcs_;
return dsts[getIdx(x, y, z)];
}
bool isDst(frMIdx x, frMIdx y, frMIdx z, frDirEnum dir) const
{
Expand Down Expand Up @@ -1305,20 +1348,31 @@ class FlexGridGraph
std::vector<FlexMazeIdx>& path,
std::vector<FlexMazeIdx>& root,
FlexMazeIdx& ccMazeIdx1,
FlexMazeIdx& ccMazeIdx2) const;
FlexMazeIdx& ccMazeIdx2,
ExpandDir expandDir) const;
void traceBackPath(const FlexMazeIdx& currGrid,
std::vector<FlexMazeIdx>& path,
std::vector<FlexMazeIdx>& root,
FlexMazeIdx& ccMazeIdx1,
FlexMazeIdx& ccMazeIdx2,
ExpandDir expandDir) const;
void expandWavefront(FlexWavefrontGrid& currGrid,
const FlexMazeIdx& dstMazeIdx1,
const FlexMazeIdx& dstMazeIdx2,
const Point& centerPt,
ExpandDir expandDir,
bool route_with_jumpers);
bool isExpandable(const FlexWavefrontGrid& currGrid, frDirEnum dir) const;
bool isExpandable(const FlexWavefrontGrid& currGrid,
frDirEnum dir,
ExpandDir expandDir) const;
FlexMazeIdx getTailIdx(const FlexMazeIdx& currIdx,
const FlexWavefrontGrid& currGrid) const;
void expand(FlexWavefrontGrid& currGrid,
const frDirEnum& dir,
const FlexMazeIdx& dstMazeIdx1,
const FlexMazeIdx& dstMazeIdx2,
const Point& centerPt,
ExpandDir expandDir,
bool route_with_jumpers);
bool hasAlignedUpDefTrack(
frLayerNum layerNum,
Expand All @@ -1335,7 +1389,7 @@ class FlexGridGraph
{
// The wavefront should always be empty here so we don't need to
// serialize it.
if (!wavefront_.empty()) {
if (!wavefrontForward_.empty() || !wavefrontBackward_.empty()) {
throw std::logic_error("don't serialize non-empty wavefront");
}
if (is_loading(ar)) {
Expand Down
Loading

0 comments on commit e7b21a8

Please sign in to comment.