Skip to content
Open
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
33 changes: 33 additions & 0 deletions Utilities/placement_viewer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Utility function to visualize placement in image format

This utlity script uses the plc_client binary provided with the Google Circuit Training to plot the placement of macros and standard cells in an image format. The input to the placement viewer is the placement files in prototxt and .plc format

### Install Google Circuit Training plc_wrapper_main library
```
# Install TF-Agents with nightly versions of Reverb and TensorFlow 2.x
$ pip install tf-agents-nightly[reverb]
# Copy the placement cost binary to /usr/local/bin and makes it executable.
$ sudo curl https://storage.googleapis.com/rl-infra-public/circuit-training/placement_cost/plc_wrapper_main \
-o /usr/local/bin/plc_wrapper_main
$ sudo chmod 555 /usr/local/bin/plc_wrapper_main
$ Clone the circuit-training repo.
$ git clone https://github.com/google-research/circuit-training.git
```
### Place the placement_viewer in circuit_training/circuit_training/

## Testing:
```
$ python3 -m circuit_training.placement_viewer.placement_viewer_test
```

## Execution:
```
$ python3 -m circuit_training.placement_viewer.placement_viewer
--init_file: Path to the init file.
--netlist_file: Path to the input netlist file.
--img_name: Path to/Prefix of the name of output image file.
```

#### Example plot: Ariane RISC-V placement done by Circuit Training after training from scratch i.e. [Full Scale Ariane example](https://github.com/google-research/circuit_training/blob/main/docs/ARIANE.md "Full Scale Ariane example")

![picture alt](https://github.com/Maria-UET/MacroPlacement/blob/bc90e6deaceca15ff0ce846f7c441eddc3f44034/Utilities/placement_viewer/test_data/ariane/ariane_final_placement.png "Ariane RISC-V placement done by Circuit Training after training from scratch")
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
79 changes: 79 additions & 0 deletions Utilities/placement_viewer/placement_viewer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import matplotlib. pyplot as plt
from matplotlib.patches import Rectangle
from absl import app
from absl import flags
from typing import Sequence, Tuple

from circuit_training.environment import plc_client

flags.DEFINE_string("netlist_file", None, "Path to the input netlist file.")
flags.DEFINE_string("init_file", None, "Path to the init file.")
flags.DEFINE_string("img_name", None, "Path to/Prefix of the name of output image file.")

flags.mark_flags_as_required([
"netlist_file",
"init_file",
"img_name",
])

FLAGS = flags.FLAGS


def _draw_placement(plc: plc_client.PlacementCost, img_name: str) -> None:
plt.figure()
fig, ax = plt.subplots()
cm=['blue','red']
plt.xlim(0, plc.get_canvas_width_height()[0])
plt.ylim(0, plc.get_canvas_width_height()[1])

for i in plc.get_macro_indices():
x = plc.get_node_location(i)[0]-(plc.get_node_width_height(i)[0]/2)
y = plc.get_node_location(i)[1]-(plc.get_node_width_height(i)[1]/2)
w = plc.get_node_width_height(i)[0]
h = plc.get_node_width_height(i)[1]
orient = plc.get_macro_orientation(i)
if orient in [ 'E', 'FE', 'W', 'FW']:
h = plc.get_node_width_height(i)[0]
w = plc.get_node_width_height(i)[1]

ax.add_patch(Rectangle((x, y),
w,h ,
facecolor = cm[plc.is_node_soft_macro(i)],
edgecolor ='black',
linewidth = 1,
linestyle="solid"))
plt.savefig(img_name)


def _get_final_canvas_dims(plc: plc_client.PlacementCost) -> Tuple[float, float, int, int]:
with open(FLAGS.init_file) as f:
lines = f.readlines()

cols= int(lines[4].split(':')[1].split(' ')[1])
rows = int(lines[4].split(':')[2].split(' ')[1])
width = float(lines[5].split(':')[1].split(' ')[1])
height = float(lines[5].split(':')[2].split(' ')[1])
return (width, height, cols, rows)


def viewer(argv: Sequence[str]) -> None:
if len(argv) > 3:
raise app.UsageError("Too many command-line arguments.")

plc = plc_client.PlacementCost(netlist_file=FLAGS.netlist_file)

# draw the initial placement based on the netlist protobuf file
_draw_placement(plc, img_name=FLAGS.img_name+'_initial_placement')
print('Initial dimensions of the canvas:', plc.get_canvas_width_height())

# draw the final placement based on the init file
width, height, cols, rows = _get_final_canvas_dims(plc)
plc.set_canvas_size(width, height) # set canvas size from the init file
plc.set_placement_grid(cols, rows) # set grid from the init file
print('Init status: ', plc.restore_placement(FLAGS.init_file)) # restore placement from the init file
print('Final dimensions of the canvas:', plc.get_canvas_width_height())
_draw_placement(plc, img_name=FLAGS.img_name+'_final_placement')


if __name__ == "__main__":
app.run(viewer)
32 changes: 32 additions & 0 deletions Utilities/placement_viewer/placement_viewer_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""Tests for placement_viewer."""
import os
import unittest
import matplotlib.pyplot as plt
from absl import app, flags
from typing import Sequence

from circuit_training.environment import plc_client
from placement_viewer import placement_viewer


class TestPlacementViewer(unittest.TestCase):
def test_plc_viewer(self):

test_dir = "circuit_training/placement_viewer/test_data/ariane/"
netlist_file = os.path.join(test_dir, "netlist.pb.txt")
init_file = os.path.join(test_dir, "rl_opt_placement.plc")
img_name = os.path.join(test_dir, "ariane")
os.system( "python3 -m circuit_training.placement_viewer.placement_viewer \
--netlist_file "+ netlist_file+
" --init_file " + init_file+
" --img_name " + img_name )

self.assertTrue((plt.imread(os.path.join(test_dir, "ariane_initial_placement.png")) ==
plt.imread(os.path.join(test_dir, "test_initial_placement.png"))).all())

self.assertTrue((plt.imread(os.path.join(test_dir, "ariane_final_placement.png")) ==
plt.imread(os.path.join(test_dir, "test_final_placement.png"))).all())


if __name__ == '__main__':
TestPlacementViewer().test_plc_viewer()
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading